I am trying to display an image stored as a pytorch tensor.
trainset = datasets.ImageFolder('data/Cat_Dog_data/train/', transform=transforms)
trainload = torch.utils.data.DataLoader(trainset, batch_size=32, shuffle=True)
images, labels = iter(trainload).next()
image = images[0]
image.shape
>>> torch.Size([3, 224, 224]) # pyplot doesn't like this, so reshape
image = image.reshape(224,224,3)
plt.imshow(image.numpy())
This method is displaying a 3 by 3 grid of the same image, always in greyscale. For example:
How do I fix this so that the single color image is displayed correctly?
That's very odd. Try putting the channels last by permuting rather than reshaping:
image.permute(1, 2, 0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With