[英]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.keras
, model.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.