【问题标题】:Using pre-trained models in transfer learning - how do I do this?在迁移学习中使用预训练模型——我该怎么做?
【发布时间】:2021-10-18 21:58:29
【问题描述】:

我正在尝试使用 VGG16 应用迁移学习,但我很难找到具有多个输出的示例。

我已经编写了自己的架构,如下所示,我用它来手动训练我的模型 - 尽管我不明白如何使用预训练模型编译相同的指标。

我该怎么做?

from keras.applications.vgg16 import VGG16

vgg16_model = VGG16(weights='imagenet',
                  include_top=False,
                  input_shape=(224,224,3))

vgg16_model.summary()

def modelG():
    input=tf.keras.layers.Input(shape=(224,224,3))
    x=input
    x=layers.Conv2D(8,(3,3),activation='relu')(x)
    x=layers.Conv2D(8,(3,3),activation='relu')(x)
    x=layers.MaxPooling2D(2)(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Conv2D(16,(3,3),activation='relu')(x)
    x=layers.Conv2D(16,(3,3),activation='relu')(x)
    x=layers.MaxPooling2D(2)(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Conv2D(32,(3,3),activation='relu')(x)
    x=layers.Conv2D(32,(3,3),activation='relu')(x)
    x=layers.MaxPooling2D(2)(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Conv2D(84,(3,3),activation='relu')(x)
    x=layers.Dropout(0.1)(x)
    x=layers.Flatten()(x)
    out_col=layers.Dense(512,activation='relu')(x)
    out_ren=layers.Dense(512,activation='relu')(x)
    out_col= layers.Dense(1,activation='sigmoid',name='col_out')(out_col)
    out_ren=layers.Dense(1,activation='relu',name='ren_out')(out_ren)
    multiOutputModel=tf.keras.models.Model(inputs=input, outputs=[out_col, out_ren])

# Compile
    multiOutputModel.compile(
              optimizer='adam', 
              loss={
                  'ren_out': 'mean_squared_error',  
                  'col_out': 'binary_crossentropy'},
              loss_weights={
                  'ren_out': 4.0, 
                  'col_out': 0.1},
              metrics={
                  'ren_out': 'mean_absolute_error', 
                  'col_out': 'accuracy'})

    tf.keras.utils.plot_model(modelB, 'modelB.png',show_shapes=True)
    return multiOutputModel

multiOutputModel.summary()

【问题讨论】:

  • 请不要通过破坏您的帖子为他人增加工作量。通过在 Stack Exchange (SE) 网络上发帖,您已在 CC BY-SA license 下授予 SE 分发内容的不可撤销的权利(无论您未来的选择如何)。根据 SE 政策,分发非破坏版本。因此,任何此类破坏性编辑都将被还原。请参阅How does deleting work?,了解有关删除本网站内容的更多信息。

标签: python tensorflow keras conv-neural-network


【解决方案1】:

您可以像处理单个输出案例一样进行操作

base_model=tf.keras.applications.VGG19(include_top=False, weights="imagenet",input_shape=img_shape, pooling='max') 
x=base_model.output
out_col=layers.Dense(512,activation='relu')(x)
out_ren=layers.Dense(512,activation='relu')(x)
out_col= layers.Dense(1,activation='sigmoid',name='col_out')(out_col)
out_ren=layers.Dense(1,activation='relu',name='ren_out')(out_ren)
multiOutputModel=tf.keras.models.Model(inputs=base_model.input, outputs=[out_col, out_ren])

请注意,在 VGG 模型中我设置 pooling='max' 所以 VGG 的输出是一维张量,因此您不需要展平层。

【讨论】:

    猜你喜欢
    • 2020-05-21
    • 1970-01-01
    • 2021-04-17
    • 2019-06-15
    • 1970-01-01
    • 2022-01-14
    • 2022-11-10
    • 2018-01-13
    • 2019-12-07
    相关资源
    最近更新 更多