Change image labels when using pytorch
I am loading an image dataset with pytorch as seen below:
dataset = datasets.ImageFolder('...', transform=transform)
loader = DataLoader(dataset, batch_size=args.batchsize)

The dataset is i na folder with structure as seen below:
dataset/
  class_1/
  class_2/
  class_3/

So in result each image in class_1 folder has a label of 0..etc.
However i would like to change these labels and randomly assign a label to each image in the dataset. What i tried is:
 new_labels = [random.randint(0, 3) for i in range(len(dataset.targets))]
 dataset.targets = new_labels

This however does not change the labels as i wanted due to some errors later in model training.
Is this the correct way to do it or is tehre a more appropriate one?

You can have a transformation for the labels:
import random

class rand_label_transform(object):
  def __init__(self, num_labels):
    self.num_labels = num_labels

  def __call__(self, labels):    
    # generate new random label
    new_label = random.randint(0, self.num_labels - 1)
    return new_label

dataset = datasets.ImageFolder('...', transform=transform, target_transform=rand_label_transform(num_labels=3))

See ImageFolder for more details.
