0

I'm working with matplotlib and I'm having trouble with a log message. My code is as follows

import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline


# obtain one batch of training images
dataiter = iter(train_loader)
images, labels = dataiter.next()
images = images.numpy() # convert images to numpy for display
classes = ['melanoma', 'nevus', 'seborrheic_keratosis']

# plot the images in the batch, along with the corresponding labels
fig = plt.figure(figsize=(25, 4))
for idx in np.arange(20):
    ax = fig.add_subplot(2, 20/2, idx+1, xticks=[], yticks=[])
    plt.imshow(np.transpose(images[idx], (1, 2, 0)))
    ax.set_title(classes[labels[idx]])

where my images have been 'normalized', that is divided by a number and scaled, such that the means of the three channels R,G,B are 0.485, 0.456 and 0.406 respectively, while the standard deviations of the three channels are 0.229, 0.224 and 0.225 respectively.

So what I get, is this log message: Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers)

Now, yes, I've had a look at this and tried the solution given there, changing the last second line of my code to plt.imshow(np.transpose(images[idx].astype('uint8'), (1, 2, 0)))

However, doing that drastically changes my images color shades.

How do I fix it? Oh, and yes I am able to get rid of the log message ofcourse, after following what was here. However, I'm not sure if that's a good workaround. My images are displayed fine with the log message and so suppressing the message, I thought, was not a bad idea. But I'm looking for confirmation and/or a fix such that there is no message generated at all.

Ayush
  • 1,510
  • 11
  • 27

1 Answers1

2

You said you tried plt.imshow(np.transpose(images[idx].astype('uint8'), (1, 2, 0)))

From that line I can understand you are using octal base.
Numpy requires float between 1 to 0 or int between 255 to 0.

So if you'll divide it by 8 like that:

plt.imshow(np.transpose(images[idx].astype('uint8'), (1/8, 2/8, 0/8)))

it should work.

Ayush
  • 1,510
  • 11
  • 27
huck dupr
  • 36
  • 5