Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Masks format #51

Closed
mehditlili opened this issue Apr 12, 2019 · 4 comments
Closed

Masks format #51

mehditlili opened this issue Apr 12, 2019 · 4 comments

Comments

@mehditlili
Copy link

mehditlili commented Apr 12, 2019

I am trying to use this to train on my own dataset. I couldn't figure out a way to access the data on kaggle without having to download the 24Gb zips. How are the GIF files formed?

In my data I currently have maks with class id's coded into them. I have two classes so my mask is a single channel image with 1's and 0's. But from the error I get when I start the training, it seems that I need a multichannel image with a channel for each class, can you confirm that?

EDIT:
I just set n_classes to 1 instead of 2 and it worked. Can you clarify why for classes 0 and 1 n_classes has to be set to 1?

@milesial
Copy link
Owner

milesial commented Apr 12, 2019

As I see it, with only 0 and 1 in the masks, you only have 1 class: the class with the ones. For the kaggle dataset the class was 'car'. The zeros do not correspond to any class. But you could see the zeros as the class 'not a car' as well, so there would be 2 "classes".

It just depends on how you define your classes and your problem. If you would have two types of objects to segment, you might have an additional id that correspond to 'nothing', here that id is 0.

@mehditlili
Copy link
Author

mehditlili commented May 21, 2019

I now have two classes + background, my mask images have 3 values, 0 for background, 1 for the first class and 2 for the second class. however when I set n_classes to 2 this happens:

Starting epoch 1/1000.
/home/vision/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/upsampling.py:129: UserWarning: nn.Upsample is deprecated. Use nn.functional.interpolate instead.
  warnings.warn("nn.{} is deprecated. Use nn.functional.interpolate instead.".format(self.name))
/home/vision/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py:1332: UserWarning: nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.
  warnings.warn("nn.functional.sigmoid is deprecated. Use torch.sigmoid instead.")
/home/vision/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py:2016: UserWarning: Using a target size (torch.Size([921600])) that is different to the input size (torch.Size([1843200])) is deprecated. Please ensure they have the same size.
  "Please ensure they have the same size.".format(target.size(), input.size()))
Traceback (most recent call last):
  File "train.py", line 139, in <module>
    img_scale=args.scale)
  File "train.py", line 80, in train_net
    loss = criterion(masks_probs_flat, true_masks_flat)
  File "/home/vision/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/module.py", line 489, in __call__
    result = self.forward(*input, **kwargs)
  File "/home/vision/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/modules/loss.py", line 504, in forward
    return F.binary_cross_entropy(input, target, weight=self.weight, reduction=self.reduction)
  File "/home/vision/anaconda3/envs/pytorch/lib/python3.7/site-packages/torch/nn/functional.py", line 2019, in binary_cross_entropy
    "!= input nelement ({})".format(target.numel(), input.numel()))
ValueError: Target and input must have the same number of elements. target nelement (921600) != input nelement (1843200)

Not sure whad I should do

@milesial
Copy link
Owner

You have to separate your target masks into 2 layers with values 0 or 1 (black and white). So you would have a target tensor with 2 channels:

  • mask == 1 (first class) -> to int values (0 or 1)
  • mask == 2 (second class) -> to int values (0 or 1)

After this processing your mask tensor should be HxWxC, with C=2 for you.
In the error you can see that pytorch expects twice as many values in the target mask, that should come from these two channels.

@mehditlili
Copy link
Author

ah I see, so I did understand it correctly at the beginning

it seems that I need a multichannel image with a channel for each class, can you confirm that?

Thank you

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants