Conv2d() Error during batch processing

username: Abhishek_Das
cooked: <p>Here is my custom VGG19 class</p>
<p>class EncoderCNN(nn.Module):<br>
def <strong>init</strong>(self, cnn_output_size):</p>
<pre><code>    super(EncoderCNN, self).__init__()
    vggnet = models.vgg19(pretrained=True)
    self.vggmodel = vggnet.features
    self.linear = nn.Linear(100352, cnn_output_size)
    self.tanh = nn.Tanh()
    self.dropout = nn.Dropout(0.5)    
    
def forward(self, image_tensor):
    #INPUT = Image as Tensor
    features = self.vggmodel(image_tensor)
    features = features.view(-1,512*14*14)
    features = self.linear(features)
    features = self.dropout(self.Tanh(features))
    print(features.size())
    return features
</code></pre>
<p><strong>When I build the model in main(), this is the structure I get:</strong></p>
<p>EncoderCNN(<br>
(vggmodel): Sequential(<br>
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(1): ReLU(inplace)<br>
(2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(3): ReLU(inplace)<br>
(4): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(5): Conv2d(64, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(6): ReLU(inplace)<br>
(7): Conv2d(128, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(8): ReLU(inplace)<br>
(9): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(10): Conv2d(128, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(11): ReLU(inplace)<br>
(12): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(13): ReLU(inplace)<br>
(14): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(15): ReLU(inplace)<br>
(16): Conv2d(256, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(17): ReLU(inplace)<br>
(18): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(19): Conv2d(256, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(20): ReLU(inplace)<br>
(21): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(22): ReLU(inplace)<br>
(23): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(24): ReLU(inplace)<br>
(25): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(26): ReLU(inplace)<br>
(27): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(28): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(29): ReLU(inplace)<br>
(30): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(31): ReLU(inplace)<br>
(32): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(33): ReLU(inplace)<br>
(34): Conv2d(512, 512, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(35): ReLU(inplace)<br>
(36): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
)<br>
(linear): Linear(in_features=100352, out_features=512, bias=True)<br>
(tanh): Tanh()<br>
(dropout): Dropout(p=0.5)<br>
)</p>
<pre><code>train_loader = torch.utils.data.DataLoader(d, **batch_size= 4**, 
                                        sampler=SubsetRandomSampler(train_indices),
                                        shuffle = False,
                                          num_workers = 0,collate_fn = customBatchBuilder)
</code></pre>
<p><strong>batch contains image, question, ques_len and ans</strong><br>
<strong>image tensor is of 3 x 448 x 448 tensor</strong><br>
<strong>Now, when I execute the below lines,</strong></p>
<pre><code>    for i, batch in enumerate(train_loader):
            image, question, ques_len, ans = batch
            optimizer.zero_grad()
            img_emb = image_model(image)
</code></pre>
<p><strong>I get this error:</strong></p>
<p>TypeError: conv2d(): argument ‘input’ (position 1) must be Tensor, not tuple</p>
<p><strong>Please help!</strong></p>

username: ptrblck
cooked: <p>Could you print the type of <code>image</code> as it seems to be a <code>tuple</code> instead of a tensor?<br>
Maybe your <code>Dataset</code> returns more than these 4 values.</p>

username: Abhishek_Das
cooked: <p>Its returning tuple.</p>
<p>this is my code for custom batch:</p>
<pre><code>def customBatchBuilder(samples):
    imgs_batch, question_batch ,length_batch,ans_batch = zip(*samples)
    # Sort sequences based on length
    seqLengths = [len(seq) for seq in question_batch]
 
    sorted_list = sorted(zip(list(imgs_batch), question_batch, seqLengths,ans_batch), key = lambda x: -x[2])
    
    imgs_batch, question_batch, seqLengths,ans_batch  = zip(*sorted_list)
    
    paddedSeqs = torch.nn.utils.rnn.pad_sequence(question_batch, batch_first=True)
   # Create tensor with padded sequences.
    print(type(imgs_batch))
    #print("##############")
    
    return imgs_batch,paddedSeqs,seqLengths,ans_batch</code></pre>