我正在尝试在CNN模型上应用转移学习,但出现以下错误。
model = model1(weights = "model1_weights", include_top=False)
-
TypeError: __call__() takes exactly 2 arguments (1 given)
谢谢
答案 0 :(得分:3)
如果您尝试通过自定义模型使用转移学习,答案取决于您保存模型架构(描述)和权重的方式。
您可以使用keras的load_model方法轻松加载模型。
from keras.models import load_model
model = load_model("model_path.h5")
您可以先从json文件加载模型描述,然后加载模型权重。
form keras.models import model_from_json
with open("path_to_json_file.json") as json_file:
model = model_from_json(json_file.read())
model.load_weights("path_to_weights_file.h5")
在加载旧模型之后,您现在可以决定丢弃哪些层(通常是最上面的全连接层)以及要冻结的层。 假设您要使用模型的前五层而无需再次训练,接下来的三层将被再次训练,最后一层将被丢弃(此处假定网络层数大于八),并添加最后一层之后是三个完全连接的层。可以按照以下步骤进行。
for i in range(5):
model.layers[i].trainable = False
for i in range(5,8):
model.layers[i].trainable = True
ll = model.layers[8].output
ll = Dense(32)(ll)
ll = Dense(64)(ll)
ll = Dense(num_classes,activation="softmax")(ll)
new_model = Model(inputs=model.input,outputs=ll)