If the target is encoded in the file name, I would recommend to write a custom Dataset.
Something like this should work:
class MyDataset(Dataset):
    def __init__(self, image_paths, transform=None):
        self.image_paths = image_paths
        self.transform = transform
    
    def get_target_from_path(self, path):
        # Implement your target extraction here
        return torch.tensor([0])
    
    def __getitem__(self, index):
        x = Image.open(self.image_paths[index])
        y = self.get_target_from_path(self.image_paths[index])
        if self.transform:
            x = self.transform(x)
        
        return x, y
    
    def __len__(self):
        return len(self.image_paths)

If you would also like to return the image names, just them to the return statement in __getitem__.