you can write a custom Dataset that not only returns the images but also their ids / paths.
For example:

  
      github.com
  
  
    pytorch/vision/blob/master/torchvision/datasets/folder.py#L122 554



def __getitem__(self, index):
    """
    Args:
        index (int): Index


    Returns:
        tuple: (image, target) where target is class_index of the target class.
    """
    path, target = self.imgs[index]
    img = self.loader(path)
    if self.transform is not None:
        img = self.transform(img)
    if self.target_transform is not None:
        target = self.target_transform(target)


    return img, target


def __len__(self):
    return len(self.imgs)



  
  
    
    
  
  


can be
return image, target, path

or
return image, target, index