Keras Convolution2D输入:检查模型输入时出错:期望convolution2d_input_1的形状

我正在学习这个很棒的教程,关于如何使用Keras创建图像分类器。一旦我训练好模型,我会将其保存到文件中,然后在下面的测试脚本中重新加载到模型中。

当我使用一张新的、从未见过的图像来评估模型时,我得到了以下异常:

错误:

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_dataexception_prefix='model input')  File "/Library/Python/2.7/site-packages/keras/engine/training.py", line 108, in standardize_input_datastr(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)`

问题出在我训练的模型上,还是我调用evaluate方法的方式上?

代码:

    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。

  2. 我需要将密集层从model.add(Dense(10))更改为model.add(Dense(1))

我还不明白如何让模型给我预测结果,但至少现在,模型评估可以运行了。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注