在迁移学习中使用预训练模型——我该怎么做?
Using pre-trained models in transfer learning - how do I do this?
我正在尝试使用 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()
你会像处理单个输出案例那样做
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 的输出是一维张量所以你不需要展平层。
我正在尝试使用 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()
你会像处理单个输出案例那样做
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 的输出是一维张量所以你不需要展平层。