mindspore_lite.Model
- class mindspore_lite.Model[源代码]
Model类用于定义MindSpore模型,便于计算图管理。
样例:
>>> import mindspore_lite as mslite >>> model = mslite.Model() >>> print(model) model_path: .
- build_from_file(model_path, model_type, context)[源代码]
从文件加载并构建模型。
参数:
model_path (str) - 定义模型路径。
model_type (ModelType) - 定义模型文件的类型。选项:ModelType::MINDIR | ModelType::MINDIR_LITE。
ModelType::MINDIR - MindSpore模型的中间表示。建议的模型文件后缀为”.mindir”。
ModelType::MINDIR_LITE - MindSpore Lite模型的中间表示。建议的模型文件后缀为”.ms”。
context (Context) - 定义用于在执行期间存储选项的上下文。
异常:
TypeError - model_path 不是str类型。
TypeError - model_type 不是ModelType类型。
TypeError - context 不是Context类型。
RuntimeError - model_path 文件路径不存在。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> import mindspore_lite as mslite >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> print(model) model_path: mobilenetv2.ms.
- get_input_by_tensor_name(tensor_name)[源代码]
按名称获取模型的输入张量。
参数:
tensor_name (str) - 张量名称。
返回:
Tensor,张量名称的输入张量。
异常:
TypeError - tensor_name 不是str类型。
RuntimeError - 按名称获取模型输入张量失败。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> import mindspore_lite as mslite >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> input_tensor = model.get_input_by_tensor_name("graph_input-173") >>> print(input_tensor) tensor_name: graph_input-173, data_type: DataType.FLOAT32, shape: [1, 224, 224, 3], format: Format.NHWC, element_num: 150528, data_size: 602112.
- get_inputs()[源代码]
获取模型的所有输入张量。
返回:
list[Tensor],模型的输入张量列表。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> import mindspore_lite as mslite >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> inputs = model.get_inputs()
- get_output_by_tensor_name(tensor_name)[源代码]
按名称获取模型的输出张量。
参数:
tensor_name (str) - 张量名称。
返回:
Tensor,张量名称的输出张量。
异常:
TypeError - tensor_name 不是str类型。
RuntimeError - 按名称获取模型输出张量失败。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> import mindspore_lite as mslite >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> output_tensor = model.get_output_by_tensor_name("Softmax-65") >>> print(output_tensor) tensor_name: Softmax-65, data_type: DataType.FLOAT32, shape: [1, 1001], format: Format.NHWC, element_num: 1001, data_size: 4004.
- get_outputs()[源代码]
获取模型的所有输出张量。
返回:
list[Tensor],模型的输出张量列表。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> import mindspore_lite as mslite >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> outputs = model.get_outputs()
- predict(inputs, outputs)[源代码]
推理模型。
参数:
inputs (list[Tensor]) - 包含所有输入张量的顺序列表。
outputs (list[Tensor]) - 模型输出按顺序填充到容器中。
异常:
TypeError - inputs 不是list类型。
TypeError - inputs 是list类型,但元素不是Tensor类型。
TypeError - outputs 不是list类型。
TypeError - outputs 是list类型,但元素不是Tensor类型。
RuntimeError - 预测推理模型失败。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> # in_data download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/input.bin >>> # 1. predict which indata is from file >>> import mindspore_lite as mslite >>> import numpy as np >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> inputs = model.get_inputs() >>> outputs = model.get_outputs() >>> in_data = np.fromfile("input.bin", dtype=np.float32) >>> inputs[0].set_data_from_numpy(in_data) >>> model.predict(inputs, outputs) >>> for output in outputs: ... data = output.get_data_to_numpy() ... print("outputs: ", data) ... outputs: [[1.0227193e-05 9.9270510e-06 1.6968443e-05 ... 6.6909502e-06 2.1626458e-06 1.2400946e-04]] >>> # 2. predict which indata is numpy array >>> import mindspore_lite as mslite >>> import numpy as np >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> inputs = model.get_inputs() >>> outputs = model.get_outputs() >>> for input in inputs: ... in_data = np.arange(1 * 224 * 224 * 3, dtype=np.float32).reshape((1, 224, 224, 3)) ... input.set_data_from_numpy(in_data) ... >>> model.predict(inputs, outputs) >>> for output in outputs: ... data = output.get_data_to_numpy() ... print("outputs: ", data) ... outputs: [[0.00035889 0.00065501 0.00052926 ... 0.00018387 0.00148318 0.00116824]] >>> # 3. predict which indata is new mslite tensor with numpy array >>> import mindspore_lite as mslite >>> import numpy as np >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> inputs = model.get_inputs() >>> outputs = model.get_outputs() >>> input_tensors = [] >>> for input in inputs: ... input_tensor = mslite.Tensor() ... input_tensor.set_data_type(input.get_data_type()) ... input_tensor.set_shape(input.get_shape()) ... input_tensor.set_format(input.get_format()) ... input_tensor.set_tensor_name(input.get_tensor_name()) ... in_data = np.arange(1 * 224 * 224 * 3, dtype=np.float32).reshape((1, 224, 224, 3)) ... input_tensor.set_data_from_numpy(in_data) ... input_tensors.append(input_tensor) ... >>> model.predict(input_tensors, outputs) >>> for output in outputs: ... data = output.get_data_to_numpy() ... print("outputs: ", data) ... outputs: [[0.00035889 0.00065501 0.00052926 ... 0.00018387 0.00148318 0.00116824]]
- resize(inputs, dims)[源代码]
调整输入形状的大小。
参数:
inputs (list[Tensor]) - 包含所有输入张量的顺序列表。
dims (list[list[int]]) - 定义输入张量的新形状的列表,应与输入张量的顺序一致。
异常:
TypeError - inputs 不是list类型。
TypeError - inputs 是list类型,但元素不是Tensor类型。
TypeError - dims 不是list类型。
TypeError - dims 是list类型,但元素不是list类型。
TypeError - dims 是list类型,元素是list类型,但元素的元素不是int类型。
ValueError - inputs 的size不等于 dims 的size。
ValueError - inputs 的元素的size不等于 dims 的元素的size。
样例:
>>> # model download link: https://download.mindspore.cn/model_zoo/official/lite/quick_start/mobilenetv2.ms >>> import mindspore_lite as mslite >>> model = mslite.Model() >>> context = mslite.Context() >>> context.append_device_info(mslite.CPUDeviceInfo()) >>> model.build_from_file("mobilenetv2.ms", mslite.ModelType.MINDIR_LITE, context) >>> inputs = model.get_inputs() >>> print("Before resize, the first input shape: ", inputs[0].get_shape()) Before resize, the first input shape: [1, 224, 224, 3] >>> model.resize(inputs, [[1, 112, 112, 3]]) >>> print("After resize, the first input shape: ", inputs[0].get_shape()) After resize, the first input shape: [1, 112, 112, 3]