Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

torchvision.datasets.ImageFolder is giving me a 3×3 grid of images intead of 1 image

my code and output

I can’t figure out why it’s giving me 9 gray images in a 3×3 grid instead of just one color image (original image is not gray and has RGB channels). I have spent 5 hours on this. Thanks for the help.

Here is my code

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

test_path = "asl_data/test/" #path to the folder
test_data = torchvision.datasets.ImageFolder(test_path, transform=torchvision.transforms.ToTensor())
def test32():
    for x, y in test_data:
        print(x.shape)
        x = x.reshape(533,800,3)
        plt.axis("off")
        plt.imshow(x)
        plt.show()
        plt.axis("off")
        plt.imshow(x[:176,:267,:])
        break
test32()

>Solution :

Classic.

You reshape instead of permute.

See this thread on the crucial difference between the two.

Fix:

x = x.permute((1, 2, 0))
plt.imshow(x)

A simple visual example:

x, y = test_data[0]  # take one image
x.shape  # torch.Size([3, 223, 320])

# see the difference
fig, ax = plt.subplots(1,2)
ax[0].imshow(x.numpy().reshape(223, 320, 3))
ax[0].set_title('Wrong reshape instead of permute')

ax[1].imshow(x.permute((1,2,0)))
ax[1].set_title('correctly permuting')

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading