You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

TensorFlow迁移学习中如何修改预训练模型的输入层名称?

修改Keras预训练模型的输入层/子模型层名称

你遇到的问题核心在于:要么是混淆了预训练模型作为子层在Sequential中的名称和预训练模型内部的输入层名称,要么是修改名称后未让Keras正确更新模型结构。以下是两种场景的具体解决方案:

场景1:修改预训练模型作为子层在Sequential中的名称

如果你在tf_model.summary()里看到的“MobilenetV3small”是整个预训练模型作为一个层的名称,直接在添加到Sequential时指定name参数即可:

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, name="custom_mobilenet_layer")
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()

也可以先修改预训练模型自身的_name属性,再添加到Sequential:

pretrained_model._name = "custom_mobilenet_layer"
tf_model.add(pretrained_model)

场景2:修改预训练模型内部的输入层名称

如果你要修改的是预训练模型内部的InputLayer名称(默认一般是MobilenetV3small_input),可以直接修改输入层的name属性,再重新构建模型确保名称生效:

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

# 修改内部输入层名称
pretrained_model.layers[0].name = "custom_input_layer"
# 重新构建模型确认名称更新
pretrained_model = tf.keras.models.Model(inputs=pretrained_model.input, outputs=pretrained_model.output)

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

tf_model = Sequential(name = 'mobilenet_model')
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()

更灵活的替代方案:使用函数式API

如果需要完全自定义输入层,推荐放弃Sequential,改用函数式API构建模型,能更精准控制输入层名称:

# 自定义输入层并指定名称
inputs = tf.keras.Input(shape=(180, 180, 3), name="custom_input_layer")

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

x = pretrained_model(inputs)
x = tf.keras.layers.Flatten()(x)
x = tf.keras.layers.Dense(512, activation='relu')(x)
outputs = tf.keras.layers.Dense(len(CLASS_NAMES), activation='softmax', name='MODEL_OUTPUT_LAYER')(x)

tf_model = tf.keras.models.Model(inputs=inputs, outputs=outputs, name='mobilenet_model')
tf_model.summary()

内容的提问来源于stack exchange,提问作者JSVJ

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 13:27:31