Vision SDK model inference is performed based on the specified input and model to obtain the output result. The OM and MindIR format are supported for inference. It also supports the inference of ATC-built models with dynamic batches, dynamic image sizes, and dynamic dimension profiles.
For details about related APIs, see Model.
Before model inference, prepare the input data and the model to be loaded, initialize the Model class based on the model path or memory, and call the Infer API of the model class to obtain the model inference result. The input data type and format must be the same as those of the model input data. If you allocate the output data memory by yourself, the output data type and format must be the same as those of the model output. The model input and output information can be queried by calling related APIs of the Model class.
Process of calling APIs of model inference is as follows:
The key APIs are described as follows:
Load the model using either of the following methods based on your service requirements:
Determine how to obtain the output data. You can either construct the output data through the Infer API or construct and receive the model inference output data by yourself.
The following is a code example of key steps of functions and features, which is for reference only and cannot be directly copied for compilation or running.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
// Initialization MxBase::MxInit(); { // Input image binary data prepared by the user std::string filePath = "./test.bin"; // Read the input data to the memory. void* dataPtr = ReadTensor(filePath); // Input data type, which is the same as the model input data type. auto dataType = MxBase::TensorDType::INT32; // Construct the input shape, which is the same as that of the model. std::vector<uint32_t> shape = {1, 128}; // Construct a tensor. MxBase::Tensor tensor(dataPtr, shape, dataType, 0); // Construct the model input. std::vector<MxBase::Tensor> inputs{tensor}; // Model path specified by the user std::string modelPath = "./test.om"; // Load the model based on the model path. MxBase::Model model(modelPath); // Perform model inference. The outputs are the inference results. std::vector<MxBase::Tensor> outputs = model.Infer(inputs); } // Deinitialization MxBase::MxDeInit(); |
1 2 3 4 |
MxBase::ModelLoadOptV2 mdlLoadOpt; mdlLoadOpt.loadType = ModelLoadOptV2::LOAD_MODEL_FROM_FILE; // Specify the model loading mode. mdlLoadOpt.modelPath = modelPath; MxBase::Model model(mdlLoadOpt); |
The process of using a MindIR model for inference is the same as that of using an OM model for inference. Note that before using a MindIR model for inference, you need to install the MindSpore Lite software package and set environment variables. The procedure is as follows.
Pay attention to vulnerabilities listed in the MindSpore open-source community and fix them in a timely manner.
1
|
tar -zxvf mindspore-lite-2.4.0-linux-{arch}.tar.gz --no-same-owner |
ARM servers:
1 2 |
export LD_LIBRARY_PATH={path}/runtime/lib:${LD_LIBRARY_PATH} export LD_LIBRARY_PATH={path}/tools/converter/lib:${LD_LIBRARY_PATH} |
x86_64 servers:
1 2 3 |
export LD_LIBRARY_PATH={path}/runtime/lib:${LD_LIBRARY_PATH} export LD_LIBRARY_PATH={path}/tools/converter/lib:${LD_LIBRARY_PATH} export LD_LIBRARY_PATH={path}/runtime/third_party/dnnl:${LD_LIBRARY_PATH} |
{path} indicates the directory generated after the MindSpore Lite software package is decompressed. Change it as required.
1
|
echo $LD_LIBRARY_PATH |