Common Algorithm Model Samples
Sample Location
The common algorithmic model samples are located at classification/, detection/ and segmentation/ folders in the onnx_ptq/examples path.
Prepare Datasets
Dataset Download Address
The dataset can be downloaded from the following address.
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.
| Dataset | Description |
| 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. |
| 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. |
| VOC Dataset | Attention |
| Cityscapes Dataset | We recommend that you unzip the downloaded dataset into the following structure. Please contact Horizon if you encounter any problems during data preparation. |
Prepare Models
When using the model conversion sample package, please prepare the corresponding floating-point model first.
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.
Demo Of Algorithm Model Samples
Taking the Resnet50 model as an example, this section illustrates the steps of floating-point to fixed-point model conversion by using the scripts in classification/resnet50/.
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 classification/resnet50/ folder to obtain the model and calibration dataset needed for the current sample.
Check If the Model is Executable
Run the script as follows:
Prepare Calibration Dataset
Run the 02_preprocess.sh script in the same directory, as follows:
- 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_compiletool will load data from the converted binary data, the format of the pre-processed binary data file is npy, the dtype isunit8.
Building Models
Run the 03_build.sh script in the same directory, as follows:
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:
For now you ONLY need to focus on the resnet50_224x224_nv12.hbm file.
Single Image Inference
Run the 04_inference.sh script to inference a single image, as follows:
- 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:
- 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
-poption; meanwhile, pay attention to the memory usage. When the value of the-poption 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:
- 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.
- The 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 sample?
In the standard delivery, when adding the sample, we have already aligned the fixed-point model accuracy with the hbm on-board accuracy in the ai_benchmark sample.
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.
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.
