0

I'm trying to finetune the last layer of the VGG-16. Here is the part of the code where i make the new model:

def train2false(model):
    for layer in model.layers:
        layer.trainable = False
    return model

def define_training_layers(model):
     model.layers = model.layers[0:21]
     model = train2false(model)
     last_layer = model.get_layer('fc7')
     out = Dense(n_classes, activation='softmax', name='fc8')(last_layer)
     model = Model(input=model.input, output=out)
     return model

def compile_model(epochs, lrate, model):
    decay = lrate / epochs
    sgd = SGD(lr=lrate, momentum=0, decay=0.0002, nesterov=True)
    model.compile(loss='categorical_crossentropy', optimizer=sgd, metrics=['accuracy'])
    print (model.summary())

    return model

def train_evaluate(model, X_train, y_train, X_test, y_test, epochs):
    model.fit(X_train, y_train, validation_data=(X_test, y_test), nb_epoch=epochs, batch_size=32)
    # Final evaluation of the model
    scores = model.evaluate(X_test, y_test, verbose=0)
    print("Accuracy: %.2f%%" % (scores[1]*100))

    return model

X_train, X_test, labels_test, labels_train, n_classes = load_dataset()
image_input = Input(shape=(3, 224, 224))
vgg_model = VGGFace(input_tensor= image_input, include_top=True)
custom_vgg_model = define_training_layers(vgg_model)
custom_vgg_model = compile_model(epochs=50, lrate=0.001, model=custom_vgg_model)
custom_vgg_model = train_evaluate(custom_vgg_model, X_train=X_train, y_train=labels_train, X_test=X_test, y_test=labels_test, epochs=50)

I get the following error:

tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 1 in both shapes must be equal, but are 1000 and 2622 for 'Assign_30' (op: 'Assign') with input shapes: [4096,1000], [4096,2622].

It works for me if i try to finetune all the fully connected part with include_top=False instead of just the softmax activation.

Is there something that i'm missing?

stop-cran
  • 4,229
  • 2
  • 30
  • 47
Eric
  • 1,108
  • 3
  • 11
  • 25

1 Answers1

1

Solved!!! I've take the pre-trained weights from https://github.com/rcmalli/keras-vggface/releases/download/v1.0/rcmalli_vggface_th_weights_th_ordering.h5 which has 2622 number of outputs and i had 1000 outputs. So just change the number of outputs for the last layer in VGG.py

Eric
  • 1,108
  • 3
  • 11
  • 25