Keras Convolution2D输入:检查模型输入时出错:预期convolution2d_input_1具有形状 [英] Keras Convolution2D Input: Error when checking model input: expected convolution2d_input_1 to have shape

查看:200
本文介绍了Keras Convolution2D输入:检查模型输入时出错:预期convolution2d_input_1具有形状的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在通过此出色的教程关于使用Keras创建图像分类器的信息.训练完模型后,我将其保存到文件中,然后在下面显示的测试脚本中将其重新加载到模型中.

I am working through this great tutorial on creating an image classifier using Keras. Once I have trained the model, I save it to a file and then later reload it into a model in a test script shown below.

当我使用从未见过的新图像评估模型时,出现以下异常:

I get the following exception when I evaluate the model using a new, never-before-seen image:

错误:

Traceback (most recent call last):
  File "test_classifier.py", line 48, in <module>
    score = model.evaluate(x, y, batch_size=16)
  File "/Library/Python/2.7/site-packages/keras/models.py", line 655, in evaluate
    sample_weight=sample_weight)
  File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 1131, in evaluate
    batch_size=batch_size)
  File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 959, in _standardize_user_data
exception_prefix='model input')
  File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 108, in standardize_input_data
str(array.shape))
Exception: Error when checking model input: expected convolution2d_input_1 to have shape (None, 3, 150, 150) but got array with shape (1, 3, 150, 198)`

我训练的模型是否存在问题,或者我是如何调用评估方法的?

Is the problem with the model that I have trained or with how I am invoking the evaluate method?

代码:

    from keras.preprocessing.image import ImageDataGenerator
    from keras.models import Sequential
    from keras.layers import Convolution2D, MaxPooling2D
    from keras.layers import Activation, Dropout, Flatten, Dense
    from keras.preprocessing.image import ImageDataGenerator, array_to_img, img_to_array, load_img

    import numpy as np
    img_width, img_height = 150, 150
    train_data_dir = 'data/train'
    validation_data_dir = 'data/validation'
    nb_train_samples = 2000
    nb_validation_samples = 800
    nb_epoch = 5
    model = Sequential()
    model.add(Convolution2D(32, 3, 3, input_shape=(3, img_width, img_height)))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(32, 3, 3))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Convolution2D(64, 3, 3))
    model.add(Activation('relu'))
    model.add(MaxPooling2D(pool_size=(2, 2)))
    model.add(Flatten())
    model.add(Dense(64))
    model.add(Activation('relu'))
    model.add(Dropout(0.5))
    model.add(Dense(1))
    model.add(Activation('sigmoid'))
    model.compile(loss='binary_crossentropy',
          optimizer='rmsprop',
          metrics=['accuracy'])
    model.load_weights('first_try.h5')
    img = load_img('data/test2/ferrari.jpeg')
    x = img_to_array(img)  # this is a Numpy array with shape (3, 150, 150)
    x = x.reshape( (1,) + x.shape )  # this is a Numpy array with shape (1, 3, 150, 150)
    y = np.array([0])
    score = model.evaluate(x, y, batch_size=16)`

推荐答案

问题有两个:

  1. 测试图像尺寸错误.是150 x 198,需要是150 x 150.

  1. The test image was the wrong size. It was 150 x 198, and needed to be 150 x 150.

我不得不将密集层从model.add(Dense(10))更改为model.add(Dense(1)).

I had to change the dense layer from model.add(Dense(10)) to model.add(Dense(1)).

我还不知道如何获得模型来给我预测,但是至少现在,模型评估可以运行了.

I don't yet understand how to get the model to give me the prediction, but at least now, the model evaluation runs.

这篇关于Keras Convolution2D输入:检查模型输入时出错:预期convolution2d_input_1具有形状的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆