使用cntk检查点进行预测

时间:2018-10-23 04:16:05

标签: deep-learning artificial-intelligence cntk

这些天,我尝试了由cntk实现的模型。但是我找不到一种用经过训练的模型预测新图片的方法。 训练好的模型另存为检查点:

trainer.save_checkpoint(os.path.join(output_model_folder, "model_{}".format(best_epoch)))

然后我得到了一些文件,例如:

enter image description here

因此,我试图像这样加载此模型检查点:

model = ct.load_model('../data/models/VGG13_majority/model_94')

上面的代码可以成功运行。然后我尝试了

model.eval(image_data)

但是我收到一个错误:enter image description here

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~更新~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~

这次我尝试了以下方法:

model = ct.load_model('../data/models/VGG13_majority/model_94')
model.eval({model.arguments[0]: [final_image]})

然后出现了新错误:

enter image description here

1 个答案:

答案 0 :(得分:2)

对于任何C.Function.eval(),您都需要传递一个字典作为参数。

因此,假设模型中只有一个input_variable,它的内容将是这样:

model = C.load_model()
model.eval({model.arguments[0]: image_data})

无论如何,我注意到您从检查点保存了模型。这样,您实际上也将“ ground_truth”输入变量保存到了损失函数中。

下次我建议您直接保存模型。通常,save_checkpoint中的文件应在restore_from_checkpoint()中使用

import cntk as C
from cntk.layers import Dense

model = Dense(10)(C.input_variable(1))
loss = C.binary_cross_entropy(model, C.input_variable(10))

trainer = C.Trainer(model, (loss,), [C.adam(model.parameters, 0.9, 0.9)])
trainer.save_checkpoint("hello")
model.save()  # used this to save the model directly

# to recover model from checkpoint use below
trainer.restore_from_checkpoint("hello")
original_model = trainer.model
print(trainer)
for i in trainer.model.arguments:
    print(i)