# ValueError: Input 0 of layer conv1_pad is incompatible with the layer: expected ndim=4, found ndim=3. Full shape received: [None, 224, 3]

I am trying to make a gender classifier. I am using MobileNet from Tensorflow with input shape as (224,224,3). After training the model, I tried to check if the model was working by passing an image to the model to predict, but it throws the error in the title. I tried debugging it and google the error and tried everything I could, but the error still persists. Please help me to understand why the error.

model = tf.keras.applications.mobilenet.MobileNet(input_shape = (224,224,3), include_top = False, pooling = 'avg')
#model.summary()

x = Dropout(rate=0.4)(model.output)
x = Dense(3)(x)
x = Softmax()(x)
model= Model(model.inputs, x)

for layer in model.layers[:-3]:
layer.trainable = False

model.compile(
loss='categorical_crossentropy'
)

datagen = tf.keras.preprocessing.image.ImageDataGenerator(preprocessing_function = tf.keras.applications.mobilenet.preprocess_input,
shear_range = 0.2, zoom_range = 0.2, horizontal_flip = True, validation_split = 0.1)
training = datagen.flow_from_directory(
'...\Pictures\Model',
target_size=(224, 224)
)

validation = datagen.flow_from_directory(
'...\Pictures\Model',
target_size=(224, 224)
)

batch_size = 32

history = model.fit_generator(generator = training, steps_per_epoch = training.samples//batch_size, epochs = 10,
validation_data = validation, validation_steps = validation.samples//batch_size, verbose = 2)

man = np.array(tf.keras.preprocessing.image.load_img('...\\Pictures\\Detected Faces\\0156224224_face.jpg', target_size = (224,224)))

model.predict(man) #Here the model throws an error


You need to make sure to add an extra dimension for the batch size, if you are passing in a single image the batch size would be 1. You can use np.expand_dims to add the extra dimension.