[Custom Data Class] How to Overlay PILMask on PILImage in show method?

I am writing a custom data class that could show an image together with its mask. The plot I want is something like the visualization that you see in CAMVID example, except I wanna implement a custom data class to visualize that (I needa write a custom data pipeline because my data structure is a bit hard to fit the default DataBlock setting).

That’s an example of visualization I want (from CAMVID):
1

And here is an example of what my data looks like (its from Kaggle Ship Detection competition):

I simply implemented a custom data class as follow that intake PILImage and PILMask and show them as one image with two items overlay:

class LabeledImage(Tuple):
    def show(self, ctx = None, **kwargs):
        img, mask = self
        ctx = mask.show(alpha = 0.5)
        return img.show(ctx = ctx)

But it seems not to work, that’s what I got when showing the instance:

3

Could any fellows give me a guide on how to make it work?

1 Like

@riven314, try the reverse order (first ctx = img.show() and then return mask.show(ctx = ctx)) as shown here below:

class LabeledImage(Tuple):
    def show(self, ctx = None, **kwargs):
        img, mask = self
        ctx = img.show()                     # <--- 1
        return mask.show(ctx = ctx)          # <--- 2
4 Likes

@farid
Thanks so much! it works now

2

2 Likes