torchvision.datasets.ImageFolder 给了我一个 3x3 的图像网格,而不是 1 个图像

发布于 2025-01-11 03:09:39 字数 654 浏览 2 评论 0原文

我的代码和输出

我不明白为什么它在 3x3 网格中给我 9 张灰色图像,而不是一张彩色图像(原始图像不是灰色的,有 RGB 通道)。我在这上面花了5个小时。感谢您的帮助。

这是我的代码

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()

my code and output

I can't figure out why it's giving me 9 gray images in a 3x3 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

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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

酷到爆炸 2025-01-18 03:09:39

经典的。

重塑而不是排列

请参阅此帖子了解两者之间的关键区别。

修复:

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

一个简单的视觉示例:

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')

在此处输入图像描述

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文