Performing Inference on an AscendCL Application Project

Creating an Application Project

  1. Choose File > New > Project... from the menu bar. The New Project window is displayed, on which choose the Ascend App > Samples > ACL ResNet-50(C++) sample project and click Change to configure the CANN version. See Figure 1.
    Figure 1 Project creation
    • CANN Version: CANN software package version. If the version is not identified or you want to change the version, click Change and select the Ascend-CANN-Toolkit installation path (to the version number or latest level) in the dialog box displayed.
    • Samples: sample project. Select ACL ResNet-50(C++).
  2. Click Next to enter the project parameter configuration page and configure the project name and directory. See Figure 2.
    Figure 2 Project parameter configuration
    • Project name: project name, which can be customized.
    • Project location: project directory. By default, the project is created in $HOME/MindstudioProjects.
  3. Click Finish.

    Figure 3 shows the generated application project directory.

    Figure 3 Application project directory

Obtaining a Model and Data Files

  1. Obtain the third-party model file.

    Download the ResNet-50 network model file (*.prototxt) and weight file (*.caffemodel) from the following links and upload them to the project_directory/model directory on the MindStudio installation server as the MindStudio installation user:

    • ResNet-50 network model file (*.prototxt): Click here to download the file.
    • ResNet-50 weight file (*.caffemodel): Click here to download the file.
  2. Convert the model.

    Convert the third-party model into an offline model (.om file) that adapts to Ascend AI Processors, and then add the .om model file to the application project.

    1. Choose Ascend > Model Converter on the menu bar to go to the model conversion page, and configure the model file and weight file on the Model Information tab page. See Figure 4.
      Figure 4 Model information configuration
    2. Click Next, moving on to the Data Pre-Processing tab page to configure data preprocessing (FP16 data does not need to be preprocessed). See Figure 5.
      Figure 5 Data preprocessing
    3. Click Next, moving on to the Advanced Options Preview tab page to configure advanced options. See Figure 6.
      Figure 6 Advanced option configuration
    4. Click Finish to start model conversion.

      The model conversion log records are printed to the Output window in the lower part of MindStudio. If the message "Model converted successfully" is displayed, the model conversion is complete. The Output window also displays the model conversion commands, environment variables, model conversion result, model output path, and model conversion log path.

      After the model conversion is complete, an .om model file that can be executed in the operating environment is generated in the Output Path on the server.

  3. Prepare inference data.

    Obtain the input images of this sample from the following links and perform operations based on the actual application scenario.

    Data image 1, Data image 2
    1. Upload the obtained files to the project_directory/data directory in the development environment as the running user. If the directory does not exist, create one.
    2. Log in to the development environment as the running user.
    3. Switch to the project_directory/data directory, run the transferPic.py script to convert the two .jpg images into .bin files, and resize the images from 1024 x 683 to 224 x 224. Find the generated .bin files in the project_directory/data directory.
      python3 ../script/transferPic.py

      If you get the following error message during script execution, the Pillow library is missing. Run the pip3 install Pillow --user command to install the Pillow library.

      ModuleNotFoundError: No module named 'PIL'

Developing an Application

After an application project is created, its src directory contains the application template code, including the code for system initialization, model execution, model uninstallation, and resource destruction. If you want to directly use the *.cpp template code in the src directory, modify the following code in sample_process.cpp based on the names and paths of the model file and image. Only relative paths can be set. Absolute paths are not supported.
  • Change the image file names and paths as needed.
    //The .. directory is relative to the directory of the executable file.
    //For example, if the executable file is stored in the out directory, .. indicates the parent directory of the out directory.
    string testFile[] = {
            "../data/dog1_1024_683.bin",
            "../data/dog2_1024_683.bin"
        };
  • Copy the *.om model file to the project_directory/model as required. If a file with the same name exists, rename it to prevent it from being overwritten.
    //The two dots (..) indicate a path relative to the directory of the executable file.
    //For example, if the executable file is stored in the out directory, .. indicates the parent directory of the out directory.
    const char* omModelPath = "../model/resnet50.om";

In the template code, APIs prefixed with acl are available to users. For details about the APIs, see "AscendCL API Reference" in the Application Software Development Guide (C&C++).

Building and Running an Application Project

  1. Perform build configuration.
    In the MindStudio project window, choose Build > Edit Build Configuration... on menu bar.
    1. Set build parameters and click to add configurations.

      The configuration option marked with (default) in the list is the default one. To switch the default option to a custom option, select the custom option and click .

      Figure 7 Build configuration
    2. Click OK to save the build project.
  2. In the MindStudio project window, choose Build > Build CMake Project to use the default configurations for incremental build.

    If no error message is displayed during the build process and the message "Build finished" is displayed, the build is successful.

  3. In the MindStudio project window, choose Run > Edit Configurations... on the menu bar to open the run configuration dialog box.
  4. Choose > CMake Application to add and configure run parameters of the application project. Figure 8 shows a configuration example.
    Figure 8 Run configuration dialog box
    • Executable: Select the path of the executable file used for compilation to the last level.
    • Working directory: working directory. (If a relative path is configured in the project, you need to select a relative working directory.)
  5. After the configuration is complete, click Apply to save the run configuration and click OK to close the run configuration dialog box.
  6. In the MindStudio project window, choose Run > Run....

    In the dialog box that is displayed, select the created run configuration and run the application.

    • If no error message is displayed and the message "Running *** finished" is displayed, the execution is complete.
    • If an error message is displayed during application execution, check the detailed logs of the operating environment. For details, see the Log Reference.
      • The default log path is $HOME/ascend/log. For details, see the "Log Files" section.
      • The default log level is ERROR. For details about how to query or change the log level, see the "Log Level Setting" section.