pytorch-UNet icon indicating copy to clipboard operation
pytorch-UNet copied to clipboard

Assertion `cur_target >= 0 && cur_target < n_classes' failed

Open abenchaaben opened this issue 5 years ago • 2 comments

Hi, Thank you for your project. It's helpful. I'm tried to launch your train.py script on a new dataset and i have encounter the following error

File "src/train.py", line 60, in metric_list=metric_list, verbose=True) File "/workspace/src/unet/model.py", line 186, in fit_dataset train_logs = self.fit_epoch(dataset, n_batch=n_batch, shuffle=shuffle) File "/workspace/src/unet/model.py", line 99, in fit_epoch training_loss = self.loss(y_out, y_batch) File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/workspace/src/unet/metrics.py", line 25, in forward ignore_index=self.ignore_index) File "/usr/local/lib/python3.6/site-packages/torch/nn/functional.py", line 2056, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) File "/usr/local/lib/python3.6/site-packages/torch/nn/functional.py", line 1873, in nll_loss ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index) RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /pytorch/aten/src/THNN/generic/SpatialClassNLLCriterion.c:109

As i search in the internet for the cause, i find that maybe it's the dimensions of the output layer. The problem is thta i did not change anything and i used a similar dataset

Thank you for your help

abenchaaben avatar Oct 24 '19 15:10 abenchaaben

Hi! Although I don't have your data with me, I am pretty confident that you didn't specify --out_channels correctly. From the exception, it follows that the cross-entropy loss function expects less classes than there are in your labeled images. Let me know the results!

cosmic-cortex avatar Oct 25 '19 06:10 cosmic-cortex

Hi, Thank you for your project. It's helpful. I'm tried to launch your train.py script on a new dataset and i have encounter the following error

File "src/train.py", line 60, in metric_list=metric_list, verbose=True) File "/workspace/src/unet/model.py", line 186, in fit_dataset train_logs = self.fit_epoch(dataset, n_batch=n_batch, shuffle=shuffle) File "/workspace/src/unet/model.py", line 99, in fit_epoch training_loss = self.loss(y_out, y_batch) File "/usr/local/lib/python3.6/site-packages/torch/nn/modules/module.py", line 493, in call result = self.forward(*input, **kwargs) File "/workspace/src/unet/metrics.py", line 25, in forward ignore_index=self.ignore_index) File "/usr/local/lib/python3.6/site-packages/torch/nn/functional.py", line 2056, in cross_entropy return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction) File "/usr/local/lib/python3.6/site-packages/torch/nn/functional.py", line 1873, in nll_loss ret = torch._C._nn.nll_loss2d(input, target, weight, _Reduction.get_enum(reduction), ignore_index) RuntimeError: Assertion `cur_target >= 0 && cur_target < n_classes' failed. at /pytorch/aten/src/THNN/generic/SpatialClassNLLCriterion.c:109

As i search in the internet for the cause, i find that maybe it's the dimensions of the output layer. The problem is thta i did not change anything and i used a similar dataset

Thank you for your help

Have you solved this problem, can you tell me a way, I also encountered the same problem

qqhe-frank avatar Mar 11 '21 15:03 qqhe-frank