Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.7k views
in Technique[技术] by (71.8m points)

keras - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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[-1].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.

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...