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 "vpc_jpeg_resnet50_imagenet_classification" sample in the python/level2_simple_inference/1_classification/vpc_jpeg_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).
According to the arguments of the app, the following functions can be implemented:
- Decode a .jpg file into an input image in YUV420SP (NV12) format, resize the image, and perform model inference. Then process the inference result and output the top 5 confidence values.
- Crop an input image in YUV420SP (NV12) format and perform model inference. Then process the inference result and output the top 5 confidence values.
- Crop and paste an input image in YUV420SP (NV12) format and perform model inference. Then process the inference result and output the top 5 confidence values.
- Encode an input image in YUV420SP (NV12) format into a .jpg image.
- Resize 8192 x 8192 input images in YUV420SP (NV12) format to 4000 x 4000.
- Crop the input images in YUV420SP (NV12) format in batches.
- Crop and paste the input images in YUV420SP (NV12) format in batches.
Main APIs
Table 1 shows the 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. |
|
Stream management |
acl.rt.create_stream |
Creates a stream. |
|
Operator loading and execution |
acl.op.set_model_dir |
Loads the directory of a model file. |
|
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. |
|
Data preprocessing |
Media data module - JPEG |
acl.media.dvpp_jpeg_decode_async |
Decodes images. |
acl.media.dvpp_jpeg_encode_async |
Encodes images. |
||
Media data module - VPC |
acl.media.dvpp_vpc_crop_async |
Crops a selected ROI. |
|
acl.media.dvpp_vpc_resize_async |
Resizes the input image to the size of the output image. |
||
acl.media.dvpp_vpc_crop_and_paste_async |
Crops the image and pastes it as the output image. |
||
acl.media.dvpp_vpc_batch_crop_async |
Crops selected ROIs. |
||
acl.media.dvpp_vpc_batch_crop_and_paste_async |
Crops selected ROIs and pastes them as the output images. |
||
Data types and operation APIs |
acl.media.dvpp_create_roi_config |
Creates the ROI configuration. |
|
acl.media.dvpp_set_roi_config series |
Sets configuration parameters of the ROI. |
||
acl.media.dvpp_set_pic_desc series |
Sets image description parameters. |
||
Model inference |
Model loading and execution |
acl.mdl.execute |
Performs synchronous model inference. |
Data postprocessing |
Data types and operation APIs |
acl.op.create_attr |
Creates data of the aclopAttr type. |
acl.create_tensor_desc |
Creates data of the aclTensorDesc type. |
||
acl.get_tensor_desc_size |
Obtains the size of a tensor description. |
||
acl.create_data_buffer |
Creates data of the aclDataBuffer type. |
||
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.media.dvpp_malloc |
Allocates memory for media data processing on the device. |
|
Memory management |
acl.rt.maclloc |
Allocates device memory. |
|
Memory management |
acl.rt.malloc_host |
Allocates host memory. |
|
Single-operator inference |
Operator loading and execution |
acl.op.execute |
Loads and executes an operator. This API is asynchronous. |
Common module |
-- |
acl.util.ptr_to_numpy |
Obtains the numpy.ndarray object based on the pointer address. |
-- |
acl.util.numpy_to_ptr |
Obtains the pointer address of the memory data of the numpy.ndarray object. |
|
Allocation destruction |
Memory management |
acl.rt.free |
Frees device memory. |
Memory management |
acl.media.dvpp_free |
Allocates memory by using acl.media.dvpp_malloc. |
|
Memory management |
acl.rt.free_host |
Frees host memory. |
|
Model loading and execution |
acl.mdl.unload |
Unloads a model. |
|
Stream management |
acl.rt.destroy_stream |
Destroys a stream. |
|
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. |
Directory Structure
The directory structure is as follows:
resnet50_imagenet_classification ├──src │ ├── acl_dvpp.py // Image resizing implementation file │ ├── acl_model.py // Model inference implementation file │ ├── acl_sample.py // Running file │ ├── acl_util.py // Implementation file of tool functions │ ├── acl_vdec.py // Video decoding implementation file │ └── constant.py // Constant definition ├── data │ └── vdec_h265_1frame_rabbit_1280x720.h265 // Video file to be processed by the user, which is obtained by the user. ├── caffe_model │ ├── aipp.cfg │ ├── resnet50.caffemodel // ResNet-50 model │ └── resnet50.prototxt // ResNet-50 network file └── model └── resnet50_aipp.om // Inference model