mindspore_lite.Model

View Source On Gitee
class mindspore_lite.Model[source]

The Model class defines 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=None, config_path='', config_dict: dict = None)[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.mindir”. Model should use .mindir as suffix.

  • model_type (ModelType) – Define The type of input model file. Option is ModelType.MINDIR. For details, see ModelType .

  • context (Context, optional) – Define the context used to transfer options during execution. Default: None. None means the Context with cpu target.

  • 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)
      

  • config_dict (dict, optional) –

    When you set config in this dict, the priority is higher than the configuration items in config_path.

    Set rank table file for inference. The content of the configuration file is as follows:

    [ascend_context]
    rank_table_file=[path_a](storage initial path of the rank table file)
    

    When set

    config_dict = {"ascend_context" : {"rank_table_file" : "path_b"}}
    

    The the path_b from the config_dict will be used to compile the model.

Raises

Examples

>>> # Testcase 1: build from file with default cpu context.
>>> import mindspore_lite as mslite
>>> model = mslite.Model()
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR)
>>> print(model)
model_path: mobilenetv2.mindir.
>>> # Testcase 2: build from file with gpu context.
>>> import mindspore_lite as mslite
>>> model = mslite.Model()
>>> context = mslite.Context()
>>> context.target = ["cpu"]
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR, context)
>>> print(model)
model_path: mobilenetv2.mindir.
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()
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR)
>>> inputs = model.get_inputs()
get_outputs()[source]

Obtains all output information Tensors of the model.

Returns

list[TensorMeta], the output TensorMeta list of the model.

predict(inputs, outputs=None)[source]

Inference model.

Parameters
  • inputs (list[Tensor]) – A list that includes all input Tensors in order.

  • outputs (list[Tensor], optional) – A list that includes all output Tensors in order, this tensor include output data buffer.

Returns

list[Tensor], the output Tensor list of the model.

Raises
  • TypeErrorinputs is not a list.

  • TypeErroroutputs is not a list.

  • TypeErrorinputs is a list, but the elements are not Tensor.

  • TypeErroroutputs 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()
>>> #default context's target is cpu
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR)
>>> inputs = model.get_inputs()
>>> in_data = np.fromfile("input.bin", dtype=np.float32)
>>> inputs[0].set_data_from_numpy(in_data)
>>> outputs = model.predict(inputs)
>>> for output in outputs:
...     data = output.get_data_to_numpy()
...     print("outputs' shape: ", data.shape)
...
outputs' shape:  (1,1001)
>>> # 2. predict which indata is numpy array
>>> import mindspore_lite as mslite
>>> import numpy as np
>>> model = mslite.Model()
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR)
>>> inputs = model.get_inputs()
>>> for input in inputs:
...     in_data = np.arange(1 * 3 * 224 * 224, dtype=np.float32).reshape((1, 3, 224, 224))
...     input.set_data_from_numpy(in_data)
...
>>> outputs = model.predict(inputs)
>>> for output in outputs:
...     data = output.get_data_to_numpy()
...     print("outputs' shape: ", data.shape)
...
outputs' shape:  (1,1001)
>>> # 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()
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR)
>>> inputs = model.get_inputs()
>>> input_tensors = []
>>> for input in inputs:
...     input_tensor = mslite.Tensor()
...     input_tensor.dtype = input.dtype
...     input_tensor.shape = input.shape
...     input_tensor.format = input.format
...     input_tensor.name = input.name
...     in_data = np.arange(1 * 3 * 224 * 224, dtype=np.float32).reshape((1, 3, 224, 224))
...     input_tensor.set_data_from_numpy(in_data)
...     input_tensors.append(input_tensor)
...
>>> outputs = model.predict(input_tensors)
>>> for output in outputs:
...     data = output.get_data_to_numpy()
...     print("outputs' shape: ", data.shape)
...
outputs' shape:  (1,1001)
resize(inputs, dims)[source]

Resizes the shapes of inputs. This method is used in the following scenarios:

  1. 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.

  2. Adjust the input size to the specify shape.

  3. 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 .

  4. The shape operator contained in the model is dynamic shape (a dimension of the shape operator contains -1).

Parameters
  • inputs (list[Tensor]) – A list that includes all input Tensors in order.

  • dims (list[list[int]]) – A list that includes the new shapes of input Tensors, should be consistent with input Tensors’ shape.

Raises
  • TypeErrorinputs is not a list.

  • TypeErrorinputs is a list, but the elements are not Tensor.

  • TypeErrordims is not a list.

  • TypeErrordims is a list, but the elements are not list.

  • TypeErrordims 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 inputs failed.

Examples

>>> import mindspore_lite as mslite
>>> model = mslite.Model()
>>> model.build_from_file("mobilenetv2.mindir", mslite.ModelType.MINDIR)
>>> inputs = model.get_inputs()
>>> print("Before resize, the first input shape: ", inputs[0].shape)
Before resize, the first input shape: [1, 3, 224, 224]
>>> model.resize(inputs, [[1, 3, 112, 112]])
>>> print("After resize, the first input shape: ", inputs[0].shape)
After resize, the first input shape: [1, 3, 112, 112]
update_weights(weights)[source]

Update constant weight of the model node.

Parameters

weights (list[list[Tensor]]) – A list that includes all update weight Tensors.

Raises
Tutorial Examples: