简体   繁体   中英

ValueError: Input 0 of layer sequential_3 is incompatible with the layer:

I started studying deep learning a few days ago, so I don't know much about it. I followed the practice of CNN model that classifies dogs vs cats using Inception V3. Model training and save are successful and I want to predict whether the picture is a dog or a cat. So I did this, but I keep getting the same error.

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

I tried so hard to solve this problem but I couldn't find the cause. I'd really appreciate your help.

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

This should do

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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