Converting an ONNX Model Using ATC Commands

This section describes how to use ATC commands to convert an .onnx model file and perform inference on the AI processor.

The GE provides the model conversion capability for model files exported from front-end frameworks such as PyTorch, TensorFlow, and MindSpore. You can use the ATC command line tool to convert the model files into offline models that adapt to the AI processor, and to execute the models in graph mode. Currently, ATC supports the following conversions:

  • From *.onnx to *.om
  • From *.pb to *.om
  • From *.air to *.om
  • From *.prototxt and *.caffemodel to *.om

The following uses the conversion from *.onnx to *.om as an example to describe the basic usage method.

  1. Obtain an ONNX network model.

    Download the model file, and then upload the file to any directory of the development environment as the CANN running user, for example, upload the file to the $HOME/module/ directory.

  2. Use the ATC command line tool to convert a model (for example, .onnx).
    atc --model=$HOME/module/resnet50.onnx --framework=5 --output=$HOME/module/out/onnx_resnet50 --soc_version=<soc_version>
    • --model: path (including the file name) of the original network model file.
    • --framework: framework type of the original network model. The value 3 indicates a TensorFlow model, 5 an ONNX model, and 1 a model file in .air format of the MindSpore framework or a standard .air file exported from TorchAir through export.
    • --output: path (including the file name) for storing the generated offline model after conversion, for example, $HOME/module/out/tf_resnet50, where $HOME/module/out/ is the path for storing the generated offline model, and tf_resnet50.om is the name of the generated offline model.
    • --soc_version: AI processor version. To query the value, perform the following steps:
      • For the following products: Run the npu-smi info command on the server where AI processor is installed to obtain the Name information. The actual value is AscendName. For example, if Name is xxxyy, the actual value is Ascendxxxyy.

        Atlas A2 training product / Atlas A2 inference product

        Atlas 200I/500 A2 inference product

        Atlas inference product

        Atlas training product

      • For the Atlas A3 training product / Atlas A3 inference product : Run the npu-smi info -t board -i id -c chip_id command on the server where AI processor is installed to obtain the Chip Name and NPU Name information. The actual value is Chip Name_NPU Name. For example, if the value of Chip Name is Ascendxxx and the value of NPU Name is 1234, the actual value is Ascendxxx_1234. Note that:
        • id: device ID, which is the NPU ID obtained by running the npu-smi info -l command.
        • chip_id: chip ID, which is obtained by running the npu-smi info -m command.
      • For the Atlas 350 Accelerator Card: Run the npu-smi info -t board -i id command on the server where AI processor is installed to obtain the Chip Name and NPU Name information. The actual value is Chip Name_NPU Name. For example, if the value of Chip Name is Ascendxxx and the value of NPU Name is 1234, the actual value is Ascendxxx_1234.

        id indicates the device ID, which is the NPU ID obtained by running the npu-smi info -l command.

    For details about more parameters and their usage, see ATC.

  3. If the following information is displayed, the model is converted:
    1
    ATC run success, welcome to the next use.
    

    After the command is executed successfully, the *.onnx model is converted to an *.om offline model adapted to the AI processor. You can view the generated *.om offline model file in the path specified by the --output parameter.

  4. Perform subsequent operations.

    After the model is converted into an offline .om model, you can call the model loading API to load the .om offline model and then call the model run API to perform inference. The following shows the simplified process of loading a model file and performing inference. For details about how to load and perform inference, see section "Model Inference" in Application Development (C&C++).

    1
    2
    3
    4
    5
    6
    7
    8
    9
    // 1. Specify the model path.
    const char* omModelPath = "./model/onnx_resnet50.om";
    uint32_t modelId;
    // 2. Load the offline model file. After the model is successfully loaded, the model ID is returned.
    ret = aclmdlLoadFromFile(omModelPath, &modelId);
    // 3. Execute the model. inputDs and outputDs are the input and output dataset objects of the model.
    ret = aclmdlExecute(modelId, inputDs, outputDs);
    // 4. Unload the model.
    ret = aclmdlUnload(modelId);