Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Help!! Unable to predict CNN model after training: How to resolve incompatible layers? #30

Open
JacobOfCorns opened this issue Aug 13, 2020 · 0 comments

Comments

@JacobOfCorns
Copy link

Help!! I can't predict my model because it's giving me the error:
ValueError: Input 0 of layer dense_3 is incompatible with the layer: expected axis -1 of input shape to have value 100352 but received input with shape [None, 131072]

I just finished training a CNN using a ResNet50 architecture and a few top layers.
This is the code I used for creating the model...

base_model = ResNet50(weights = None, include_top=False, input_shape=(200, 200, 3))

x = base_model.output
x = Flatten()(x)
x = Dropout(0.2)(x)
x = Dense(32, activation='relu')(x)
x = Dense(16, activation='relu')(x)
predictions = Dense(num_class, activation='softmax')(x)

# The model to be trained
model = Model(inputs=base_model.input, outputs=predictions)

model.compile(loss='binary_crossentropy', 
              optimizer='rmsprop', 
              metrics=['accuracy'])

callbacks_list = [keras.callbacks.EarlyStopping(monitor='val_acc', verbose=1)]
model.summary()

As you can see, I only used a few layers on top of the ResNet50 architecture. I also used an image size input of 200, 200, 3
Went it calls back, this is the summary for the FLATTEN to the 2ND LAST DENSE LAYER.

__________________________________________________________________________________________________
flatten_1 (Flatten)             (None, 100352)       0           conv5_block3_out[0][0]           
__________________________________________________________________________________________________
dropout_1 (Dropout)             (None, 100352)       0           flatten_1[0][0]                  
__________________________________________________________________________________________________
dense_3 (Dense)                 (None, 32)           3211296     dropout_1[0][0]                  
__________________________________________________________________________________________________

The dense layer expects an input of shape 100,352 but instead it receives an input of 131,072!! Hence, the value error when running the predict code via...

img_path = 'train/10_right.jpeg'
img = image.load_img(img_path, target_size =(256,256))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)

preds = model.predict(x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant