如果要用其他模型轉換的onnx檔,該如何設定input_params.json?

你好,我今天從TF_hub中下載一個Tensorflow的saved_model.pb模型。將其透過tf2onnx以及ONNX_Convertor轉成onnx檔後,接著效仿範例教學,將input_params.json、batch_input_params.json、含有100張圖片的資料夾以及model_convert.onnx(轉好的onnx檔) 放入一個資料夾,並將其掛載到docker kneron toolchain。

接著跑下面的指令,將模型轉換成NPU所需架構作最佳化

python /workspace/scripts/convert_model.py onnx model_convert.onnx model_convert_konnx_opt.onnx

就會得到一個檔案,名稱為model_convert_knoox_opt.onnx

然後執行模型量化

python /workspace/scripts/fpAnalyserCompilerIpevaluator_520.py

錯誤就是在這裡發生

(base) root@024db55b00a4:/data1# python /workspace/scripts/fpAnalyserCompilerIpevaluator_520.py

/workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service

 from . import _distributor_init

Traceback (most recent call last):

 File "/workspace/scripts/fpAnalyserCompilerIpevaluator_520.py", line 40, in <module>

  model_config = ModelConfig(args.config)

 File "/workspace/scripts/utils/load_config.py", line 52, in __init__

  input_name))

utils.load_config.LoadConfigException: Error while loading /data1/input_params.json: model_info.model_inputs is required but not found

Cannot find the corresponding input image folder of model input `inputs`


data1的內容如下


底下是input_params.json的內容

{
    "model_info": {
        "input_onnx_file": "/data1/model_convert_konnx_opt.onnx",
        "model_inputs": [
            {
                "model_input_name": "images",
                "input_image_folder": "/data1/test_img_100"
            }
        ]
    },

    "preprocess": {
        "img_preprocess_method": "kneron",
        "img_channel": "RGB",
        "radix": 8,
        "keep_aspect_ratio": true,
        "pad_mode": 1,
        "p_crop": {
            "crop_x": 0,
            "crop_y": 0,
            "crop_w": 0,
            "crop_h": 0
        }
    },

    "simulator_img_files": [
        {
            "model_input_name": "images",
            "input_image": "/data1/000000000009.jpg"
        }
    ]
}


請問是inpur_params.json需要修改嗎?還是有其他問題?謝謝

Comments

  • 為了發揮耐能加速棒的最大效能,每個模型在轉換前都必須調整至對應的架構,若模型中有加速棒不支援的架構則也可能導致轉換編譯過程中會有錯誤,建議可以先從操作步驟中的classification或detection中選擇模型來體驗。

    若要進一步分析錯誤的原因,可能要請你提供 model_convert_knoox_opt.onnx 來確認模型中是否有不合規範的地方,包含版本、使用的op等都有可能需要調整

  • 你好,因為這裡上傳大小有限制,我傳到雲端,連結如下

    我使用的模型是從 https://tfhub.dev/google/imagenet/inception_v3/classification/5 下載的(TF2 Saved Model (v5))

  • 你好,你所提供的google drive連結似乎沒有開放權限,可能要請你開放權限我們才有辦法下載確認

  • 你好,已經開權限了,不好意思。

  • inpur_params.json 應修改"model_input_name": "inputs",


  • 你好,我已將 input_params.json 修改成 "model_input_name": "inputs",如下圖。

    {
        "model_info": {
            "input_onnx_file": "/data1/model_convert_konnx_opt.onnx",
            "model_inputs": [
                {
                    "model_input_name": "inputs",
                    "input_image_folder": "/data1/test_img_100"
                }
            ]
        },
        "preprocess": {
            "img_preprocess_method": "kneron",
            "img_channel": "RGB",
            "radix": 8,
            "keep_aspect_ratio": true,
            "pad_mode": 1,
            "p_crop": {
                "crop_x": 0,
                "crop_y": 0,
                "crop_w": 0,
                "crop_h": 0
            }
        },
        "simulator_img_files": [
            {
                "model_input_name": "inputs",
                "input_image": "/data1/000000000009.jpg"
            }
        ]
    }
    


    並執行下列指令

    (base) root@44919e63eef8:/data1# python /workspace/scripts/fpAnalyserCompilerIpevaluator_520.py
    


    會產生另一個錯誤如下

    /workspace/miniconda/lib/python3.7/site-packages/numpy/__init__.py:156: UserWarning: mkl-service package failed to import, therefore Intel(R) MKL initialization ensuring its correct out-of-the box operation under condition when Gnu OpenMP had already been loaded by Python process is not assured. Please install mkl-service package, see http://github.com/IntelPython/mkl-service
      from . import _distributor_init
    Traceback (most recent call last):
      File "/workspace/scripts/fpAnalyserCompilerIpevaluator_520.py", line 43, in <module>
        bie_file = run_knerex(model_config, threads, 520)
      File "/workspace/scripts/utils/run_knerex.py", line 44, in run_knerex
        enable_crop=config.enable_crop)
      File "/workspace/scripts/utils/img_preprocess.py", line 205, in img2txt_bin
        img_data = preprocess(image, clrspace, pos, crop_size, resize_size, keep_aspect_ratio, mode, bCrop, "temp.txt", bNeedSaveCrop, bCvtOnly)
      File "/workspace/scripts/utils/img_preprocess.py", line 35, in preprocess
        img_data = resize(image, resize_dim, keep_aspect_ratio, outFile, bSaveCrop, clrspace)
      File "/workspace/scripts/utils/img_preprocess.py", line 46, in resize
        x.thumbnail(size, Image.ANTIALIAS)
      File "/workspace/miniconda/lib/python3.7/site-packages/PIL/Image.py", line 2308, in thumbnail
        if x / y >= aspect:
    ZeroDivisionError: division by zero
    


    請問該怎麼辦?

    另外,請問您的那張圖片是如何產生的?

    謝謝

  • 圖片是用 Netron 模型視覺化工具,可圖示了解模型結構

  • 從模型的截圖來看,模型的解析度並非固定值(1xunk__1955xunk__1956x3),若要在加速棒上執行則需將模型的解析度訂下來後才可執行後續轉換,類似1x224x224x3或其他解析度,請重新輸出模型固定解析度後再跑一次toolchain

  • 我了解了,謝謝

  • 您好,想請問一下我們拿耐能提供的tensorflow model (https://github.com/kneron/ConvertorExamples/tree/master/tensorflow_example)

    轉成耐能要的格式後跑下面的指令,將模型轉換成NPU所需架構作最佳化

    python /workspace/scripts/convert_model.py onnx target.onnx target_opt.onnx
    

    有成功輸出 target_opt.onnx。

    接著照著耐能document center內所述,執行以下步驟

    cd /workspace/scripts && ./compilerIpevaluator_520.sh target_opt.onnx
    

    成功產生compiler資料夾

    接著執行

    python /workspace/scripts/fpAnalyserCompilerIpevaluator_520.py -t 8
    

    卻出現如下的錯誤,請問是example model不能使用嗎?還是有少做什麼步驟?

    謝謝。

  • Hi Jasmine,

    fpAnalyserCompilerIpevaluator_520.py在執行時預設會去尋找/data1/input_params.json,或是透過引數-c 輸入json的路徑,script會依據json內的資訊找到對應的onnx與圖片來做量化與編譯

    從錯誤訊息來看,模型onnx的input dimesion可能設定有誤,可以先檢查看看,若還是有問題的話,還請你提供你轉出來的onnx與input_params.json方便我們確認

  • 以下是我的input_params.json檔

    {
      "model_info": {
        "input_onnx_file": "/data1/target_opt.onnx",
        "model_inputs": [
          {
            "model_input_name": "conv1/BiasAdd__7:0_kn",
            "input_image_folder": "/data1/test_img_100"
          }
        ]
      },
      "preprocess": {
        "img_preprocess_method": "kneron",
        "img_channel": "RGB",
        "radix": 8,
        "keep_aspect_ratio": true,
        "pad_mode": 1,
        "p_crop": {
          "crop_x": 0,
          "crop_y": 0,
          "crop_w": 0,
          "crop_h": 0
        }
      },
      "simulator_img_files": [
        {
          "model_input_name": "conv1/BiasAdd__7:0_kn",
          "input_image": "/data1/test_img_100/204.jpg"
        }
      ]
    }
    

    下面連結內是我的target_opt.onnx

    謝謝 !

  • Hi Jasmine,

    tensorflow example mnst是黑白的模型

    所以"img_channel"要改"L"

  • 了解,謝謝您!

  • edited November 2021

    您好,

    我執行 模型量化發生錯誤

    1. python /workspace/scripts/fpAnalyserBatchCompile_520.py -c /data1/batch_input_params.jso

    跑到最後跳出錯誤,以下是我json檔內容


    一直找不到問題原因,附上json/onnx檔,感謝


  • Hello,

    模型的轉換會有三個階段 ONNX -> BIE (量化模型) -> NEF (編譯後的量化模型,供KL520 / KL720使用)

    依據不同的script可以逐步轉換也可以直接ONNX到NEF,從你使用fpAnalyserBatchCompile_520.py表示你希望從ONNX直接轉換成NEF,這樣的話參數中也需要有量化照片等資訊 (需填寫項目 "input_params")

    詳細請參考連結文件中的 B. How to configure the batch_input_params.json? 來填寫你的json file


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