Generating masks the same size as the test image

How can I generate masks that are the same size as the input image? For example in a segmentation example I train a model with say size 256. The predictions of the masks are then 256 by 256 in size.

However if the test image is say 412 by 320 and you have to generate a mask that is the same size as the test image, how would you do that?

Appreciate any suggestions thanks.

Have you tried passing in a 412 x 320 image? You would need a custom pipeline to generate the image itself (IE PILImage -> Resize(412x320) -> Normalize.

And then potentially do a learn.dls.decode_batch() passing in the input tensor and output tensor

(I can provide some pseudo/actual code if wanted)

1 Like

Hey Zack, I did pass the 412 X 320 image but the generated mask is 256 X 256. I also tried using resize but get errors stating that it only works with 3D,4D and 5D tensors.

So to understand your thoughts (and images in the test set vary so not all images are the same size):

learn predict generates a tensor, covert that to a PILImage and then resize? I think I tried this but get the same error as before (probably implementing incorrectly)

Pseudo code would be awesome!

1 Like

The issue is the architecture itself has a specified image size when it is made. (Check DynamicUnet’s init). I’d need to play around with some ideas on increasing the size while reusing the same model. Otherwise patches may be needed.

1 Like

Appreciate any help will check out the DynamicUnet’s init. I do have some psedi code that I can share with the error messages but won’t be able to do that until later today

I was able to solve getting the predictions to be the same size as the test images the following way although it may not be the most efficient way.

Creating a function to get the image size of each image in the test set and then re-create the dataloader re-sized to the the input test image and then get the mask prediction using item_tfms

item_tfms = [ToTensor, Resize((ims, iml))] where ims and iml are image dimensions. Here is the full code snippet

def resize_inf(image_path, save_path):
    test_imgs = get_image_files(image_path)
    for i in test_imgs:
        img_name = str(i)
        img_name = img_name.split(os.sep)[-1]; seg_name = img_name.split('.')[0]
        r =
        ims = r.shape[0]; iml = r.shape[1]
        print(ims, iml)
        item_tfms = [ToTensor, Resize((ims, iml))]
        tnsc = DataBlock(blocks=(ImageBlock, MaskBlock),
        dls = tnsc.dataloaders(path/'imageCOPY', bs=8, num_workers=0)
        learn = Learner(dls, model, metrics=[dice, jacard], config=config, cbs=[GetResult(), ShowGraphCallback(), ActivationStats()]).to_fp16()
        j = learn.predict(i); t = TensorImage(j[0])
        plt.imsave(save_path + f'{seg_name}.PNG', t)