HMCAN: A Hierarchical Multimodal Contextual Attention Network for Visual Question Answering
HMCAN: Hierarchical Multimodal Contextual Attention Network/n/nThis code implements the HMCAN (Hierarchical Multimodal Contextual Attention Network) model, a novel deep learning architecture for Visual Question Answering (VQA). The model utilizes a hierarchical attention mechanism to effectively capture the contextual relationships between the visual and textual inputs, enhancing its ability to understand and answer complex questions./n/n### Model Structure/n/nThe HMCAN model consists of the following components:/n/n1. Textual Embedding: The input question is encoded into a sequence of word embeddings. Each word is represented by a vector, capturing its semantic meaning./n/n2. Visual Feature Extraction: The input image is processed by a convolutional neural network (CNN) to extract visual features./n/n3. Hierarchical Multimodal Contextual Attention: The model utilizes two TextImage/_Transformer modules to perform hierarchical multimodal contextual attention. This involves two steps:/n * Step 1: Word-Level Attention: The model attends to specific words in the question that are relevant to the visual features./n * Step 2: Sentence-Level Attention: The model attends to specific sentences in the question that are relevant to the overall visual context./n/n4. Classifier: The final representation of the question and image is fed into a classifier to predict the answer./n/n### Code Implementation/n/npython/nclass HMCAN(nn.Module):/n def __init__(self, configs, alpha):/n super(HMCAN, self).__init__()/n self.word_length = configs.max_word_length/n self.alpha = alpha/n/n self.contextual_transform = TextImage_Transformer(/n configs.contextual_transform, configs.contextual_transform.output_dim)/n/n self.contextual_transform2 = TextImage_Transformer(/n configs.contextual_transform, configs.contextual_transform.output_dim)/n/n self.conv = nn.Conv2d(2048, 768, 1) # Create a 2D convolutional layer/n self.bn = nn.BatchNorm2d(768) # Normalize the 4D array, 768 is the number of image channels/n/n self.classifier = nn.Sequential(nn.Linear(768*6, 256),/n nn.ReLU(True),/n nn.BatchNorm1d(256),/n nn.Linear(256, 2)/n ) # A sequence container that sorts the modules for building the neural network, the output of the first module will be used as the input of the second module/n/n/n def forward(self, e, f): # Forward propagation/n cap_lengths = len(e)/n/n e_f_mask = torch.ones(cap_lengths, self.word_length).cuda() # Define a tensor with all 1s/n f_e_mask = torch.ones(cap_lengths, 16).cuda()/n/n e = torch.squeeze(e, dim=1) # [batch_size, 40, 768] Compress e, remove the dimension with index 1/n e1 = e[:, :self.word_length, :]/n e2 = e[:, self.word_length: self.word_length*2, :]/n e3 = e[:, self.word_length*2:, :]/n # e = self.fc(e) # [batch_size, 40, 64]/n/n f = F.relu(self.bn(self.conv(f))) # [batch_size, 768, 4, 4] Activation function? What is this f?/n f = f.view(f.shape[0], f.shape[1], -1) # [batch_size, 768, 16]/n f = f.permute([0, 2, 1]) # [batch_size, 16, 768] Transpose the dimensions/n/n c1_e1_f = self.contextual_transform(e1, e_f_mask, f)/n c1_f_e1 = self.contextual_transform2(f, f_e_mask, e1)/n a = self.alpha/n/n c1 = a * c1_e1_f + (1 - a) * c1_f_e1/n/n c2_e2_f = self.contextual_transform(e2, e_f_mask, f)/n c2_f_e2 = self.contextual_transform2(f, f_e_mask, e2)/n/n c2 = a * c2_e2_f + (1 - a) * c2_f_e2/n/n c3_e3_f = self.contextual_transform(e3, e_f_mask, f)/n c3_f_e3 = self.contextual_transform2(f, f_e_mask, e3)/n/n c3 = a * c3_e3_f + (1 - a) * c3_f_e3/n/n x = torch.cat((c1, c2, c3), dim=1)/n x = self.classifier(x)/n/n/n return x/n/n/n### Mask Operations/n/nIn the forward propagation, e_f_mask and f_e_mask are used to perform mask operations on the input, controlling which parts of the input are considered during the calculation. /n/n**e_f_mask** is a tensor with shape (cap_lengths, word_length), where cap_lengths represents the length of the input e, and word_length represents the maximum number of words in each sentence. Each element in this tensor is 1, indicating that the corresponding word is valid. During the calculation, the model will not consider parts that exceed word_length./n/n**f_e_mask** is a tensor with shape (cap_lengths, 16), where cap_lengths represents the length of the input e. Each element in this tensor is 1, indicating that the corresponding feature is valid. During the calculation, the model will not consider features that exceed the length of the input e./n/nBy using these two mask tensors, the model can flexibly handle inputs of different lengths and only focus on the valid parts, improving computational efficiency. /n/nNote:/n/nThe code snippets provided above are for illustration purposes. You can further customize the model according to your specific needs. It's highly recommended to thoroughly understand the concepts of visual question answering, hierarchical attention, and multimodal contextual attention to effectively implement and fine-tune the HMCAN model./n
原文地址: https://www.cveoy.top/t/topic/fQeO 著作权归作者所有。请勿转载和采集!