Hi, I am wondering is there a way to access the targets attributes for dataset which is imported by ImageFolder? I have a training set with 6 classes: building, forest, sea, street, glacier, and mountain. I only want to preserve the forest class label and mark the rest to unforest. I tried this:
dataset.targets[dataset.targets != 1] = 0
which didn’t work. Because it said it doesn’t have attribue targets
Maybe you are using an older version.
Could you update torchvision and check that attribute again?
Also note that dataset.targets is a Python list in ImageFolder, so this indexing won’t work and you should cast it to a tensor before:
Could be. I’m usually just install torchvision from source, as it is really easy and gives you all the new features.
You would have to clone the repo and just run python setup.py install as described here.
Hi,I have a follow-up question on that. I successfully convert the target’s attributes. But when I load the data with dataloader it still preserves the original targets.
import os
import matplotlib.pyplot as plt
import numpy as np
import torch
import torchvision.datasets as datasets
import torchvision.transforms as transforms
from torch.utils.data import Subset, DataLoader
def LoadCIFAR10_py():
strDirPath = os.getcwd() + "\\DATA\\"
aTrainData = datasets.CIFAR10(strDirPath, train=True, download=True)
aTestData = datasets.CIFAR10(strDirPath, train=False, download=True)
print("DATASET: Train=", len(aTrainData), " Test=", len(aTestData))
#print(type(aTrainData), aTrainData)
#print(aTestData[0])
return aTrainData, aTestData
def CreateBinarySubset(aData, nLabel1, nLabel2):
# We create a tensor that has `True` at an index if the sample belongs to class 1
idxLabel1 = torch.tensor(aData.targets) == nLabel1
# Similarly, this tensor has `True` at an index if the sample belongs to class 8
idxLabel2 = torch.tensor(aData.targets) == nLabel2
# print(idxLabel1.shape, idxLabel2.shape)
# Merge these two so that we have one Boolean tensor that has True at the index
# where the sample is of class Auto or Truck, and False otherwise.
index_mask = idxLabel1 | idxLabel2
data_indices = index_mask.nonzero().reshape(-1)
oDataSubset = Subset(aData, data_indices)
oDataloader = DataLoader(oDataSubset, shuffle=False, batch_size=8, num_workers=2)
return oDataloader, oDataSubset
if __name__ == "__main__":
## MAIN
aTrainData, aTestData = LoadCIFAR10_py()
### Here is extracted labels 1 and 9
oTrainDataloader, oTrainDataSubset = CreateBinarySubset(aTrainData, 1, 9)
print('Training subset: ', len(oTrainDataSubset))
oTestDataloader, oTestDataSubset = CreateBinarySubset(aTestData, 1, 9)
print('Testing subset: ', len(oTestDataSubset))
# Problem:
# Now i want to change the label 9 to label 0
# oTestDataSubset does not have target
#this is not working
aTrainData.targets[aTrainData.targets == 9] = 10 ## not working
Also, your code is quite hard to read as you haven’t formatted it. You can post code snippets by wrapping them into three backticks ```, which would make it easier to debug your issue.