`show_image` displays color image for MNIST_SAMPLE dataset

Hi everyone!

I did not find this question in the forum so I’ve decided to open a new topic (please let me know if this has already been answered elsewhere).

I’m following along with the Chapter 4 code in a new notebook from scratch and am running into an issue. When I use show_image to display an image, I get an image with pixel colors of purples and yellows instead of black and white. When I run the Chapter 4 colab notebook (from the course site) with the same code, I see the black and white image as expected.

Here’s the colab I’m working in: https://colab.research.google.com/drive/19y5b86lfir64Arzj3_fQZGnsB-C-0AUI?usp=sharing

And here’s the code:

!pip install fastai --upgrade -q
!pip install fastcore --upgrade -q

path = untar_data(URLs.MNIST_SAMPLE)
Path.BASE_PATH = path

threes = (path/'train'/'3').ls().sorted()
sevens = (path/'train'/'7').ls().sorted()

seven_tensors = [tensor(Image.open(o)) for o in sevens]
three_tensors = [tensor(Image.open(o)) for o in threes]

stacked_sevens = torch.stack(seven_tensors).float()/255
stacked_threes = torch.stack(three_tensors).float()/255

mean3 = stacked_threes.mean(0)
show_image(mean3);
show_image(three_tensors[1]);

Here’s a screenshot of the output:

Screen Shot 2020-09-14 at 8.05.18 AM

Even when I use show_image for the .png file, I see the same issue:

Screen Shot 2020-09-14 at 8.11.16 AM

Thanks!

Vishal

I found a solution—I can assign cmap="binary" and show_image will display the black and white image.

Screen Shot 2020-09-14 at 8.56.55 AM

The chapter 4 notebook, has this line:

matplotlib.rc('image', cmap='Greys')

it sets the default color map (see https://stackoverflow.com/questions/33185037/how-to-set-default-colormap-in-matplotlib). Unless you do that you would have to specify it as you suggest below.

Instead of binary, you might want to use gray, e.g:

show_image(img,cmap='gray')
4 Likes

ah thanks I totally missed that! I see it now.