warmspringwinds / pytorch-segmentation-detection

Image Segmentation and Object Detection in Pytorch
MIT License
746 stars 170 forks source link

Changing the number of classes #9

Open aoussou opened 6 years ago

aoussou commented 6 years ago

Hi there,

First, thanks a lot for the good work, it's really useful!

I am trying to train the model on 1 only one class (that class + background) using the code in resnet_34_8s_train.ipynb in a .py file . I am confident my dataset only has one class, so I change the number of class from 21 to 2, but I get the following error after when starting the first iteration:

RuntimeError: cuda runtime error (59) : device-side assert triggered at /opt/conda/conda-bld/pytorch_1518238441757/work/torch/lib/THC/generic/THCStorage.cu:58

I just wanted to make sure that for only 1 class, I should set number_of_classes = 2 instead of 21, and that you are able to make work with you home code work with a different number of classes? The full error is below:

  File "<ipython-input-1-574834e79b43>", line 1, in <module>
    runfile('/home/ft_fcnpt/pytorch-segmentation-detection-master/pytorch_segmentation_detection/recipes/pascal_voc/segmentation/py_version2.py', wdir='/home/john/ft_fcnpt/pytorch-segmentation-detection-master/pytorch_segmentation_detection/recipes/pascal_voc/segmentation')

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 705, in runfile
    execfile(filename, namespace)

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/spyder/utils/site/sitecustomize.py", line 94, in execfile
    builtins.execfile(filename, *where)

  File "/home/ft_fcnpt/pytorch-segmentation-detection-master/pytorch_segmentation_detection/recipes/pascal_voc/segmentation/py_version2.py", line 280, in <module>
    loss.backward()

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/torch/autograd/variable.py", line 167, in backward
    torch.autograd.backward(self, gradient, retain_graph, create_graph, retain_variables)

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/torch/autograd/__init__.py", line 99, in backward
    variables, grad_variables, retain_graph)

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/torch/autograd/function.py", line 91, in apply
    return self._forward_cls.backward(self, *args)

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/torch/nn/_functions/thnn/upsampling.py", line 283, in backward
    grad_input = UpsamplingBilinear2dBackward.apply(grad_output, ctx.input_size, ctx.output_size)

  File "/home/anaconda3/envs/pt27/lib/python2.7/site-packages/torch/nn/_functions/thnn/upsampling.py", line 296, in forward
    grad_output = grad_output.contiguous()
warmspringwinds commented 6 years ago

Hi @nayriz . Thank you for reporting. That's a bug, change this line in your notebook:

logits_flatten = flatten_logits(logits, number_of_classes=21)

In your case, number_of_classes=2

Let me know if that works

aoussou commented 6 years ago

Dear Daniil,

Thank you for your prompt reply. I had already changed that line. I have also changed the following lines:

fcn = resnet_dilated.Resnet34_8s(num_classes=2)

and

number_of_classes = 2

labels = range(number_of_classes)

Did you mean to say I should leave those unchanged and only change the line your mentioned?

Thanks alot!

warmspringwinds commented 6 years ago

Did that help to resolve your problem? You should change everything related to number of classes.

Also check this file: https://github.com/warmspringwinds/pytorch-segmentation-detection/blob/master/pytorch_segmentation_detection/recipes/endovis_2017/segmentation/resnet_18_8s_train.ipynb

It's a training file for a different dataset where we performed binary segmentation (similar to your problem).

aoussou commented 6 years ago

I had already done that when I submitted the issue, so I guess it doesn't solve my problem.

Thank you for pointing out at the other notebook, I'll definitely have a look at it!

warmspringwinds commented 6 years ago

Great! Try it out.

Also bear in mind that your labels should be 0 and 1 (background and your class) in the annotations and 255 for regions that you want to ignore.

and let me know if you need more help

aoussou commented 6 years ago

Thanks a lot, when you say

your labels should be 0 and 1 (background and your class) in the annotations

you mean for the endovis files, right? For the Pascal Voc the labels should be the class number.

Also, it is possible to train the model with resnet_34_8s, right?

warmspringwinds commented 6 years ago

@nayriz I mean that depending on the number of classes in your own dataset you should change all the related variables.

Yes, in Endovis we did it with 2 classes and I thought it might be easier for you as you mentioned that your dataset has 2 classes. And yes, it is possible to do 2 classes with resnet_34

Sorry, the structure is a little bit messy so far -- I will change that in a future.