qubvel / segmentation_models

Segmentation models with pretrained backbones. Keras and TensorFlow Keras.
MIT License
4.7k stars 1.03k forks source link

fine tune model #274

Open DecentMakeover opened 4 years ago

DecentMakeover commented 4 years ago

Hello Thanks for sharing your work.

Lets say i have 5 classes in my first dataset =[cat, dog, cheetah, lion, tiger] i train a model for these classes.

How can i use the weights trained for this model and fine tune a model on 3 classes =[panda, bear, gorilla]

since the classes in the second dataset are not present in the first one.How can this be done?

Any suggestion will be helpful.Thanks in advance.

cklingspor commented 4 years ago

Hi there,

I hope this question is still of interest. I will assume that you talk about a computer vision problem using a deep neural network. What you are talking about is commonly referred to as transfer learning. Transfer learning uses the fact, that the lower layers in a convolutional network learn lower level features like straight lines or circles, etc.. The classification usually takes place in the fully connected layers at the top of your network. In order to reuse the weights on different classes, you have to do the following:

  1. Save your old model including its weights: E.g.: https://machinelearningmastery.com/save-load-keras-deep-learning-models/
  2. Load your old model and freeze the weights in the convolutional layers (setting the layers .trainable attribute to False): https://keras.io/getting-started/faq/#how-can-i-freeze-keras-layers
  3. Change the number of output nodes of your last layer. In your example from 5 to 3.

Best regards