模型轉換問題

  • onnx 模型在轉換後,hard validation 的運算數值沒有問題,但是在實際硬體跑時,精准度誤差十分大,在追加量化的圖片(約 600張)之後也沒有好轉,這方面有解決的方法嗎?


  • 對 onnx 模型轉換時,嘗試了 224x224 360x360 512x512 三種大小的模型,僅有 224x224 可以運作,其餘都會發生以下錯誤。


[common][error][exceptions.cc:17][HandleError] Hardware not support: value of NPU register exceeds limit

terminate called after throwing an instance of 'HardwareNotSupport'

  what():  Hardware not support: value of NPU register exceeds limit

./compilerIpevaluator_720.sh: line 32:   186 Aborte d                 $LIBS_FOLDER/compiler/compile 720 $model $TMP_FOLDER/config_compiler.json warning compile.log

附件為模型轉換過程的步驟以及相關 onnx 模型

Comments

  • Hello,

    想請問你使用的是KL520還是KL720呢?

    初步看起來模型中含有不支援的operator,這可能會導致後續的錯誤,請參考下圖將紅線以下的部分移除,屆時你會拿到四個output data( 51x28x28, 34x14x14, 34x7x7, 51x3x3),再將這四個output node的資料在PC上做後續reshape, concat等非神經網路的操作後就可以拿到你預期的三個結果 (conf, loc, iou)


    若沒有現成工具可以移除onnx中的operator的話,也可以參考我們文件中所提供的工具editor.py


  • 使用 kl720 執行

    嘗試了切除 transpose 之後的 模型 node 並從新轉換,結果的精准度誤差還是十分大

    在使用 editor.py 進行切除時 因為其中 onnx.utils.polish_model(m) 會出現錯誤 所以執行時會著解掉該行

    以下是切除的指令

    sed '31s/^/# /' libs/ONNX_Convertor/optimizer_scripts/editor.py > libs/ONNX_Convertor/optimizer_scripts/editor_bypass_polish.py
    python libs/ONNX_Convertor/optimizer_scripts/editor_bypass_polish.py \
        $modelPath $cutPostModelPath \
        --cut \
          Transpose_51 \
          Transpose_57 \
          Transpose_63 \
          Transpose_69 \
        --rename-output \
          onnx::Transpose_242 output_1 \
          onnx::Transpose_249 output_2 \
          onnx::Transpose_256 output_3 \
          onnx::Transpose_263 output_4
    
  • edited May 2022

    @Wayne_Zhou@compal.com

    Hi Wayne,

    看過您 onnx 模型轉換的流程,建議您使用 Kneron 最新的 ToolChain docker (kneron/toolchain:v0.17.2) ,還有使用 python API 來轉換模型 (http://doc.kneron.com/docs/#toolchain/manual/)。

    如 Ethon 所描述,因為您的模型中含有不支援的operator,這可能會導致後續轉換時出現錯誤,所以您必須移除不支援的 operators。

    您在使用 editor.py 進行切除,通常不建議您將該行onnx.utils.polish_model(m) 著解掉,因為那個 function 是有用來確認 onnx model 是否符合 onnx 規範的功用,如果出現錯誤訊息,您的 onnx model 可能是不符合 onnx 規範。


    • onnx 模型在轉換後,hard validation 的運算數值沒有問題,但是在實際硬體跑時,精准度誤差十分大,在追加量化的圖片(約 600張)之後也沒有好轉,這方面有解決的方法嗎?

    這邊方便詢問您一下,精確掉很多是掉多少呢?然後您的 hard validate 指的是什麼呢?

    您是否可以提供像這樣的範例讓我們重現精確度掉非常大的問題(http://doc.kneron.com/docs/#toolchain/yolo_example/) (Step 4: Check ONNX model and preprocess and postprocess are good, Step 6: Check if BIE model accuracy is good enough, Step 8. Check NEF model)


    • 對 onnx 模型轉換時,嘗試了 224x224 360x360 512x512 三種大小的模型,僅有 224x224 可以運作,其餘都會發生以下錯誤。

    您成功切除後,在最新的 ToolChain docker 就可以順利進行 onnx 模型轉換囉。

  • 您在使用 editor.py 進行切除,通常不建議您將該行onnx.utils.polish_model(m) 著解掉,因為那個 function 是有用來確認 onnx model 是否符合 onnx 規範的功用,如果出現錯誤訊息,您的 onnx model 可能是不符合 onnx 規範。

    關於這點,執行 onnx.utils.polish_model(m) 而發生錯誤的節點,即是我要切除地方,所以導致死結(我不切除不符合規範的節點,就不能轉換,但是該節點使我無法切除)。

    我僅註解第31行的程式碼,editor.py 當中輸出時也有執行 onnx.utils.polish_model(m) (113 行) ,我想最後輸出應該是沒問題的?


    這邊方便詢問您一下,精確掉很多是掉多少呢?然後您的 hard validate 指的是什麼呢?

    hard validate 指的是 /workspace/scripts/hardware_validate_520.py /workspace/scripts/hardware_validate_720.py

    您是否可以提供像這樣的範例讓我們重現精確度掉非常大的問

    附件有使用實際硬體與原始模型運行的比較

    當我試圖用 ktc.kneron_inference 運行時,會發生一些狀況

    使用 運行 bie 模型時

    ---------------------------------------------------------------------------
    ValueError                                Traceback (most recent call last)
    <timed exec> in <module>
    
    /workspace/E2E_Simulator/python_flow/kneron_inference.py in kneron_inference(pre_results, nef_file, onnx_file, bie_file, model_id, input_names, radix, data_type, reordering, ioinfo_file, dump, platform)
         59 
         60             input_files = dynasty.prep_dynasty(
    ---> 61                 pre_results, input_folder, output_folder, input_names, platform, True, str(bie))
         62             output = dynasty.dynasty_inference(
         63                 str(bie), "bie", str(platform), data_type, input_files, input_names,
    
    /workspace/E2E_Simulator/python_flow/dynasty/dynasty.py in prep_dynasty(pre_results, input_folder, output_folder, input_names, platform, is_bie, model_file)
         89             np_radix = np.array([*radix[1:], radix[0]])  # set to channel last to match input
         90             np_radix = np.broadcast_to(np_radix, (1, height, width, channel))
    ---> 91             np_radix = np.power(2, np_radix)
         92 
         93             new_result = result * np_radix
    
    ValueError: Integers to negative integer powers are not allowed.
    

    使用 運行 nef 模型時

    ---------------------------------------------------------------------------
    TypeError                                 Traceback (most recent call last)
    <timed exec> in <module>
    
    /workspace/E2E_Simulator/python_flow/kneron_inference.py in kneron_inference(pre_results, nef_file, onnx_file, bie_file, model_id, input_names, radix, data_type, reordering, ioinfo_file, dump, platform)
         39             platform, model_name, input_folder, output_folder = nef.setup_nef(nef_path, model_id)
         40             input_files = nef.prep_csim_inputs(
    ---> 41                 pre_results, input_folder, str(platform), nef_path.parent, model_name)
         42             output = nef.nef_inference(
         43                 model_name, str(platform), data_type, nef_path.parent, input_files, input_folder,
    
    /workspace/E2E_Simulator/python_flow/nef/nef.py in prep_csim_inputs(pre_results, input_folder, platform, nef_folder, model_name)
        156 
        157         radix = utils.get_radix(setup, index, str(platform))
    --> 158         utils.convert_pre_numpy_to_rgba(result, input_file, radix, platform, setup, index)
        159 
        160     return input_files
    
    /workspace/E2E_Simulator/python_flow/utils/utils.py in convert_pre_numpy_to_rgba(data, output, radix, platform, setup_file, input_num)
        263     # conversion for CSIM/Dynasty bit-match purposes and rounding issues
        264     if new_data.dtype == np.float64 or new_data.dtype == np.float32:
    --> 265         new_data *= (1 << radix)
        266     new_data = np.round(new_data)
        267     new_data = np.clip(new_data, -128, 127).astype(np.int8)
    
    TypeError: ufunc 'multiply' output (typecode 'O') could not be coerced to provided output parameter (typecode 'f') according to the casting rule ''same_kind''
    
    
    
    

    您成功切除後,在最新的 ToolChain docker 就可以順利進行 onnx 模型轉換囉。

    這部分已可正常轉換

  • 確認一下,所以onnx轉換問題在切除不支援的op後就可以轉換了嗎?

    另外,實際硬體與原始模型運行的比較,這邊指的是onnx模型的推論結果與實際上在520 / 720上執行嗎? 所以已經有成功在520 / 720上執行了嗎? 請問要使用的是520還是720呢?

    執行ktc.kneron_inference 運行時會發生一些狀況,關於這點,能否請你提供你執行的方式,也就是該api所需要的各項參數是怎麼設定的,也可以先參考網站內的說明檢查各項參數是否正確

    http://doc.kneron.com/docs/#toolchain/python_app/app_flow_manual/#python-api-inference

    kneron_inference(pre_results, nef_file="", onnx_file="", bie_file="", model_id=None, input_names=[], radix=8, data_type="float", reordering=[], ioinfo_file="", dump=False, platform=520)

  • 確認一下,所以onnx轉換問題在切除不支援的op後就可以轉換了嗎?

    另外,實際硬體與原始模型運行的比較,這邊指的是onnx模型的推論結果與實際上在520 / 720上執行嗎? 所以已經有成功在520 / 720上執行了嗎? 請問要使用的是520還是720呢?

    對 在實際硬體 目前是兩邊都有嘗試轉換

    執行ktc.kneron_inference 運行時會發生一些狀況,關於這點,能否請你提供你執行的方式,也就是該api所需要的各項參數是怎麼設定的,也可以先參考網站內的說明檢查各項參數是否正確

    我有參考過,主要問題是出在 radix 上面,原始模型的輸入範圍是 (0~255)

    根據 http://doc.kneron.com/docs/#toolchain/command_line/

    A. How to configure the input_params.json?
    ...
    // The radix information for the npu image process.
    // The formula for radix is 7 – ceil(log2 (abs_max)).
    // For example, if the image processing method we utilize is "kneron",
    // the related image processing formula is "kneron": RGB/256 - 0.5,
    // and the processed value range will be (-0.5, 0.5).
    // abs_max = max(abs(-0.5), abs(0.5)) = 0.5
    // radix = 7 – ceil(log2(abs_max)) = 7 - (-1) = 8
    "radix": 8,
    

    計算為 7 - ceil(log2(255)) = 7 - 8 = -1

    但是 kneron_inference() 好像無法處理

  • @Wayne_Zhou@compal.com

    Hi Wayne_Zhou,

    這裡有幫您確認過,kneron_inference() 是可以處理 radix = -1 的情況,可能還得請您檢查一下其他可能的原因。

    執行ktc.kneron_inference 運行時會發生一些狀況,關於這點,能否請你提供你執行的方式,也就是該api所需要的各項參數是怎麼設定的,也可以先參考網站內的說明檢查各項參數是否正確

    http://doc.kneron.com/docs/#toolchain/python_app/app_flow_manual/#python-api-inference

    kneron_inference(pre_results, nef_file="", onnx_file="", bie_file="", model_id=None, input_names=[], radix=8, data_type="float", reordering=[], ioinfo_file="", dump=False, platform=520)

  • 附件整理了kneron_inference執行過程的 notebook 沒有發現進一步的原因


  • 附件整理了kneron_inference執行過程的 notebook 沒有發現進一步的原因

    該附件上傳後有發現些小錯誤,這裡重傳一份


  • @Wayne_Zhou@compal.com

    Hi Wayne_Zhou,

    看過您整理的 kneron-inference,得知您是想要使用 KL520,這裡列出幾點給您參考:

    1. 您的 kneron inference on Onnx

    onnxOutputData = ktc.kneron_inference([inputData], onnx_file=modelOnnxPath, input_names=["input"], platform=platform, radix=-1)

    這裡的 API 是對 onnx inference,radix 可以不用加。

      

    2. 您的 kneron inference on Bie file

    bieOutputData = ktc.kneron_inference([inputData], bie_file=modelBiePath, input_names=["input"], platform=platform, radix=-1)

    這裡會造成錯誤,較有可能是有經過 perprocess 後 inputData 的問題。


    3. 您的 kneron inference on Nef file

    nefOutputData = ktc.kneron_inference([inputData], nef_file=modelNefPath, input_names=["input"], platform=platform, radix=-1)

    如同第二點。


    4. Visualize result for kneron inference on onnx

    onnxVisualImageData = postProcess(onnxOutputData, inputImageData)

    imshow(width=500, img=onnxVisualImageData)

    這邊是 onnx 的 inference,所以這個與量化無關,與量化有關的是第二點與第三點。


    關於 2、3、4 點,您是否可以提供可以執行的 Scptris 還有您的環境(ex. (1)cv2_imgproc、(2)yunet_utils、(3)image_10.jpg 這些等等),方便復現這些問題並 troubleshooting。

  • 這裡的 API 是對 onnx inference,radix 可以不用加。

    OK

    關於 2、3、4 點,您是否可以提供可以執行的 Scptris 還有您的環境(ex. (1)cv2_imgproc、(2)yunet_utils、(3)image_10.jpg 這些等等),方便復現這些問題並 troubleshooting。

    附於附件


  • @Wayne_Zhou@compal.com

    Hi Wayne_Zhou,

    我這邊執行您提供的 Script 是可以跑 2、3點的,不過我使用的 model 是根據您提供出來的線索這邊自己轉的,您是否也能提供一下您那邊 360x360 的 onnx (sim-cutPost-opt.onnx)、Bie (sim-cutPost-opt.quan.wqbi.bie) 跟 Nef (sim-cutPost-opt-kl{platform}.nef)。

  • 您是否也能提供一下您那邊 360x360 的 onnx (sim-cutPost-opt.onnx)、Bie (sim-cutPost-opt.quan.wqbi.bie) 跟 Nef (sim-cutPost-opt-kl{platform}.nef)。

    附於附件

  • @Andy Hsieh

    請問還有缺少任何我這邊可以提供的資料?

  • @Wayne_Zhou@compal.com

    Hi Wayne_Zhou,

    關於使用您提供的資料,先前提到的 2、3 點,我這裡有復現出您的問題了,並且問題已經有把您的問題交給相關同仁也得到回覆了。

    主要是 radix 出現的問題沒有錯,下一版的 Kneorn ToolChain 會改善此問題。

    這邊建議您解決的辦法有三種:

    1. 您在這版 Knenn ToolChain (kneron/toolchain:v0.17.2) ,必須要想辦法修改 preprocess 的方法, 讓 radix 不是 -1。
    2. 對 Knenn ToolChain 降版。
    3. 或是等待 Knenn ToolChain 下一版的 relase。

    至於先前提到的第四點 onnx inference 的問題 (Visualize result for Kneron and onnx Runtime inference on onnx 推論結果不一致),可能發生的問題是因為 Kneron inference 輸出的順序與 onnx runtime 不一樣,您可以參考 http://doc.kneron.com/docs/#toolchain/python_app/app_flow_manual/

The discussion has been closed due to inactivity. To continue with the topic, please feel free to post a new discussion.