且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何从 Tensorflow 中的训练模型中删除最后一层

更新时间:2023-12-01 22:50:16

你不需要弹出"一个层,你只需要不加载它:

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)