Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove the last layer from trained model in Tensorflow

I want to remove the last layer of 'faster_rcnn_nas_lowproposals_coco' model which downloaded from https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md. I know I in Keras we can use model.layers.pop() to remove the last layer.

But I searched in the Internet and there are no equivalent function in tensorflow. If there are no equivalent function in tensorflow, are there anyone can tell me how to load trained Model zoo by Keras?

like image 381
mrSmith91 Avatar asked Mar 28 '19 07:03

mrSmith91


1 Answers

You don't need to "pop" a layer, you just have to not load it:

For the example of Mobilenet (but put your downloaded model here) :

model = mobilenet.MobileNet()
x = model.layers[-2].output 

The first line load the entire model, the second load the outputs of the before the last layer. You can change layer[-x] with x being the outputs of the layer you want. So, for loading the model without the last layer, x should be equal to -2.

Then it's possible to use it like this :

x = Dense(256)(x)
predictions = Dense(15, activation = "softmax")(x)
model = Model(inputs = model.input, outputs = predictions)
like image 117
Thibault Bacqueyrisses Avatar answered Oct 17 '22 11:10

Thibault Bacqueyrisses