I am working on a segmentation task, where my original images and masked images are in separate folder namely images and segmentation. I tried to build the DataBlock with the code,
This is a bit of a guess, so my explanation may not be 100%
But I think @muellerzr is right that is the fact you are using ImageBlock and not MaskBlock
The check is making sure the predicted mask is the same size as your labels
(Assuming that your images are colour) As you have passed in your labels as images with the ImageBlock, they still have the 3 colour channels.
So when fastai flattens them to check that they are the same size as your 1 channel predicted mask it is 3 times larger than it should be
1204224 / 401408 = 3
This might be wrong, so I would suggest you double check your self - using the ipython debug to confirm the types/ shapes of the variables should give you the answer.
Oh and just a little extra thing, your loss function isn’t Dice. It’s nn.MSELoss()
Your metric is Dice, which won’t effect how your model trains, just what get printed out
I think Dice loss has been added in the latest update, but don’t quote me!
Ok I think (not totally sure) that if your segmented images are in a separate folder, you need a label_func which can map from the input image path to the output image path.
def label_func(o):
""" takes in a file as found from `get_image_files`
and maps to the path name of the segmented image
"""
return Path("segmented_images") / o.name # or whatever is right for your directory setup.
DataBlock(blocks=(ImageBlock, ImageBlock),
splitter=RandomSplitter(0.2, seed=42),
get_items=get_image_files,
get_y=label_func,)
Trying out different things, but the pain point is I have my segmented images in a separate folder. Thanks for your answer, it really helps and now I was able to figure out where I was wrong.