How make customised dataset for semantic segmentation?

There are some minor issues in your code:

  • The error is thrown, since you are returning self.len in __len__, without defining it. You should rather return something like len(self.image_list) (, which won’t currently work, but that’s the next point).
  • You are loading all images in __init__ without storing image_list. Your __getitem__ won’t be able to call self.image_list to open an image. However, even if you properly stored image_list using self.image_list = [], you are trying to open the image again. I would recommend to just store image paths in __init__ and load the images in __getitem__.
  • Currently you are not using any target. It seems just the data is loaded without the segmentation masks.
  • Dataset.random_split is not defined by default. You could split the image/mask paths and create two separate Datasets, one for training and another for validation.

Here is a small example I’ve written some time ago.
Let me know, if you can use it as a starter code and adapt it to your use case.

2 Likes