0

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.

9
  • You would have to reshape your array before plotting it. Do you know the predefined shape of each image? Commented Jun 4, 2022 at 17:26
  • And what is the length of each such array? Commented Jun 4, 2022 at 17:33
  • mnist data is usually given flattened to (784,) while the original images are (28, 28). you have three times as much data, so you seem to have color information too. Commented Jun 4, 2022 at 18:47
  • 1
    @Pelicer You could reshape your array using numpy: b = np.reshape(a, (28, 28,3)), but you would need to check the channel order Commented Jun 4, 2022 at 19:12
  • 1
    @Pelicer Every image has 3 channels depicting R,G and B. Each of these channels is an array of size (28*28), so that's why the shape: (28 * 28 *3). I do not know the order in which to place the channels, whether it should be RGB or BGR. Commented Jun 4, 2022 at 19:22

1 Answer 1

1

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.