3
votes

I'm trying to create a CNN + Regression model here through the code below:

# Create the base model from the pre-trained model MobileNet V2
cnn_model = keras.applications.MobileNetV2(input_shape=IMG_SHAPE,
                                            include_top=False,
                                            weights='imagenet')

# The Regression Model
regression_model = keras.Sequential([
                        keras.layers.Dense(64, activation='relu', 
                                            input_shape=cnn_model.output_shape),
                        keras.layers.Dense(64, activation='relu')
                    ])

prediction_layer = tf.keras.layers.Dense(1)

# Final Model
model = keras.Sequential([
            cnn_model,
            regression_model,
            prediction_layer
        ])

Now the issue is that I get the WARNING below:

WARNING:tensorflow:Model was constructed with shape Tensor("dense_12_input:0", shape=(None, None, 7, 7, 1280), dtype=float32) for input (None, None, 7, 7, 1280), but it was re-called on a Tensor with incompatible shape (None, 7, 7, 1280).

Does anyone know as to why this warning is coming up and how I can combat it, unless it's harmless.

1

1 Answers

4
votes

It seems as though adding a flatten after the CNN solved my problem. Since we want to pass a flattened vector to the fully connected layer. The model should look like:

model = keras.Sequential([
            cnn_model, keras.layers.Flatten(),
            regression_model,
            prediction_layer
        ])