Converting RGB Masks to masks with class index for segmentation

I think I was able figure out how to fix this issue. Basically I added this three lines to the mask_to_class function to convert my input image which was in PIL format to an np arrray and kept everything else the same as this post:

mask = np.array(pilImage) #convert from pil Image to numpy array

mask = mask[...,:3]#remove alpha values

target = torch.from_numpy(mask)

I then called the mask_to_class function in the transform function in place of the mask = TF.to_tensor(mask).

After doing this I don’t get the error anymore and len(colors) is 2 as expected, but I do get this error:

input and target batch or spatial sizes don't match: target [1 x 1000 x 750], input [1 x 2 x 750 x 10
00]

If anyone has an idea on how to deal with this let me know