mindspore_lite.Model
- class mindspore_lite.Model[source]
The Model class is used to define a MindSpore Lite’s model, facilitating computational graph management.
Examples
>>> import mindspore_lite as mslite >>> model = mslite.Model() >>> print(model) model_path: .
- build_from_file(model_path, model_type, context, config_path='')[source]
Load and build a model from file.
- Parameters
model_path (str) – Path of the input model when build from file. For example, “/home/user/model.ms”. Options: MindSpore model: “model.mindir” | MindSpore Lite model: “model.ms”. For details, see ModelType .
model_type (ModelType) – Define The type of input model file. Options: ModelType.MINDIR | ModelType.MINDIR_LITE.
context (Context) – Define the context used to transfer options during execution.
config_path (str, optional) –
Define the config file path. the config file is used to transfer user defined options during build model. In the following scenarios, users may need to set the parameter. For example, “/home/user/config.txt”. Default: “”.
- Usage 1: Set mixed precision inference. The content and description of the configuration file are as
follows:
[execution_plan] [op_name1]=data_Type: float16 (The operator named op_name1 sets the data type as Float16) [op_name2]=data_Type: float32 (The operator named op_name2 sets the data type as Float32)
- Usage 2: When GPU inference, set the configuration of TensorRT. The content and description of the
configuration file are as follows:
[ms_cache] serialize_Path=[serialization model path](storage path of serialization model) [gpu_context] input_shape=input_Name: [input_dim] (Model input dimension, for dynamic shape) dynamic_Dims=[min_dim~max_dim] (dynamic dimension range of model input, for dynamic shape) opt_Dims=[opt_dim] (the optimal input dimension of the model, for dynamic shape)
- Raises
TypeError – model_path is not a str.
TypeError – model_type is not a ModelType.
TypeError – context is not a Context.
TypeError – config_path is not a str.
RuntimeError – model_path does not exist.
RuntimeError – config_path does not exist.
RuntimeError – load configuration by config_path failed.
RuntimeError – build from file failed.
Examples
>>> 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)[source]
Obtains the input Tensor of the model by name.
- Parameters
tensor_name (str) – the name of one of the input Tensor of the model.
- Returns
Tensor, the input Tensor of the model obtained by the name of the Tensor.
- Raises
TypeError – tensor_name is not a str.
RuntimeError – get input by Tensor name failed.
Examples
>>> 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()[source]
Obtains all input Tensors of the model.
- Returns
list[Tensor], the input Tensor list of the model.
Examples
>>> 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)[source]
Obtains the output Tensor of the model by name.
- Parameters
tensor_name (str) – the name of one of the output Tensor of the model.
- Returns
Tensor, the output Tensor of the model obtained by the name of the Tensor.
- Raises
TypeError – tensor_name is not a str.
RuntimeError – get output by Tensor name failed.
Examples
>>> 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()[source]
Obtains all output Tensors of the model.
- Returns
list[Tensor], the output Tensor list of the model.
Examples
>>> 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)[source]
Inference model.
- Parameters
- Raises
TypeError – inputs is not a list.
TypeError – inputs is a list, but the elements are not Tensor.
TypeError – outputs is not a list.
TypeError – outputs is a list, but the elements are not Tensor.
RuntimeError – predict model failed.
Examples
>>> # 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.02271215e-05 9.92699006e-06 1.69684317e-05 ... 6.69087376e-06 2.16263197e-06 1.24009384e-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.00052925 ... 0.00018388 0.00148316 0.00116824]] >>> # 3. predict which indata is from new MindSpore Lite's 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.00052925 ... 0.00018388 0.00148316 0.00116824]]
- resize(inputs, dims)[source]
Resizes the shapes of inputs. This method is used in the following scenarios:
If multiple inputs of the same size need to predicted, you can set the batch dimension of dims to the number of inputs, then multiple inputs can be performed inference at the same time.
Adjust the input size to the specify shape.
When the input is a dynamic shape (a dimension of the shape of the model input contains -1), -1 must be replaced by a fixed dimension through resize .
The shape operator contained in the model is dynamic shape (a dimension of the shape operator contains -1).
- Parameters
- Raises
TypeError – inputs is not a list.
TypeError – inputs is a list, but the elements are not Tensor.
TypeError – dims is not a list.
TypeError – dims is a list, but the elements are not list.
TypeError – dims is a list, the elements are list, but the element’s elements are not int.
ValueError – The size of inputs is not equal to the size of dims .
RuntimeError – resize input failed.
Examples
>>> 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]