我正在尝试在我的 CNN 模型上应用迁移学习,但出现以下错误。
model = model1(weights = "model1_weights", include_top=False)
——
TypeError: __call__() takes exactly 2 arguments (1 given)
谢谢
我正在尝试在我的 CNN 模型上应用迁移学习,但出现以下错误。
model = model1(weights = "model1_weights", include_top=False)
——
TypeError: __call__() takes exactly 2 arguments (1 given)
谢谢
如果您尝试使用自定义模型使用迁移学习,答案取决于您保存模型架构(描述)和权重的方式。
您可以使用 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)