WARNING : tensorflow:Model was constructed with shape

Question:

I created a model. but when I want the model to do the estimation, I get an error.

inputs = tf.keras.Input(shape=(512, 512,1))

conv2d_layer = tf.keras.layers.Conv2D(32, (2,2), padding='Same')(inputs)
conv2d_layer = tf.keras.layers.Conv2D(32, (2,2), activation='relu', padding='Same')(conv2d_layer)

bn_layer = tf.keras.layers.BatchNormalization()(conv2d_layer)
mp_layer = tf.keras.layers.MaxPooling2D(pool_size=(2,2))(bn_layer)
drop = tf.keras.layers.Dropout(0.25)(mp_layer)

conv2d_layer = tf.keras.layers.Conv2D(64, (2,2), activation='relu', padding='Same')(drop)
conv2d_layer = tf.keras.layers.Conv2D(64, (2,2), activation='relu', padding='Same')(conv2d_layer)

bn_layer = tf.keras.layers.BatchNormalization()(conv2d_layer)
mp_layer = tf.keras.layers.MaxPooling2D(pool_size=(2,2), strides=(2,2))(bn_layer)
drop = tf.keras.layers.Dropout(0.25)(mp_layer)

flatten_layer = tf.keras.layers.Flatten()(drop)

dense_layer = tf.keras.layers.Dense(512, activation='relu')(flatten_layer)
drop = tf.keras.layers.Dropout(0.5)(dense_layer)
outputs = tf.keras.layers.Dense(2, activation='softmax')(drop)

model = tf.keras.Model(inputs=inputs, outputs=outputs, name='tumor_model')
model.summary()

Train Images Shape (342, 512, 512, 1)
Train Labels Shape (342, 2)
Test Images Shape (38, 512, 512, 1)
Test Labels Shape (38, 2)

Problem Here:


pred = model.predict(test_images[12])

WARNING:tensorflow:Model was constructed with shape (None, 512, 512, 1) for input KerasTensor(type_spec=TensorSpec(shape=(None, 512, 512, 1), dtype=tf.float32, name=’input_1′), name=’input_1′, description="created by layer ‘input_1’"), but it was called on an input with incompatible shape (32, 512, 1, 1).

Answers:

The error is telling you that test_images.shape is (32,512,1,1). Print out
test_images.shape then find out what is wrong with how you created the test_images

Answered By: Gerry P

To call predict on one sample, you may need to restore the batch dimension (which is 1), otherwise the first dimension 512 is considered as the sample size, which causes the problem. So you may try:

pred = model.predict(test_images[12].reshape(1,512, 512, 1))
Answered By: Erda Wen