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 !
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()
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.