Keras模型拼装

在训练较大网络时, 往往想加载预训练的模型, 但若想在网络结构上做些添补, 可能出现问题一二...

一下是添补的几种情形, 此处以单输出回归任务为例:

# 添在末尾:
base_model = InceptionV3(weights='imagenet', include_top=False)
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1, activation='relu')(x)

model = Model(inputs=base_model.input, outputs=x)
model.summary()
# 添在开头和末尾:
# 在开头加1x1卷积层, 使4通道降为3通道, 再传入InceptionV3
def head_model(input_shape=(150, 150, 4)):
    input_tensor = Input(input_shape)
    x = Conv2D(128, (1, 1), activation='relu')(input_tensor)
    x = Conv2D(3, (1, 1), activation='relu')(x)
    model = Model(inputs=input_tensor, outputs=x, name='head')
    return model

head_model = head_model()
body_model = InceptionV3(weights='imagenet', include_top=False)
base_model = Model(head_model.input, body_model(head_model.output))
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(1, activation='relu')(x)

model = Model(inputs=base_model.inputs, outputs=x, name='net')
base_model.summary()
# 两数据输入流合并于末尾:
base_model = InceptionV3(weights='imagenet', include_top=False, input_shape=(150, 150, 3))
flat = Flatten()(base_model.output)
input_K = Input((100, ))    # another_input
K_flow = Activation(activation='linear')(input_K)
x = concatenate([flat, K_flow])    # 合流
x = Dense(1024, activation='relu')(x)
x = Dense(512, activation='relu')(x)
x = Dense(1, activation='relu')(x)
model = Model(inputs=[*base_model.inputs, input_K], outputs=x)    # 数据生成器那里也以这种形式生成([x_0, x_1], y)即可.
model.summary()

References:

末尾

开头

末尾合流_0末尾合流_1

附相关问题:

#开头

在名为convXd_Y的shape得到的是(a, b, c, d), 但本应该为(z, y, x, w) -- 在确保没有模型拼接时的低级错误时, 可尝试将在pre-trained的模型前的那几层, 如Conv2D层, 赋以如name='head_conv_0'等 与 框架默认赋予的形如convXd_Y 不会冲突的名字, 不然按默认的来, pre-trained的模型中的第一个卷积层属性会被赋予你开头新添加的第一个卷积层中, 故生上错. 但其实, 你也可以手动先从pre-trained的模型中get_weights(), 继而逐层往新搭建的模型里set_weights(), 详见Keras相关文档.

#末尾合流

ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 512)_1

ValueError: The shape of the input to "Flatten" is not fully defined (got (None, None, 512)_2