Yes, I am dealing with a segmentation task. There is a choice to load whole dataset from ssd to ram or dynamically load the images. The idea is to save memory. I know now what should I do, I should load the masks using get_target_from_path, instead of, mask = Image.open(mask_file[0]).