【问题标题】:Trained Keras model is not working with custom images. What could be the cause of it?训练有素的 Keras 模型不适用于自定义图像。可能是什么原因造成的?
【发布时间】:2020-11-09 12:41:54
【问题描述】:

我训练了一个模型来分类不同的水果和蔬菜。我使用了来自 Kaggle-https://www.kaggle.com/moltean/fruits

的 Fruits-360 数据集

该模型的准确度为 98%,是的,它正确地预测了我从测试数据集中提供的每张图像。但问题是当我给它一张来自互联网的随机水果图片或从手机捕获的图像时,它总是预测它是错误的。是什么原因造成的,如何解决?

这是python代码:

  1       import cv2
          from tensorflow.keras.models import Sequential, save_model, load_model
          from keras.preprocessing.image import ImageDataGenerator
          import numpy as np
          import pandas as pd
          import matplotlib.pyplot as plt
          from keras.preprocessing.image import load_img
          from keras.preprocessing.image import img_to_array
          import matplotlib.pyplot as plt
          import matplotlib.image as mpimg

  2       filepath = '../input/Models/my_model.h5'

  3       train_datagen = ImageDataGenerator(
                       rescale=1./255,
                       shear_range=0.2,
                       zoom_range=0.2,
                       horizontal_flip=True)

          test_datagen = ImageDataGenerator(rescale=1./255)

          train_generator = train_datagen.flow_from_directory(
                     '../input/fruits/fruits-360/Training',
                      target_size=(100, 100),
                      batch_size=100,
                      class_mode='categorical')

          test_generator = test_datagen.flow_from_directory(
                     '../input/fruits/fruits-360/Test',
                     target_size=(100, 100),
                     batch_size=100,
                     class_mode='categorical')

  4       model = load_model(filepath, compile = True)

  5       input_image_path = '../input/real-images/20200720_125831.jpg'

  6       from PIL import Image
          import numpy as np
          from skimage import transform
          def load(filename):
             np_image = Image.open(filename)
             np_image = np.array(np_image).astype('float32')/255
             np_image = transform.resize(np_image, (100, 100, 3))
             np_image = np.expand_dims(np_image, axis=0)
             return np_image
  
  7       image = load(input_image_path)
          predictions=model.predict(image)
          img=mpimg.imread(input_image_path)
          imgplot = plt.imshow(img)
          plt.show()

          idx_to_name = {x:i for (x,i) in enumerate(train_generator.class_indices)}
          idx_to_name[np.argmax(predictions)]




当预测图像来自测试数据集及其右侧时的输出 [1]:https://i.stack.imgur.com/9plUJ.png

当预测图像是从互联网上随机获取时的输出,你可以看到它的错误 [2]:https://i.stack.imgur.com/z4s6N.png

【问题讨论】:

  • 您已将图像路径变量用作“input_image_path”,并使用变量“path”加载图像,并且给定代码中没有变量“path”。我很困惑。 rme你能说清楚吗?
  • 哦抱歉这是我在编辑时的错误,路径是 input_image_path。我清除了错误顺便说一句

标签: python tensorflow keras deep-learning classification


【解决方案1】:

训练集与您使用随机互联网图像作为模型的输入提供的非常不同。

训练集由没有“噪音”的图像组成,它们是水果的纯粹表示,周围没有额外的项目。相反,您尝试分类的图像周围有一些东西(在您的情况下是一只手)。

因此,如果您希望这种分类起作用,您需要一个不同的数据集来训练您的模型。

参考这里https://www.tensorflow.org/tutorials/images/classification 例如,看看页面中间的猫和狗图像,它们都在真实的上下文中。

查看该文档中使用的关于增强的技术和其他关于如何使类似的东西可用的提示。

【讨论】:

  • 我不能对输入图像进行任何预处理,使它们像训练集图像一样,然后交给模型进行预测。
  • 不是对输入图像进行预处理,而是让你的训练数据有不同类型的图像,这样你就可以做出更好的模型。
【解决方案2】:

我建议您通过裁剪图像对图像进行预处理,以便感兴趣区域(水果或蔬菜)尽可能占据图像中的大部分像素。这样做会使随机选择的图像的概率分布更接近网络训练时的图像分布。在我看来,对有很多混乱的图像进行训练的想法并不是一个好主意。毕竟,您希望您的网络专注于识别水果和蔬菜,而不是试图处理杂乱无章的图像。例如,我对面部图像做了很多工作。我特意制作了一个训练、测试和验证集,其中所有图像都被裁剪,以最大限度地提高全脸像素所占的百分比。训练后的模型对测试集的分类准确率为 98%。如果我从互联网上随机选择面部图像并裁剪它们以最大化仅代表全脸的像素百分比,我会得到类似的结果。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-11-08
    • 1970-01-01
    • 1970-01-01
    • 2020-07-02
    • 2019-11-02
    • 2022-01-22
    相关资源
    最近更新 更多