Sample Overview
Sample Obtaining
Visit the Ascend samples repository on Gitee and download the sample package that matches your CANN version in use. For the version mapping, see "Release Notes" in the README file. Find the resnet50_async_imagenet_classification sample in the python/level2_simple_inference/1_classification/resnet50_async_imagenet_classification directory.
Function 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 into an offline model (.om file) adapted to the Ascend AI Processor. In the sample, load the .om file and perform n (an application parameter configured by the user. It is defaulted to 4 and can be set by the --execute_times parameter.) times of asynchronous inference on two .jpg images. Then, process the obtained n inference results, and output the class indexes with the top 5 confidence values of each image.
Main APIs
|
Function |
ACL Module |
ACL Interface Function |
Description |
|---|---|---|---|
|
Resource initialization |
Initialization |
acl.init |
Initializes the ACL configuration. |
|
Device management |
acl.rt.set_device |
Specifies the device for computation. |
|
|
Context management |
acl.rt.create_context |
Creates a context. |
|
|
Model initialization |
Model loading and execution |
acl.mdl.load_from_file |
Loads the model from the .om file to the device. |
|
Data types and operation APIs |
acl.mdl.create_desc |
Creates data for describing the model. |
|
|
Data types and operation APIs |
acl.mdl.get_desc |
Obtains data for describing the model. |
|
|
Model inference |
Model loading and execution |
acl.mdl.execute_async |
Performs asynchronous model inference. |
|
Synchronization |
Synchronization |
acl.rt.subscribe_report |
Specifies the thread that processes the callback function in the stream. |
|
Synchronization |
acl.rt.unsubscribe_report |
Unsubscribes from a thread and specifies a callback function in a stream to not be handled by a specified thread. |
|
|
Synchronization |
acl.rt.launch_callback |
Adds a callback function executed on the host or device to the stream task queue. |
|
|
Synchronization |
acl.rt.process_report |
Triggers callback handling after a specified period. |
|
|
Data exchange |
Memory management |
acl.rt.memcpy |
Sends data from the host to the device or from the device to the host. |
|
Memory management |
acl.rt.malloc |
Allocates device memory. |
|
|
Memory management |
acl.rt.malloc_host |
Allocates memory on the host. |
|
|
Common module |
-- |
acl.util.ptr_to_numpy |
Converts void* data to NumPy data. |
|
-- |
acl.util.numpy_to_ptr |
Converts NumPy data to void* data. |
|
|
-- |
acl.util.start_thread |
Starts a callback function thread. |
|
|
-- |
acl.util.stop_thread |
Reclaims a callback function thread. |
|
|
Data postprocessing |
Data types and operation APIs |
acl.mdl.get_dataset_buffer |
Obtains dataset information. |
|
Data types and operation APIs |
acl.mdl.get_dataset_num_buffers |
Obtains dataset information. |
|
|
Allocation destruction |
Memory management |
acl.rt.free |
Frees device memory. |
|
Memory management |
acl.rt.free_host |
Frees the memory on the host. |
|
|
Model loading and execution |
acl.mdl.unload |
Unloads a model. |
|
|
Context management |
acl.rt.destroy_context |
Destroys a context. |
|
|
Device management |
acl.rt.reset_device |
Resets the current device and reclaims the resources on the device. |
|
|
Deinitialization |
acl.finalize |
Deinitializes ACL. |
Asynchronous Model Inference Process
Figure 1 shows the asynchronous model inference process.
Directory Structure
The directory structure is as follows:
resnet50_async_imagenet_classification ├──src │ ├── acl_net.py // Running file │ └── constant.py // Constant definition ├── data │ ├── dog1_1024_683.jpg // Test image data │ └── dog2_1024_683.jpg // Test image data ├── caffe_model │ ├── resnet50.caffemodel // ResNet-50 model │ └── resnet50.prototxt // ResNet-50 network file └── model └── resnet50.om // Model file generated after conversion
