Error while running Pytorch DataLoading tutorial

I am trying to use my custom dataset and hence downloaded the the code data_loading_tutorial available in the Pytorch tutorials.

The code works fine till the line:

transformed_dataset = FaceLandmarksDataset(csv_file=‘faces/face_landmarks.csv’,
_ root_dir=‘faces/’,

_ transform=transforms.Compose([
Rescale(256),
_ RandomCrop(224),
_ ToTensor()
_ ]))

I skipped the part of manual iteration after that using the for loop and tried to run the rest of the code from
dataloader = DataLoader(transformed_dataset, batch_size=4,
shuffle=True, num_workers=4)

It shows the error : BrokenPipeError: [Errno 32] Broken pipe

To point out more precisely ,I would like to inform that the dataloader object is created. The error is basically created from enumerate(dataloader)

I cannot understand what might be the reason of this. Please suggest a way around this . Thanks !

Are you using Windows for this code?
If so, you should protect your code with

if __name__=='__main__':
    run()

Otherwise Windows might try to create processes recursively, yielding your error.
Let me know, if this is the case.

Yes I am using Windows 10.
Can you please tell me where I should include the two lines ?

You could add this line at the bottom of your code, if you wrap the other code into the run() function.
Alternatively, you could wrap your whole code into the if-statement.
The important point is, that your main code should be executed inside this if-statement.
Otherwise Windows will execute the whole script, which will create processes recursively.
Example:

import torch
...

def run():
    model = ...
    # training
    for batch_idx, (data, target) in enumerate(loader):
        ...

if __name__=='__main__':
    run()
1 Like

Its working , but a new issue is that the images which where supposed to be shown , are not being shown :confused:

In fact, I suspect that the part in the if block is not being run at all.

How are you visualizing the images?


This is the code:

if __name__=='main':
    
    def show_landmarks_batch(sample_batched):
        """Show image with landmarks for a batch of samples."""
        images_batch, landmarks_batch = \
                sample_batched['image'], sample_batched['landmarks']
        batch_size = len(images_batch)
        im_size = images_batch.size(2)
    
        grid = utils.make_grid(images_batch)
        plt.imshow(grid.numpy().transpose((1, 2, 0)))
    
        for i in range(batch_size):
            plt.scatter(landmarks_batch[i, :, 0].numpy() + i * im_size,
                        landmarks_batch[i, :, 1].numpy(),
                        s=10, marker='.', c='r')
    
            plt.title('Batch from dataloader')
    
    
    for i_batch, sample_batched in enumerate(dataloader):
      print(i_batch, sample_batched['image'].size(),
          sample_batched['landmarks'].size())
    
    # observe 4th batch and stop
      if i_batch == 3:
        plt.figure()
        show_landmarks_batch(sample_batched)
        plt.axis('off')
        plt.ioff()
        plt.show()
        break

Basically it shows a batch of 4 images using matplotlib with the help of the function show_landmarks_batch.
Its a part of the code in Pytorch tutorials in the Pytorch website.

I did this, but nothing shows up…