How to create a dataset containing one datapoint and its augmentations?

Hi,
I have an imagenet dataset. I want to extract only one image of the dataset and apply random augmentations on this single data point. So basically the transform I define is as following:

sample_transform = transforms.Compose([
        transforms.RandomResizedCrop(img_size, scale=(0.2, 1.0)),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize(mean=_IMAGENET_RGB_MEANS, std=_IMAGENET_RGB_STDS),
    ])

And assume that my one sample is as follows:

from torch.utils.data import Subset
def extract_sample(data, idx):
    return Subset(data, [idx])
single_sample = extract_sample(data, idx = 0)

Not I want to create a dataset by applying sample_transform, 64 times on single_sample (in order to get 64 different augmentations of single_sample). How can I do that?

Thanks in advance for your help!

You can just iterate 64 times and apply sample_transform(single_sample) inside the loop.

1 Like

Thanks!
Could you also tell me how I can concatenate them in a single final dataset?

You could wrap list of the transformed sample in e.g. a TensorDataset afterwards:

samples = []
for _ in range(64):
    for sample in single_sample:
        samples.append(sample)
        
samples = torch.stack(samples)
print(samples.shape)
# torch.Size([64, 3, 32, 32])

dataset = torch.utils.data.TensorDataset(samples)