使用算法套件快速完成水表读数识别
本示例围绕真实AI需求场景,介绍算法开发套件在水表表盘读数识别算法开发任务上的使用流程。
算法开发套件中目前提供自研(ivg系列)和开源(mm系列)共两套算法资产,可应用于分类、检测、分割和OCR等任务中。本示例中将组合使用自研分割算法(ivgSegmentation)和开源OCR算法(mmOCR)完成水表读数识别项目,并使用算法开发套件将其部署为华为云在线服务。
本案例教程仅适用于“华北-北京四”区域,新版Notebook。
准备数据
- 登录OBS控制台,创建OBS对象桶,区域选择“华北-北京四”。
- 登录ModelArts控制台,选择控制台区域为“华北-北京四”。
- 在“全局配置”页面查看是否已经配置授权,允许ModelArts访问OBS。如果没有配置授权,请参考配置访问授权(全局配置)添加授权。
- 分别下载本案例的数据集,水表表盘分割数据集和水表表盘读数OCR识别数据集到OBS桶中,OBS路径示例如下:
obs://{OBS桶名称}/water_meter_segmentation 水表表盘分割数据集
obs://{OBS桶名称}/water_meter_crop 水表表盘读数OCR识别数据集
准备开发环境
- 在页面中,创建基于pytorch1.8-cuda10.2-cudnn7-ubuntu18.04镜像,类型为GPU的Notebook,具体操作请参见创建Notebook实例章节。
如果需要使用本地IDE(PyCharm或VS Code)远程连接Notebook,需要开启SSH远程开发。本案例以在线的JupyterLab为例介绍整个过程。图1 创建Notebook实例
- 在页面的列表中,单击操作栏的“打开”,进入JupyterLab页面。JupyterLab操作请参见JupyterLab简介及常用操作。
图2 使用JupyterLab打开
- 打开JupyterLab的Terminal。此处以Terminal为例介绍整个过程。
图3 打开Terminal
Step1 创建算法工程
- 在JupyterLab的Terminal中,在work目录下执行ma-cli createproject命令创建工程,根据提示输入工程名称,例如:water_meter。然后直接回车选择默认参数,并选择跳过资产安装步骤(选择6)。
图4 创建工程
- 执行以下命令进入工程目录。
cd water_meter
- 执行以下命令拷贝项目数据到Notebook中。
python manage.py copy --source {obs_dataset_path} --dest ./data/raw/water_meter_crop python manage.py copy --source {obs_dataset_path} --dest ./data/raw/water_meter_segmentation
{obs_dataset_path}路径为Step1 准备数据中下载到OBS中的数据集,比如“obs://{OBS桶名称}/water_meter_segmentation”和“obs://{OBS桶名称}/water_meter_crop”
图5 拷贝数据集到Notebook中
Step2 使用deeplabv3完成水表区域分割任务
- 首先安装ivgSegmentation套件。
python manage.py install algorithm ivgSegmentation==1.0.2
如果提示ivgSegmentation版本不正确,可以通过命令python manage.py list algorithm查询版本。
- 安装ivgSegmentation套件后,在JupyterLab界面左侧的工程目录中进入“./algorithms/ivgSegmentation/config/sample”文件夹中查看目前支持的分割模型,以sample为例(sample默认的算法就是deeplabv3),文件夹中包括config.py(算法外壳配置)和deeplabv3_resnet50_standard-sample_512x1024.py(模型结构)。
- 表盘分割只需要区分背景和读数区域,因此属于二分类,需要根据项目所需数据集对配置文件进行修改,如下所示:
修改“./algorithms/ivgSegmentation/config/sample/config.py”文件。
# config.py alg_cfg = dict( ... data_root='data/raw/water_meter_segmentation', # 修改为真实路径本地分割数据集路径 ... )修改完后按Ctrl+S保存。
- 修改“./algorithms/ivgSegmentation/config/sample/deeplabv3_resnet50_standard-sample_512x1024.py”文件。
# deeplabv3_resnet50_standard-sample_512x1024.py gpus=[0] ... data_cfg = dict( ... num_classes=2, # 修改为2类 ... ... train_scale=(512, 512), # (h, w)#size全部修改为(512, 512) ... train_crop_size=(512, 512), # (h, w) ... test_scale=(512, 512), # (h, w) ... infer_scale=(512, 512), # (h, w) )修改完按Ctrl+S保存。
- 在water_meter工程目录下,安装deeplabv3预训练模型。
python manage.py install model ivgSegmentation:deeplab/deeplabv3_resnet50_cityscapes_512x1024
图6 安装deeplabv3预训练模型
- 训练分割模型。(推荐使用GPU进行训练)
# shell python manage.py run --cfg algorithms/ivgSegmentation/config/sample/config.py --gpus 0
训练好的模型会保存在指定位置中,默认为“output/deeplabv3_resnet50_standard-sample_512x1024/checkpoints/”中。
- 验证模型效果。
模型训练完成后,可以在验证集上计算模型的指标,首先修改配置文件的模型位置。
修改“./algorithms/ivgSegmentation/config/sample/config.py”。
# config.py alg_cfg = dict( ... load_from='./output/deeplabv3_resnet50_standard-sample_512x1024/checkpoints/checkpoint_best.pth.tar', # 修改训练模型的路径 ... )
# shell python manage.py run --cfg algorithms/ivgSegmentation/config/sample/config.py --pipeline evaluate
- 模型推理。
模型推理能够指定某一张图片,并且推理出图片的分割区域,并进行可视化,首先需要指定需要推理的图片路径。
修改“./algorithms/ivgSegmentation/config/sample/config.py”
alg_cfg = dict( ... img_file = './data/raw/water_meter_segmentation/image/train_10.jpg' # 指定需要推理的图片路径 ... )
执行如下命令推理模型效果。
# shell python manage.py run --cfg algorithms/ivgSegmentation/config/sample/config.py --pipeline infer
推理输出的图片路径在“./output/deeplabv3_resnet50_standard-sample_512x1024”下。
图7 水表表盘分割结果可视化
- 导出算法SDK。
算法开发套件支持将模型导出成一个模型SDK,方便进行模型部署等下游任务。
# shell python manage.py export --cfg algorithms/ivgSegmentation/config/sample/config.py --is_deploy
图8 SDK导出示意图
Step4 水表读数识别
- 首先安装mmocr套件。
python manage.py install algorithm mmocr==0.2.1
- 安装mmocr套件后,“./algorithms/mmocr/config/textrecog”文件夹中包括config.py(算法外壳配置),需要根据所需算法和数据集路径修改配置文件。以下以robust_scanner算法为例。
修改“./algorithms/mmocr/algorithm/configs/textrecog/robustscanner_r31_academic.py”,
# robustscanner_r31_academic.py ... train_prefix = 'data/raw/water_meter_crop/' # 修改数据集路径改为水表ocr识别数据集路径 train_img_prefix1 = train_prefix + 'train' train_ann_file1 = train_prefix + 'train.txt' ... test_prefix = 'data/raw/water_meter_crop/' test_img_prefix1 = test_prefix + 'val/' test_ann_file1 = test_prefix + 'val.txt'
- 安装robust_scanner预训练模型。
python manage.py install model mmocr:textrecog/robust_scanner/robustscanner_r31_academic
- 训练OCR模型。
初次使用mmcv时需要编译mmcv-full,该过程较慢,可以直接使用官方预编译的依赖包。
预编译包URL: https://download.openmmlab.com/mmcv/dist/cu102/torch1.6.0/index.html
pip uninstall mmcv -y pip install https://download.openmmlab.com/mmcv/dist/cu102/torch1.6.0/mmcv_full-1.3.9-cp37-cp37m-manylinux1_x86_64.whl
将./algorithms/mmocr/config/textrecog/config.py中的epoch(迭代数量)改为2,如下图所示:
训练OCR模型。(仅使用GPU进行训练,大概需要四分钟)# shell python manage.py run --cfg algorithms/mmocr/config/textrecog/config.py
训练好的模型会保存在指定位置中,默认为output/robustscanner_r31_academic/文件夹中。
- 验证模型效果。
模型训练完成后,可以在验证集上计算模型的指标,首先修改配置文件的模型位置。
修改./algorithms/mmocr/config/textrecog/config.py
# config.py ... model_path = './output/robustscanner_r31_academic/latest.pth' ...
# shell python manage.py run --cfg algorithms/mmocr/config/textrecog/config.py --pipeline evaluate
- 可选:模型推理。
模型推理能够指定某一张图片,并且推理出图片的分割区域,并进行可视化。首先需要指定待推理的图片路径,修改algorithms/mmocr/config/textrecog/config.py文件,具体如下。
修改“./algorithms/mmocr/algorithm/configs/textrecog/robust_scanner/config.py”
... infer_img_file='./data/raw/water_meter_crop/val/train_10.jpg' # 指定需要推理的图片路径 ...
# shell python manage.py run --cfg algorithms/mmocr/config/textrecog/config.py --pipeline infer
推理输出的图片路径在“output/robustscanner_r31_academic/vis”文件夹下。
图9 表盘读数识别结果图
- 导出算法SDK。
# shell python manage.py export --cfg algorithms/mmocr/config/textrecog/config.py
部署为在线服务
本次展示仅部署OCR服务, 包括本地部署和线上部署, 部署上线后调用部署服务进行本地图片的推理,获取水表的预测读数。部署在线服务,需要指定OBS桶以便保存部署所需要的文件。
1.在“algorithms/mmocr/config/textrecog/config.py”文件中配置OBS桶。
修改./algorithms/mmocr/algorithm/configs/textrecog/robust_scanner/config.py
# 替换为用户自己的OBS桶信息
obs_bucket = 'obs://{your_obs_bucket_path}'
2.依次执行下述命令:
python manage.py export --cfg algorithms/mmocr/config/textrecog/config.py --is_deploy # 导出部署模型所需文件 python manage.py deploy --cfg algorithms/mmocr/config/textrecog/config.py # 本地部署调试
本地部署成功后的输出结果
#
...
[Conda environment created successfully.]
local_service_port is 127.0.0.1:42153
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
[07/05 09:40:14][INFO][ma_cau-deployer.py 49]: {
"text": "00326",
"score": 0.9999999046325684
}
[07/05 09:40:14][INFO][ma_cau-deployer.py 59]: ************************ End Deployer ************************
python manage.py deploy --cfg algorithms/mmocr/config/textrecog/config.py --launch_remote本地部署成功后可直接进行在线部署,大约需要12分钟。
Step5 清除资源和数据
通过此示例学习完成创建算法套件流程后,如果不再使用,建议您清除相关资源,避免造成资源浪费和不必要的费用。
- 停止Notebook:在“Notebook”页面,单击对应实例操作列的“停止”。
- 删除数据:前往OBS,删除上传的数据,然后删除文件夹及OBS桶。
- 停止在线服务:在Modelarts部署上线->在线服务界面,单击对应在线服务操作列的“更多“->“停止“。