Fast-SCNN-pytorch icon indicating copy to clipboard operation
Fast-SCNN-pytorch copied to clipboard

assert target.dim() == 3 error

Open midtom opened this issue 5 years ago • 10 comments

I got a error in loss.py line 54. I print my target size is (2,768,768,3), but the code shows it should be dim 3. my predict size is (2,3,768,768). I use my own dataset, picture annotation like cityscapes. then I get this error. I don't know what happen.

midtom avatar Jan 02 '20 12:01 midtom

Hi @midtom How did you resolve this issue? I am facing it at the moment when I use my own model for training. Thanks in advance.

dronefreak avatar Apr 18 '20 11:04 dronefreak

@dronefreak ,@midtom did you resolve this issue? I have the same problem,can you give me some advice? thank you!

yanzhou-li avatar Feb 22 '21 11:02 yanzhou-li

@dronefreak ,@midtom did you resolve this issue? I have the same problem,can you give me some advice? thank you!

Please confirm whether the label is single channel.

Tramac avatar Feb 22 '21 11:02 Tramac

This is essentially a dimension mismatch issue, looks like your target size is in channels_last format whereas your prediction is in channels_first. Reshaping either of the two tensors will solve your issue. Also, please make sure that your both target and predictions are single channel tensors.

dronefreak avatar Feb 22 '21 11:02 dronefreak

thank you so much. i got it

yanzhou-li avatar Feb 22 '21 12:02 yanzhou-li

@Tramac,@dronefreak,i meet anthor problem: when i reshape both my target size and image size are channels_first,an error was reported below: Traceback (most recent call last): File "train.py", line 208, in <module> trainer.train() File "train.py", line 130, in train for i, (images, targets) in enumerate(self.train_loader): File "/home/liyanzhou/anaconda3/envs/py36torch10_fastscnn/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 560, in __next__ batch = self.collate_fn([self.dataset[i] for i in indices]) File "/home/liyanzhou/anaconda3/envs/py36torch10_fastscnn/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 560, in <listcomp> batch = self.collate_fn([self.dataset[i] for i in indices]) File "/home/liyanzhou/pycharm_project/demo/Fast-SCNN-pytorch-master/data_loader/segbase.py", line 50, in __getitem__ img = self.transform(img) File "/home/liyanzhou/anaconda3/envs/py36torch10_fastscnn/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 61, in __call__ img = t(img) File "/home/liyanzhou/anaconda3/envs/py36torch10_fastscnn/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 164, in __call__ return F.normalize(tensor, self.mean, self.std, self.inplace) File "/home/liyanzhou/anaconda3/envs/py36torch10_fastscnn/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 208, in normalize tensor.sub_(mean[:, None, None]).div_(std[:, None, None]) RuntimeError: The size of tensor a (768) must match the size of tensor b (3) at non-singleton dimension 0

the image size and target size are transform transpose img shape: (1, 768, 768) transform transpose mask shape: (1, 768, 768)

how to resolve it?i need some advice,thank you!

yanzhou-li avatar Feb 22 '21 14:02 yanzhou-li

Oh you would need to expand the dimensions using torch.unsqueeze(), this will add a 4th dim to your tensor (batch size), which is expected by your pytorch model. Input tensors to the model should have 4 dimensions, [batch_size, channels, height, width].

dronefreak avatar Feb 22 '21 14:02 dronefreak

Wonderful!Maybe I read the images was wrong! I should read them with RGB,but I read them with single channel.Thank you! Now the network can run,but the loss is high loss =0.1887. The learning rate is 0.001. By the way,the shape of outputs and target are torch.Size([4, 2, 768, 768]) torch.Size([768, 768]),is this normal? @Tramac @dronefreak

yanzhou-li avatar Feb 23 '21 08:02 yanzhou-li

Okay so that means you have a batch size of 4 and perhaps you are doing a binary (2-class) classification? Target size seems a bit off, as there should be the num_channels and batch_dim as well. Ideally your label should have the same size as that of your output, otherwise you would run into potential dimension mismatch issues, while calculating your losses.

dronefreak avatar Feb 23 '21 10:02 dronefreak

你好,李yanzhou已经收到你的邮件,谢谢!

yanzhou-li avatar Apr 13 '22 10:04 yanzhou-li