Basic Sample User Guide

The Basic Sample Package provide the Quick start sample based on resnet50 model. You can experience and develop applications based on this sample, lowering the development threshold.

Deliverables Description

The Basic Sample package is located in the samples/ucp_tutorial/dnn/basic_samples/ path of the horizon_j6_open_explorer release package and consists the following main contents:

RELEASE PACKAGEDESCRIPTIONS
basic_samplesincluding sample codes and scripts.
Note

The on-board model needs to be obtained first by executing the resolve_runtime_sample.sh script in the samples/ai_toolchain/model_zoo/runtime/basic_samples directory of OE package.

Directory of the Basic Sample Package is shown as below:

├── code # source code of samples ├── 00_quick_start # a quick-start single image inference sample using the resnet50 model ├── CMakeLists.txt └── src ├── 01_api_tutorial # BPU SDK API sample code ├── CMakeLists.txt ├── mem ├── model └── roi_infer ├── build.sh # compilation script ├── build_aarch64.sh # compilation script, running in aarch64 environment ├── build_x86.sh # compilation script, running in x86 environment ├── CMakeLists.txt └── resolve.sh # run dependency acquisition script ├── runtime ├── data # preset data └── cls_data ├── model ├── README.md └── runtime -> ../../../../../model_zoo/runtime/basic_samples # soft link, pointed to the model folder in OE package. In the board-side runtime environment, you need to specify the model path ├── script # aarch64 scripts to run samples ├── 00_quick_start ├── 01_api_tutorial ├── aarch64 # aarch64 executable programs generated by the compilation and dependencies └── README.md └── script_x86 # x86 scripts to run samples ├── 00_quick_start ├── 01_api_tutorial ├── x86 # x86 executable programs generated by the compilation and dependencies └── README.md └── README.md
  • The code directory contains source code.

  • The code/00_quick_start folder contains a quick-start sample based on the dnn API.

    This script inference a single image and parse the results using resnet50.

  • The code/build.sh is a application compilation script, and you need to specify the compilation parameters, called by build_aarch64.sh and build_x86.sh.

  • The code/build_aarch64.sh is a quick application compilation script, and the compiled application runs in the aarch64 environment.

  • The code/build_x86.sh is a quick application compilation script, and the compiled application runs in the x86 environment.

  • The code/resolve.sh is the program running dependency acquisition script, which needs to be executed before the user compiles the model.

  • The runtime folder contains scripts to run samples, preset data and related models.

Prerequisites

Prepare the Dev Board

  1. After getting the development board, upgrade the system image file to the version recommended by the sample package.

  2. Make sure the local development machine and development board can be connected remotely.

Compilation

Take the following steps to perform the compilation:

  1. Install the gcc-11-aarch64-linux-gnu and g++-11-aarch64-linux-gnu cross-compilation tool.

  2. In the ucp_tutorial/tools/basic_samples directory, there is a pre-configured build script build.sh, with the options -a x86 and -a aarch64 to support two types of builds respectively, and executing the build.sh script directly will complete the one-click build, and the generated files will be saved to the ucp_tutorial/vp/vp_samples directory. Moreover, the directory encompasses two compilation scripts, namely build_aarch64.sh and build_x86.sh, tailored to distinct compilation configurations. Employing these scripts mirrors the functionality of the build.sh script.

  3. After executing the compilation script, the executable programs and dependency files required to run the samples will be generated and copied to the runtime/script or runtime/script_x86 directory.

Note

By default, the cross-compilation tool specified in the build.sh script is located in the /usr/bin directory. You can manually modify the build.sh script if you want to change the location.

How to Use (Basic Samples)

Note

The version in the log and command line below is just an example, and the version in the actual printed log is subject to the version in the OE package.

If you want to run the example in an x86 environment, please use the script under runtime/script_x86.

Sample scripts are in the runtime/script folder, and the directory structure after compilation is shown as follows:

script: ├── 00_quick_start ├── README.md └── run_resnet.sh ├── 01_api_tutorial ├── README.md ├── model.sh ├── roi_infer.sh └── sys_mem.sh ├── aarch64 # executable programs generated by the compilation and dependencies ├── bin ├── run_resnet ├── model_example ├── roi_infer └── sys_mem_example └── lib ├── libdnn.so ├── libhbrt4.so ├── libhbtl_ext_dnn.so ├── libhbtl.so ├── libhbucp.so ├── libhlog.so ├── libhlog.so.1 ├── libhlog.so.1.3.2 ├── libopencv_world.so.3.4 └── libperfetto_sdk.so └── README.md

Before running, deploy the runtime directory to the board end and execute the corresponding script.

Note
  • To obtain the model publisher for the basic_samples sample package, you can execute the resolve_runtime_sample.sh in the samples/ai_toolchain/model_zoo/runtime/basic_samples dictionary of the OE package.

  • To obtain other dependencies of the basic_samples sample package, you can execute the resolve.sh script in the samples/ucp_tutorial/dnn/basic_samples/code directory of the OE package.

  • The model folder contains the path of the model, the runtime folder is a soft link and the link path is ../../../model_zoo/runtime/basic_samples.

quick_start

Under 00_quick_start directory, we provide a quick-start example to introduce the use process of model inference. Taking the resnet50 as an example, it includes the complete process code from data preparation to model inference, to execution of post-processing, and finally to generating classification results.

00_quick_start/ ├── README.md └── run_resnet.sh
  • run_resnet.sh: This script implements a sample function that uses resnet50 to read a single image for inference. To run it, go to the 00_quick_start, and then execute sh run_resnet.sh directly, as shown in the following code block:

    #!/bin/sh root@hobot:/userdata/app/horizon/basic_samples/runtime/script/00_quick_start# sh run_resnet.sh ../aarch64/bin/run_resnet --model_file=../../model/runtime/resnet50/resnet50_224x224_nv12.hbm --image_file=../../data/cls_images/zebra_cls.jpg --top_k=5 [I][60872][04-05][17:29:24:752][main.cc:249][run_resnet][DNN_BASIC_SAMPLE] input[0] name is input [I][60872][04-05][17:29:24:752][main.cc:265][run_resnet][DNN_BASIC_SAMPLE] output[0] name is output [I][60872][04-05][17:29:24:755][main.cc:153][run_resnet][DNN_BASIC_SAMPLE] read image to tensor as nv12 success [I][60872][04-05][17:29:24:758][main.cc:192][run_resnet][DNN_BASIC_SAMPLE] TOP 0 result id: 340 [I][60872][04-05][17:29:24:758][main.cc:192][run_resnet][DNN_BASIC_SAMPLE] TOP 1 result id: 292 [I][60872][04-05][17:29:24:758][main.cc:192][run_resnet][DNN_BASIC_SAMPLE] TOP 2 result id: 9 [I][60872][04-05][17:29:24:758][main.cc:192][run_resnet][DNN_BASIC_SAMPLE] TOP 3 result id: 353 [I][60872][04-05][17:29:24:758][main.cc:192][run_resnet][DNN_BASIC_SAMPLE] TOP 4 result id: 343

api_tutorial

The API tutorial refers to the samples in the 01_api_tutorial folder. These samples help developers understand how to use the model inference APIs. This folder contains the following scripts:

├── model.sh ├── roi_infer.sh └── sys_mem.sh
  • The model.sh script reads model information. To run it, go to the 01_api_tutorial, and then execute sh model.sh, as shown in the following code block:

    #!/bin/sh root@hobot:/userdata/app/horizon/basic_samples/runtime/script/01_api_tutorial# sh model.sh ../aarch64/bin/model_example --model_file_list=../../model/runtime/resnet50/resnet50_224x224_nv12.hbm [I][61637][04-05][18:02:48:032][model_example.cc:96][model_example][DNN_BASIC_SAMPLE] model count:1, model[0]: resnet50_224x224_nv12 [I][61637][04-05][18:02:48:032][model_example.cc:105][model_example][DNN_BASIC_SAMPLE] hbDNNGetModelHandle [resnet50_224x224_nv12] success! [I][61637][04-05][18:02:48:032][model_example.cc:175][model_example][DNN_BASIC_SAMPLE] [resnet50_224x224_nv12] Model Info: input num: 1, input[0] validShape: ( 1, 3, 224, 224 ), stride: ( 0, 0, 0, 0 ), tensorType: 1, output num: 1, output[0] validShape: ( 1, 1000 ), stride: ( 4000, 4 ), tensorType: 13
  • The roi_infer.sh script helps understand the model inference method with ROI input. The function of the sample code is to convert an image into nv12 data, and then use the given ROI box for inference.

  • The sys_mem.sh script helps understand how to use the hbUCPMalloc, hbUCPMallocCached, hbUCPMemFlush and hbUCPFree APIs. To run it, go to the 01_api_tutorial folder, and then execute sh sys_mem.sh.

Helper Tools (Log)

There are 2 types of logs: sample log and dnn log. Wherein, sample log refers to the log in the Basic Sample Package release package; while dnn log refers to the log in the embedded dnn library.Developers can specify logs according to their own needs.

Sample Log

The sample log mainly uses hlog, which is mainly divided into 7 levels:

The log level can be set to 0, 1, 2, 3, 4, 5 and 6, corresponding to trace, debug, info, warn, error, critical and never, with the default being info.

dnn Log

For the configuration of dnn logs, please read the Configuration Info section in the Model Inference API Introduction.