plt.imshow()中图像数据的无效尺寸

问题描述 投票:4回答:1

我正在使用mnist数据集在喀拉斯背景下训练胶囊网络。训练后,我想显示mnist数据集中的图像。为了加载图像,使用了mnist.load_data()。数据存储为(x_train,y_train),(x_test,y_test)。现在,为了可视化图像,我的代码如下:

img_path = x_test[1]  
print(img_path.shape)
plt.imshow(img_path)
plt.show()

代码给出的输出如下:

(28, 28, 1)

以及以下关于plt.imshow(img_path)的错误:

TypeError: Invalid dimensions for image data

如何显示png格式的图像。救命!

python-3.x matplotlib keras typeerror mnist
1个回答
0
投票

根据使用np.sqeeze的@sdcbr的注释,减少了不必要的尺寸

import numpy as np  
import matplotlib.pyplot as plt
plt.imshow(np.squeeze(img_path))
© www.soinside.com 2019 - 2024. All rights reserved.