Interpolate error in pytorch

Varun1: <p>I am developing a model for the first time and, i had faced a issue with the interpolate<br>
I am trying a multi task model as a project and when i am trying to find the loss i am using interpolate to for the segmentation</p>
<p>below is my training code for the segmentation</p>
<pre><code class="lang-auto">def train_seg(model, opts, crits, dataloader, loss_coeffs, grad_norm=0.0):
    model.train()

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    loss_meter = AverageMeter()
    pbar = tqdm(dataloader)

    for sample in pbar:
        loss = 0.0
        image , targets = sample
       
        #FORWARD
        outputs , out = model(image)
        outputs = [outputs]
        targets = [targets]
#         print(targets.shape)
#         print(targets.squeeze(dim=0).shape)
#         print(targets.squeeze(dim=1).shape)
#         print(targets[0].size()[1:])
#         print(outputs[0].size()[1:])  
   

        for out, target, crit, loss_coeff in zip(outputs, targets, crits, [loss_coeffs[0]]):
            #TODO: Increment the Loss
            print(out.shape)
            outs = F.interpolate(out, size=target.size()[1:] , mode="bilinear" , align_corners= False)
            print(outs)
            print(outs.shape)
            loss += loss_coeff * crit(
                F.interpolate(
                    out, size=target.size()[1:], mode="bilinear", align_corners=False
                ).squeeze(dim=1),
                target.squeeze(dim=1),)
           

        # BACKWARD
        for opt in opts:
            opt.zero_grad()
        loss.backward()

        if grad_norm &gt; 0.0:
            torch.nn.utils.clip_grad_norm_(model.parameters(), grad_norm)
        #TODO: Run one step
        for opt in opts:
            opt.step()

        loss_meter.update(loss.item())
        pbar.set_description(
            "Loss {:.3f} | Avg. Loss {:.3f}".format(loss.item(), loss_meter.avg)
        )
</code></pre>
<p>The initialization code</p>
<pre><code class="lang-auto">from networks.utils import MeanIoU, RMSE

val_every = 5
loss_coeffs = [0.5, 0.5]

for i in range(start_epoch, n_epochs):
    for sched in opt_scheds:
        sched.step(i)
   
    print("Epoch {:d}".format(i))
    print("Train Segmentation")
    train_seg(hydranet, optims, [crit_segm], train_city_loader, loss_coeffs)
    print("Validate Segmentaition")
    if i % val_every == 0:
        metrics = [MeanIoU(num_classes[0])]

        with torch.no_grad():
            vals = validate_seg(hydranet, metrics, test_city_loader)
        saver.maybe_save(new_val=vals, dict_to_save={"state_dict": hydranet.state_dict(), "epoch": i})
   
    print("Epoch {:d}".format(i))
    print("Train Depth")
    train_depth(hydranet, optims, [crit_depth], train_nyu_loader , loss_coeffs)
    print("Validate Depth")
    if i % val_every == 0:
        metrics = [RMSE(ignore_val=ignore_depth),]
        with torch.no_grad():
            vals = validate_depth(hydranet, metrics, test_nyu_loader)
        saver.maybe_save(new_val=vals, dict_to_save={"state_dict": hydranet.state_dict(), "epoch": i})
</code></pre>
<p>here the shape of the out is (5,36,180,320)<br>
5 is the batch number<br>
36 is the class<br>
180 x 320 =&gt; height x width<br>
then the target shape is (5,1,180,320)<br>
here the target has single channel and has labels from 0 to 36</p>
<p>This is my error which i face</p>
<pre><code class="lang-auto">Epoch 0
Train Segmentation

  0%|          | 0/20 [00:03&lt;?, ?it/s]

torch.Size([5, 32, 180, 320])

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
Cell In[19], line 12
     10 print("Epoch {:d}".format(i))
     11 print("Train Segmentation")
---&gt; 12 train_seg(hydranet, optims, [crit_segm], train_city_loader, loss_coeffs)
     13 print("Validate Segmentaition")
     14 if i % val_every == 0:

Cell In[18], line 27, in train_seg(model, opts, crits, dataloader, loss_coeffs, grad_norm)
     23 for out, target, crit, loss_coeff in zip(outputs, targets, crits, [loss_coeffs[0]]):
     24     #TODO: Increment the Loss
     25     print(out.shape)
     26     loss += loss_coeff * crit(
---&gt; 27         F.interpolate(
     28             out, size=target.size()[1:], mode="bilinear", align_corners=False
     29         ).squeeze(dim=1),
     30         target.squeeze(dim=1),)
     33 # BACKWARD
     34 for opt in opts:

File ~/Downloads/arun/Or/lib/python3.11/site-packages/torch/nn/functional.py:3916, in interpolate(input, size, scale_factor, mode, align_corners, recompute_scale_factor, antialias)
   3914 if isinstance(size, (list, tuple)):
   3915     if len(size) != dim:
-&gt; 3916         raise ValueError(
   3917             "Input and output must have the same number of spatial dimensions, but got "
   3918             f"input with spatial dimensions of {list(input.shape[2:])} and output size of {size}. "
   3919             "Please provide input tensor in (N, C, d1, d2, ...,dK) format and "
   3920             "output size in (o1, o2, ...,oK) format."
   3921         )
   3922     if not torch.jit.is_scripting():
   3923         if not all(_is_integer(x) for x in size):

ValueError: Input and output must have the same number of spatial dimensions, but got input with spatial dimensions of [180, 320] and output size of torch.Size([1, 180, 320]). Please provide input tensor in (N, C, d1, d2, ...,dK) format and output size in (o1, o2, ...,oK) format.

</code></pre>
<p>Even though the out shape is torch.Size([5, 32, 180, 320]) but it only takes the [180,320] only what may be the cause of this error reason<br>
Help me asap</p>