Image Classification Based on Caffe ResNet-50 (Asynchronous Inference, Excluding Data Pre-Processing)
Sample Overview
Sample path: ${pyACL_install_path}/sample/acl_execute_model/acl_resnet_async/
${pyACL_install_path} indicates the installation path of the pyACL package. (The pyACL package is installed independently. If the pyACL package is a combined package, for example, Toolkit and NNRT, ${pyACL_install_path} indicates the directory where pyACL is located after the combination package is successfully installed.)
If the cann-toolkit or cann-nnrt software package is not installed in the environment, upload the code sample file acl_resnet_async from the environment where the cann-toolkit or cann-nnrt software package is installed.
Function Description
This sample shows how to classify images based on the Caffe ResNet-50 network (single input and single batch).
Convert the model file of the Caffe ResNet-50 network into an offline model (.om file) that adapts to Ascend AI Processors. In the sample, load the .om file and perform n (an application parameter configured by the user) 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. |
Synchronous wait |
Synchronous wait |
acl.rt.subscribe_report |
Specifies the thread that processes the callback function in the stream. |
Synchronous wait |
acl.rt.unsubscribe_report |
Unsubscribes a thread and specifies a callback function in a stream to not be handled by a specified thread. |
|
Synchronous wait |
acl.rt.launch_callback |
Adds a callback function executed on the host or device to the stream task queue. |
|
Synchronous wait |
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 memory on the device. |
|
Memory management |
acl.rt.malloc_host |
Allocates memory on the host. |
|
Common module |
-- |
acl.util.ptr_to_numpy |
Converts a pointer to data of the NumPy type. |
-- |
acl.util.numpy_to_ptr |
Converts data of the NumPy type to a pointer. |
|
-- |
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. |
|
Resource release |
Memory management |
acl.rt.free |
Frees the memory on the device. |
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. |
|
Initialization |
acl.finalize |
Deinitializes the ACL. |
Asynchronous Model Inference Process
Figure 10-5 shows the asynchronous model inference process.
Directory Structure
The directory structure is as follows:
acl_resnet_async ├── acl_net.py //Running file ├── constant.py //Constant definition ├── caffe_model │ ├── resnet50.caffemodel //ResNet-50 model │ └── resnet50.prototxt //ResNet-50 model file ├── data │ ├── dog1_1024_683.jpg //Test picture data │ └── dog2_1024_683.jpg //Test picture data └── model └── resnet50.om //Model file generated after conversion
Application Running
Model Conversion
- Log in to the development environment as the HwHiAiUser user (running user).
- For details, see "Getting Started with ATC" in ATC Tool Instructions.
- Prepare data.
Obtain the ResNet-50 weight file (*.caffemodel) and model file (resnet50.prototxt) from the following URL and upload the files to the acl_resnet_async/caffe_model directory in the development environment as the HwHiAiUser user (running user):
https://github.com/Ascend-Huawei/models/tree/master/computer_vision/classification/resnet50: find the download link in the README_en.md file.
- Convert the ResNet-50 network into an offline model (.om file) that adapts to Ascend AI Processors.
Go to the acl_resnet_async directory and run the following command:
atc --model=caffe_model/resnet50.prototxt --weight=caffe_model/resnet50.caffemodel --framework=0 --output=model/resnet50 --soc_version=Ascend310 --input_format=NCHW --input_fp16_nodes=data -output_type=FP32 --out_nodes=prob:0
--output: The generated resnet50.om file is stored in the acl_resnet_async/model directory.
Application Running
- Upload the data directory, model directory and all Python script files in the acl_resnet_async directory of the development environment to the same directory (for example, /home/HwHiAiUser/acl_resnet_async) of the operating environment (host) as the running user.
If the development environment and operating environment are on the same host, skip this step.
- Configure the operating environment by referring to Environment Variable Configuration.
- Run the acl_net.py command. After the command is successfully run, the following information is displayed: