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_imagenet_classification" sample in the python/level2_simple_inference/1_classification/resnet50_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) that adapts to Ascend AI Processors. In the sample, load the .om file, infer two .jpg images synchronously, obtain the inference results, process the inference results, and outputs the class indexes with the top 5 confidence values.
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 |
Performs synchronous model inference. |
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. |
|
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. |
Synchronous Model Inference Process
Figure 1 shows the synchronous model inference process.
Directory Structure
The directory structure is as follows:
resnet50_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
