Can I load and train a Keras model again with new targets?

Viewed 232

I am aware that that after training a Neural Network model already, I could save it and in the future load the model to train again. However, what if I would like to incorporate new targets into the model to train?

For instance, I am building a face recognition model for the employees in my company. When new employees join my company, am I able to load and train the existing model with new targets without having to train the whole data set again?

I thought of initializing a keras.utils.to_categorical vector which extends another numpy.zeroes vector element-wise for future target training. May I know if this approach is correct?

1 Answers

Yes, you can. For example, VGG-Face model has 2622 outputs. Those outputs are using for finding facial embeddings. We can drop its some final layers and change the number of outputs. For example, I added 101 layers but the base model has 2622 outputs. Those are the new targets and I can start the training.

#!pip install deepface
from deepface import DeepFace
base_model = DeepFace.build_model("VGG-Face")

second_model = Convolution2D(101, (1, 1), name='predictions')(base_model.layers[-4].output)
second_model = Flatten()(second_model)
second_model = Activation('softmax')(second_model)

from tensorflow.keras.models import Model
new_model = Model(inputs=base_model.input, outputs=second_model)

new_model.fit(train_x, train_y, epochs=5000, validation_data=(test_x, test_y))
Related