Search code examples
tensorflowkerasdeep-learningtransfer-learning

ValueError: Dimensions must be equal ResNet-50 Transfer Learning TF


I am trying to finetune ResNet-50 in keras for an artwork style classifier on the wikiart dataset. I have a training and a test DataSet with the following shapes:

 <_MapDataset element_spec=(TensorSpec(shape=(32, 224, 224, 3), dtype=tf.int64, name=None), TensorSpec(shape=(32,), dtype=tf.int64, name=None))>)

This is the model:

tf_input = tf.keras.layers.Input(shape=(img_height, img_height, 3))
base_model = tf.keras.applications.resnet50.ResNet50(input_tensor=tf_input, include_top=False)
x = base_model.output
x = tf.keras.layers.GlobalAveragePooling2D()(x)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(512, activation='relu')(x)
predictions = tf.keras.layers.Dense(n_classes, activation='softmax')(x)
model = tf.keras.Model(inputs=base_model.input, outputs=predictions)
base_learning_rate = 0.001
model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
              loss=tf.keras.losses.SparseCategoricalCrossentropy(),
              metrics=[tf.keras.metrics.Accuracy(name='accuracy')])

When I try to train it with model.fit(train, epochs=epochs, validation_data=test), I get the following error:

ValueError: Dimensions must be equal, but are 32 and 27 for '{{node Equal}} = Equal[T=DT_FLOAT, incompatible_shape_error=true](Cast_1, functional_49_1/dense_43_1/Softmax)' with input shapes: [32], [32,27].

I am really lost, and any help would be appreciated.


Solution

  • As you are using SparseCategoricalCrossentropy as loss I assume you have labels as a one dimensional scaler array.

    But then you should Also change you accuracy metric to work on these kind of predictions namely you should change your code as:

    tf_input = tf.keras.layers.Input(shape=(img_height, img_height, 3))
    base_model = tf.keras.applications.resnet50.ResNet50(input_tensor=tf_input, include_top=False)
    x = base_model.output
    x = tf.keras.layers.GlobalAveragePooling2D()(x)
    x = tf.keras.layers.Flatten()(x)
    x = tf.keras.layers.Dense(512, activation='relu')(x)
    predictions = tf.keras.layers.Dense(n_classes, activation='softmax')(x)
    model = tf.keras.Model(inputs=base_model.input, outputs=predictions)
    base_learning_rate = 0.001
    model.compile(optimizer=tf.keras.optimizers.Adam(learning_rate=base_learning_rate),
                  loss=tf.keras.losses.SparseCategoricalCrossentropy(),
                  metrics=[tf.keras.metrics.SparseCategoricalAccuracy(name='accuracy')])