基于AI算法開發套件進行水表讀數識別
算法工程外殼介紹
算法工程外殼介紹
云原生的產品化算法開發架構
基于算法外殼的水表讀數識別
水表識別項目流程介紹
獲取真實水表數據
基于圖片分割算法分割出水表讀數區域
基于圖片OCR算法識別出讀數
如何基于算法外殼和算法套件完成上述流程
獲取數據
2. 識別讀數
目標:完成水表讀數識別
部署為在線服務
云原生的產品化算法開發架構
算法外殼+算法套件基于云上資源和IDE開發工具,串聯ModelArts開發、訓練和部署等功能,高效管理AI算法開發的全生命周期。
基于算法外殼的水表讀數識別
圍繞真實AI需求場景,介紹算法外殼和算法套件在AI開發中的使用流程。
水表識別項目流程介紹
基于CV算法識別水表讀數的流程大致如下:
下圖水表的示數為00018
分割算法會輸出水表讀數區域四個角的坐標點(像素點),并覆蓋不同顏色的蒙版區分圖片不同的區域,最后可以裁剪出讀數區域用于后續的OCR識別任務。
OCR任務識別出水表讀數為00018,與水表示數一致。
如何基于算法外殼和算法套件完成上述流程
算法外殼中目前提供自研(ivg系列)和開源(mm系列)共兩套算法資產,均可應用于分類、檢測、分割和OCR等任務中。接下來,我們將組合使用自研分割算法(ivgSegmentation)和開源OCR算法(mmOCR)完成水表讀數識別項目,并將其部署為華為云在線服務。操作流程如下圖所示。
# 下載并解壓分割用數據集 wget https://cnnorth4-modelarts-sdk.obs.cn-north-4.myhuaweicloud.com:443/custom.zip unzip custom.zip -d ./data/raw/ # 下載并解壓OCR用數據集 wget https://cnnorth4-modelarts-sdk.obs.cn-north-4.myhuaweicloud.com:443/crop.zip unzip crop.zip -d ./data/raw/ #### 安裝分割資產和OCR資產 接下來將使用deeplabv3完成分割任務,使用XXX完成OCR任務 - **安裝分割套件和預訓練模型**
python manage.py install algorithm ivgSegmentation
python manage.py install model ivgSegmentation:deeplab/deeplabv3_resnet50_cityscapes_1024x512
- **安裝OCR套件和預訓練模型**
python manage.py install algorithm mmocr
python manage.py install model mmocr:textrecog/robust_scanner/robustscanner_r31_academic
#### 參數配置和模型訓練 ### 1. 分割水表讀數區域 #### 目標:使用`deeplabv3`完成水表區域分割任務: 1. 安裝ivgSegmentation套件后,可以在`./algorithms/ivgSegmentation/config`中查看目前支持的分割模型,以sample為例(sample默認的算法就是deeplabv3),文件夾中包括`config.py`(算法外殼配置)和`deeplabv3_resnet50_standard-sample_1024x512.py`(模型結構)。 2. 表盤分割只需要區分背景和讀數區域,因此屬于二分類,需要根據項目所需數據集對配置文件進行修改,如下所示: ```python # config.py alg_cfg = dict( ... data_root='data/raw/crop', # 修改為真實路徑 ... ) # deeplabv3_resnet50_standard-sample_1024x512.py data_cfg = dict( ... num_classes=2, # 修改為2類 ... palate=[[128, 64, 128], [244, 35, 232]] # 可視化調色板也改成兩種顏色即可 )
訓練分割模型
# shell python manage.py run --cfg algorithms/ivgSegmentation/config/sample/config.py
訓練好的模型會保存在指定位置中,默認為output/${algorithm}/checkpoints中。
驗證模型效果
模型訓練完成后,可以在驗證集上計算模型的指標,首先修改配置文件的模型位置:
# config.py alg_cfg = dict( ... load_from='output/deeplabv3p_resnet50_cityscapes_1024x512/checkpoints/checkpoint_best.pth.tar', # 修改訓練模型的路徑 ... )
# shell python manage.py run --cfg algorithms/ivgSegmentation/config/sample/config.py --pipeline evaluate
模型推理
模型推理能夠指定某一張圖片,并且推理出圖片的分割區域,并進行可視化,首先需要指定需要推理的圖片路徑
alg_cfg = dict( ... img_file='./data/raw/crop/image/train_27.jpg' # 指定需要推理的圖片路徑 )
# shell python manage.py run --cfg algorithms/ivgSegmentation/config/sample/config.py --pipeline infer
導出SDK
算法外殼支持將模型導出成一個sdk,方便進行模型部署等下游任務:
# shell python manage.py export --cfg algorithms/ivgSegmentation/config/sample/config.py
2. 識別讀數
安裝mmocr套件后,./algorithms/mmocr/config/textdet文件夾中包括config.py(算法外殼配置),需要根據所需算法和數據集路徑修改配置文件。以下以robust_scanner算法為例。
為了方便處理,我么可以在./algorithms/mmocr/experiment目錄下復制一個textrecog文件夾,以下所指的配置文件都是新復制的配置文件:
# shell cp ./algorithms/mmocr/config/textdet ./algorithms/mmocr/config/textrecogn
# config.py ... alg_name = 'robustscanner_r31_academic' # 修改模型名稱 ... model_path = './output/robustscanner_r31_academic/best_0_word_acc_epoch_1.pth' # 修改預訓練模型位置 ... alg_cfg = dict( cfg=f'algorithms/{alg_home}/algorithm/configs/textrecog/robust_scanner/' \ 'robustscanner_r31_academic.py', # 修改robustscanner算法配置文件路徑 ) # ./algorithms/mmocr/algorithm/configs/textrecog/robust_scanner/robustscanner_r31_academic.py ... train_prefix = 'data/raw/crop/' # 修改數據集路徑 train_img_prefix1 = train_prefix + 'train' train_ann_file1 = train_prefix + 'train.txt' ... test_prefix = 'data/raw/crop/' test_img_prefix1 = test_prefix + 'val/' test_ann_file1 = test_prefix + 'val.txt' data = dict( ... train=dict( ... datasets=[ train1 ], ...), val=dict( ... datasets=[test1], .), test=dict( ... datasets=[test1], ))
訓練分割模型
# shell python manage.py run --cfg algorithms/mmocr/config/textrecogn/config.py
訓練好的模型會保存在指定位置中,默認為output/${algorithm}/checkpoints中。
驗證模型效果
模型訓練完成后,可以在驗證集上計算模型的指標,首先修改配置文件的模型位置:
# config.py alg_cfg = dict( ... load_from='./output/robustscanner_r31_academic/latest.pth', # 修改訓練模型的路徑 ... )
# shell python manage.py run --cfg algorithms/mmocr/config/textrecogn/config.py --pipeline evaluate
模型推理
模型推理能夠指定某一張圖片,并且推理出圖片的分割區域,并進行可視化,首先需要指定需要推理的圖片路徑
alg_cfg = dict( ... img_file="your infer image" # 指定需要推理的圖片路徑 )
# shell python manage.py run --cfg algorithms/mmocr/config/textrecogn/config.py --pipeline infer
導出SDK
算法外殼支持將模型導出成一個sdk,方便進行模型部署等下游任務:
# shell python manage.py export --cfg algorithms/mmocr/config/textrecogn/config.py
本次展示僅部署OCR服務, 包括本地部署和線上部署, 部署上線后調用部署服務進行本地圖片的推理,獲取水表的預測讀數。
# 本地部署 python manage.py deploy --cfg --cfg algorithms/mmocr/config/textrecogn/config.py The model source location is /home/ma-user/work/water_meter/export/robustscanner_r31_academic/Linux_x86_64_GPU_PyTorch_Common_py Validate config.json successfully. Create local model successfully, model_id is:0f71de32-b9c0-41b7-98b0-d1395253f783 Validate config.json successfully. Service name is mmocr_robustscanner_r31_academic [Exsiting Conda environment(/home/ma-user/anaconda3/envs/modelarts_env_47ce3a59b9b6f8767c7aa98229bf4233) found to run current task.] local_service_port is 127.0.0.1:42329 Deploying the local service ... Successfully deployed the local service. You can check the log in /home/ma-user/work/water_meter/export/robustscanner_r31_academic/Linux_x86_64_GPU_PyTorch_Common_py/log.txt INFO:ma_cau:{ "text": "00326", "score": 0.9999996662139893 }
# 在線部署 python manage.py deploy --cfg algorithms/mmocr/config/textrecogn/config.py --launch_remote
AI OCR
版權聲明:本文內容由網絡用戶投稿,版權歸原作者所有,本站不擁有其著作權,亦不承擔相應法律責任。如果您發現本站中有涉嫌抄襲或描述失實的內容,請聯系我們jiasou666@gmail.com 處理,核實后本網站將在24小時內刪除侵權內容。