从数组输出中读取图像无效的形状错误

发布于 2025-02-04 10:53:35 字数 979 浏览 2 评论 0原文

我很陌生地使用Python的图像。我有一个作业,要求我在图像中执行一些预处理,这些图像是从NPZ文件给我的。然后,我将数据分为火车,验证和测试,这就是外观:

train_set = PathMNIST(mode="train") #89996
val_set = PathMNIST(mode="val") #10004
test_set = PathMNIST(mode="test") #7180

print(len(train_set))
print(len(val_set))
print(len(test_set))

集合中的每个项目看起来像这样:

(array([220, 208, 227, ..., 222, 209, 228], dtype=uint8), 0)

因此,据我了解,它是一个元组,其中第一个位置代表图像,第二位置代表标签。我要做的是将阵列阅读为第一个位置的图像,以便我可以对其进行一些预处理。但是,当我尝试绘制它时,我会得到无效的形状错误:

plt.figure(figsize=(10, 10))
for i, (images, labels) in enumerate(train_set):
  ax = plt.subplot(3, 3, i + 1)
  plt.imshow(images)
  plt.title(np.argmax(labels[i]))
  plt.axis("off")

#output: TypeError: Invalid shape (2352,) for image data

所以我想我的问题是:如何将数组读取为图像并这样对待?我看着一些问题和关于它的话题,但我无法做到它适用于我的场景。

I am very new at working with images in Python. I have an assignment which requires me to perform some preprocessing in images, which are given to me from an NPZ file. I have, then, split the data into train, validation and testing, and this is how it looks:

train_set = PathMNIST(mode="train") #89996
val_set = PathMNIST(mode="val") #10004
test_set = PathMNIST(mode="test") #7180

print(len(train_set))
print(len(val_set))
print(len(test_set))

And each item in the sets looks like this:

(array([220, 208, 227, ..., 222, 209, 228], dtype=uint8), 0)

So, as I understand it, it is a tuple, in which the first position represents the image, and the second position represents a label. What I am trying to do is to read the array in the first position as an image so I can apply some pre processings to it. However, when I try to plot it I get an Invalid shape error:

plt.figure(figsize=(10, 10))
for i, (images, labels) in enumerate(train_set):
  ax = plt.subplot(3, 3, i + 1)
  plt.imshow(images)
  plt.title(np.argmax(labels[i]))
  plt.axis("off")

#output: TypeError: Invalid shape (2352,) for image data

So I guess my question is: how do I read that array as an image and treat it as such? I looked at some questions and topics about it, but I cannot make it work for my scenario.

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

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

发布评论

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

评论(1

木格 2025-02-11 10:53:35

您的阵列在2352的尺寸的1d中。

正如克里斯托夫所提到的,MNIST数据集中的每个数据点都是大小的RGB图像(28 * 28 * 3)。它们每个人都被扁平化为2352尺寸的1D数组。

我们可以使用numpy::

b = np.reshape(a, (28, 28,3))

Your array is in 1D of size 2352.

As Christoph mentioned, every data point in the MNIST data set is an RGB image of size (28 * 28 * 3). And each of them have been flattened out to 1D array of size 2352.

We can reshape it using numpy:

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