首页 > 解决方案 > 如何从我的测试数据生成器显示错误分类在 keras 中的图像?

问题描述

我正在使用图像数据生成器进行二进制分类。我已经建立了我的模型,现在我想显示按我的模式错误分类的图像。

我的代码如下。我正在尝试构建一个分类错误的图像和标签,然后从我的测试生成器中显示 100 个图像。

images = []
labels = []
for i in range(100):
    img, label = next(val_generator)
    img = img_to_array(img[i])
    img = np.expand_dims(img, axis=0)
    result = model.predict_classes(img)
    if result != label[i]:
        images.append(img)
        labels.append(label)

我想显示图像[]。编码:

images[0]
images[0].reshape(-1)
img0 = array_to_img(images[0])
plt.plot(images[0])

我得到错误:

Traceback (most recent call last):

  File "<ipython-input-723-c79b5280f8fc>", line 1, in <module>
    img0 = array_to_img(images[0])

  File "/home/idu/.local/lib/python3.6/site-packages/keras/preprocessing/image.py", line 184, in array_to_img
    return image.array_to_img(x, data_format=data_format, scale=scale, **kwargs)

我认为这是关于从数组中重塑我的图像。如何显示错误分类的图像?谢谢!

  File "/home/idu/.local/lib/python3.6/site-packages/keras_preprocessing/image/utils.py", line 257, in array_to_img
    'Got array with shape: %s' % (x.shape,))

ValueError: Expected image array to have rank 3 (single image). Got array with shape: (1, 512, 512, 1)

标签: kerasclassificationpredictimshowimage-classification

解决方案


推荐阅读