Load Segmentation Masks using Run-length Encoding

Hi everyone,

After lesson 3 of the fast.ai course I wanted to train an image segmentation model on the Airbus Ship Detection dataset from Kaggle.

I tried using the SegmentationLabelList to import the data but it only allows to provide the labels (segments) as file names of the corresponding mask images. The problem is that the ship detection dataset provides the masks using Run-length Encoding.

My first intuition was to implement my custom SegmentationLabelList class where I replace the open(self, fn) function by replacing the open_mask function by the open_mask_rle function. The former opens the mask from a file name and the latter creates the makes from the run-length encoded mask. The full code of my custom class is:

class EncodedSegmentationLabelList(SegmentationLabelList):
  "`ItemList` for encoded segmentation masks."
  def open(self, fn): return open_mask_rle(fn, shape=(768, 768))

Note that 768 is used because this is the image size of the dataset.

However, when I try to create an item list as

src = (EncodedSegmentationLabelList.from_df(path=path, df=df_segment, folder='train', cols='ImageId')
        .label_from_df(cols='EncodedPixels', classes=codes))

I get the following error message:

Edit: the error code does not seem to be produced by the masks itself but by the classes member of the SegmentationLabelList class. My codes variable is codes = array(['no ship', 'ship']) which is length 2.

Does anyone have an idea how to proceed?

Thank you!

Hello @markkvdb,

I ran into the same problem as you when training for the carvana cars competition. My solution is far from optimal as I failed to load the RLE masks at runtime, what I did is convert all the masks into png images and then use the standard SegmentationLabelList.

You can take a look my code here


Hi @lgvaz,

Thanks for the code and I will apply your approach until I found a way to create a custom ItemList class.

1 Like

Please share that solution when you figure it out :smiley: