Tramac / awesome-semantic-segmentation-pytorch

Semantic Segmentation on PyTorch (include FCN, PSPNet, Deeplabv3, Deeplabv3+, DANet, DenseASPP, BiSeNet, EncNet, DUNet, ICNet, ENet, OCNet, CCNet, PSANet, CGNet, ESPNet, LEDNet, DFANet)
Apache License 2.0
2.82k stars 581 forks source link

Change # of classes of cityscapes #92

Closed RAYRAYRAYRita closed 4 years ago

RAYRAYRAYRita commented 4 years ago

Hello! I want to train bisenet on cityscapes with just 11 classes. So in bisenet.py, I set NUM_CLASS = 11 and changed the code : self._key = np.array([-1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, 2, 3, 4, -1, -1, -1, 5, -1, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, -1, -1, 16, 17, 18]) to: self._key = np.array( [-1, -1, -1, -1, -1, -1, -1, -1, 0, 1, -1, -1, 2, 3, -1, -1, -1, -1, 4, -1, 5, 6, 7, -1, 8, 9, -1, 10, 10, 10, -1, -1, -1, -1, -1])

And set nclass=11 in loss.py line 41.

However I met this error:

Traceback (most recent call last): File "train.py", line 368, in <module> trainer.train() File "train.py", line 245, in train loss_dict = self.criterion(outputs, targets) File "/home/robotics/anaconda2/envs/py3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 493, in __call__ result = self.forward(*input, **kwargs) File "/home/robotics/temp/mystuff/Pytorch samples/awesome-semantic-segmentation-pytorch/core/utils/loss.py", line 179, in forward return dict(loss=self._aux_forward(*inputs)) File "/home/robotics/temp/mystuff/Pytorch samples/awesome-semantic-segmentation-pytorch/core/utils/loss.py", line 169, in _aux_forward loss = super(MixSoftmaxCrossEntropyOHEMLoss, self).forward(preds[0], target) File "/home/robotics/temp/mystuff/Pytorch samples/awesome-semantic-segmentation-pytorch/core/utils/loss.py", line 157, in forward return self.criterion(pred, target) File "/home/robotics/anaconda2/envs/py3/lib/python3.7/site-packages/torch/nn/modules/module.py", line 493, in __call__ result = self.forward(*input, **kwargs) File "/home/robotics/anaconda2/envs/py3/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 942, in forward ignore_index=self.ignore_index, reduction=self.reduction) File "/home/robotics/anaconda2/envs/py3/lib/python3.7/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 "/home/robotics/anaconda2/envs/py3/lib/python3.7/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: weight tensor should be defined either for all or no classes at /opt/conda/conda-bld/pytorch_1556653114079/work/aten/src/THCUNN/generic/SpatialClassNLLCriterion.cu:27** Could anyone do me a favor ??!

Tramac commented 4 years ago

Hi, maybe you need to set the key to -1~9 if nclass=11.

RAYRAYRAYRita commented 4 years ago

@Tramac I found where I had ignored. I used ohem and I had to modify the weight in loss function which was originally for 19 classes. Thanks for your kind reply!

sarathsrk commented 4 years ago

How to set keys for 1 valid class and background? Plz help

RAYRAYRAYRita commented 4 years ago

How to set keys for 1 valid class and background? Plz help

Do you mean that you just need 1 class and ignore the rest? In this case, I think you just need to set the key of the class you need to 0, and the rest of classes to -1.