I literally tried everything.Need help. #Error#. TypeError: conv2d(): argument 'input' (position 1) must be Tensor, not tuple

rai24: <h1>Define Autoencoder</h1>
<p>#dropout_p= 0.5<br>
class Autoencoder(nn.Module):</p>
<pre><code>def __init__(self):
    super(Autoencoder, self).__init__()
    self.encoder = nn.Sequential(
       # nn.Dropout2d(dropout_p= 0.5),
        nn.Conv2d(1, 16,kernel_size=3,stride =1, padding= 1,bias= False),  
        nn.ReLU(True),
        nn.MaxPool2d( 2, 2,return_indices=True),  
        nn.Conv2d(16,32,kernel_size=3,stride =1, padding=1),  
        nn.ReLU(True),
        nn.MaxPool2d(2, 2,return_indices=True),  
        nn.Conv2d(32,64,kernel_size=3,stride =1, padding= 1),  
        nn.ReLU(True),
        nn.MaxPool2d(2, 2,return_indices=True),  
        
    )
    self.decoder = nn.Sequential(
        nn.MaxUnpool2d(2, 2),
        nn.ConvTranspose2d(64, 32, kernel_size=3), 
        nn.ReLU(True),
        nn.MaxUnpool2d(2, 2),
        nn.ConvTranspose2d(32, 16, kernel_size=3),  
        nn.ReLU(True),
        nn.MaxUnpool2d(2, 2),
        nn.ConvTranspose2d(16, 8, kernel_size=3)
        #nn.Tanh()
    )

def forward(self, x):
    x = self.encoder(x)
    encoded_x = x
    x = self.decoder(x)
    #x=F.dropout(x,training=self.training)
    return x, encoded_x
</code></pre>
<p>num_epochs = 5<br>
batch_size = 128<br>
learning_rate = 1e-5<br>
model= Autoencoder()<br>
criterion = nn.CrossEntropyLoss()<br>
optimizer = torch.optim.Adam(model.parameters(), lr=learning_rate)<br>
for epoch in range(num_epochs):<br>
for data in loader_train_set:<br>
img, label = data<br>
img = img.view(-1,1,320,240)<br>
img = Variable(img)<br>
# img = torch.from_numpy(numpy.array(img))<br>
encoded_output = model.encoder(img)<br>
loss = criterion(encoded_output, img)<br>
optimizer.zero_grad()<br>
loss.backward()<br>
optimizer.step()</p>
<p>print(‘epoch [{}/{}], loss:{:.4f}’.format(epoch+1, num_epochs, loss.data()))</p>

albanD: <p>Hi,</p>
<p>The problem is that you’re using <code>MaxPool2d</code> with <code>return_indices=True</code>.<br>
This means that it will return a tuple with both the value and the indices.<br>
This is not handled by the convolution layer that follows.</p>

rai24: <p>Thank you. It worked, but got “RuntimeError: only batches of spatial targets supported (3D tensors) but got targets of dimension: 4” error</p>

albanD: <p>This happens in the CrossEntropyLoss right?<br>
This is because the input is not of the right shape. You should check the doc <a href="https://pytorch.org/docs/stable/nn.html#torch.nn.CrossEntropyLoss">here</a>.<br>
If you want to do classification for each pixel given the score in the channel, you want to transpose the output to be of size <code>nb_samples x nb_classes</code>. And the label should be <code>nb_samples</code>. Where <code>nb_samples = batch_size * height * width</code>.</p>

rai24: <p>Can you please write it in a code</p>

albanD: <p>It depends a bit on the task you want to do.<br>
<em>if</em> you want cross entropy for each pixel independently:</p>
<pre><code class="lang-python">output_of_net = torch.rand(batch, nb_class, height, width)
label = torch.LongTensor(batch, height, width)

output_of_net_linearized = output_of_net.permute(0, 2, 3, 1).reshape(-1, output_of_net.size(1))
label_linearized = label.view(-1)
loss = criterion(output_of_net_linearized, label_linearized)
</code></pre>