pythontensorflowdeep-learningneural-networksiamese-network

Tensorflow ValueError: Shapes (64, 1) and (1, 1) are incompatible


I'm trying to build a Siamese Neural Network to analyze the MNIST dataset, however when trying to fit the model to the dataset I encounter this problem according to which I have training data and labels shapes' mismatch. I tried changing the loss function as well as tried to squeeze the labels array, and neither of "solutions" worked.

Here are the train and labels arrays' shapes:

pairTrain shape: (120000, 2, 28, 28, 1)
labelTrain shape: (120000, 1)

Here's my model:

def build_model(input_shape, embedDim=48):
    inputs = Input(input_shape)

    x = Conv2D(64, (2, 2), padding="same", activation="relu", input_shape=input_shape)(inputs)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    x = Conv2D(32, (2, 2), padding="same", activation="relu")(x)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    x = Conv2D(16, (2, 2), padding="same", activation="relu")(x)
    x = MaxPooling2D()(x)
    x = Dropout(0.3)(x)

    outputs = Flatten()(x)
    outputs = Dense(embedDim)(outputs)

    model = Model(inputs, outputs)

    return model

And finally here's the code that generates the error itself:


imgA = Input(shape=(28, 28, 1))
imgB = Input(shape=(28, 28, 1))

featA = build_model((28, 28, 1))(imgA)
featB = build_model((28, 28, 1))(imgB)

distance = Lambda(euclidean_distance)([featA, featB])
output = Dense(1, activation="sigmoid")(distance)

model = Model([imgA, imgB], output)

model.compile(optimizer="adam", loss="categorical_crossentropy", metrics=["accuracy"])
history = model.fit(
    [pairTrain[:, 0], pairTrain[:, 1]], labelTrain,
    validation_data=[[pairTest[:, 0], pairTest[:, 1]], labelTest],
    batch_size=64,
    epochs=10
)

model.save("output/siamese_model")

Please help me to resolve the problem.


Solution

  • I was not able to reproduce the error using the below code. I suspect that your labels shape is different than the one you reported or it does not contain strictly binary data (0s and 1s) only.

    Also, you should use tf.keras.losses.BinaryCrossentropy instead of tf.keras.losses.CategoricalCrossentropy as your labels should be binary with the sigmoid activation in the last layer.

    from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Input, Flatten, Dense, Lambda
    from tensorflow.keras.models import Model
    import tensorflow as tf
    
    def build_model(input_shape, embedDim=48):
        inputs = Input(input_shape)
    
        x = Conv2D(64, (2, 2), padding="same", activation="relu", input_shape=input_shape)(inputs)
        x = MaxPooling2D()(x)
        x = Dropout(0.3)(x)
    
        x = Conv2D(32, (2, 2), padding="same", activation="relu")(x)
        x = MaxPooling2D()(x)
        x = Dropout(0.3)(x)
    
        x = Conv2D(16, (2, 2), padding="same", activation="relu")(x)
        x = MaxPooling2D()(x)
        x = Dropout(0.3)(x)
    
        outputs = Flatten()(x)
        outputs = Dense(embedDim)(outputs)
    
        model = Model(inputs, outputs)
    
        return model
    
    imgA = Input(shape=(28, 28, 1))
    imgB = Input(shape=(28, 28, 1))
    
    featA = build_model((28, 28, 1))(imgA)
    featB = build_model((28, 28, 1))(imgB)
    
    distance = Lambda(lambda x: x[0]-x[1])([featA, featB])
    output = Dense(1, activation="sigmoid")(distance)
    
    model = Model([imgA, imgB], output)
    
    pairTrain =  tf.random.uniform((10, 2, 28, 28, 1))
    labelTrain = tf.random.uniform(shape=(10, 1), minval=0, maxval=2, dtype=tf.int32)
    
    pairTest =  tf.random.uniform((10, 2, 28, 28, 1))
    labelTest = tf.random.uniform(shape=(10, 1), minval=0, maxval=2, dtype=tf.int32)
    
    model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
    history = model.fit(
        [pairTrain[:, 0], pairTrain[:, 1]], labelTrain,
        validation_data=[[pairTest[:, 0], pairTest[:, 1]], labelTest],
        batch_size=64,
        epochs=10
    )
    
    model.save("output/siamese_model")
    
    Epoch 1/10
    1/1 [==============================] - 2s 2s/step - loss: 0.7061 - accuracy: 0.5000 - val_loss: 0.6862 - val_accuracy: 0.7000
    Epoch 2/10
    1/1 [==============================] - 0s 80ms/step - loss: 0.7882 - accuracy: 0.4000 - val_loss: 0.6751 - val_accuracy: 0.6000
    Epoch 3/10
    1/1 [==============================] - 0s 81ms/step - loss: 0.6358 - accuracy: 0.5000 - val_loss: 0.6755 - val_accuracy: 0.6000
    Epoch 4/10
    1/1 [==============================] - 0s 79ms/step - loss: 0.7027 - accuracy: 0.5000 - val_loss: 0.6759 - val_accuracy: 0.6000
    Epoch 5/10
    1/1 [==============================] - 0s 82ms/step - loss: 0.6970 - accuracy: 0.4000 - val_loss: 0.6752 - val_accuracy: 0.6000
    Epoch 6/10
    1/1 [==============================] - 0s 83ms/step - loss: 0.7564 - accuracy: 0.4000 - val_loss: 0.6779 - val_accuracy: 0.6000
    Epoch 7/10
    1/1 [==============================] - 0s 73ms/step - loss: 0.7123 - accuracy: 0.6000 - val_loss: 0.6818 - val_accuracy: 0.6000