天天看點

paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

這個主要參考PaddleOCR下的 /doc/doc_ch/inference.md

先按照這裡的教程學習模型轉化:

一、訓練模型轉inference模型

檢測模型轉inference模型

下載下傳超輕量級中文檢測模型:

wget -P ./ch_lite/ https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_det_train.tar && tar xf ./ch_lite/ch_ppocr_mobile_v2.0_det_train.tar -C ./ch_lite/
           

上述模型是以MobileNetV3為backbone訓練的DB算法,将訓練好的模型轉換成inference模型隻需要運作如下指令:

# -c 後面設定訓練算法的yml配置檔案
# -o 配置可選參數
# Global.pretrained_model 參數設定待轉換的訓練模型位址,不用添加檔案字尾 .pdmodel,.pdopt或.pdparams。
# Global.load_static_weights 參數需要設定為 False。
# Global.save_inference_dir參數設定轉換的模型将儲存的位址。

python tools/export_model.py -c configs/det/ch_ppocr_v2.0/ch_det_mv3_db_v2.0.yml -o Global.pretrained_model=./ch_lite/ch_ppocr_mobile_v2.0_det_train/best_accuracy Global.load_static_weights=False Global.save_inference_dir=./inference/det_db/
           

轉inference模型時,使用的配置檔案和訓練時使用的配置檔案相同。另外,還需要設定配置檔案中的

Global.pretrained_model

參數,其指向訓練中儲存的模型參數檔案。 轉換成功後,在模型儲存目錄下有三個檔案:

inference/det_db/
    ├── inference.pdiparams         # 檢測inference模型的參數檔案
    ├── inference.pdiparams.info    # 檢測inference模型的參數資訊,可忽略
    └── inference.pdmodel           # 檢測inference模型的program檔案
           

儲存後生成的檔案:

paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

上面是使用下載下傳的模型轉換的,下面介紹使用自己訓練的模型轉化:

使用自己訓練的模型轉化

承接paddleocr學習筆記(四)評估、推理 将裡面使用的自己訓練的模型儲存成推理模型

# 儲存自己訓練的模型
python tools/export_model.py -c configs/det/det_mv3_db.yml -o Global.pretrained_model=./output/db_mv3/best_accuracy Global.load_static_weights=False Global.save_inference_dir=./inference/db_mv3/
           

可以看到儲存的模型如下:

paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

二、文本檢測模型推理

文本檢測模型推理,預設使用DB模型的配置參數。當不使用DB模型時,在推理時,需要通過傳入相應的參數進行算法适配,細節參考下文。

1. 超輕量中文檢測模型推理

超輕量中文檢測模型推理,可以執行如下指令:使用的是第一步中下載下傳解壓轉化的推理模型

python tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/"
           

可視化文本檢測結果預設儲存到

./inference_results

檔案夾裡面,結果檔案的名稱字首為'det_res'。

檢測結果如下:

[2021/02/27 14:52:09] root INFO: Predict time of ./doc/imgs/00018069.jpg: 1.1728620529174805
[2021/02/27 14:52:09] root INFO: The visualized image saved in ./inference_results\det_res_00018069.jpg
           
paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

通過參數

limit_type

det_limit_side_len

來對圖檔的尺寸進行限制, 

limit_type

可選參數為[

max

min

], 

det_limit_size_len

 為正整數,一般設定為32 的倍數,比如960。

參數預設設定為

limit_type='max', det_limit_side_len=960

。表示網絡輸入圖像的最長邊不能超過960, 如果超過這個值,會對圖像做等寬比的resize操作,確定最長邊為

det_limit_side_len

。 設定為

limit_type='min', det_limit_side_len=960

 則表示限制圖像的最短邊為960。

如果輸入圖檔的分辨率比較大,而且想使用更大的分辨率預測,可以設定det_limit_side_len 為想要的值,比如1216。

python tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/" --det_limit_type=max --det_limit_side_len=1216
           

檢測結果如下:

[2021/02/27 14:58:47] root INFO: Predict time of ./doc/imgs/00018069.jpg: 1.1828348636627197
[2021/02/27 14:58:47] root INFO: The visualized image saved in ./inference_results\det_res_00018069.jpg
           
paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

 如果想使用CPU進行預測,執行指令如下:通過檢測顯存變化,确實前面的顯存上升又下降了,這個就沒有顯存變化,同時通過觀察推理時間,CPU比GPU快很多,這主要是因為模型較小GPU的優勢不明顯,其次GPU加載模型比CPU慢,完成第一次推理後GPU的推理速度才會明顯快于CPU,是以看速度應該在批量測試中去比較。

python tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/det_db/"  --use_gpu=False
           

檢測結果:

[2021/02/27 15:03:34] root INFO: Predict time of ./doc/imgs/00018069.jpg: 0.13463759422302246
[2021/02/27 15:03:34] root INFO: The visualized image saved in ./inference_results\det_res_00018069.jpg
           
paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

使用自己訓練轉化的推理模型測試: 

python tools/infer/predict_det.py --image_dir="./doc/imgs/00018069.jpg" --det_model_dir="./inference/db_mv3/"
           

 檢測結果如下:由于我隻是訓練了icdar2015的1000張資料,而且是自然場景多是街景圖檔,不包含文檔類圖檔,跟百度官方大資料集訓練的模型效果自然是沒有可比性,此處隻是說明轉化模型的正确性。

[2021/02/27 15:06:36] root INFO: Predict time of ./doc/imgs/00018069.jpg: 1.1948025226593018
[2021/02/27 15:06:36] root INFO: The visualized image saved in ./inference_results\det_res_00018069.jpg
           
paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)

使用icdar2015中的資料檢測一下自己訓練的模型:

python tools/infer/predict_det.py --image_dir="./doc/imgs_en/img_10.jpg" --det_model_dir="./inference/db_mv3/"
           

檢測結果如下:

[2021/02/27 15:11:13] root INFO: Predict time of ./doc/imgs_en/img_10.jpg: 1.1848294734954834
[2021/02/27 15:11:13] root INFO: The visualized image saved in ./inference_results\det_res_img_10.jpg
           
paddleocr學習筆記(五)将訓練模型(checkpoints模型)轉化為推理模型(inference模型)