I need to use a modified version of data loader in my study.
Assume that I have a basic train loader like this:
train_data = datasets.MNIST(root='../../Data', train=True, download=False, transform=transforms.ToTensor()) train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=False)
First I use it in the beginning.
But then for a different task, I need to add a noise to all samples in train dataset. And then I should be able use the noisy data, using a new data loader.
I can update all samples with noise using below code, but I don’t know how to save that modified train dataset as a data loader and use it later in different code sections.
def noise(x, eps, clip_min, clip_max): eta = torch.FloatTensor(*x.shape).normal_(mean=0,std=eps).to(x.device) adv_x = x + eta if clip_min is not None and clip_max is not None: adv_x = torch.clamp(adv_x, min=clip_min, max=clip_max) return adv_x my_noisy_train_loader = train_loader for i, (image,label) in enumerate(my_noisy_train_loader): image = noise(image,0.3,0,1) #How to update noisy train loader?
Could you please suggest how can I modify the data loader and use afterwards?
Maybe I need to create a custom modified dataset(noisy MNIST dataset lets say), and load this new modified dataset using a new data loader, but I also do not know how to modify and save datasets.MNIST so that I can use it later on.