繁体   English   中英

ValueError: 层序号_3 的输入 0 与层不兼容:

[英]ValueError: Input 0 of layer sequential_3 is incompatible with the layer:

我前几天开始学习深度学习,所以对它了解不多。 我遵循 CNN model 的做法,使用 Inception V3 对狗和猫进行分类。 Model 训练和保存成功,我想预测图片是狗还是猫。 所以我这样做了,但我一直收到同样的错误。

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()

我非常努力地解决这个问题,但我找不到原因。 我真的很感谢你的帮助。

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

这应该做

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM