davidtvs / PyTorch-ENet

PyTorch implementation of ENet
MIT License
389 stars 129 forks source link

the question about output shape #48

Open lucky26418 opened 3 years ago

lucky26418 commented 3 years ago

I got a problem when i test the model with cityscapes's dataset like this. How can I solve it?

Traceback (most recent call last): File "main.py", line 348, in loaders, w_class, class_encoding = load_dataset(dataset) File "main.py", line 124, in load_dataset color_labels = utils.batch_transform(labels, label_to_rgb) File "D:\Projects\ENetProjects\PyTorch-ENet-master\utils.py", line 21, in batch_transform transf_slices = [transform(tensor) for tensor in torch.unbind(batch)] File "D:\Projects\ENetProjects\PyTorch-ENet-master\utils.py", line 21, in transf_slices = [transform(tensor) for tensor in torch.unbind(batch)] File "D:\Applications\anaconda3\lib\site-packages\torchvision\transforms\transforms.py", line 61, in call img = t(img) File "D:\Projects\ENetProjects\PyTorch-ENet-master\transforms.py", line 92, in call color_tensor[channel].maskedfill(mask, color_value) RuntimeError: output with shape [360, 480] doesn't match the broadcast shape [3, 360, 480]

davidtvs commented 3 years ago

How exactly are you running? What are the command-line arguments?

lucky26418 commented 3 years ago

my command-line arguments are like this: python main.py -m test --save-dir D:\Projects\PyTorch-ENet-master\save\ENet_Cityscapes --name ENet --dataset cityscapes --dataset-dir D:\DATASET\cityscapes --imshow-batch --height 512 --width 1024

And I successfully run the camvid dataset(Including train , test and cityscapes's training).

davidtvs commented 3 years ago

The annotations of the Cityscapes test dataset are not public and since you have --imshow-batch it tries to display the annotation which then ends up causing the error you got. If you remove --imshow-batch it should run the model in the test dataset but the metrics will be all set to nan like in #8

lucky26418 commented 3 years ago

thanks for your prompt reply, but I got a new problem like this:

Traceback (most recent call last): File "main.py", line 321, in test(model, test_loader, w_class, class_encoding) File "main.py", line 239, in test loss, (iou, miou) = test.run_epoch(args.print_step) File "D:\Projects\PyTorch-ENet-master\test.py", line 49, in run_epoch loss = self.criterion(outputs, labels) File "D:\Applications\anaconda3\lib\site-packages\torch\nn\modules\module.py", line 550, in call result = self.forward(*input, **kwargs) File "D:\Applications\anaconda3\lib\site-packages\torch\nn\modules\loss.py", line 932, in forward ignore_index=self.ignore_index, reduction=self.reduction) File "D:\Applications\anaconda3\lib\site-packages\torch\nn\functional.py", line 2317, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) File "D:\Applications\anaconda3\lib\site-packages\torch\nn\functional.py", line 2117, 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 (3D tensors) but got targets of size: : [10, 3, 360, 480]

I try to change the code "loss = self.criterion(outputs, labels)" (File "D:\Projects\PyTorch-ENet-master\test.py", line 49, in run_epoch) like this "loss = self.criterion(outputs, labels, torch.squeeze(target).long())", there is still the same error. I read your other answers about the error "supported (3D tensors) but got targets of dimension: 4.", so I don't understand why this error happened here. looking forward to your support,thanks.

davidtvs commented 3 years ago

@lucky26418 , sorry for the late reply. Hope that in meantime you have resolved the issue since unfortunately, I could not reproduce your error using the command you posted before: python main.py -m test --save-dir D:\Projects\PyTorch-ENet-master\save\ENet_Cityscapes --name ENet --dataset cityscapes --dataset-dir D:\DATASET\cityscapes --imshow-batch --height 512 --width 1024

Are you still trying to run with the command above?

Stone-SL commented 3 years ago

Hi, I got the same issue .Did you have solved about this issue.I hope I can get your help,thank you!

davidtvs commented 3 years ago

@Stone-SL, please post here the command-line arguments that you are using and the error trace

Stone-SL commented 3 years ago

This is my command-line arguements: python main.py -m test --save-dir save/ENet_Cityscapes/ --name ENet --dataset cityscapes --dataset-dir Datasets/cityscapes/ And this is the error trace: Traceback (most recent call last): File "/home/ubuntu/project/SL/pyTorch-ENet/main.py", line 319, in test(model, test_loader, w_class, class_encoding) File "/home/ubuntu/project/SL/pyTorch-ENet/main.py", line 240, in test loss, (iou, miou) = test.run_epoch(args.print_step) File "/home/ubuntu/project/SL/pyTorch-ENet/test.py", line 52, in run_epoch loss = self.criterion(outputs, labels) File "/home/ubuntu/anaconda3/envs/pytorch1.7/lib/python3.6/site-packages/torch/nn/modules/module.py", line 727, in _call_impl result = self.forward(*input, **kwargs) File "/home/ubuntu/anaconda3/envs/pytorch1.7/lib/python3.6/site-packages/torch/nn/modules/loss.py", line 962, in forward ignore_index=self.ignore_index, reduction=self.reduction) File "/home/ubuntu/anaconda3/envs/pytorch1.7/lib/python3.6/site-packages/torch/nn/functional.py", line 2470, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) File "/home/ubuntu/anaconda3/envs/pytorch1.7/lib/python3.6/site-packages/torch/nn/functional.py", line 2268, in nll_loss ignore_index) RuntimeError: 1only batches of spatial targets supported (3D tensors) but got targets of size: : [10, 3, 360, 480]

davidtvs commented 3 years ago

@Stone-SL, I'm unable to reproduce your error. If I run the command you posted on my machine the model is loaded and run on the test dataset. The output while and after evaluating should look like this:

>>>> Running test dataset
>>>> Avg. loss: 0.0000 | Mean IoU: nan
unlabeled: nan
road: nan
sidewalk: nan
building: nan
wall: nan
fence: nan
pole: nan
traffic_light: nan
traffic_sign: nan
vegetation: nan
terrain: nan
sky: nan
person: nan
rider: nan
car: nan
truck: nan
bus: nan
train: nan
motorcycle: nan
bicycle: nan