'ValueError: Input 0 of layer conv1_pad is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: (None, 1)

I'm getting this error when I try to replicate Facial Recognition using MobileNet on the VGGFace2 dataset.

ValueError: Input 0 of layer conv1_pad is incompatible with the layer: expected ndim=4, found ndim=2. Full shape received: (None, 1)

The below code is snippet of data generator where X is an dict with np array of images

    # Generate data
    X = {
        "anchor_input":np.array(anchor_images),
        "positive_input":np.array(positive_images),
        "negative_input":np.array(negative_images),
    }
    Y = np.zeros((self.batch_size,2))
    return (X, Y)

Output for X["anchor_input"].shape is (20, 224, 224, 3) where 20 is the batch size.

inpShape=(224,224,3)
model = tf.keras.applications.MobileNetV2(
     input_shape=inpShape,
     alpha=1.0,
     include_top=False,
     weights="imagenet",
     input_tensor=None,
     pooling=None,
     classes=1000,
     classifier_activation="softmax"
 )

outputLayer = model.get_layer('block_14_add').output


x = tf.keras.layers.Conv2D(80,(3,1),padding="same",name="extra_conv0")(outputLayer)
x = tf.keras.layers.Conv2D(64,(3,3),padding="same",name = "extra_conv1")(x)
x = tf.keras.layers.AveragePooling2D()(x)

x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(64)(x)

baseNetwork = tf.keras.Model(inputs=model.input,outputs=x,name="basemodel")

input1 = tf.keras.layers.Input(shape=inpShape,name='anchor_input')
input2 = tf.keras.layers.Input(shape=inpShape,name='positive_input')
input3 = tf.keras.layers.Input(shape=inpShape,name='negative_input')

anchor_out = baseNetwork(input1)
positive_out = baseNetwork(input2)
negative_out = baseNetwork(input3)

output = DistanceLayer()(anchor_out,positive_out,negative_out)

inputs = (input1,input2,input3)
contrastive_model = tf.keras.Model(inputs=inputs,outputs=output)

contrastive_model.compile(
    optimizer=tf.keras.optimizers.Adam(learning_rate=1e-3),
    loss=contrastive_loss_mutated,
    metrics=[contrastive_loss_mutated,contrastive_accuracy])

history1 = contrastive_model.fit(
    trainGenerator,
    epochs = 25,
    validation_data=testGenerator,
    callbacks=my_callbacks)

Please help me out with this.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source