Sample Overview

Sample Obtaining

Click image decoding + scaling + synchronous inference to obtain the sample.

Function Usage

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 into an offline model (.om file) adapted to the Ascend AI Processor. In the sample, load the .om file, decode, resize and infer two .jpg images, obtain the inference results, process the inference results, and outputs the class indexes with the top confidence values.

During model conversion, set CSC parameters to convert YUV420SP images into RGB images to meet the input requirements of the model.

Main APIs

The following table lists the main APIs.

Initialization

  • Call acl.init to initialize the configuration.
  • Call acl.finalize to deinitialize the configuration.

Device management

  • Call acl.rt.set_device to specify the compute device.
  • Call acl.rt.get_run_mode to obtain the running mode of the software stack. The internal processing process varies according to the running mode.
  • Call acl.rt.reset_device to reset the current device and reclaim the associated resources.

Stream management

  • Call acl.rt.create_stream to create a stream.
  • Call acl.rt.destroy_stream to destroy a stream.
  • Call acl.rt.synchronize_stream to block the programs until all tasks in the specified stream are complete.

Memory management

  • Call acl.rt.malloc_host to allocate memory on the host.
  • Call acl.rt.free_host to deallocate the memory on the host.
  • Call acl.rt.malloc to allocate device memory.
  • Call acl.rt.free to deallocate device memory.

If device memory is needed to store the input or output data before media data processing, call acl.media.dvpp_malloc to allocate memory and acl.media.dvpp_free to deallocate memory.

Data transfer

If your app runs on the host, call the acl.rt.memcpy API.

  • 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.

Media data processing V1

  • Call acl.media.dvpp_jpeg_decode_async to decode *.jpg images into YUV420SP images.
  • Call acl.media.dvpp_vpc_resize_async to resize a YUV420SP image to a 224 x 224 image.

Model inference

  • Call acl.mdl.load_from_file_with_mem to load a model from an .om file.
  • Call acl.mdl.execute to perform model inference.

    Before inference, use the CSC parameters in the .om file to convert a YUV420SP image into an RGB image.

  • Call acl.mdl.unload to unload a model.

Data postprocessing (single-operator calling)

Process the model inference result. Call Cast to convert the data type of the inference result from float32 to float16, and then call ArgMaxD to search for the class indexes with the maximum confidence values in the inference result.

Load the single-operator model file by calling acl.op.set_model_dir, execute the Cast operator by calling acl.op.cast, and execute the ArgMaxD operator by calling acl.op.execute_v2.

Directory Structure

The directory structure is as follows:

vpc_resnet50_imagenet_classification
├──scripts
│ ├── host_version.conf // Version number configuration file.
│ └── testcase_300.sh // Run script.
├──src
│ ├── acl_dvpp.py // Image resizing implementation file
│ ├── acl_model.py // Model inference implementation file
│ ├── acl_op.py // Single-operator precision conversion implementation file
│ ├── acl_sample.py // Running file
│ ├── acl_util.py // Implementation file of tool functions 
│ └── constant.py // Constant definition
├── data // Test data, which needs to be deployed by users.
│ ├── fusion_result.json // File generated after atc conversion, which records the fused operator information.
│ ├── dog1_1024_683.jpg
│ └── dog2_1024_683.jpg
├── caffe_model // Model deployed by users.
│ ├── aipp.cfg // Model configuration data.
│ ├── resnet50.caffemodel // ResNet-50 model
│ └── resnet50.prototxt // ResNet-50 network file
├── op_models // Directory generated after ATC conversion.
│ ├── 0_Cast_0_2_1000_1_2_1000.om // Precision conversion custom operator
│ ├── 1_ArgMaxD_1_2_1000_3_2_1.om // Precision conversion custom operator
│ └── op_list.json // Configuration file of the precision conversion operator
└── model // Directory generated after the inference model is converted.
│ └── resnet50_aipp.om 
└── README_CN.md