I use a CNN for classification using the following code (Summarized!) without problem.
cnn_input = Input((128, 32,3))
cnn_output = Conv2D(32, (3, 3), padding= same , activation=LeakyReLU(alpha=0.01)) (cnn_input)
fc_input = Flatten() (cnn_output)
fc_input = Dense(128, activation=LeakyReLU(alpha=0.01)) (fc_input)
full_output = Dense(8, activation="softmax") (fc_input)
model = models.Model(inputs=cnn_input, outputs=full_output)
But when I use a pretrained network (e.g., ResNet50 or VGG16) instead of the utilized CNN, I get the following error.
from tensorflow.keras.applications import ResNet50
feature_extractor = ResNet50(weights= imagenet ,
input_shape=(128, 32, 3),
include_top=False)
feature_extractor.trainable = False
input_ = tf.keras.Input(shape=(128, 32, 3))
x = feature_extractor(input_, training=False)
x = tf.keras.layers.Dense(128, activation= relu )(x)
output_ = tf.keras.layers.Dense(8, activation="softmax")(x)
model = tf.keras.Model(input_, output_)
Error:
categorical_crossentropy
target.shape.assert_is_compatible_with(output.shape)
ValueError: Shapes (None, None) and (None, None, None, 8) are incompatible
How to resolve this?
I tried to use a pretrained CNN using flow_from_dataframe, but I got an unexpected error that was not occurring in the case of utilizing a new CNN, while all conditions and code were the same!