中文字幕 另类精品,亚洲欧美一区二区蜜桃,日本在线精品视频免费,孩交精品乱子片免费

<sup id="3hn2b"></sup>

    1. <sub id="3hn2b"><ol id="3hn2b"></ol></sub><legend id="3hn2b"></legend>

      1. <xmp id="3hn2b"></xmp>

      2. "); //-->

        博客專欄

        EEPW首頁 > 博客 > 征程 6 yolov5s-rgb-nhwc量化指南

        征程 6 yolov5s-rgb-nhwc量化指南

        發(fā)布人:地平線開發(fā)者 時間:2025-05-13 來源:工程師 發(fā)布文章

        在 征程 6 平臺,我們可以按照這個方式編譯 input_typr_rt 為 rgb,且 layout 為 NHWC 的模型。這樣做的好處是,當(dāng)用戶的數(shù)據(jù)輸入源本身就是 NHWC 的 rgb 圖像時,這么做可以避免額外的數(shù)據(jù)處理操作。這里以 yolov5s 為例進(jìn)行介紹。


        一、模型信息

        輸入節(jié)點(diǎn)


        img

        輸出節(jié)點(diǎn)(其中一個)

        img


        輸入輸出信息總覽


        img


        二、校準(zhǔn)數(shù)據(jù)

        seed100 文件夾存放原始圖像,可借助 horizon_model_convert_sample 的腳本完成校準(zhǔn)數(shù)據(jù)的處理。

        02_preprocess.sh 腳本內(nèi)容如下:



        set -e -v
        cd
        $(dirname $
        0) || exit
        python3 ../../data_preprocess.py \
         --src_dir ./seed100 \
         --dst_dir ./calibration_data_rgb_f32 \
         --pic_ext .rgb \
         --read_mode opencv \
         --saved_data_type float32


        preprocess.py 腳本相關(guān)內(nèi)容修改如下:


        from horizon_tc_ui.data.transformer import (PadResizeTransformer,
                                                   BGR2RGBTransformer,
                                                   NormalizeTransformer)
        def calibration_transformers():
           transformers = [
               PadResizeTransformer(target_size=(384, 2048)),
               BGR2RGBTransformer(data_format="HWC"),
               NormalizeTransformer(255.0)
           ]
           return transformers

        這段代碼的主要作用是創(chuàng)建一個由多個數(shù)據(jù)預(yù)處理步驟組成的轉(zhuǎn)換器(transformers)列表。這些轉(zhuǎn)換器會對輸入數(shù)據(jù)進(jìn)行處理,以適應(yīng)深度學(xué)習(xí)模型的要求。


        PadResizeTransformer:這個轉(zhuǎn)換器的作用是對輸入數(shù)據(jù)進(jìn)行填充(padding)和調(diào)整大小(resize),通常是為了確保輸入的圖像尺寸與模型要求的輸入尺寸一致。

        BGR2RGBTransformer:這個轉(zhuǎn)換器將輸入圖像從 BGR 格式轉(zhuǎn)換為 RGB 格式。BGR 是 OpenCV 默認(rèn)的顏色格式,而許多深度學(xué)習(xí)框架(如 TensorFlow 或 PyTorch)更習(xí)慣使用 RGB 格式。因此,這個轉(zhuǎn)換器是為了進(jìn)行格式的轉(zhuǎn)換。

        NormalizeTransformer:這個轉(zhuǎn)換器用于對圖像進(jìn)行歸一化處理,將像素值縮放到指定的范圍。歸一化是深度學(xué)習(xí)中常見的預(yù)處理步驟,有助于提高模型的收斂速度和性能。


        這段代碼的作用是定義并返回一個包含多個數(shù)據(jù)預(yù)處理步驟的轉(zhuǎn)換器列表。這些預(yù)處理步驟包括:

          填充和調(diào)整大小:將圖像調(diào)整為目標(biāo)尺寸 (384, 2048)。

          顏色格式轉(zhuǎn)換:將圖像從 BGR 格式轉(zhuǎn)換為 RGB 格式。

          歸一化:將圖像像素值縮放到 0 到 1 的范圍內(nèi)。

        這些步驟通常用于圖像數(shù)據(jù)預(yù)處理,以便將原始圖像調(diào)整為模型輸入所需要的格式和數(shù)值范圍。

        三、YAML

        calibration_parameters:
         cal_data_dir: "./calibration_data_rgb_f32"
         quant_config: {
           "model_config": {
             "all_node_type": "int8",
             "activation": {
               "calibration_type": "max",
               "max_percentile": 0.99999,
             },
           },
         }  
        compiler_parameters:
         compile_mode: latency
         debug: true
         jobs: 32
         optimize_level: O2
        input_parameters:
         input_name: input
         input_shape: 1x384x2048x3
         input_layout_rt: NHWC
         input_layout_train: NHWC
         input_type_rt: rgb
         input_type_train: rgb
         std_value: 255.0
        model_parameters:
         march: nash-m
         onnx_model: ./yolov5s.onnx
         output_model_file_prefix: yolov5s
         working_dir: ./model_output

        四、模型編譯

        hb_compile -c ./config.yaml
        +-------------+-------------------+------------------+
        | TensorName  | Calibrated Cosine | Quantized Cosine |
        +-------------+-------------------+------------------+
        | featuremap1 | 0.999015          | 0.998999         |
        | featuremap2 | 0.999408          | 0.999395         |
        | featuremap3 | 0.999311          | 0.999321         |
        +-------------+-------------------+------------------+

        五、python 推理

        import cv2
        import numpy as np
        from horizon_tc_ui.hb_runtime import HBRuntime
        def prepare_onnx_input():
           data = cv2.imread('./seed.jpg').astype(np.float32)
           data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
           data = data / 255.0
           data = data[np.newaxis,:,:,:]
           return data
        def prepare_bc_input():
           data = cv2.imread('./seed.jpg').astype(np.uint8)
           data = cv2.cvtColor(data, cv2.COLOR_BGR2RGB)
           data = (data - 128).astype(np.int8)
           data = data[np.newaxis,:,:,:]
           return data
        def infer_onnx():
           data = prepare_onnx_input()
           sess = HBRuntime("yolov5s.onnx")
           input_names = sess.input_names
           output_names = sess.output_names
           input_feed = {input_names[0]: data}
           output = sess.run(output_names, input_feed)
           print("==========infer_onnx==========")
           print(output[0][0][0][0])
           return 0
        def infer_ori_onnx():
           data = prepare_onnx_input()
           sess = HBRuntime("./model_output/yolov5s_original_float_model.onnx")
           input_names = sess.input_names
           output_names = sess.output_names
           input_feed = {input_names[0]: data}
           output = sess.run(output_names, input_feed)
           print("==========infer_ori_onnx==========")
           print(output[0][0][0][0])
        def infer_opt_onnx():
           data = prepare_onnx_input()
           sess = HBRuntime("./model_output/yolov5s_optimized_float_model.onnx")
           input_names = sess.input_names
           output_names = sess.output_names
           input_feed = {input_names[0]: data}
           output = sess.run(output_names, input_feed)
           print("==========infer_opt_onnx==========")
           print(output[0][0][0][0])
        def infer_calib_onnx():
           data = prepare_onnx_input()
           sess = HBRuntime("./model_output/yolov5s_calibrated_model.onnx")
           input_names = sess.input_names
           output_names = sess.output_names
           input_feed = {input_names[0]: data}
           output = sess.run(output_names, input_feed)
           print("==========infer_calib_onnx==========")
           print(output[0][0][0][0])
        def infer_ptq_onnx():
           data = prepare_onnx_input()
           sess = HBRuntime("./model_output/yolov5s_ptq_model.onnx")
           input_names = sess.input_names
           output_names = sess.output_names
           input_feed = {input_names[0]: data}
           output = sess.run(output_names, input_feed)
           print("==========infer_ptq_onnx==========")
           print(output[0][0][0][0])
        def infer_quantized_bc():
           data = prepare_bc_input()
           sess = HBRuntime("./model_output/yolov5s_quantized_model.bc")
           input_names = sess.input_names
           output_names = sess.output_names
           input_feed = {input_names[0]: data}
           output = sess.run(output_names, input_feed)
           print("==========infer_quantized_bc==========")
           print(output[0][0][0][0])
        if
        name
        == "
        __main__
        ":
           infer_onnx()
           infer_ori_onnx()
           infer_opt_onnx()
           infer_calib_onnx()
           infer_ptq_onnx()
           infer_quantized_bc()

        這段代碼的主要作用是使用不同的模型和輸入數(shù)據(jù)進(jìn)行推理(inference),并輸出推理結(jié)果。它通過調(diào)用 HBRuntime 來加載不同的模型進(jìn)行推理。下面是對代碼的詳細(xì)分析:

        1.導(dǎo)入必要的庫:

        cv2:用于圖像處理,加載和轉(zhuǎn)換圖像。

        numpy:用于數(shù)值計(jì)算,尤其是矩陣操作。

        HBRuntime:從 horizon_tc_ui.hb_runtime 導(dǎo)入,顯然是一個用于執(zhí)行推理任務(wù)的接口,可能是用于調(diào)用經(jīng)過優(yōu)化的計(jì)算圖或深度學(xué)習(xí)模型。

        2.準(zhǔn)備輸入數(shù)據(jù):

        prepare_onnx_input():該函數(shù)讀取圖像文件 seed.jpg,將其轉(zhuǎn)換為 RGB 格式,進(jìn)行歸一化處理,并為模型的輸入格式添加額外的維度(np.newaxis)。數(shù)據(jù)被歸一化到 [0, 1] 范圍,以便于輸入 ONNX 模型。

        prepare_bc_input():此函數(shù)和 prepare_onnx_input() 類似,不過它讀取的圖像經(jīng)過不同的處理。圖像被轉(zhuǎn)換為 np.uint8 類型,減去 128 后轉(zhuǎn)換為 np.int8 類型,這通常是用于處理量化后的模型數(shù)據(jù)格式。

        3.進(jìn)行推理的不同函數(shù):

        每個 infer_*_onnx 或 infer_quantized_bc 函數(shù)負(fù)責(zé)加載不同的模型并執(zhí)行推理:

        infer_onnx():加載 yolov5s.onnx 模型并進(jìn)行推理。

        infer_ori_onnx():加載原始浮點(diǎn)數(shù)版本的 yolov5s 模型 (yolov5s_original_float_model.onnx)。

        infer_opt_onnx():加載優(yōu)化過的浮點(diǎn)數(shù)版本的 yolov5s 模型 (yolov5s_optimized_float_model.onnx)。

        infer_calib_onnx():加載經(jīng)過校準(zhǔn)的浮點(diǎn)數(shù)模型 (yolov5s_calibrated_model.onnx)。

        infer_ptq_onnx():加載經(jīng)過量化后的浮點(diǎn)數(shù)模型 (yolov5s_ptq_model.onnx)。

        infer_quantized_bc():加載量化后的 BC 格式模型 (yolov5s_quantized_model.bc)。

        4.推理過程:

        每個 infer_* 函數(shù)都會執(zhí)行以下步驟:

        調(diào)用相應(yīng)的準(zhǔn)備輸入函數(shù)(如 prepare_onnx_input() 或 prepare_bc_input()),將圖像轉(zhuǎn)換為模型所需的輸入格式。

        使用 HBRuntime 加載不同的模型。

        獲取模型的輸入和輸出節(jié)點(diǎn)名稱。

        將輸入數(shù)據(jù)傳入模型進(jìn)行推理。

        輸出推理結(jié)果的一部分(通過 output[0][0][0][0] 打印輸出,可能是某個檢測框的值)。

        5.主函數(shù) if name == "__main__"::

        在主程序執(zhí)行時,依次調(diào)用上述所有的推理函數(shù)進(jìn)行推理,打印每個模型的推理結(jié)果。

        六、總結(jié):

        這段代碼主要用于進(jìn)行不同版本的 YOLOv5 模型的推理,涉及到原始浮點(diǎn)數(shù)模型、優(yōu)化后的浮點(diǎn)數(shù)模型、量化模型、校準(zhǔn)模型等,并對每種模型進(jìn)行推理結(jié)果輸出。通過不同的輸入準(zhǔn)備函數(shù),代碼還演示了如何處理不同的數(shù)據(jù)格式(如浮點(diǎn)數(shù)和量化后的數(shù)據(jù))。


        輸出信息如下,可以看到,數(shù)值大體相當(dāng),可以認(rèn)為推理結(jié)果正確。


        ==========infer_onnx==========
        [ -0.36140022   0.14068425  -0.12884808   0.38683856 -10.027128
         -3.727932    -2.6472187   -3.4907613   -0.8754431   -1.8876309
         -1.6180705   -2.6833398   -2.4786358   -3.0784476   -3.320454
         -4.3665814   -3.2660558   -3.767973    -4.428035    -3.4952142
         -2.8823838   -4.920804    -0.36190268   0.24379599  -0.52514255
         -0.40856832 -11.233256    -3.9329526   -2.7249336   -3.4358976
         -0.8108312   -1.852678    -1.4934196   -2.7427323   -2.4955802
         -3.2720697   -3.3685834   -4.5204425   -3.2479987   -3.8060267
         -4.4632807   -3.5123816   -2.8149266   -5.1647396   -0.35967097
          0.22670399  -0.854579    -0.18010648 -13.903754    -4.169511
         -2.4058053   -3.4251635   -0.8236269   -1.8188286   -1.6522415
         -2.8259125   -2.4029486   -3.3103113   -3.409004    -4.688325
         -3.2148345   -3.948554    -4.4465227   -3.5018692   -2.8768973  -5.213859  ]
        ==========infer_ori_onnx==========
        [ -0.36140022   0.14068425  -0.12884808   0.38683856 -10.027128
         -3.727932    -2.6472187   -3.4907613   -0.8754431   -1.8876309
         -1.6180705   -2.6833398   -2.4786358   -3.0784476   -3.320454
         -4.3665814   -3.2660558   -3.767973    -4.428035    -3.4952142
         -2.8823838   -4.920804    -0.36190268   0.24379599  -0.52514255
         -0.40856832 -11.233256    -3.9329526   -2.7249336   -3.4358976
         -0.8108312   -1.852678    -1.4934196   -2.7427323   -2.4955802
         -3.2720697   -3.3685834   -4.5204425   -3.2479987   -3.8060267
         -4.4632807   -3.5123816   -2.8149266   -5.1647396   -0.35967097
          0.22670399  -0.854579    -0.18010648 -13.903754    -4.169511
         -2.4058053   -3.4251635   -0.8236269   -1.8188286   -1.6522415
         -2.8259125   -2.4029486   -3.3103113   -3.409004    -4.688325
         -3.2148345   -3.948554    -4.4465227   -3.5018692   -2.8768973  -5.213859  ]
        ==========infer_opt_onnx==========
        [ -0.36140022   0.14068425  -0.12884808   0.38683856 -10.027128
         -3.727932    -2.6472187   -3.4907613   -0.8754431   -1.8876309
         -1.6180705   -2.6833398   -2.4786358   -3.0784476   -3.320454
         -4.3665814   -3.2660558   -3.767973    -4.428035    -3.4952142
         -2.8823838   -4.920804    -0.36190268   0.24379599  -0.52514255
         -0.40856832 -11.233256    -3.9329526   -2.7249336   -3.4358976
         -0.8108312   -1.852678    -1.4934196   -2.7427323   -2.4955802
         -3.2720697   -3.3685834   -4.5204425   -3.2479987   -3.8060267
         -4.4632807   -3.5123816   -2.8149266   -5.1647396   -0.35967097
          0.22670399  -0.854579    -0.18010648 -13.903754    -4.169511
         -2.4058053   -3.4251635   -0.8236269   -1.8188286   -1.6522415
         -2.8259125   -2.4029486   -3.3103113   -3.409004    -4.688325
         -3.2148345   -3.948554    -4.4465227   -3.5018692   -2.8768973  -5.213859  ]
        ==========infer_calib_onnx==========
        [ -0.43238506   0.11002721  -0.07907629   0.40861583  -9.97794
         -3.6856174   -2.735222    -3.633584    -0.96581066  -1.6290709
         -1.5608525   -2.6134243   -2.3609822   -3.004236    -3.2058396
         -4.3830314   -3.0389607   -3.8800378   -4.4044924   -3.417421
         -2.7247229   -4.8871512   -0.41328928   0.18615645  -0.5093392
         -0.40168345 -11.165181    -3.8985913   -2.8401194   -3.6001463
         -0.93232083  -1.6206057   -1.4628205   -2.6981122   -2.3167949
         -3.2422874   -3.2519443   -4.5322804   -3.002402    -3.9381328
         -4.4415045   -3.4783902   -2.6498706   -5.107289    -0.43616575
          0.1885212   -0.8195747   -0.17303436 -13.778434    -4.1264076
         -2.5370815   -3.6029017   -0.91676974  -1.5773652   -1.6292106
         -2.8174675   -2.254434    -3.2408853   -3.2774894   -4.6843886
         -2.9586797   -4.0764017   -4.4135203   -3.4192595   -2.7116857  -5.1625004 ]
        ==========infer_ptq_onnx==========
        [ -0.43238506   0.11002721  -0.07907629   0.40861583  -9.97794
         -3.6856174   -2.735222    -3.633584    -0.96581066  -1.6290709
         -1.5608525   -2.6134243   -2.3609822   -3.004236    -3.2058396
         -4.3830314   -3.0389607   -3.8800378   -4.4044924   -3.417421
         -2.7247229   -4.8871512   -0.41328928   0.18615645  -0.5093392
         -0.40168345 -11.165181    -3.8985913   -2.8401194   -3.6001463
         -0.93232083  -1.6206057   -1.4628205   -2.6981122   -2.3167949
         -3.2422874   -3.2519443   -4.5322804   -3.002402    -3.9381328
         -4.4415045   -3.4783902   -2.6498706   -5.107289    -0.43616575
          0.1885212   -0.8195747   -0.17303436 -13.778434    -4.1264076
         -2.5370815   -3.6029017   -0.91676974  -1.5773652   -1.6292106
         -2.8174675   -2.254434    -3.2408853   -3.2774894   -4.6843886
         -2.9586797   -4.0764017   -4.4135203   -3.4192595   -2.7116857  -5.1625004 ]
        ==========infer_quantized_bc==========
        [ -0.37202302   0.18693314  -0.06567554   0.40963683  -9.98054
         -3.6307201   -2.703453    -3.4887328   -0.9094247   -1.7791512
         -1.54388     -2.5430217   -2.399686    -2.996389    -3.1917665
         -4.3041177   -2.9921875   -3.7818878   -4.3069286   -3.3589668
         -2.8220906   -4.8398676   -0.34859642   0.2652311   -0.50713307
         -0.40864512 -10.916675    -3.8250697   -2.8117514   -3.4346225
         -0.8636422   -1.7796087   -1.4381862   -2.6121027   -2.3534453
         -3.2200432   -3.2179508   -4.427741    -2.9437149   -3.8163087
         -4.323068    -3.4090939   -2.7401025   -5.0440025   -0.37643808
          0.2674079   -0.8191366   -0.1740098  -13.797277    -4.0533657
         -2.4911644   -3.4408677   -0.8496779   -1.7336257   -1.605081
         -2.7330828   -2.2852495   -3.2210054   -3.2354999   -4.5808883
         -2.8986108   -3.9594183   -4.282491    -3.3463652   -2.8071992  -5.093343  ]

        至此,模型的量化和 python 推理驗(yàn)證就結(jié)束了。我們可以愉快地開始 C++推理了


        *博客內(nèi)容為網(wǎng)友個人發(fā)布,僅代表博主個人觀點(diǎn),如有侵權(quán)請聯(lián)系工作人員刪除。




        相關(guān)推薦

        技術(shù)專區(qū)

        關(guān)閉