Images and labels of tiny image net dataset: error (object has no attribute 'image')

Hi all, I have the following code to prepare triplet images and labels of tiny image net dataset

   kwargs = {'num_workers': 0, 'pin_memory': True} if args.no_cuda else {}

    transform = transforms.Compose([transforms.RandomSizedCrop(224),
                                    transforms.RandomHorizontalFlip(),
                                    transforms.ToTensor(),
                                    transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))])
    
    imagenet_traindata = datasets.ImageFolder('./tiny-imagenet-200/train', transform=transform)
    triplet_train_dataset = ImageNet_t(imagenet_traindata,'./tiny-imagenet-200')
    train_set = torch.utils.data.DataLoader(triplet_train_dataset, batch_size=args.train_batch_size, shuffle=True, **kwargs)
    
    imagenet_testdata = datasets.ImageFolder('./tiny-imagenet-200/test', transform=transform)
    triplet_test_dataset = ImageNet_t(imagenet_testdata,'./tiny-imagenet-200')
    test_set = torch.utils.data.DataLoader(triplet_test_dataset, batch_size=args.test_batch_size, shuffle=False, **kwargs)

where ImageNet_t is as

lass ImageNet_t(Dataset):
    
    raw_folder = 'raw'
    processed_folder = 'processed'
    training_file = 'training.pt'
    test_file = 'test.pt'
    train_triplet_file = 'train_triplets.txt'
    test_triplet_file = 'test_triplets.txt'

    def __init__(self, root, ImageNet_dataset, n_train_triplets=2000, n_test_triplets=700, train=True, transform=None):
        self.ImageNet_dataset = ImageNet_dataset
        self.train = train
        self.transform = transform        
        self.root = root
        self.count_n_train_triplets=0
        self.count_n_test_triplets=0

        if self.train:
            print("train")
            self.train_data=self.ImageNet_dataset.image
            self.train_labels=self.ImageNet_dataset.target
            self.make_triplet_list(n_train_triplets)
            triplets = []
            for line in open(os.path.join(root, self.processed_folder, self.train_triplet_file)):
                if self.count_n_train_triplets==n_train_triplets:
                    break
                elif not (line.split()==[]):
                    triplets.append((int(line.split()[0]), int(line.split()[1]), int(line.split()[2]))) # anchor, close, far
                    self.count_n_train_triplets+=1
                else:
                    continue
                    
            self.triplets_train = triplets
.
.
.

in lines
self.train_data=self.ImageNet_dataset.image
self.train_labels=self.ImageNet_dataset.target
I need to obtain the image and its label, but the below error has occurred.

'str' object has no attribute 'image'

how can I do that?
any help would be appreciated.

You are passing a string as the second argument in:

ImageNet_t(imagenet_traindata,'./tiny-imagenet-200')

which will be assigned to ImageNet_dataset in the __init__:

def __init__(self, root, ImageNet_dataset,

which will raise this error, so you might either want to pass the arguments in a different order, remove the root argument etc.

Hi ptrblck, I pass arguments in the below order:

    triplet_train_dataset = ImageNet_t('./tiny-imagenet-200', imagenet_traindata)

the error

in __init__
    self.train_data=self.ImageNet_dataset.image

AttributeError: 'ImageFolder' object has no attribute 'image'

has been occurred, my problem is how can I extract data and labels of the Tiny ImageNet dataset?

The ImageFolder will lazily load the data in its __getitem__, so you won’t be able to directly access an internal attribute with all images.
You could either iterate the dataset or DataLoader and store the images in e.g. a list.

1 Like