def imshow(image, ax=None, title=None):
“”“Imshow for Tensor.”“”
if ax is None:
fig, ax = plt.subplots()
# PyTorch tensors assume the color channel is the first dimension
# but matplotlib assumes is the third dimension
image = np.array(image)
image = image.numpy().transpose((1, 2, 0))
# Undo preprocessing
mean = np.array([0.485, 0.456, 0.406])
std = np.array([0.229, 0.224, 0.225])
image = (std * image) + mean
# Image needs to be clipped between 0 and 1 or it looks like noise when displayed
image = np.clip(image, 0, 1)
ax.imshow(image)
return ax
image_path = ‘flowers/train/10/image_07086.jpg’
img = process_image(image_path)
#img.shape
imshow(img)
#(np.array([0.229, 0.224, 0.225])).shape
#img = img.transpose ((1,2,0))
#img
AttributeError Traceback (most recent call last)
in ()
24 img = process_image(image_path)
25 #img.shape
—> 26 imshow(img)
27 #(np.array([0.229, 0.224, 0.225])).shape
28 #img = img.transpose ((1,2,0))
in imshow(image, ax, title)
7 # but matplotlib assumes is the third dimension
8 image = np.array(image)
----> 9 image = image.numpy().transpose((1, 2, 0))
10
11 # Undo preprocessing
AttributeError: ‘numpy.ndarray’ object has no attribute ‘numpy’