Including the PIL filename in transforms

I am trying to include the filename path in a custom image transform (so that I can compare an image with its binary map during loading) but the PIL image object does not include the filename of the image. I am wondering if there is any way to pass the corresponding file path to the transform with the PIL image.

You can use dataloader. Set the dataloader in such a way that it can return image, binary mask and image filename. I am assuming there are two folders, one for image and other for masks and both have same filenames.

class DataLoaderEx(Dataset):
    
    def __init__(self,img_path,mask_path):
               self.img_path = img_path
               self.mask_path = mask_path
               self.files = glob.glob(img_path + '*.bmp')
    def __len__(self):
              return len(self.files)
    def __getitem__(self, index):
             img_path = self.files[index]
            mask_path = os.path.join(self.mask_path,os.path.basename(img_path))
             # Here you can read the image using PIL with img and mask path
            return image,mask.filename

Hope this one helps.