Lack of Documentation on Transfer Learning
Prerequisites
Please answer the following question for yourself before submitting an issue.
- [/ ] I checked to make sure that this issue has not been filed already.
1. The entire URL of the documentation with the issue
https://github.com/tensorflow/models/tree/master/research//object_detection/meta_architectures/ssd_meta_arch.py
2. Describe the issue
Background: In the function 'restore_from_objects' on line 1295, the argument 'fine_tune_checkpoint_type' can take 3 options: 'classification', 'detection' and 'full'. As far as I can tell (there is no documentation on this) this corresponds to the value in the config file for the network. However, 'full' does nothing and raises an error. I am trying to perform transfer learning on only the last layer for the classification head and the box prediction head and so want to load and freeze everything else. It is incredibly unclear but I don't think setting as 'detection' restores the weights for the classification and box prediction towers. The main issue is the lack of documentation of how everything links together. Restoring the weights and freezing everything is obviously possible looking at the tutorial (https://github.com/tensorflow/models/blob/master/research/object_detection/colab_tutorials/eager_few_shot_od_training_tf2_colab.ipynb), but I chose to use the API as I was trying to avoid writing custom training code. My current focus is for the ssd_meta_architecture but I believe this applies to all architectures.
Issue: There is basically no documentation (that I have found) that tells you how to load everything but the last layer, add a new one, and then train it. Could this be addressed? Navigating the git repo trying to piece everything together yourself is far too time-consuming.
Are there any updates on this? I appreciate writing documentation takes a while so I was, probably naively, hoping for a workaround in the short term, but I do think there is a problem in general with the API lacking clear written documentation outside of docstrings.
For those who happen to read this with the same problem in the future (if it hasn't been addressed), a quick workaround I used was changing the final step in the load_fine_tune_checkpoint function in line 306 in model_lib_v2.py to be the same as the tutorial I linked. I am still testing it to see if anything weird happens so will not be sharing my code at this time as I don't wont to share bad code. In doing so, I do take out some of the checks to make sure it is a valid checkpoint so this solution may not be usable by everyone.
Hi @Jon573
Any updates on this? I've been trying to train a model and freeze the weights, but when I set 'fine_tune_checkpoint_type' to detection or classification, nothing freezes.
Hi, I have had success but have been really busy and forgot to update. Follow the tutorial link in my original post - it shows you the way of loading a model and then updating the layers that you want to be trained - it is the general blueprint I followed. I ended up completely replacing the load_finetune_checkpoint function with the method from the tutorial and modifying the eager_train_step function (also in model_lib_v2.py) to train only the layers I wanted to be trained. As mentioned, this takes away a few checks so use at your own risk. The main difficulty I had was finding the names of the layers to train and names of the network sections to restore.
Once the model has been loaded as shown in the tutorial, the layer names can be found with the following code:
for layer in detection_model.trainable_variables:
print(layer.name)
# or do whatever logic you want
To get the names of the network sections to restore, use:
reader = tf.train.load_checkpoint(checkpoint_path)
a = reader.get_variable_to_shape_map()
for section in a:
print(section)
# or, again, do whatever logic you want
Once you know these, you can alter the blueprint from the tutorial to work with any network in the tf2 model zoo. I will warn you, some of the names are a bit obscure and can take a second to understand.
Hi @Jon573, I recently trained model for receipt class detection using SSD efficientd0 512*512 model. I trained the model with 5 classes but now I want to add more classes to model time to time whenever there is a new class. Is the approach that you are using will be helpful in my scenario. Please help me.
Hi, I have had success but have been really busy and forgot to update. Follow the tutorial link in my original post - it shows you the way of loading a model and then updating the layers that you want to be trained - it is the general blueprint I followed. I ended up completely replacing the load_finetune_checkpoint function with the method from the tutorial and modifying the eager_train_step function (also in model_lib_v2.py) to train only the layers I wanted to be trained. As mentioned, this takes away a few checks so use at your own risk. The main difficulty I had was finding the names of the layers to train and names of the network sections to restore.
Once the model has been loaded as shown in the tutorial, the layer names can be found with the following code:
for layer in detection_model.trainable_variables: print(layer.name) # or do whatever logic you wantTo get the names of the network sections to restore, use:
reader = tf.train.load_checkpoint(checkpoint_path) a = reader.get_variable_to_shape_map() for section in a: print(section) # or, again, do whatever logic you wantOnce you know these, you can alter the blueprint from the tutorial to work with any network in the tf2 model zoo. I will warn you, some of the names are a bit obscure and can take a second to understand.
Hi, I have so many doubts when it comes to model architecutre and how to add custom layers on top of the pretrained architecture model. if we could connect over email it would be good. just mail a hi @ [email protected]