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.