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

In Pytorch,if

for step, (images, labels) in enumerate(data_loader):

Can the images here be loaded into images in Dataloader? Or does it require a corresponding path, and a folder called images with images?


I am not sure to understand the question.
You can give any Dataset you want (ImageFolder or not) and this code will behave exactly the same.

Yes,I use ImageFolder. If I load the dataset in this way, can I use

for step, (images, labels) in enumerate(data_loader): 

to traverse the data in the entire dataset?

def get_base(dataset_root, batch_size, category, datatype):
    """Get Hurricane datasets loader."""
    # image pre-processing
    pre_process = transforms.Compose([transforms.Resize((224,224)),   
                                         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),

    base_dataloader = data.DataLoader(

    return base_dataloader

Yes this will iterate over the whole dataset given you batch_size samples at a time.

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?

IIRC what the Image folder does is to load from the folders, using the folder names as the labels.
So each sample is a pair (image, label) loaded from disk.
Then the dataloader concatenate a bunch of these to make your batch.

Isn’t the data read by ImageFolder in the form of a dictionary?

No I don’t think so.

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?

 for step, (x, y) in enumerate(data_loader):
            images = make_variable(x)
            labels = make_variable(y.squeeze_())


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

OK! But if there are these steps later, do I need to convert tensor x and y into variables?

preds = classifier(encoder(images))
loss = criterion(preds, labels)

No, they should remain Tensors all along.