Image Classification with ResNet-50 (Asynchronous Inference)
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.

Principles
The following table lists the key functions involved in this sample.
Initialization |
|
|---|---|
Device Management |
|
Stream Management |
|
Memory Management |
|
Data Transfer |
aclrtMemcpy (used when the app runs on the host):
Data transfer is not required if your app runs in the board environment. |
Model Inference |
|
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