I need help to find a way to change the labels of my data (ex: change all 5 into 7).
I am using the MNIST dataset, and split it in several subsets that I save in files and load later. For my experiments I cannot directly use the Subsets, I have to save them in a file and load them after. And I cannot change the label before saving the data.
I tried this among other solutions: dataset.targets[dataset.targets == 5] = 7 after loading the data.
But subset has no attribute ‘targets’ and ‘SubsetLoader’ object has no attribute ‘targets’.
Is there a way to change the labels of my subsets then ?
mnist_dataset = datasets.MNIST(root='../data', train=True, download=True, transform=transform)
def random_split_strat(clients):
nb_clients = clients
num_classes = 10
r = []
a = [ran.random() for _ in range(nb_clients)]
s = sum(a)
a = [ i/s for i in a ]
for i in range(len(a)):
r.append(a[i])
mnist_subsets = random_split(mnist_dataset, r, generator=torch.Generator().manual_seed(42))
for i, subset in enumerate(mnist_subsets):
filename = f'mnist_subset_{i}.pt'
torch.save(subset, path+filename)