【PaddleHub模型贡献】一行代码实现水表的数字表盘分割
本文介绍将水表数字表盘分割模型贡献到PaddleHub的方法。先安装必要库,复现模型:准备数据集,配置GPU,定义图像预处理流程和数据集,用DeepLabv3p训练模型并导出。接着转换模型为PaddleHub模型,补充代码实现旋转剪裁等功能,最后测试安装与调用,实现水表数字表盘分割。

【PaddleHub模型贡献】一行代码实现水表的数字表盘分割
一、安装必要的库
In [3]!pip install paddlex -i https://mirror.baidu.com/pypi/simple!pip install --upgrade paddlepaddle-gpu -i https://pypi.tuna.tsinghua.edu.cn/simple!pip install --upgrade paddlehub==2.0.1 -i https://pypi.tuna.tsinghua.edu.cn/simple登录后复制
二、模型训练
项目作者使用PaddleX做的语义分割,因为作者没有直接公开训练好的模型,所以这里我们先按照作者的思路复现模型。
免费影视、动漫、音乐、游戏、小说资源长期稳定更新! 👉 点此立即查看 👈
1.准备表盘数据集
In [ ]!unzip -oq /home/aistudio/data/data73852/water.zip登录后复制
2. 模型训练
2.1 配置GPU
In [ ]# 设置使用0号GPU卡(如无GPU,执行此代码后仍然会使用CPU训练模型)import matplotlibmatplotlib.use('Agg') import osos.environ['CUDA_VISIBLE_DEVICES'] = '0'import paddlex as pdx登录后复制 2.2 定义图像预处理流程transforms
定义数据处理流程,其中训练和测试需分别定义,训练过程包括了部分测试过程中不需要的数据增强操作,如在本示例中,训练过程使用了RandomHorizontalFlip和RandomPaddingCrop两种数据增强方式,更多图像预处理流程transforms的使用可参见paddlex.seg.transforms。
In [ ]from paddlex.seg import transformstrain_transforms = transforms.Compose([ transforms.RandomHorizontalFlip(), transforms.Resize(target_size=512), transforms.RandomPaddingCrop(crop_size=500), transforms.Normalize()])eval_transforms = transforms.Compose([ transforms.Resize(512), transforms.Normalize()])登录后复制
2.3 定义数据集Dataset
实例分割使用SegDataset格式的数据集,因此采用pdx.datasets.SegDataset来加载数据集,该接口的介绍可参见文档pdx.datasets.SegDataset。
In [ ]train_dataset = pdx.datasets.SegDataset( data_dir='water', file_list='water/train.txt', label_list='water/class_names.txt', transforms=train_transforms, shuffle=True)eval_dataset = pdx.datasets.SegDataset( data_dir='water', file_list='water/val.txt', label_list='water/class_names.txt', transforms=eval_transforms)登录后复制
2024-03-11 14:54:48 [INFO]150 samples in file water/train.txt2024-03-11 14:54:48 [INFO]11 samples in file water/val.txt登录后复制
2.4 模型开始训练
使用本数据集在P40上训练,如有GPU,模型的训练过程预估为13分钟左右;如无GPU,则预估为5小时左右。更多训练模型的参数可参见文档paddlex.seg.DeepLabv3p。模型训练过程每间隔save_interval_epochs轮会保存一次模型在save_dir目录下,同时在保存的过程中也会在验证数据集上计算相关指标,具体相关日志参见文档。
In [ ]num_classes = len(train_dataset.labels)model = pdx.seg.DeepLabv3p(num_classes=num_classes)model.train( num_epochs=40, train_dataset=train_dataset, train_batch_size=4, eval_dataset=eval_dataset, learning_rate=0.01, save_interval_epochs=1, # pretrain_weights='output/deeplab4/best_model', save_dir='output/water')登录后复制
最后一轮的输出如下所示:
2024-03-11 15:02:56 [INFO][TRAIN] Epoch=40/40, Step=1/37, loss=0.010831, lr=0.000362, time_each_step=0.18s, eta=0:0:102024-03-11 15:02:56 [INFO][TRAIN] Epoch=40/40, Step=3/37, loss=0.010944, lr=0.000344, time_each_step=0.2s, eta=0:0:102024-03-11 15:02:57 [INFO][TRAIN] Epoch=40/40, Step=5/37, loss=0.009099, lr=0.000326, time_each_step=0.22s, eta=0:0:102024-03-11 15:02:57 [INFO][TRAIN] Epoch=40/40, Step=7/37, loss=0.011186, lr=0.000308, time_each_step=0.24s, eta=0:0:102024-03-11 15:02:57 [INFO][TRAIN] Epoch=40/40, Step=9/37, loss=0.008269, lr=0.00029, time_each_step=0.25s, eta=0:0:102024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=11/37, loss=0.011792, lr=0.000272, time_each_step=0.25s, eta=0:0:102024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=13/37, loss=0.010976, lr=0.000254, time_each_step=0.26s, eta=0:0:92024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=15/37, loss=0.01399, lr=0.000236, time_each_step=0.26s, eta=0:0:92024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=17/37, loss=0.009998, lr=0.000217, time_each_step=0.26s, eta=0:0:82024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=19/37, loss=0.012266, lr=0.000198, time_each_step=0.26s, eta=0:0:82024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=21/37, loss=0.011713, lr=0.00018, time_each_step=0.13s, eta=0:0:52024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=23/37, loss=0.010291, lr=0.00016, time_each_step=0.11s, eta=0:0:52024-03-11 15:02:58 [INFO][TRAIN] Epoch=40/40, Step=25/37, loss=0.010211, lr=0.000141, time_each_step=0.09s, eta=0:0:42024-03-11 15:02:59 [INFO][TRAIN] Epoch=40/40, Step=27/37, loss=0.02097, lr=0.000121, time_each_step=0.08s, eta=0:0:42024-03-11 15:02:59 [INFO][TRAIN] Epoch=40/40, Step=29/37, loss=0.008198, lr=0.000101, time_each_step=0.07s, eta=0:0:32024-03-11 15:02:59 [INFO][TRAIN] Epoch=40/40, Step=31/37, loss=0.010346, lr=8.1e-05, time_each_step=0.06s, eta=0:0:32024-03-11 15:02:59 [INFO][TRAIN] Epoch=40/40, Step=33/37, loss=0.009331, lr=6e-05, time_each_step=0.06s, eta=0:0:32024-03-11 15:02:59 [INFO][TRAIN] Epoch=40/40, Step=35/37, loss=0.01259, lr=3.8e-05, time_each_step=0.06s, eta=0:0:32024-03-11 15:02:59 [INFO][TRAIN] Epoch=40/40, Step=37/37, loss=0.013072, lr=1.4e-05, time_each_step=0.06s, eta=0:0:32024-03-11 15:02:59 [INFO][TRAIN] Epoch 40 finished, loss=0.011522, lr=0.000195 .2024-03-11 15:02:59 [INFO]Start to evaluating(total_samples=11, total_steps=3)...100%|██████████| 3/3 [00:02<00:00, 1.00it/s]2024-03-11 15:03:02 [INFO][EVAL] Finished, Epoch=40, miou=0.814756, category_iou=[0.99168644 0.63782582], oacc=0.991806, category_acc=[0.99431391 0.84710874], kappa=0.774722, category_F1-score=[0.99582587 0.77886893] .2024-03-11 15:03:03 [INFO]Model saved in output/water/epoch_40.2024-03-11 15:03:03 [INFO]Current evaluated best model in eval_dataset is epoch_35, miou=0.8284633456567256登录后复制
3.模型导出
模型训练时会自动保存模型参数,我们需要把训练模型导出成可预测模型。
In [ ]!paddlex --export_inference --model_dir=output/water/best_model --save_dir=./inference_model登录后复制
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import impW0311 15:49:28.613981 782 device_context.cc:362] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.0, Runtime API Version: 10.1W0311 15:49:28.618839 782 device_context.cc:372] device: 0, cuDNN Version: 7.6.2024-03-11 15:49:32 [INFO]Model[DeepLabv3p] loaded.2024-03-11 15:49:32 [INFO]Model for inference deploy saved in ./inference_model.登录后复制
三、封装Module
下面正式开始模型转换!
1.模型转换
PaddleX模型可以快速转换成PaddleHub模型,只需要用下面这一句命令即可:
In [ ]!hub convert --model_dir inference_model \ --module_name WatermeterSegmentation \ --module_version 1.0.0 \ --output_dir outputs登录后复制
转换成功后的模型保存在outputs文件夹下,我们解压一下:
In [ ]!gzip -dfq /home/aistudio/outputs/WatermeterSegmentation.tar.gz!tar -xf /home/aistudio/outputs/WatermeterSegmentation.tar登录后复制
2.补充代码
刚刚转换的模型其实已经是PaddleHub的Module了,但是原项目中,作者做了一些图片的裁剪等操作,把数字提取出来了,因此,我们需要把这部分代码补充进去。
完整的module.py文件内容如下:
from __future__ import absolute_importfrom __future__ import divisionimport osimport cv2import argparseimport base64import paddlex as pdxfrom math import *import time, math, reimport numpy as npimport paddlehub as hubfrom paddlehub.module.module import moduleinfo, runnable, servingdef base64_to_cv2(b64str): data = base64.b64decode(b64str.encode('utf8')) data = np.fromstring(data, np.uint8) data = cv2.imdecode(data, cv2.IMREAD_COLOR) return datadef cv2_to_base64(image): # return base64.b64encode(image) data = cv2.imencode('.webp', image)[1] return base64.b64encode(data.tostring()).decode('utf8')def read_images(paths): images = [] for path in paths: images.append(cv2.imread(path)) return images'''旋转图像并剪裁'''def rotate( img, # 图片 pt1, pt2, pt3, pt4, imgOutSrc): # print(pt1,pt2,pt3,pt4) withRect = math.sqrt((pt4[0] - pt1[0]) ** 2 + (pt4[1] - pt1[1]) ** 2) # 矩形框的宽度 heightRect = math.sqrt((pt1[0] - pt2[0]) ** 2 + (pt1[1] - pt2[1]) **2) # print("矩形的宽度",withRect, "矩形的高度", heightRect) angle = acos((pt4[0] - pt1[0]) / withRect) * (180 / math.pi) # 矩形框旋转角度 # print("矩形框旋转角度", angle) if withRect > heightRect: if pt4[1]>pt1[1]: # print("顺时针旋转") pass else: # print("逆时针旋转") angle=-angle else: # print("逆时针旋转") angle=90 - angle height = img.shape[0] # 原始图像高度 width = img.shape[1] # 原始图像宽度 rotateMat = cv2.getRotationMatrix2D((width / 2, height / 2), angle, 1) # 按angle角度旋转图像 heightNew = int(width * fabs(sin(radians(angle))) + height * fabs(cos(radians(angle)))) widthNew = int(height * fabs(sin(radians(angle))) + width * fabs(cos(radians(angle)))) rotateMat[0, 2] += (widthNew - width) / 2 rotateMat[1, 2] += (heightNew - height) / 2 imgRotation = cv2.warpAffine(img, rotateMat, (widthNew, heightNew), borderValue=(255, 255, 255)) # cv2.imwrite("imgRotation.webp", imgRotation) # 旋转后图像的四点坐标 [[pt1[0]], [pt1[1]]] = np.dot(rotateMat, np.array([[pt1[0]], [pt1[1]], [1]])) [[pt3[0]], [pt3[1]]] = np.dot(rotateMat, np.array([[pt3[0]], [pt3[1]], [1]])) [[pt2[0]], [pt2[1]]] = np.dot(rotateMat, np.array([[pt2[0]], [pt2[1]], [1]])) [[pt4[0]], [pt4[1]]] = np.dot(rotateMat, np.array([[pt4[0]], [pt4[1]], [1]])) # 处理反转的情况 if pt2[1]>pt4[1]: pt2[1],pt4[1]=pt4[1],pt2[1] if pt1[0]>pt3[0]: pt1[0],pt3[0]=pt3[0],pt1[0] imgOut = imgRotation[int(pt2[1]):int(pt4[1]), int(pt1[0]):int(pt3[0])] cv2.imwrite(imgOutSrc, imgOut) # 裁减得到的旋转矩形框@moduleinfo( name='WatermeterSegmentation', type='CV/semantic_segmentatio', author='郑博培、彭兆帅', author_email='2733821739@qq.com', summary='Digital dial segmentation of water meter', version='1.0.0')class MODULE(hub.Module): def _initialize(self, **kwargs): self.default_pretrained_model_path = os.path.join( self.directory, 'assets') self.model = pdx.deploy.Predictor(self.default_pretrained_model_path, **kwargs) def predict(self, images=None, paths=None, data=None, batch_size=1, use_gpu=False, **kwargs): all_data = images if images is not None else read_images(paths) total_num = len(all_data) loop_num = int(np.ceil(total_num / batch_size)) res = [] for iter_id in range(loop_num): batch_data = list() handle_id = iter_id * batch_size for image_id in range(batch_size): try: batch_data.append(all_data[handle_id + image_id]) except IndexError: break out = self.model.batch_predict(batch_data, **kwargs) res.extend(out) return res def cutPic(self, picUrl): # seg = hub.Module(name='WatermeterSegmentation') image_name = picUrl im = cv2.imread(image_name) result = self.predict(images=[im]) # 将多边形polygon转矩形 contours, hier = cv2.findContours(result[0]['label_map'], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) print(type(contours[0])) n = 0 m = 0 for index,contour in enumerate(contours): if len(contour) > n: n = len(contour) m = index image = cv2.imread(image_name) # 获取最小的矩形 rect = cv2.minAreaRect(contours[m]) box = np.int0(cv2.boxPoints(rect)) # 获取到矩形的四个点 tmp = cv2.drawContours(image, [box], 0, (0, 0, 255), 3) imgOutSrc = 'result.webp' rotate(image, box[0], box[1], box[2], box[3], imgOutSrc) res = [] res.append(imgOutSrc) return res @serving def serving_method(self, images, **kwargs): """ Run as a service. """ images_decode = [base64_to_cv2(image) for image in images] results = self.predict(images_decode, **kwargs) res = [] for result in results: if isinstance(result, dict): # result_new = dict() for key, value in result.items(): if isinstance(value, np.ndarray): result[key] = cv2_to_base64(value) elif isinstance(value, np.generic): result[key] = np.asscalar(value) elif isinstance(result, list): for index in range(len(result)): for key, value in result[index].items(): if isinstance(value, np.ndarray): result[index][key] = cv2_to_base64(value) elif isinstance(value, np.generic): result[index][key] = np.asscalar(value) else: raise RuntimeError('The result cannot be used in serving.') res.append(result) return res @runnable def run_cmd(self, argvs): """ Run as a command. """ self.parser = argparse.ArgumentParser( description="Run the {} module.".format(self.name), prog='hub run {}'.format(self.name), usage='%(prog)s', add_help=True) self.arg_input_group = self.parser.add_argument_group( title="Input options", description="Input data. Required") self.arg_config_group = self.parser.add_argument_group( title="Config options", description= "Run configuration for controlling module behavior, not required.") self.add_module_config_arg() self.add_module_input_arg() args = self.parser.parse_args(argvs) results = self.predict( paths=[args.input_path], use_gpu=args.use_gpu) return results def add_module_config_arg(self): """ Add the command config options. """ self.arg_config_group.add_argument( '--use_gpu', type=bool, default=False, help="whether use GPU or not") def add_module_input_arg(self): """ Add the command input options. """ self.arg_input_group.add_argument( '--input_path', type=str, help="path to image.")if __name__ == '__main__': module = MODULE(directory='./new_model') images = [cv2.imread('./cat.webp'), cv2.imread('./cat.webp'), cv2.imread('./cat.webp')] res = module.predict(images=images)登录后复制 3.模型测试
首先安装我们刚刚写好的Module:
In [ ]!hub install WatermeterSegmentation登录后复制
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/setuptools/depends.py:2: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/__init__.py:107: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import MutableMapping/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/rcsetup.py:20: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Iterable, Mapping/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/matplotlib/colors.py:53: DeprecationWarning: Using or importing the ABCs from 'collections' instead of from 'collections.abc' is deprecated, and in 3.8 it will stop working from collections import Sized[2024-03-11 16:42:50,225] [ INFO] - Successfully uninstalled WatermeterSegmentation[2024-03-11 16:42:50,441] [ INFO] - Successfully installed WatermeterSegmentation-1.0.0登录后复制
模型调用:
In [4]import cv2import paddlehub as hubseg = hub.Module(name='WatermeterSegmentation')res = seg.cutPic(picUrl="water/images/val/20200521105032.webp")登录后复制
[2024-03-11 17:13:36,113] [ WARNING] - The _initialize method in HubModule will soon be deprecated, you can use the __init__() to handle the initialization of the object登录后复制
登录后复制
预测结果如下。
输入图片:
最终将截取的图片显示效果如下:
相关攻略
常见报错解析:“Access Not Configured”故障排除指南 许多开发者和团队成员在使用OpenClaw集成飞书时,都曾遭遇过一个典型的中断提示:“access not configured”(访问未配置)。该提示会明确显示您的飞书账户ID及一组唯一的配对验证码,并指出需要联系机器人所有
OpenClaw 常用指令大全与使用详解 openclaw status:此命令是查看OpenClaw系统整体健康状态的核心指令,执行后即获取服务运行状况的全面报告,是日常运维的首要诊断工具。 openclaw gateway restart:在修改网关配置后,必须运行此指令以重启网关服务,使配置文
如何通过 OpenClaw 实现 Chrome 浏览器自动化操控 在软件开发与自动化测试领域,持续学习是常态。本文旨在详细介绍如何利用 OpenClaw 连接并控制一个已开启的 Chrome 浏览器实例,实现点击、文本输入、文件上传、页面滚动、屏幕截图以及执行 JavaScript 等自动化操作。整
项目概述 你是否希望将强大的 AI 助手带入日常聊天?本教程将指导你完成搭建流程,让你能在 QQ 上直接调用 OpenClaw 智能助手,实现无门槛的 AI 对话体验。 架构说明 ┌─────────────┐ ┌──────────────┐ ┌─────────────┐ │ QQ 用户 │ ─
一 下载并安装Node js,全程保持默认设置 首先,请前往Node js官方网站的下载中心:https: nodejs org zh-cn download。根据您的操作系统(Windows Mac Linux)下载对应的安装程序。运行安装向导时,整个过程非常简单,您只需连续点击“下一步”按钮
热门专题
热门推荐
速览攻略:世界圣羽翼王核心打法与全面解析 本攻略将为你完整呈现《洛克王国》世界圣羽翼王的通关秘籍,深度剖析两种高效实战打法:追求极致速度的“燃薪虫四回合速通”与稳定输出的“酷拉无限连击流”。文章将进一步解析这位翼系精灵王的技能机制、属性克制关系及其在PVE与PVP中的实战定位,帮助你彻底掌握应对其隐
速览:工程系统核心机制解析 在《异种航员2》中,工程系统是整个抵抗力量赖以运转的“战略后勤中枢”。无论是研发新武器、生产重型装甲还是制造先进飞行器,所有实体装备的产出都依赖于此。简言之,该系统的核心运作围绕着两大关键:工程师人力的高效配置与全球稀缺资源的精细化调度。工程师的数量直接决定了每个项目的建
核心速览 在《洛克王国世界》中,治愈兔是一位兼具功能性任务角色与实战辅助能力的精灵。它的价值不仅在剧情推进中体现,更在于对战里出色的治疗与防护表现。本文将为你全面解析治愈兔的精准获取位置、种族属性特点以及实战技能搭配,助你顺利捕捉并最大化其在队伍中的作用。所有关键信息将通过清晰的图文内容详细展示,确
速览 在《红色沙漠》中,挑战传说之狼这一强大的任务BOSS,需要玩家进行充分的准备并遵循完整的任务流程。整个过程环环相扣,你必须首先参与塞莱斯特家族的势力任务,通过完成任务将家族声望提升至指定等级,才能解锁【传说之狼】的专属讨伐任务,最终直面这个传说中的强大生物。 红色沙漠传说之狼怎么打 归根结底,
【宝可梦Pokopia】舒适度全解析:快速提升环境等级的核心秘诀 你是否正在探索《宝可梦Pokopia》世界,并希望有效提升宝可梦栖息地的舒适度?舒适度不仅是衡量宝可梦快乐程度的晴雨表,更是解锁游戏核心内容、加速发展的关键驱动指标。本攻略将系统性地为你揭示提升舒适度的核心途径,涵盖从装饰栖息地、建造





