TypeError: Invalid shape (1, 28, 28) for image data with Matplotlib

Ask Questions Forum: ask Machine Learning Questions to our readersCategory: Other frameworkTypeError: Invalid shape (1, 28, 28) for image data with Matplotlib
Chris Staff asked 1 month ago

I am getting the following error:


# File "C:\Users\chris\Anaconda3\envs\tensorflow_gpu\lib\site-packages\matplotlib\image.py", line 690, in set_data
# .format(self._A.shape))
# TypeError: Invalid shape (1, 28, 28) for image data

It comes from imshow:

plt.imshow(visualization)

Why does this happen?

1 Answers
Best Answer
Chris Staff answered 1 month ago

Simple – imshow expects images to be structured as (rows, columns) for grayscale data and (rows, columns, channels) and possibly (rows, columns, channels, alpha) values for RGB(A) data.

You will thus have to reshape your grayscale visualization image into (28, 28) to make it work.

Your Answer

6 + 1 =