RuntimeError: input and target batch or spatial sizes don't match

hectorist: <p>Hello, I am trying on a U-net implementation and I receive this error, I try to change sizes and apply combinations in order to work it but I have nothing.  Here is the error:</p>
<pre><code class="lang-auto">  0%|          | 0/675 [00:00&lt;?, ?it/s]
Traceback (most recent call last):
  File "C:\Users\ASUS\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "&lt;ipython-input-14-e260720ac9e3&gt;", line 11, in &lt;module&gt;
    loss = criterion(outputs, batch_y)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\loss.py", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\functional.py", line 2021, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\functional.py", line 1840, in nll_loss
    ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: input and target batch or spatial sizes don't match: target [50 x 1 x 2], input [50 x 1 x 64 x 64] at C:/w/1/s/windows/pytorch/aten/src\THCUNN/generic/SpatialClassNLLCriterion.cu:23

</code></pre>
<p>My train function:</p>
<blockquote>
<pre><code>def train(net_):
BATCH_SIZE = 50  # first thing to modify
EPOCHS = 3
optimizer = torch.optim.SGD(model.parameters(), lr=0.01, momentum=0.99)
criterion = nn.CrossEntropyLoss()
for epoch in range(EPOCHS):
    for i in tqdm(range(0, len(train_X), BATCH_SIZE)):
        batch_X = train_X[i:i + BATCH_SIZE].view(-1, 1, 64, 64)
        batch_y = train_y[i:i + BATCH_SIZE].view(50, 1, -1)
        batch_X, batch_y = batch_X.to(device), batch_y.to(device)

        optimizer.zero_grad()  # for specific parts of network
         net.zero_grad(), generally optimizer.zero_grad()
        outputs = model(batch_X)
        loss = criterion(outputs, batch_y)
        loss.backward()
        optimizer.step()

    print(f"Epoch: {epoch}. Loss: {loss}")
train(model)
</code></pre>
</blockquote>
<pre><code class="lang-auto">batch_X.size()
Out[15]: torch.Size([50, 1, 64, 64])

batch_y.size()
Out[16]: torch.Size([50, 1, 2])

outputs.size()
Out[17]: torch.Size([50, 1, 64, 64])
</code></pre>
<p>When I removed <code>.view(50, 1, -1)</code>; the error is:</p>
<pre><code class="lang-auto"> 0%|          | 0/675 [00:00&lt;?, ?it/s]
Traceback (most recent call last):
  File "C:\Users\ASUS\AppData\Roaming\Python\Python37\site-packages\IPython\core\interactiveshell.py", line 3331, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "&lt;ipython-input-6-c11297e71625&gt;", line 15, in &lt;module&gt;
    loss = criterion(outputs, batch_y)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\module.py", line 532, in __call__
    result = self.forward(*input, **kwargs)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\modules\loss.py", line 916, in forward
    ignore_index=self.ignore_index, reduction=self.reduction)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\functional.py", line 2021, in cross_entropy
    return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
  File "C:\Program Files\Python37\lib\site-packages\torch\nn\functional.py", line 1840, in nll_loss
    ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index)
RuntimeError: 1only batches of spatial targets supported (non-empty 3D tensors) but got targets of size: : [50, 2]
</code></pre>
<p>Thank you already ^^</p>

ptrblck: <p>I assume you are working on a multi-class segmentation use case, since you are using <code>nn.CrossEntropyLoss</code>.<br>
If that’s the case, your output should have the shape <code>[batch_size, nb_classes, height, width]</code>.<br>
While the number of dimensions is correct, it seems you are only dealing with a single class.<br>
Also, the target is expected to have the shape <code>[batch_size, height, width]</code> and contain the class indices in the range <code>[0, nb_classes-1]</code>, while your target has the shape <code>[batch_size, 1, 2]</code>.</p>
<p>Also, if you are working on a binary segmentation use case, you should either increase the number of output channels of the last conv to 2 or use <code>nn.BCEWithLogitsLoss</code> instead.</p>

hectorist: <p>I am working on multi-class segmentation.<br>
I tried to equalize height and width, but I could’t because “it is invalid for input of size”:</p>
<p>I think, what my problem is about defining train_y.  I am new these concepts, I kind of did bungee jumping into deep learning. So I am missing very fundamental things, I suppose.<br>
here is my previously defined train_y and train_x :</p>
<pre><code class="lang-auto">tdata = np.load("D:\\Neural_Networks\\coursera_v3\\coursera\\training_data.npy", allow_pickle=True)
# print(len(training_data))


X = torch.Tensor([i[0] for i in training_data]).view(-1, 64, 64)
X = X / 255.0
y = torch.Tensor([i[1] for i in training_data])

VAL_PCT = 0.1
val_size = int(len(X) * VAL_PCT)
print(val_size)

train_X = X[:-val_size]
train_y = y[:-val_size]

# test_X = X[:-val_size]
# test_y = y[:-val_size]
train_y = train_y.long()
</code></pre>
<pre><code class="lang-auto">&gt;  train_X.size()
&gt;  torch.Size([33748, 64, 64])
&gt;  train_y.size()
&gt;  torch.Size([8750, 2])
</code></pre>
<p>By the way, data is images of dogs:</p>
<pre><code class="lang-auto">&gt; training_data.size
&gt; Out[6]: 24998
&gt; training_data.ndim
&gt; Out[7]: 2
</code></pre>
<p>/// Note: I think, it was a mistake trying to convert from a CNN architecture <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> ; data was set for that one.   Yet, it is a good practice and I am stuck. Thank you.</p>

ptrblck: <p>Based on the shape of <code>train_y</code>, your target might be one-hot encoded.<br>
If that’s the case, you could transform it to the right shape and values via:</p>
<pre><code class="lang-python">train_y = torch.argmax(train_y, 1)
</code></pre>