Ascend
Inference Application
model.eval
interface for model validationWhen the pre-trained models are saved in local, the steps of performing inference on validation dataset are as follows: firstly creating a model, then loading the model and parameters using load_checkpoint
and load_param_into_net
in mindspore
module, and finally performing inference on the validation dataset once being created. The method of processing the validation dataset is the same as that of the training dataset.
network = LeNet5(cfg.num_classes)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
print("============== Starting Testing ==============")
param_dict = load_checkpoint(args.ckpt_path)
load_param_into_net(network, param_dict)
dataset = create_dataset(os.path.join(args.data_path, "test"),
cfg.batch_size,
1)
acc = model.eval(dataset, dataset_sink_mode=args.dataset_sink_mode)
print("============== {} ==============".format(acc))
In the preceding information:
model.eval
is an API for model validation. For details about the API, see https://www.mindspore.cn/docs/api/en/r1.5/api_python/mindspore.html#mindspore.Model.eval.
Inference sample code: https://gitee.com/mindspore/models/blob/r1.5/official/cv/lenet/eval.py.
When the pre-trained models are saved remotely, the steps of performing inference on the validation dataset are as follows: firstly determining which model to be used, then loading the model and parameters using mindspore_hub.load
, and finally performing inference on the validation dataset once being created. The method of processing the validation dataset is the same as that of the training dataset.
model_uid = "mindspore/ascend/0.7/googlenet_v1_cifar10" # using GoogleNet as an example.
network = mindspore_hub.load(model_uid, num_classes=10)
net_loss = nn.SoftmaxCrossEntropyWithLogits(sparse=True, reduction="mean")
net_opt = nn.Momentum(network.trainable_params(), cfg.lr, cfg.momentum)
model = Model(network, net_loss, net_opt, metrics={"Accuracy": Accuracy()})
print("============== Starting Testing ==============")
dataset = create_dataset(os.path.join(args.data_path, "test"),
cfg.batch_size,
1)
acc = model.eval(dataset, dataset_sink_mode=args.dataset_sink_mode)
print("============== {} ==============".format(acc))
In the preceding information:
mindspore_hub.load
is an API for loading model parameters. Please check the details in https://www.mindspore.cn/hub/api/en/r1.5/index.html#module-mindspore_hub.
model.predict
API to perform inferencemodel.predict(input_data)
In the preceding information:
model.predict
is an API for inference. For details about the API, see https://www.mindspore.cn/docs/api/en/r1.5/api_python/mindspore.html#mindspore.Model.predict.
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。