使用 Keras Functional API 微调模型
fine tune a model using Keras Functional API
我正在使用 VGG16 在我的数据集上对其进行微调。
这是模型:
def finetune(self, aux_input):
model = applications.VGG16(weights='imagenet', include_top=False)
# return model
drop_5 = Input(shape=(7, 7, 512))
flatten = Flatten()(drop_5)
# aux_input = Input(shape=(1,))
concat = Concatenate(axis=1)([flatten, aux_input])
fc1 = Dense(512, kernel_regularizer=regularizers.l2(self.weight_decay))(concat)
fc1 = Activation('relu')(fc1)
fc1 = BatchNormalization()(fc1)
fc1_drop = Dropout(0.5)(fc1)
fc2 = Dense(self.num_classes)(fc1_drop)
top_model_out = Activation('softmax')(fc2)
top_model = Model(inputs=drop_5, outputs=top_model_out)
output = top_model(model.output)
complete_model = Model(inputs=[model.input, aux_input], outputs=output)
return complete_model
我有两个模型输入。在上面的函数中,我将 Concatenate 用于展平数组和我的 aux_input。
我不确定这是否适用于 imagenet 权重。
当我运行这个时,我得到一个错误:
ValueError: Graph disconnected: cannot obtain value for tensor
Tensor("aux_input:0", shape=(?, 1), dtype=float32) at layer
"aux_input". The following previous layers were accessed without
issue: ['input_2', 'flatten_1']
不确定我哪里出错了。
如果重要,这是拟合函数:
model.fit(x={'input_1': x_train, 'aux_input': y_aux_train}, y=y_train, batch_size=batch_size,
epochs=maxepoches, validation_data=([x_test, y_aux_test], y_test),
callbacks=[reduce_lr, tensorboard], verbose=2)
但是,当我调用 model.summary()
时,在这个 fit
函数之前出现错误。
问题是您在 top_model
中使用了 aux_input
,但您没有在 top_model
的定义中将其指定为输入。尝试用以下内容替换 top_model
和 output
的定义:
top_model = Model(inputs=[drop_5, aux_input], outputs=top_model_out)
output = top_model([model.output, aux_input])
我正在使用 VGG16 在我的数据集上对其进行微调。
这是模型:
def finetune(self, aux_input):
model = applications.VGG16(weights='imagenet', include_top=False)
# return model
drop_5 = Input(shape=(7, 7, 512))
flatten = Flatten()(drop_5)
# aux_input = Input(shape=(1,))
concat = Concatenate(axis=1)([flatten, aux_input])
fc1 = Dense(512, kernel_regularizer=regularizers.l2(self.weight_decay))(concat)
fc1 = Activation('relu')(fc1)
fc1 = BatchNormalization()(fc1)
fc1_drop = Dropout(0.5)(fc1)
fc2 = Dense(self.num_classes)(fc1_drop)
top_model_out = Activation('softmax')(fc2)
top_model = Model(inputs=drop_5, outputs=top_model_out)
output = top_model(model.output)
complete_model = Model(inputs=[model.input, aux_input], outputs=output)
return complete_model
我有两个模型输入。在上面的函数中,我将 Concatenate 用于展平数组和我的 aux_input。 我不确定这是否适用于 imagenet 权重。
当我运行这个时,我得到一个错误:
ValueError: Graph disconnected: cannot obtain value for tensor Tensor("aux_input:0", shape=(?, 1), dtype=float32) at layer "aux_input". The following previous layers were accessed without issue: ['input_2', 'flatten_1']
不确定我哪里出错了。
如果重要,这是拟合函数:
model.fit(x={'input_1': x_train, 'aux_input': y_aux_train}, y=y_train, batch_size=batch_size,
epochs=maxepoches, validation_data=([x_test, y_aux_test], y_test),
callbacks=[reduce_lr, tensorboard], verbose=2)
但是,当我调用 model.summary()
时,在这个 fit
函数之前出现错误。
问题是您在 top_model
中使用了 aux_input
,但您没有在 top_model
的定义中将其指定为输入。尝试用以下内容替换 top_model
和 output
的定义:
top_model = Model(inputs=[drop_5, aux_input], outputs=top_model_out)
output = top_model([model.output, aux_input])