更改 TensorFlow 迁移学习中预训练模型的输入层名称

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

我正在尝试更改从 Keras 导入的预训练 TensorFlow 模型的输入层名称

我可以添加输出层名称,但添加输入模型名称始终失败。是否可以更改预训练模型的输入层名称?

我在 keras 文档中也找不到有关此内容的任何信息。不确定我是否遗漏了什么。

tf_model = Sequential(name = 'mobilenet_model')

pretrained_model = tf.keras.applications.MobileNetV3Small(include_top = False,
                                                  input_shape = (180, 180, 3),
                                                  pooling = 'avg',
                                                  classes = len(CLASS_NAMES),
                                                  weights = 'imagenet')

for layer in pretrained_model.layers:
    layer.trainable = False

tf_model.add(pretrained_model)
tf_model.add(Flatten())
tf_model.add(Dense(512, activation = 'relu'))
tf_model.add(Dense(len(CLASS_NAMES), activation = 'softmax', name = 'MODEL_OUTPUT_LAYER'))
tf_model.summary()

目前输入的型号名称是默认的“MobilenetV3small”,但我想将其更改为其他名称。

python tensorflow keras tensorflow-lite transfer-learning
1个回答
0
投票

从这里得到了解决方案 - Keras 重命名模型和图层伙计们。

解决方案是在编译模型之前使用 _name 属性。

input_layer = tf_model.layers[0]
input_layer._name = 'MODEL_INPUT_LAYER'
© www.soinside.com 2019 - 2024. All rights reserved.