Object Detection with YOLOv3 (Dynamic Batch/Image Size)

Sample Obtaining

Refer to YOLOV3_dynamic_batch_detection_picture to obtain the sample.

Description

This sample implements object detection based on the Caffe YOLOv3 network in the dynamic batch/image size scenario.

The Caffe YOLOv3 network is converted into an .om offline model adapted to the Ascend AI Processor. In the conversion command, set the batch size profiles (for example, 1, 2, 4, and 8) or the image size profiles (for example, 416, 416, 832, 832, 1248, and 1248). In your app, load the .om file, select the batch/image size for inference by passing the corresponding argument, and output the inference result to a file.

Principles

The following table lists the key functions involved in this sample.

Initialization

  • aclInit: initializes AscendCL.
  • aclFinalize: deinitializes AscendCL.

Device Management

  • aclrtSetDevice: sets the compute device.
  • aclrtGetRunMode: obtains the run mode of the software stack. The internal processing varies depending on the run mode.
  • aclrtResetDevice: resets the compute device and cleans up all resources associated with the device.

Stream Management

  • aclrtCreateStream: creates a stream.
  • aclrtDestroyStream: destroys a stream.

Memory Management

  • aclrtMalloc: allocates device memory.
  • aclrtFree: frees device memory.

Data Transfer

aclrtMemcpy (used when the app runs on the host):

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

Model Inference

  • aclmdlLoadFromFileWithMem: loads a model from an .om file.
  • aclmdlSetDynamicBatchSize or aclmdlSetDynamicHWSize: sets the runtime batch or image size.
  • aclmdlExecute: performs synchronous model inference.
  • aclmdlUnload: unloads a model.

Data Postprocessing

DumpModelOutputResult: outputs the model inference result to a file. (After the executable file is executed, the inference result file is stored in the same directory in the operating environment as the executable file of the app.)

processModel.DumpModelOutputResult();

Directory Structure

The sample directory is organized as follows:

├── data
│   ├── tools_generate_data.py            //Script for generating test data

├── inc
│   ├── model_process.h              //Header file that declares functions related to model processing
│   ├── sample_process.h              //Header file that declares functions related to resource initialization and destruction
│   ├── utils.h                       //Header file that declares common functions (such as the file reading function)

├── src
│   ├── acl.json         //Configuration file for system initialization
│   ├── CMakeLists.txt         //Build script
│   ├── main.cpp               //Implementation file of the main function, for image classification
│   ├── model_process.cpp      //Implementation file of model processing functions
│   ├── sample_process.cpp     //Implementation file of functions related to resource initialization and destruction
│   ├── utils.cpp              //Implementation file of common functions (such as the file reading function)

├── .project     //Project information file, including the project type, project description, and type of the target device
├── CMakeLists.txt    //Build script that calls the CMakeLists file in the src directory

App Build and Run (Ascend EP Mode)

Refer to YOLOV3_dynamic_batch_detection_picture to obtain the sample. View the README file in the sample.

App Build and Run (Ascend RC Mode)

Refer to YOLOV3_dynamic_batch_detection_picture to obtain the sample. View the README file in the sample.