Change labels in Data Loader


(Shangeth Rajaa) #1

I have a data set of images, labels . I took a subset of it and want to change the labels of the whole subset to a single label.

eg: MNIST 0,1,2,3,4,5,6,7,8,9 ; lets say i want labels of 5,6,7,8,9 be 5. so final data labels be 0,1,2,3,4,5.

How to do it?


#2

You could set the new value using a condition on your targets:

dataset = datasets.MNIST(
    root='PATH',
    transform=transforms.ToTensor()
)

dataset.targets[dataset.targets > 5] = 5
print(dataset.targets.unique())
> tensor([0, 1, 2, 3, 4, 5])

(Shangeth Rajaa) #3

AttributeError: ‘MNIST’ object has no attribute ‘targets’


#4

In older torchvision versions, you had to use train_labels or test_labels depending if the train argument was set to True or False, respectively.