13

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?

mrSmith91
  • 338
  • 1
  • 6
  • 18

1 Answers1

24

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)
nb2904
  • 3
  • 4
Thibault Bacqueyrisses
  • 2,281
  • 1
  • 6
  • 18
  • I have a .ckpt file, for example : model.ckpt file. Could you tell me how to get model = mobilenet.MobileNet() by tensorflow? It so simple in Keras, but in tensorflow I don't really know how to create model = mobilenet.MobileNet(). – mrSmith91 Mar 28 '19 at 08:58
  • You can try the method described here : https://stackoverflow.com/questions/42894104/used-pretraing-model-in-tensorflow-ckpt-file – Thibault Bacqueyrisses Mar 28 '19 at 10:47
  • Thanks for helping me! But I found the solution: just run file train.py in folder object_detection and set is_training = True. The rest, TF will take care for you. – mrSmith91 Mar 28 '19 at 11:33
  • Glad that you have found it ! – Thibault Bacqueyrisses Mar 28 '19 at 12:50
  • This is one of the most elegant solutions I found in the internet! model.summary() will not even show the "skipped" layers which is rather nice! – 2Obe Aug 27 '21 at 09:40