Shuffle ImageFolder labels

I have to randomly shuffle a fraction of the labels of a dataset. I’ve always done it doing the following (Example on CIFAR10):

train_dataset = dsets.CIFAR10(root='./data', 
                    train=True, 
                    transform=transform_train,
                    download=True)




for i in range(len(train_dataset.targets)):
if (i % 3 == 0):
    train_dataset.targets[i] = random.randint(0, 9)

Particularly, the code above shuffles 33% of the labels. I want to do the same using ImageFolder, prebuilt on torchvision, but somehow it fails to shuffle the labels without giving any error.

In fact, I have checked the training set, and what I noticed was that the label train_dataset[0][1] was unchanged. Furthermore, if I manually try to change it by doing

train_dataset[0][1] = random.randint(0,9)

It gives an error (which makes sense, it is a Tuple)

How can I shuffle a percentage of the labels? Thank you!

Your posted code doesn’t seem to shuffle the dataset, but instead randomly assigns a new label to the current target tensor.
Are you sure you want to use this approach?

I want to shuffle a fraction of the dataset (or randomly reassign the labels), in order to train a neural network on a noisy dataset. And this approach works for CIFAR and MNIST (torchvision) but it fails for ImageFolder. The thing is that I don’t know why it does it, and how to fix it.
Thanks!

For ImageFolder, you could try to manipulate the dataset.samples attribute, which is a list of tuples for the image path and the target.