For step, (images, labels) in enumerate(data_loader)

username: ky_Pa
cooked: <p>In Pytorch,if</p>
<pre><code class="lang-auto">for step, (images, labels) in enumerate(data_loader):
</code></pre>
<p>Can the images here be loaded into images in Dataloader? Or does it require a corresponding path, and a folder called images with images?</p>

username: albanD
cooked: <p>Hi,</p>
<p>I am not sure to understand the question.<br>
You can give any Dataset you want (ImageFolder or not) and this code will behave exactly the same.</p>

username: ky_Pa
cooked: <p>Yes,I use ImageFolder. If I load the dataset in this way, can I use</p>
<pre><code class="lang-auto">for step, (images, labels) in enumerate(data_loader): 
</code></pre>
<p>to traverse the data in the entire dataset?</p>
<pre><code class="lang-auto">def get_base(dataset_root, batch_size, category, datatype):
    """Get Hurricane datasets loader."""
    # image pre-processing
    pre_process = transforms.Compose([transforms.Resize((224,224)),   
                                     transforms.ToTensor(),   
                                     transforms.Normalize(
                                         mean=(0.485, 0.456, 0.406),
                                         std=(0.229, 0.224, 0.225)
                                     )])

    # datasets and data_loader
    base_dataset = datasets.ImageFolder(               
        os.path.join(dataset_root, 'samples', category, datatype),
        transform=pre_process)

    base_dataloader = data.DataLoader(
        dataset=base_dataset,
        batch_size=batch_size,
        shuffle=True,
        drop_last=True,
        num_workers=4)

    return base_dataloader</code></pre>

username: albanD
cooked: <p>Yes this will iterate over the whole dataset given you <code>batch_size</code> samples at a time.</p>

username: ky_Pa
cooked: <p>Thank you!But I want to know whether (images, labels) is named by myself or depends on ImageFolder? Isn’t the data read by ImageFolder in the form of a dictionary? Do the keys and values in the dictionary have specific names?</p>

username: albanD
cooked: <p>IIRC what the Image folder does is to load from the folders, using the folder names as the labels.<br>
So each sample is a pair (image, label) loaded from disk.<br>
Then the dataloader concatenate a bunch of these to make your batch.</p>
<blockquote>
<p>Isn’t the data read by ImageFolder in the form of a dictionary?</p>
</blockquote>
<p>No I don’t think so.</p>

username: ky_Pa
cooked: <p>Thank you very much! I almost understand what you mean.In other words, the default form of loading from the disk using the Image folder is a pair (image, label). When I use for loop,the two positions in parentheses( _ , _ ) can be named arbitrarily such as x, y.Then convert x and y to variables, where x and y correspond to the images in the folder and folder names respectively.Right?</p>

username: ky_Pa
cooked: <pre><code class="lang-auto"> for step, (x, y) in enumerate(data_loader):
            images = make_variable(x)
            labels = make_variable(y.squeeze_())</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p>Yes. Note that you don’t need to make Variables anymore in pytorch. So the Tensor <code>x</code> and <code>y</code> should be already good to be used in your net (you might want to send them to the GPU if you use one with <code>x = x.cuda()</code> but that’s it).</p>

username: ky_Pa
cooked: <p>OK! But if there are these steps later, do I need to convert tensor x and y into variables?</p>
<pre><code class="lang-auto">preds = classifier(encoder(images))
loss = criterion(preds, labels)</code></pre>

username: albanD
cooked: <p>No, they should remain Tensors all along.</p>