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

username: S_M
cooked: <p>Hi all, I have the following code to prepare triplet images and labels of tiny image net dataset</p>
<pre><code class="lang-auto">   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)
</code></pre>
<p>where ImageNet_t is as</p>
<pre><code class="lang-auto">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
.
.
.
</code></pre>
<p>in lines<br>
self.train_data=self.ImageNet_dataset.image<br>
self.train_labels=self.ImageNet_dataset.target<br>
I need to obtain the image and its label, but the below error has occurred.</p>
<pre><code class="lang-auto">'str' object has no attribute 'image'
</code></pre>
<p>how can I do that?<br>
any help would be appreciated.</p>

username: ptrblck
cooked: <p>You are passing a string as the second argument in:</p>
<pre><code class="lang-python">ImageNet_t(imagenet_traindata,'./tiny-imagenet-200')
</code></pre>
<p>which will be assigned to <code>ImageNet_dataset</code> in the <code>__init__</code>:</p>
<pre><code class="lang-python">def __init__(self, root, ImageNet_dataset,
</code></pre>
<p>which will raise this error, so you might either want to pass the arguments in a different order, remove the <code>root</code> argument etc.</p>

username: S_M
cooked: <aside class="quote no-group" data-username="ptrblck" data-post="2" data-topic="116857">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>which will raise this error, so you might either want to pass the arguments in a different order, remove the <code>root</code> argument etc.</p>
</blockquote>
</aside>
<p>Hi ptrblck, I pass arguments in the below order:</p>
<pre><code class="lang-auto">    triplet_train_dataset = ImageNet_t('./tiny-imagenet-200', imagenet_traindata)
</code></pre>
<p>the error</p>
<pre><code class="lang-auto">in __init__
    self.train_data=self.ImageNet_dataset.image

AttributeError: 'ImageFolder' object has no attribute 'image'
</code></pre>
<p>has been occurred, my problem is how can I extract data and labels of the Tiny ImageNet dataset?</p>

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