如何从Tensorflow中的训练模型中删除最后一层 [英] How to remove the last layer from trained model in Tensorflow

查看:697
本文介绍了如何从Tensorflow中的训练模型中删除最后一层的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想删除从 https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md . 我知道我在Keras中可以使用model.layers.pop()删除最后一层.

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.

但是我在Internet上搜索,并且在tensorflow中没有等效功能. 如果张量流中没有等效函数,有谁能告诉我如何加载Keras训练有素的Model Zoo?

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?

推荐答案

您无需弹出"图层,只需不必加载它即可.

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

以Mobilnet为例(但将下载的模型放在此处):

For the exemple of Mobilnet (but put your downloaded model here) :

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

第一行加载整个模型,第二行加载最后一层之前的输出. 您可以更改"layer [-x]",其中x是所需图层的输出!

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)

希望我能帮忙!

这篇关于如何从Tensorflow中的训练模型中删除最后一层的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆