Target size (torch.Size([4, 512, 512])) must be the same as input size (torch.Size([4, 1, 512, 512]))

username: 163_Nayem
cooked: <hr>
<p>ValueError                                Traceback (most recent call last)<br>
/tmp/ipykernel_24/1691612924.py in <br>
1 model_trainer = Trainer(model)<br>
----&gt; 2 model_trainer.start()</p>
<p>/tmp/ipykernel_24/785556323.py in start(self)<br>
81     def start(self):<br>
82         for epoch in range(self.num_epochs):<br>
—&gt; 83             self.iterate(epoch, “train”)<br>
84             state = {<br>
85                 “epoch”: epoch,</p>
<p>/tmp/ipykernel_24/785556323.py in iterate(self, epoch, phase)<br>
60         for itr, batch in enumerate(dataloader):<br>
61             images, targets = batch<br>
—&gt; 62             loss, outputs = self.forward(images, targets)<br>
63             loss = loss / self.accumulation_steps<br>
64             if phase == “train”:</p>
<p>/tmp/ipykernel_24/785556323.py in forward(self, images, targets)<br>
44         masks = targets.to(self.device)<br>
45         outputs = <a href="http://self.net" rel="noopener nofollow ugc">self.net</a>(images)<br>
—&gt; 46         loss = self.criterion(outputs, masks)<br>
47         return loss, outputs<br>
48</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1189                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1190             return forward_call(*input, **kwargs)<br>
1191         # Do not call functions when jit is used<br>
1192         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>/tmp/ipykernel_24/3657000512.py in forward(self, input, target)<br>
33<br>
34     def forward(self, input, target):<br>
—&gt; 35         loss = self.alpha*self.focal(input, target) - torch.log(dice_loss(input, target))<br>
36         return loss.mean()</p>
<p>/opt/conda/lib/python3.7/site-packages/torch/nn/modules/module.py in _call_impl(self, *input, **kwargs)<br>
1188         if not (self._backward_hooks or self._forward_hooks or self._forward_pre_hooks or _global_backward_hooks<br>
1189                 or _global_forward_hooks or _global_forward_pre_hooks):<br>
 → 1190             return forward_call(*input, **kwargs)<br>
1191         # Do not call functions when jit is used<br>
1192         full_backward_hooks, non_full_backward_hooks = <span class="chcklst-box fa fa-square-o fa-fw"></span>, <span class="chcklst-box fa fa-square-o fa-fw"></span></p>
<p>/tmp/ipykernel_24/3657000512.py in forward(self, input, target)<br>
17         if not (target.size() == input.size()):<br>
18             raise ValueError(“Target size ({}) must be the same as input size ({})”<br>
—&gt; 19                              .format(target.size(), input.size()))<br>
20         max_val = (-input).clamp(min=0)<br>
21         loss = input - input * target + max_val + \</p>
<p>ValueError: Target size (torch.Size([4, 512, 512])) must be the same as input size (torch.Size([4, 1, 512, 512]))</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/3/933130bb4fd2a0209910ed0e4853ad4dc12c9058.png" data-download-href="https://discuss.pytorch.org/uploads/default/933130bb4fd2a0209910ed0e4853ad4dc12c9058" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/933130bb4fd2a0209910ed0e4853ad4dc12c9058_2_690x216.png" alt="image" data-base62-sha1="l07zPSvUOVgESEB95t3h8kbcJhS" width="690" height="216" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/933130bb4fd2a0209910ed0e4853ad4dc12c9058_2_690x216.png, https://discuss.pytorch.org/uploads/default/optimized/3X/9/3/933130bb4fd2a0209910ed0e4853ad4dc12c9058_2_1035x324.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/9/3/933130bb4fd2a0209910ed0e4853ad4dc12c9058.png 2x" data-dominant-color="ECEFF0"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">1116×350 30.7 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>

username: marksaroufim
cooked: <p>The error mesage is describing the problem. Do you see the extra <code>1</code> in your in your input size? You might want to either <code>input = input.squeeze()</code> to remove the extra 1 so your shapes match. Or unsqueeze your target to add an extra <code>1</code> which should work via <code>target = target.unsqueeze(1)</code></p>