ValueError:sequence_3层的输入0与该层不兼容:

时间:2020-06-20 15:27:20

标签: python tensorflow keras predict

几天前,我开始学习深度学习,所以我对此并不了解。 我遵循CNN模型的实践,该模型使用Inception V3对狗和猫进行了分类。 模型训练和保存成功,我想预测图片是狗还是猫。 所以我做到了,但是我仍然遇到同样的错误。

ValueError: Input 0 of layer sequential_3 is incompatible with the layer:  
expected axis -1 of input shape to have value 2048 
but received input with shape [None, 224, 224, 3]
# load and prepare the image
def load_image(filename):
    # load the image
    img = load_img(filename, target_size=(224, 224))
    # convert to array
    img = img_to_array(img)
    # reshape into a single sample with 3 channels
    img = img.reshape(1, 224, 224, 3)
    # center pixel data
    img = img.astype('float32')
    img = img - [123.68, 116.779, 103.939]
    return img
# load an image and predict the class
def run_example():
    # load the image
    img = load_image('sample_image.jpg')
    # load model
    model = load_model('model_fin.h5')
    # predict the class
    result = model.predict(img)
    print(result[0])
 
run_example()
extractor = Sequential()
extractor.add(InceptionV3(include_top=False, weights='imagenet', input_shape=IMG_SIZE))
extractor.add(layers.GlobalAveragePooling2D())

extractor_output_shape = extractor.get_output_shape_at(0)[1:]

model = Sequential()
model.add(layers.InputLayer(input_shape=extractor_output_shape))

model.add(layers.Dense(2, activation='sigmoid'))

model.summary()

我竭尽全力解决此问题,但找不到原因。 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

IMG_size = (224,224,3)
extractor.add(InceptionV3(include_top=False, weights='imagenet', input_shape=IMG_SIZE)

这应该做