Image Classification with ResNet-50 (Asynchronous Inference)

Sample Obtaining

Refer to resnet50_async_imagenet_classification to obtain the sample.

Description

This sample shows how to classify images based on the Caffe ResNet-50 network (single input with batch size = 1).

Convert the model file of the Caffe ResNet-50 network to an .om offline model adapted to the Ascend AI Processor. In the sample, load the .om file and perform n (an app parameter configured by the user) times of asynchronous inference on two .jpg images. Then, process the obtained n inference results and output the class index with the top 1 confidence value of each image.

Figure 1 Sample diagram

Principles

The following table lists the key functions involved in this sample.

Initialization

  • aclInit: initializes AscendCL.
  • aclFinalize: deinitializes AscendCL.

Device Management

  • aclrtSetDevice: sets the compute device.
  • aclrtGetRunMode: obtains the run mode of the software stack. The internal processing varies depending on the run mode.
  • aclrtResetDevice: resets the compute device and cleans up all resources associated with the device.

Stream Management

  • aclrtCreateStream: creates a stream.
  • aclrtDestroyStream: destroys a stream.

Memory Management

  • aclrtMallocHost: allocates host memory.
  • aclrtFreeHost: frees host memory.
  • aclrtMalloc: allocates device memory.
  • aclrtFree: frees device memory.

Data Transfer

aclrtMemcpy (used when the app runs on the host):

  • Transfers decode source data from the host to the device.
  • Transfers the inference result from the device to the host.

Data transfer is not required if your app runs in the board environment.

Model Inference

  • aclmdlLoadFromFileWithMem: loads a model from an .om file.
  • Create a thread (for example, t1), call aclrtProcessReport in the thread function, and trigger the callback function (for example, CallBackFunc for processing the model inference result) after a specified time.
  • aclrtSubscribeReport: subscribes thread t1 for handling callback function CallBackFunc in the stream.
  • aclmdlExecuteAsync: performs asynchronous model inference.
  • aclrtLaunchCallback: adds a callback function (CallBackFunc) to be executed on the host or device to the stream task queue.
  • aclrtSynchronizeStream: waits for stream tasks to complete.
  • aclrtUnSubscribeReport: unsubscribes from a thread. The callback function (CallBackFunc) of the stream is no longer processed by the specified thread (t1).
  • aclmdlUnload: unloads a model.

Data Postprocessing

The sample processes the model inference result and prints the class indexes with the top 1 confidence values on the terminal.

The sample provides a user-defined API DumpModelOutputResult, which is used to write the model inference result to a file (after the executable file is executed, the inference result file is generated in the directory of the executable file in the operating environment). This API is not called by default. To call it, insert the following code in model_process.cpp before the OutputModelResult call.

// OutputModelResult prints the top 1 confidence value with index.
// If want to dump output result to file in the current directory,
// use function DumpModelOutputResult. 
ModelProcess::DumpModelOutputResult(data.second);
ModelProcess::OutputModelResult(data.second);

Directory Structure

The sample directory is organized as follows:

├── data
│   ├── dog1_1024_683.jpg            //Test image. Obtain the test image according to the guide and save it to the data directory.
│   ├── dog2_1024_683.jpg            //Test image. Obtain the test image according to the guide and save it to the data directory.

├── inc
│   ├── memory_pool.h                 //Header file that declares functions related to memory pool processing
│   ├── model_process.h              //Header file that declares functions related to model processing
│   ├── sample_process.h              //Header file that declares functions related to resource initialization and destruction
│   ├── utils.h                       //Header file that declares common functions (such as the file reading function)

├── script
│   ├── transferPic.py               //Script that converts a .jpg image to a .bin file and resize it from 1024 x 683 to 224 x 224.

├── src
│   ├── acl.json         //Configuration file for system initialization
│   ├── CMakeLists.txt         //Build script
│   ├── main.cpp               //Implementation file of the main function, for image classification
│   ├── memory_pool.cpp        //Implementation file of functions related to memory pool processing
│   ├── model_process.cpp      //Implementation file of model processing functions
│   ├── sample_process.cpp     //Implementation file of functions related to resource initialization and destruction
│   ├── utils.cpp              //Implementation file of common functions (such as the file reading function)

├── .project     //Project information file, including the project type, project description, and type of the target device
├── CMakeLists.txt    //Build script that calls the CMakeLists file in the src directory

App Build and Run (Ascend EP Mode)

Refer to resnet50_async_imagenet_classification to obtain the sample. View the README file in the sample.

App Build and Run (Ascend RC Mode)

Refer to resnet50_async_imagenet_classification to obtain the sample. View the README file in the sample.