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

查看:104
本文介绍了如何从 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.

但是我在网上搜了一下,tensorflow里面没有等价的功能.如果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:

以 Mobilenet 为例(但将您下载的模型放在这里):

For the example of Mobilenet (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天全站免登陆