How to apply transformation on a custom dataset that is sliced from cifar10?

I have used the following code to get only 60% of ‘deer’ class and 60% of ‘dog’ class from Cifar10.

from torchvision.datasets import CIFAR10
from torch.utils.data import Subset

ds = CIFAR10(root='for_custom_dataset/', train=True, download=True)
dog_indices, deer_indices = [], []
dog_idx, deer_idx = ds.class_to_idx['dog'], ds.class_to_idx['deer']

for i in range(len(ds)):
  current_class = ds[i][1]
  if current_class == dog_idx:
    dog_indices.append(i)
  elif current_class == deer_idx:
    deer_indices.append(i)
dog_indices = dog_indices[:int(0.6 * len(dog_indices))]
deer_indices = deer_indices[:int(0.6 * len(deer_indices))]
new_dataset = Subset(ds, dog_indices+deer_indices)

I want to apply transformations on the 'new_dataset ’ which is of type ‘torch.utils.data.dataset.Subset object’.

I am aware of applying transformations on cifar10 directly like,

my_transforms = transforms.Compose([ transforms.ToTensor() ])

train_dataset = datasets.CIFAR10(root='dataset/', train=True, 
                                 transform=my_transforms, download=True)
train_loader = DataLoader(dataset=train_dataset, batch_size=batch_size, shuffle=True)

but, how can I perform the same transformation on ‘new_dataset’ object.

The passed my_transforms will still be applied on the dataset wrapped by the Subset.
Subset is only a thin wrapper, which uses the specified indices and forwards it to the underlying Dataset. The same Dataset.__getitem__ (with a subset of indices) will be used to load and transform the samples.