加载keras模型后如何删除Keras中的层(Efficient Net B7)

问题描述 投票:0回答:1

我以这种方式加载模型:

model = EfficientNetB7(weights='imagenet', include_top=False, input_shape=input_shape) 

我想做的是删除位置的图层:

model.layers[1] #Rescaling
model.layers[2] #Normalization

我尝试的是:

del_res= Dense(1, activation='relu', name='remove_rescaling')(base_model.layers[1].output)
del_nor= Dense(1, activation='relu', name='remove_normalization')(base_model.layers[2].output)

但重点是两层都还在那里。

我什至尝试过:

model.layer.pop(1)
model.layer.pop(2)

但无事可做!

你有什么推荐吗?

tensorflow keras deep-learning tf.keras keras-layer
1个回答
0
投票

您正在尝试从 EfficientNetB7 模型中删除特定层。如果直接删除图层不起作用,您可能需要在没有这些图层的情况下重建模型。

这里有一个建议:

from tensorflow.keras import applications

inputx = Input((512, 512, 3))
base = applications.EfficientNetB2(
            include_top  = False,
            weights      = None,
            input_tensor = inputx
        )
    

truncated_model = Model(inputs = base.layers[3].input, outputs = base.layers[-1].output) #truncates the functional model   
© www.soinside.com 2019 - 2024. All rights reserved.