Matplotlib in cifar10 tutorial does not plot

Hello everyone, I am new in Pytorch.
Recently i would like to try with Pytorch, but when I run the tutorial woth CIFAR10 dataset.
http://pytorch.org/tutorials/beginner/blitz/cifar10_tutorial.html#sphx-glr-beginner-blitz-cifar10-tutorial-py
I could not plot anything. what is the wrong ?
here is the piece of code that I implemented from the tutorial:

import torch
import torchvision
import torchvision.transforms as transforms

########################################################################

The output of torchvision datasets are PILImage images of range [0, 1].

We transform them to Tensors of normalized range [-1, 1].

transform = transforms.Compose(
[transforms.ToTensor(),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))])

trainset = torchvision.datasets.CIFAR10(root=’./data’, train=True,
download=True, transform=transform)
trainloader = torch.utils.data.DataLoader(trainset, batch_size=4,
shuffle=True, num_workers=2)

testset = torchvision.datasets.CIFAR10(root=’./data’, train=False,
download=True, transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=4,
shuffle=False, num_workers=2)

classes = (‘plane’, ‘car’, ‘bird’, ‘cat’,
‘deer’, ‘dog’, ‘frog’, ‘horse’, ‘ship’, ‘truck’)

########################################################################

Let us show some of the training images, for fun.

import matplotlib.pyplot as plt
import numpy as np

functions to show an image

def imshow(img):
img = img / 2 + 0.5 # unnormalize
npimg = img.numpy()
plt.imshow(np.transpose(npimg, (1, 2, 0)))

however, I can plot the categories with name. but no image displayed. The Matplotlib example can be successfully plotted. So annoying and wired. please help .thanks

Hi Daniel,

Are you running the code from command line ?
If you are using Jupyter Notebook Add : %matplotlib inline and execute it before plotting.

Hope it helps.
~Gokkul

THanks a lot.
I am using command line, not using jupyter… it was wired to me . I can plot other examples of Matplotlib…but this piece of code…to see the sample data from CIFAR10…

@dormouse Are you getting any error? i am able to run it locally on my jupyter notebook and see the images plotted. There are no Syntax error in the code too.

Did you run the rest of the code ? :

# get some random training images
dataiter = iter(trainloader)
images, labels = dataiter.next()

# show images 
imshow(torchvision.utils.make_grid(images)) # <------ Here is where plot is called
# print labels
print(' '.join('%5s' % classes[labels[j]] for j in range(4)))

Can you provide more information so that i could help you?

~Gokkul

With command line, use plt.show() to show the images.

Yes i run the code you listed here. There’s no error.i will try with jupyter. I will let you know.

@dormouse Like @SimonW Said you will have to add plt.show() to create a new window that shows the images.

@SimonW Thanks ! Totally forgot that part :slight_smile:

Thanks. that is the case. i was wondering if i have some matplotlib configuration error…you are right, plt.show() is missing.

thanks a lot @Gokkulnath that is the reason.