How to make a "list : target" Custom Dataset?

Hello, I am a Pytorch beginner.
I’m trying to make a custom Dataset which is not simply like “one image: one target" mode.
I want to have a “5 input images : one target image” mode dataset. So how can I do that ?

Thanks very much !

Hey,
You should look at this tutorial. I think it’s what you’re looking for.
But basically you need to create your own dataset class like:

class Dataset(Dataset):
    """dataset."""

    def __init__(self, csv_file, root_dir, transform=None):
        """
        Args:
            csv_file (string): Path to the csv file with names of all images
            root_dir (string): Directory with all the images.
            transform (callable, optional): Optional transform to be applied
                on a sample.
        """
        self.names = pd.read_csv(csv_file)
        self.root_dir = root_dir
        self.transform = transform

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

    def __getitem__(self, idx):
        img_name1 = os.path.join(self.root_dir,
                                self.names.iloc[idx, 0])
        image1 = io.imread(img_name1)
        img_name2 = os.path.join(self.root_dir,
                                self.names.iloc[idx, 1])
        image2 = io.imread(img_name2)
        img_name3 = os.path.join(self.root_dir,
                                self.names.iloc[idx, 2])
        image3 = io.imread(img_name3)
        target_name = os.path.join(self.root_dir,
                                self.names.iloc[idx, 3])
        target = io.imread(target_name)
        sample = {'image1': image1, 'image2': image2, 'image3': image3, 'target': target}

        if self.transform:
            sample = self.transform(sample)

        return sample
1 Like

Nice! I never thought about using dictionary and csv file …
Actually following the tutorial, I just know how to load an exist dataset and some other specific examples.

I will try it ! Thank you !

1 Like