plt.imshow() 中图像数据的尺寸无效
Invalid dimension for image data in plt.imshow()
我正在使用 mnist 数据集在 keras 背景下训练胶囊网络。
训练后,我想显示来自 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格式的图片。求助!
根据@sdcbr 的评论,使用 np.sqeeze 减少了不必要的维度。如果图像是二维的,则 imshow 函数可以正常工作。如果图像有 3 个维度,那么你必须减少额外的 1 个维度。但是,对于更高暗淡的数据,您必须将其减少到 2 暗淡,因此 np.sqeeze 可能会应用多次。 (或者你可以使用一些其他的暗淡减少函数来获得更高的暗淡数据)
import numpy as np
import matplotlib.pyplot as plt
img_path = x_test[1]
print(img_path.shape)
if(len(img_path.shape) == 3):
plt.imshow(np.squeeze(img_path))
elif(len(img_path.shape) == 2):
plt.imshow(img_path)
else:
print("Higher dimensional data")
您可以使用 tf.squeeze
从张量的形状中删除大小为 1 的维度。
plt.imshow( tf.shape( tf.squeeze(x_train) ) )
示例:
plt.imshow(test_images[0])
TypeError: Invalid shape (28, 28, 1) for image data
更正:
plt.imshow((tf.squeeze(test_images[0])))
Number 7
matplotlib.pyplot.imshow()
不支持形状 (h, w, 1)
的图像。只需将图像重塑为 (h, w)
即可删除图像的最后一个维度:newimage = reshape(img,(h,w))
.
我正在使用 mnist 数据集在 keras 背景下训练胶囊网络。 训练后,我想显示来自 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格式的图片。求助!
根据@sdcbr 的评论,使用 np.sqeeze 减少了不必要的维度。如果图像是二维的,则 imshow 函数可以正常工作。如果图像有 3 个维度,那么你必须减少额外的 1 个维度。但是,对于更高暗淡的数据,您必须将其减少到 2 暗淡,因此 np.sqeeze 可能会应用多次。 (或者你可以使用一些其他的暗淡减少函数来获得更高的暗淡数据)
import numpy as np
import matplotlib.pyplot as plt
img_path = x_test[1]
print(img_path.shape)
if(len(img_path.shape) == 3):
plt.imshow(np.squeeze(img_path))
elif(len(img_path.shape) == 2):
plt.imshow(img_path)
else:
print("Higher dimensional data")
您可以使用 tf.squeeze
从张量的形状中删除大小为 1 的维度。
plt.imshow( tf.shape( tf.squeeze(x_train) ) )
示例:
plt.imshow(test_images[0])
TypeError: Invalid shape (28, 28, 1) for image data
更正:
plt.imshow((tf.squeeze(test_images[0])))
Number 7
matplotlib.pyplot.imshow()
不支持形状 (h, w, 1)
的图像。只需将图像重塑为 (h, w)
即可删除图像的最后一个维度:newimage = reshape(img,(h,w))
.