Home > Back-end >  Reading image from array outputs invalid shape error
Reading image from array outputs invalid shape error

Time:06-05

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.

CodePudding user response:

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))
  • Related