Image Classification with ResNet-50 (Synchronous Inference)

Sample Obtaining

Refer to resnet50_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, decode, resize, and run synchronous inference of the two .jpg images, process the obtained inference results, and output the class indexes with the top 5 confidence values 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

  • 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.
  • aclmdlExecute: performs synchronous model inference.
  • aclmdlUnload: unloads a model.

Data Postprocessing

The sample processes the model inference result and prints the class indexes with the top 5 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 sample_process.cpp before the OutputModelResult call.

//Print the top 5 confidence values with indexes using DumpModelOutputResult
//If the output result needs to be dumped to a file in the current directory
modelProcess.DumpModelOutputResult();
modelProcess.OutputModelResult();

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
│   ├── 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
│   ├── 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_imagenet_classification to obtain the sample. View the README file in the sample.

App Build and Run (Ascend RC Mode)

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