我将使用预先训练的模型(先前使用save_best_only
的{{1}}参数进行保存)来进行双输入转移学习。我有以下内容:
ModelCheckpoint
当我尝试使用:
pretrained_model = load_model('best_weight.h5')
def combined_net():
u_model = pretrained_model
u_output = u_model.layers[-1].output
v_model = pretrained_model
v_output = v_model.layers[-1].output
concat = concatenate([u_output, v_output])
#hidden1 = Dense(64, activation=activation)(concat) #was 128
main_output = Dense(1, activation='sigmoid', name='main_output')(concat) # pretrained_model.get_layer("input_1").input
model = Model(inputs=[u_model.input, v_model.input], outputs=main_output)
opt = SGD(lr=0.001, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
我遇到以下错误:
best_weights_file="weights_best_of_pretrained_dual.hdf5"
checkpoint = ModelCheckpoint(best_weights_file, monitor='val_acc', verbose=1, save_best_only=True, mode='max')
callbacks = [checkpoint]
base_model = combined_net()
print(base_model.summary)
history = base_model.fit([x_train_u, x_train_v], y_train,
batch_size=batch_size,
epochs=epochs,
callbacks=callbacks,
verbose=1,
validation_data=([x_test_u, x_test_v], y_test),
shuffle=True)
显然,ValueError: The list of inputs passed to the model is redundant. All inputs should only appear once. Found: [<tf.Tensor 'input_1_5:0' shape=(None, None, None, 3) dtype=float32>, <tf.Tensor 'input_1_5:0' shape=(None, None, None, 3) dtype=float32>]
行似乎引起了错误。
我要做的就是对双输入到单输出模型使用预训练的模型(“ best_weight.h5”)。这两个输入都与先前初始化的相同,并且model = Model(inputs=[u_model.input, v_model.input], outputs=main_output)
层应将由加载的模型构造的每个模型的最后一层之前的层连接起来。
我尝试了几种在线查找方法,但是无法正确设置模型。
我希望有人能帮助我
编辑:
预训练模型如下所示:
concatenate
答案 0 :(得分:1)
这里是正确的方法。当我定义combined_net
时,我定义了2个新输入,用于以相同方式提供pre_trained
模型
def vgg_16():
b_model = tf.keras.applications.VGG16(weights='imagenet', include_top=False)
x = b_model.output
x = GlobalAveragePooling2D()(x)
x = Dense(256, activation='relu')(x)
predictions = Dense(1, activation='sigmoid')(x)
model = Model(inputs=b_model.input, outputs=predictions)
for layer in model.layers[:15]:
layer.trainable = False
opt = SGD(lr=0.003, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
main_model = vgg_16()
# main_model.fit(...)
pretrained_model = Model(main_model.input, main_model.layers[-2].output)
def combined_net():
inp_u = Input((224,224,3)) # the same input dim of pretrained_model
inp_v = Input((224,224,3)) # the same input dim of pretrained_model
u_output = pretrained_model(inp_u)
v_output = pretrained_model(inp_v)
concat = concatenate([u_output, v_output])
main_output = Dense(1, activation='sigmoid', name='main_output')(concat)
model = Model(inputs=[inp_u, inp_v], outputs=main_output)
opt = SGD(lr=0.001, nesterov=True)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
base_model = combined_net()
base_model.summary()