简体   繁体   English

如何删除(弹出)Keras InceptionV3预训练模型的初始层?

[英]How to remove (pop) initial layers of Keras InceptionV3 pre-trained model?

I am trying to use pre-trained InceptionV3 model. 我正在尝试使用预先训练过的InceptionV3模型。 However, I want to remove initial five layers and add my custom layers. 但是,我想删除最初的五个图层并添加我的自定义图层。 How can I do that? 我怎样才能做到这一点? I tried model.layers.pop(0) , but that alone will not solve the problem. 我尝试过model.layers.pop(0) ,但仅凭这一点无法解决问题。

Edit: 编辑:

tf.keras does not help either as mentioned in the first answer: 如第一个答案所述, tf.keras也没有帮助:

在此输入图像描述

model.layers.pop() doesn't work in the same way in tf.keras as it doesn in Keras . model.layers.pop()不会以同样的方式在工作tf.keras ,因为它在没有按Keras In tf.keras , model.layers is a view of the model. tf.kerasmodel.layers是模型的视图。 You can't remove the layers but what you can do is define the layer for which you want the output. 您无法删除图层,但可以执行的操作是定义要输出的图层。 For example, 例如,

base_model = InceptionV3(shape=shape, weights="imagenet", include_top=True)

# you don't want the last five layers:
base_model_output = base_model.layers[-6].output

# new layers
outputs = Dense(....)(base_model_output)
model = Model(base_model.input, outputs)

Since the first few layers starting from the input are changed, then the pretrained weights cannot be used. 由于从输入开始的前几个层被改变,因此不能使用预训练的权重。 So, the architecture can be directly taken from here and modified accordingly instead of trying complex surgeries. 因此,可以直接从这里获取架构并相应地进行修改,而不是尝试复杂的外科手术。

https://github.com/keras-team/keras-applications/blob/master/keras_applications/inception_v3.py https://github.com/keras-team/keras-applications/blob/master/keras_applications/inception_v3.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用预训练的InceptionV3加速Keras CNN - How to speed up my Keras CNN with pre-trained InceptionV3 如何从预训练的模型中删除最后一层。 我已经尝试过model.layers.pop()但它不起作用 - How to remove the last layer from a pre-trained model. I have tried model.layers.pop() but it is not working 如何使用 PyTorch 在预训练模型上添加新层? (给出了 Keras 示例。) - How can I add new layers on pre-trained model with PyTorch? (Keras example given.) 如何在Keras中加载卷积神经网络前几层的权重并删除预训练的model? - How to load the weights of the first few layers of Convolutional Neural Network in Keras and delete the pre-trained model? 从自定义预训练 model 中删除层 - Remove layers from a custom pre-trained model 使用预训练的 CNN (inceptionv3) 训练 3D 医学图像 - Using a pre-trained CNN (inceptionv3) for training 3D medical images 如何导入预训练的 InceptionV4 模型以在 Kaggle 中训练我们的模型? - How to import InceptionV4 model which is pre-trained to train our model in Kaggle? 如何使用功能 keras API 在预训练的非序列 model 中的激活层之后插入丢失层? - How to insert dropout layers after activation layers in a pre-trained non-sequential model using functional keras API? 如何将顶层添加到预先训练的功能模型中 - How to add top layers to a pre-trained functional model 如何从 keras 中预训练的 model 预测图像 - How to predict image from a pre-trained model in keras
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM