Common Algorithm Model Samples

Sample Location

The common algorithmic model samples are located at 02_preq_examples/, 03_classification/, 04_detection/ and 07_segmentation/ folders in the horizon_model_convert_sample path.

Prepare Datasets

Dataset Download Address

The dataset can be downloaded from the following address.

DatasetAddress
ImageNethttps://www.image-net.org/download.php
COCOhttps://cocodataset.org/
VOChttp://host.robots.ox.ac.uk/pascal/VOC/ (need to download both versions 2007 and 2012)
Cityscapeshttps://github.com/mcordts/cityscapesScripts
CIFAR-10http://www.cs.toronto.edu/~kriz/cifar.html

Dataset Reference Structure

To facilitate your subsequent steps, after the dataset has been downloaded, you need to process the evaluation dataset according to the structure suggested by Horizon below.

DatasetDescription
ImageNet Dataset

We recommend that you unzip the downloaded dataset into the following structure. Please contact Horizon if you encounter any problems during data preparation.

imagenet/ ├── calibration_data │ ├── ILSVRC2012_val_00000001.JPEG │ ├── ... │ └── ILSVRC2012_val_00000100.JPEG ├── ILSVRC2017_val.txt ├── val │ ├── ILSVRC2012_val_00000001.JPEG │ ├── ... │ └── ILSVRC2012_val_00050000.JPEG └── val.txt

COCO Dataset

We recommend that you unzip the downloaded dataset into the following structure. Please contact Horizon if you encounter any problems during data preparation.

coco/ ├── calibration_data │ ├── COCO_val2014_000000181007.jpg │ ├── ... │ └── COCO_val2014_000000181739.jpg └── coco_val2017 ├── annotations │ ├── instances_train2017.json │ └── instances_val2017.json └── images ├── 000000000139.jpg ├── 000000000285.jpg ├── ... ├── 000000581615.jpg └── 000000581781.jpg

VOC Dataset

Attention
Please note that the VOC2012 directory currently stores two datasets, VOC2007 and VOC2012, so please follow the directory structure below for the evaluation dataset. Please contact Horizon if you encounter any problems during data preparation.

VOCdevkit/ └── VOC2012 ├── Annotations │ ├── 2007_000027.xml │ ├── ... │ └── 2012_004331.xml ├── ImageSets │ ├── Action │ │ ├── jumping_train.txt │ │ ├── jumping_trainval.txt │ │ ├── jumping_val.txt │ │ ├── ... │ │ ├── val.txt │ │ ├── walking_train.txt │ │ ├── walking_trainval.txt │ │ └── walking_val.txt │ ├── Layout │ │ ├── train.txt │ │ ├── trainval.txt │ │ └── val.txt │ ├── Main │ │ ├── aeroplane_train.txt │ │ ├── aeroplane_trainval.txt │ │ ├── aeroplane_val.txt │ │ ├── ... │ │ ├── train.txt │ │ ├── train_val.txt │ │ ├── trainval.txt │ │ ├── tvmonitor_train.txt │ │ ├── tvmonitor_trainval.txt │ │ ├── tvmonitor_val.txt │ │ └── val.txt │ └── Segmentation │ ├── train.txt │ ├── trainval.txt │ └── val.txt ├── JPEGImages │ ├── 2007_000027.jpg │ ├── ... │ └── 2012_004331.jpg ├── SegmentationClass │ ├── 2007_000032.png │ ├── ... │ └── 2011_003271.png ├── SegmentationObject │ ├── 2007_000032.png │ ├── ... │ └── 2011_003271.png └── train.txt

Citys capes Dataset

We recommend that you unzip the downloaded dataset into the following structure. Please contact Horizon if you encounter any problems during data preparation.

cityscapes/ ├── cityscapes_calibration_data │ ├── aachen_000000_000019_leftImg8bit.png │ ├── ... │ └── aachen_000099_000019_leftImg8bit.png ├── gtFine │ ├── test │ │ ├── berlin │ │ ├── ... │ │ └── munich │ ├── train │ │ ├── aachen │ │ ├── ... │ │ └── zurich │ └── val │ ├── frankfurt │ ├── lindau │ └── munster ├── leftImg8bit │ ├── test │ │ ├── berlin │ │ ├── ... │ │ └── munich │ ├── train │ │ ├── aachen │ │ ├── ... │ │ └── munich │ └── val │ ├── frankfurt │ ├── lindau │ └── munster ├── license.txt └── README

CIF AR-10 Dataset

We recommend that you unzip the downloaded dataset into the following structure. Please contact Horizon if you encounter any problems during data preparation.

cifar-10/ ├── batches.meta ├── cifar10_val.txt ├── data_batch_1 ├── data_batch_2 ├── data_batch_3 ├── data_batch_4 ├── data_batch_5 ├── readme.html └── test_batch

Prepare Models

When using the model conversion sample package, please prepare the corresponding floating-point model first.

Note

If you need to do this process in the sample folder, you need to execute the 00_init.sh script in the folder first to get the corresponding original model and dataset.

Sources and modifications (if any) of the original models, please refer to below subsections.

MobileNetv1

  1. model source: https://github.com/shicai/MobileNet-Caffe

  2. md5sum code:

    md5sumFile
    3fd6889ec48bda46451d67274144e2a8mobilenet.caffemodel
    8922f90f629d428fecf866e798ac7c08mobilenet_deploy.prototxt

MobileNetv2

  1. model source: https://github.com/shicai/MobileNet-Caffe

  2. md5sum code:

    md5sumFile
    54aab8425ea068d472e8e4015f22360cmobilenet_v2.caffemodel
    13101ee86ab6d217d5fd6ed46f7a4faamobilenet_v2_deploy.prototxt

ResNet50

  1. model source: https://pytorch.org/vision/main/models/generated/torchvision.models.resnet50.html

  2. md5sum code:

    md5sumFile
    ebaeb70c972d1a3e6eb06c0e1541eeb4resnet50.onnx

GoogleNet

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/GoogleNet

  2. md5sum code:

    md5sumFile
    f107ae6806ea1016afbc718210b7a617googlenet.onnx

EfficientNet_Lite0/1/2/3/4

Attention

To quickly start running samples and avoid the risks caused by third party tools, we strongly recommend using the off-the-shelf ONNX model in the model_zoo/mapper/ directory in Horizon model release package. If you are interested in reproducing the model conversion process of tflite2onnx, you can still try using the below 3rd-party tool, however, we do not guarantee the quality and success of the conversion.

  1. model source: obtain the TAR package from https://github.com/tensorflow/tpu/tree/master/models/official/efficientnet/lite

  2. md5sum of the converted ONNX models in Horizon model_zoo:

    md5sumFile
    001a329bd367fbec22b415c7a33d7bdbefficientnet_lite0_fp32.onnx
    1205e95aea66650c71292bde236d55a9efficientnet_lite1_fp32.onnx
    474741c15494b79a89fe51d89e0c43c7efficientnet_lite2_fp32.onnx
    550455b41848d333f8359279c89a6baeefficientnet_lite3_fp32.onnx
    bde7fe57eadb4a30ef76f68da622dcd5efficientnet_lite4_fp32.onnx
  3. Download and get .tflite from the TAR package, and then convert it to an ONNX model using the tflite2onnx tool (https://pypi.org/project/tflite2onnx/).

Note that model layouts may vary by tflite2onnx version. If the input layout of the converted ONNX model is NHWC, when building, the configure input_layout_train of the EfficientNet_Lite0/1/2/3/4 should be NHWC.

Vargconvnet

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/VargConvNet

  2. md5sum code:

    md5sumFile
    e21b8db17916f9046253bbe0bb8de3efvargconvnet.onnx

Efficientnasnet_m

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/EfficientnasNet

  2. md5sum code:

    md5sumFile
    fc36c052c6f034c0b64a6197b91b0c62efficientnasnet-m.onnx

Efficientnasnet_s

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/EfficientnasNet

  2. md5sum code:

    md5sumFile
    e2744bd748f4265f4488676835a6ca24efficientnasnet-s.onnx

ResNet18

  1. model source: https://pytorch.org/vision/main/models/generated/torchvision.models.resnet18.html

  2. md5sum code:

    md5sumFile
    62de4ff68317c65ab4bb6a451e719e6dresnet18.onnx

YOLOv2_Darknet19

Attention
  • To quickly start running samples and avoid the risks caused by third party tools, we strongly recommend using the off-the-shelf Caffe model in the model_zoo/mapper/ directory in Horizon model release package. If you are interested in reproducing the model conversion process of darknet2caffe, you can still try using the below 3rd-party tool, however, we do not guarantee the quality and success of the conversion.
  • To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.
  1. To get YOLOv2_Darknet19, first download the .cfg and .weight files of YOLOv2 608x608 from YOLO's official website (https://pjreddie.com/darknet/yolo/) and convert it to a Caffe model using the darknet2caffe conversion tool (https://github.com/xingyanan/darknet2caffe).
Note

As this conversion tool is a simplified version, you should modify the .prototxt file generated by this tool before the conversion, change the 'Reshape' layer to 'Passthrough' layer, and then add an NCHW2NHWC Permute operation to the output node.

For details about the parameters of the modified Passthrough layer, refer to the yolov2.prototxt sample.

2.md5sum code:

md5sumFile
7aa7a6764401cebf58e73e72fcbd2a45yolov2.caffemodel
72e9a51c1e284e4b66e69f72ca9214c8yolov2_transposed.prototxt

YOLOv3_Darknet53

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. YOLOv3_Darknet53 model:

URL: https://github.com/ChenYingpeng/caffe-yolov3/ .

The caffemodel file can be downloaded from the Baidu Cloud URL provided in the README.md file in GitHub, in which you should add an NCHW2NHWC Permute operation to the output node.

  1. md5sum code:

    md5sumFile
    935af6e1530af5c0017b3674adce95e9yolov3_transposed.prototxt
    9a0f09c850656913ec27a6da06d9f9ccyolov3.caffemodel

YOLOv5x

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. YOLOv5x model: Download the corresponding pt file from https://github.com/ultralytics/yolov5/releases/tag/v2.0
Important

When cloning the source code, be sure to use [v2.0] tags, otherwise the conversion may fail.

  1. md5sum code:

    md5sumFile
    2e296b5e31bf1e1b6b8ea4bf36153ea5yolov5l.pt
    16150e35f707a2f07e7528b89c032308yolov5m.pt
    42c681cf466c549ff5ecfe86bcc491a0yolov5s.pt
    069a6baa2a741dec8a2d44a9083b6d6eyolov5x.pt
def forward(self, x): # x = x.copy() # for profiling z = [] # inference output self.training |= self.export for i in range(self.nl): x[i] = self.m[i](x[i]) # conv bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85) # x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous() x[i] = x[i].permute(0, 2, 3, 1).contiguous()
Note

Remove the reshape from 4D to 5D at the end of each output branch (i.e., not splitting the channel from 255 to 3x85), then convert the layout from NHWC to NCHW before dumping.

The bottom left image displays the visualization of a certain output node before modifying the model, while the bottom right image displays the visualization of the corresponding output node after the modification.

Attention

When using the export.py script:

  1. Because Horizon algorithm toolchain only supports ONNX opset [10] ~ [19], please modify the opset_version parameter in the torch.onnx.export as per the version you use.
  2. Change the default input name parameter in the torch.onnx.export from 'image' into 'data' to keep it consistent with that of in the YOLOv5s sample in the model conversion sample package.
  3. Change the default data input size in the parser.add_argument from 640x640 to 672x672 as in the YOLOv5x sample in the model conversion sample package.

SSD_MobileNetv1

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. SSD_MobileNetv1 model: Obtain the Caffe model from URL: https://github.com/chuanqi305/MobileNet-SSD

  2. md5sum code:

    md5sumFile
    bbcb3b6a0afe1ec89e1288096b5b8c66mobilenet_iter_73000.caffemodel
    3c230e4415195a50c6248be80c49882dMobileNetSSD_deploy.prototxt

Efficientdetd0

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/EfficientDet

  2. md5sum code:

    md5sumFile
    ec4129c4b300cd04f1e8f71e0fe54ca5efficientdet_nhwc.onnx

CenterNet_Resnet101

Attention

To achieve optimal on-board performance:

  • We put the maxpool and sigmoid nodes into the model and specify them to be compiled as BPU nodes for reducing the amount of computation during post-processing.

  • We modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/Centernet

  2. md5sum code:

    md5sumFile
    db195ff784792f475e573c5126401d2acenternet_resnet101_coco_modify.onnx

Fcos_efficientnetb0

Attention
  • This model is a trained model using the PTQ method.
  • To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.
  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/blob/master/Fcos_Efficientnetb0

  2. md5sum code:

    md5sumFile
    9f9a1fe8508e2bd068e70146eb559b4ffcos_efficientnetb0.onnx

Yolov4

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/YoloV4

  2. md5sum code:

    md5sumFile
    aaa3c3e5e4c4c1d4830b6501b1720e4dyolov4_efficientnetb0.onnx

YOLOv3_VargDarknet

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. YOLOv3_VargDarknet model

URL: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/Yolov3_VargDarknet . The model file can be downloaded from the Baidu Cloud URL provided in the README.md file in GitHub.

  1. md5sum code:

    md5sumFile
    fd4e46bc7c9798b51778d3aa09c5053ayolov3_vargdarknet53.onnx

Fcos_resnet50

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/Fcos_Resnet50

  2. md5sum code:

    md5sumFile
    0218942777615fac2f54cefdac4fbfa7fcos_resnet50.onnx

Fcos_resnext101

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/Fcos_Resnext101

  2. md5sum code:

    md5sumFile
    4b80efd22448021721ac5a860909c59ffcos_resnext101.onnx

Unet_mobilenet

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Dequantize node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/MobilenetUnet

  2. md5sum code:

    md5sumFile
    21c6c645ebca92befbebc8c39d385c1etf_unet_trained.onnx

DeeplabV3plus_efficientnetb0

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Reshape and Cast node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/DeeplabV3Plus

  2. md5sum code:

    md5sumFile
    cf3a683f31b4b0ebe090647729f869d9deeplabv3plus_efficientnetb0.onnx

Fastscnn_efficientnetb0

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Reshape and Cast node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/FastSCNN

  2. md5sum code:

    md5sumFile
    c1ace8f08a9c7b9c91509fa68327d0c8fastscnn_efficientnetb0.onnx

Deeplabv3plus_dilation1248

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Transpose node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/DeeplabV3Plus

  2. md5sum code:

    md5sumFile
    ad002e572cbb49e1e99d893aac69f3e3deeplabv3_cityscapes_dila1248_permute.onnx

Deeplabv3plus_efficientnetm1

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Reshape, Cast, and Transpose node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/DeeplabV3Plus

  2. md5sum code:

    md5sumFile
    0a1dfd01e173c68630d9e8dc1a6036fedeeplabv3plus_efficientnetm1.onnx

Deeplabv3plus_efficientnetm2

Attention

To achieve optimal on-board performance, we modified the remove_node_type parameter in the YAML file used for hbm model compilation and removed the Reshape, Cast, and Transpose node from the hbm model.

  1. model source: https://github.com/HorizonRobotics-Platform/ModelZoo/tree/master/DeeplabV3Plus

  2. md5sum code:

    md5sumFile
    c11a2673c4b3cf6e5d7bf1a051925d38deeplabv3plus_efficientnetm2.onnx

Demo Of Algorithm Model Samples

Taking the GoogleNet model as an example, This section illustrates the steps of floating-point to fixed-point model conversion by using the scripts in 03_classification/04_googlenet/.

Docker Container Prep

First complete the docker installation and configuration and enter the docker container accoridng to Docker Container Deployment .

Obtain The Original Model And Calibration Dataset

Execute 00_init.sh in the 03_classification/04_googlenet/ folder to obtain the model and calibration dataset needed for the current sample.

# 1. Enter the folder the demo script resides cd samples/ai_toolchain/horizon_model_convert_sample/03_classification/04_googlenet/ # 2. Execute the script to obtain the original model and calibration dataset sh 00_init.sh

Check If the Model is Executable

1.Run the script as follows:

# Execute model checking sh 01_check.sh

2.Output the model inspection result:

Use the hb_compile tool to check whether the model can be supported by Horizon ASIC.

Prepare Calibration Dataset

Run the 02_preprocess.sh script in the same directory, as follows:

# Convert the images in 01_common/data/imagenet/calibration_data # to: ./calibration_data_rgb_f32 sh 02_preprocess.sh
Note
  • We extracted 100 images from the ImageNet dataset and use them as a calibration dataset. Before calibration, we pre-processed the data and convert short size resize/crop size/NHWC to NCHW/to rgb.
  • The hb_compile tool will load data from the converted binary data, the format of the pre-processed binary data file is npy, the dtype is unit8.

Building Heterogeneous Models

Run the 03_build.sh script in the same directory, as follows:

sh 03_build.sh
Note

The above script uses the hb_compile tool to convert the model. The most important thing to focus on is the conversion configuration file, refer to Model Quantized Compilation section.

The output of the above script is as follows:

ls model_output | cat googlenet_224x224_nv12.hbm googlenet_224x224_nv12_calibrated_model.onnx googlenet_224x224_nv12_ptq_model.onnx googlenet_224x224_nv12_optimized_float_model.onnx googlenet_224x224_nv12_original_float_model.onnx googlenet_224x224_nv12_quantized_model.bc googlenet_224x224_nv12.html googlenet_224x224_nv12.json googlenet_224x224_nv12_quant_info.json googlenet_224x224_nv12_advice.json googlenet_224x224_nv12_node_info.csv
Note

For now you ONLY need to focus on the googlenet_224x224_nv12.hbm file.

Single Image Inference

Run the 04_inference.sh script to inference a single image, as follows:

sh 04_inference.sh
Note
  • As image pre-processing, model data post-processing are required in the image inference, we offered a Python sample script, please refer to sh 04_inference.sh.
  • This script is to perform the inference against a single image and verify whether the inference result meets the expectations. If you want to perform accuracy evaluation, refer to the script 05_evaluate.sh.

Accuracy Evaluation

Run the 05_evaluate.sh script to evaluate the accuracy, as follows:

export PARALLEL_PROCESS_NUM=${parallel_process_num} sh 05_evaluate.sh
Note
  • As image pre-processing, model data post-processing are required in accuracy evaluation, awe offered a Python sample script, please refer to sh 05_evaluate.sh.
  • To accelerate the evaluation, you can adjust the number of concurrent processes by using the -p option; meanwhile, pay attention to the memory usage. When the value of the -p option is set to 0 or left unfilled, the fixed-point models in the CPU environment will be processed by 10 processes, while other scenarios using 1 process.

FAQ

Why is the reproduced accuracy slightly different from the indicators in the documentation?

There are two possible reasons:

  1. There may be minor differences in calculation methods when in different server environments, which can cause small data fluctuations in the accuracy of the compiled fixed-point ONNX models in different server environments when compared with the documentation.
  2. The user side uses different versions of third-party libraries such as OpenCV and NumPy, which may produce different results after pre-processing, and this may also lead to slight data fluctuations in reproduced accuracy when compared with the documentation.

No need to worry much about this situation, the records provided in the documentation is only for reference, and it is ok that your reproduced accuracy is slightly different from those in documents.

Why is the fixed-point model accuracy not aligned with the on-board accuracy of the hbm file in the ai_benchmark example?

In the standard delivery, when adding the example, we have already aligned the fixed-point model accuracy with the hbm on-board accuracy in the ai_benchmark example.

If you find any unaligned accuracy, we recommend you first checking the model input for consistency.

When executing the fixed-point model evaluation script, you use the dataset of image type, while for the hbm model used on board, you use the binary dataset converted by the hb_eval_preprocess tool.

Based on this, if the dataset you used on-board is not generated by using the above methods, we recommend that you first use our data preprocessing tool (i.e., hb_eval_preprocess) to regenerate the dataset needed for on-board running on the same server that you run the fixed-point model accuracy and rerun the on-board accuracy to ensure the model inputs are consistent.

Attention

Make sure to use the same environment to generate the dataset by using the hb_eval_preprocess tool and to run the fixed-point model accuracy.