googlecodelabs / tensorflow-for-poets-2

Apache License 2.0
508 stars 463 forks source link

Retrain from deeper within the architecture (include more layers) #125

Open Momut1 opened 5 years ago

Momut1 commented 5 years ago

Hello all, I would really appreciate some assistance on how to modify the retrain.py to retrain from deeper within the model than just the final layer. As far as I understand, as it stands, the retrain.py script scraps the final layer and retrains a new one in its place and then adds a softmax to the end with the prerequisite number of classes to fit your purpose. Now my question is - how can one go deeper than that and retrain from, say, the middle of the architecture in order to be able to recognize more features. Say I want to add some dense layers like so:

x=base_model.output x=GlobalAveragePooling2D()(x) x=Dense(1024,activation='relu')(x) #we add dense layers so that the model can learn more complex functions and classify for better results. x=Dense(1024,activation='relu')(x) #dense layer 2 x=Dense(512,activation='relu')(x) #dense layer 3 preds=Dense(17,activation='softmax')(x) #final layer with softmax activation

and freeze all the top layers like so: # Freeze pretrained weights for layer in model.layers: layer.trainable=False

I've managed to narrow down that the function that needs to be augmented to account for this is add_final_retrain_ops , but how exactly one goes about incorporating the changes, I have no idea. Does someone have experience with this?