基于PaddleX的齿轮瑕疵检测

举报
livingbody 发表于 2022/11/22 00:51:14 2022/11/22
【摘要】 一、基于PaddleX的齿轮瑕疵检测 1.赛事背景说明近年来,AI+工业瑕疵检测已成为工业智能领域的重要应用场景,能够进一步提升工业检测效率和精度、降低人力成本。本赛题选取齿轮配件异常检测作为AI+工业瑕疵检测比赛场景,鼓励选手通过机器视觉技术提升齿轮异常检测速度和准确率。齿轮配件异常检测是工业瑕疵检测的痛点场景。齿轮传动装置是机械装备的重要基础件,与带链、摩擦、液压等传动方式相比,具有功...

一、基于PaddleX的齿轮瑕疵检测

1.赛事背景说明

近年来,AI+工业瑕疵检测已成为工业智能领域的重要应用场景,能够进一步提升工业检测效率和精度、降低人力成本。本赛题选取齿轮配件异常检测作为AI+工业瑕疵检测比赛场景,鼓励选手通过机器视觉技术提升齿轮异常检测速度和准确率。

齿轮配件异常检测是工业瑕疵检测的痛点场景。齿轮传动装置是机械装备的重要基础件,与带链、摩擦、液压等传动方式相比,具有功率范围大、传动效率高、运动平稳、传动比准确、使用寿命长、结构紧凑等特点,同时安全、可靠、性价比优越的优点,决定了它在通用机械装备领域中的不可替代性。齿轮作为一种典型的动力传递器件,其质量的好坏直接影响着机械产品性能。

目前机器视觉行业依然由少数国际龙头垄断。美国康耐视(cognex)及日本基恩士(Keyence)几乎垄断全球 50%以上的视觉检测市场,两者均基于核心零部件和技术(操作系统、传感器等)提供相应解决方案。国内机器视觉检测方案虽已有长足发展,但与世界巨头相比仍存较大差距。因此,齿轮异常检测任务对于提升我国工业质检效率,保障产品质量具有重要意义。

2.赛题任务

赛题链接

赛题选取制造领域的齿轮配件异常检测场景,提供真实生产环境数据集,要求基于百度飞桨国产开发框架构建算法模型,比拼算法精度、召回率等指标,从而提升国产框架在工业智能领域的应用能力,解决企业实际生产痛点问题。参赛团队构建算法模型,实现从测试数据集中自动检测齿面黑皮、齿底黑皮、磕碰三类缺陷的目标。

3.数据集介绍

本任务数据集为一汽红旗汽车提供的齿轮配件在生产加工中的真实数据,所有数据在生产流水线中拍摄而得。

根据赛事要求为确保数据隐私性,数据仅可通过赛题平台下载,下载后仅可用于本次比赛,禁止在其他途径传播,各位读者可以到比赛页面注册登录,然后点击页面最下方的【数据下载】获得任务数据集。

数据集中的图片均为真实缺陷齿轮的平面展开图,并由专业人员标注。样图中会明确标识影像中所包含的缺陷和识别线类型。

下面从左至右分别为齿轮示意图、原始图像和标注后的例图:

下面是典型缺陷的局部放大图:



训练数据文件结构

将提供用于训练的图像数据和识别标签,文件夹结构:

|-- Images/train # 存放测试图像数据,jpeg编码图像文件

|-- Annotations # 存放属性标签标注数据

数据标注文件的结构上,属于比较标注的coco格式标注。

二、数据处理

1.标注文件整理

数据集文件名是中文,解压的时候要指定编码(抄坑总,确实好,记住了)

# 数据集解压缩:读者可以将数据集上传到AI Studio,再根据实际项目的具体路径,解压数据集
# 注意由于数据集文件名是中文,解压的时候要指定编码(也可以本地对数据集改名后再上传)
!unzip -qoa -O GBK data/data163113/齿轮检测数据集.zip -d ./data/
# 整理数据集结构
!mv data/齿轮检测数据集/train/train_coco.json data/齿轮检测数据集/
!rm data/齿轮检测数据集/train/Thumbs.db
!mkdir data/JPEGImages
!mv data/齿轮检测数据集/train/*.jpg data/JPEGImages/
!mv data/齿轮检测数据集/train_coco.json data/annotations.json
!rm data/齿轮检测数据集 -rf
# 统计文件数量
import glob

# 加载训练集路径
img_dir = 'data/JPEGImages/'
# 加载训练集图片目录
train_imgs = glob.glob(img_dir + '/*.jpg')
print('数据集图片数量: {}'.format(len(train_imgs)))
数据集图片数量: 2000
!dir data -l
总用量 7176
-rw-r--r-- 1 aistudio aistudio 7177387 6月  23 10:35 annotations.json
drwxrwxrwx 6 aistudio aistudio    4096 8月  28 09:39 data163113
drwxr-xr-x 2 aistudio aistudio  159744 8月  28 09:53 JPEGImages
  • 经查看,图片有2000张
  • 格式整理为PaddleX常用的coco格式

2.PaddleX安装

!python -m pip install --upgrade -q pip --user
!pip install -q -U paddlex

3.数据集划分

# 按比例切分数据集
!paddlex --split_dataset --format COCO --dataset_dir data --val_value 0.2
  • Train samples: 1600
  • Eval samples: 400

三、模型训练

1.transforms定义

# 定义训练和验证时的transforms
# API说明:https://github.com/PaddlePaddle/PaddleX/blob/develop/docs/apis/transforms/transforms.md

import paddlex as pdx
from paddlex import transforms as T

train_transforms = T.Compose([
    # T.MixupImage(mixup_epoch=-1), 
    T.RandomDistort(),
    T.RandomHorizontalFlip(), 
    T.RandomVerticalFlip(),         
    T.BatchRandomResize(
        target_sizes=[320, 352, 384, 416, 448, 480, 512, 544, 576, 608],
        interp='RANDOM'), 
    # T.Resize(target_size=224, interp='LINEAR'),
    T.Normalize(
            mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])

eval_transforms = T.Compose([
    T.Resize(
        224, interp='CUBIC'), T.Normalize(
            mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
])
[08-28 10:02:45 MainThread @utils.py:79] WRN paddlepaddle version: 2.3.1. The dynamic graph version of PARL is under development, not fully tested and supported


/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/parl/remote/communication.py:38: DeprecationWarning: 'pyarrow.default_serialization_context' is deprecated as of 2.0.0 and will be removed in a future version. Use pickle or the pyarrow IPC functionality instead.
  context = pyarrow.default_serialization_context()
/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
2022-08-28 10:02:46,648-WARNING: type object 'QuantizationTransformPass' has no attribute '_supported_quantizable_op_type'
2022-08-28 10:02:46,650-WARNING: If you want to use training-aware and post-training quantization, please use Paddle >= 1.8.4 or develop version

2.数据集定义

# 定义训练和验证所用的数据集
# API说明:https://github.com/PaddlePaddle/PaddleX/blob/develop/docs/apis/datasets.md

train_dataset = pdx.datasets.CocoDetection(
    data_dir='data/JPEGImages',
    ann_file='data/train.json',
    transforms=train_transforms,
    shuffle=True)

eval_dataset = pdx.datasets.CocoDetection(
    data_dir='data/JPEGImages',
    ann_file='data/val.json',
    transforms=eval_transforms)
loading annotations into memory...
Done (t=0.30s)
creating index...
index created!
2022-08-28 10:06:35 [INFO]	Starting to read file list from dataset...
2022-08-28 10:06:35 [INFO]	1121 samples in file data/train.json, including 1121 positive samples and 0 negative samples.
loading annotations into memory...
Done (t=0.02s)
creating index...
index created!
2022-08-28 10:06:35 [INFO]	Starting to read file list from dataset...
2022-08-28 10:06:35 [INFO]	277 samples in file data/val.json, including 277 positive samples and 0 negative samples.

3.模型定义

# YOLO检测模型的预置anchor生成
# API说明: https://github.com/PaddlePaddle/PaddleX/blob/release/2.0.0/paddlex/tools/anchor_clustering/yolo_cluster.py
import numpy as np
anchors = train_dataset.cluster_yolo_anchor(num_anchors=9, image_size=480)
anchor_masks = [[6, 7, 8], [3, 4, 5], [0, 1, 2]]

# 初始化模型,并进行训练
# 可使用VisualDL查看训练指标,参考https://github.com/PaddlePaddle/PaddleX/tree/release/2.0.0/tutorials/train#visualdl可视化训练指标
num_classes = len(train_dataset.labels)
model = pdx.det.YOLOv3(
    num_classes=num_classes,
    backbone='DarkNet53',
    anchors=anchors.tolist() if isinstance(anchors, np.ndarray) else anchors,
    anchor_masks=[[6, 7, 8], [3, 4, 5], [0, 1, 2]],
    label_smooth=True,
    ignore_threshold=0.6)
100%|██████████| 1121/1121 [00:00<00:00, 20013.00it/s]


2022-08-28 10:07:25 [WARNING]	Extremely small objects found. 32 of 22857 labels are < 3 pixels in width or height
2022-08-28 10:07:25 [INFO]	Running kmeans for 9 anchors on 22857 points...


Evolving anchors with Genetic Algorithm: fitness = 0.7917: 100%|██████████| 1000/1000 [00:05<00:00, 171.65it/s]
W0828 10:07:38.093415  1037 gpu_resources.cc:61] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 11.2, Runtime API Version: 10.1
W0828 10:07:38.097923  1037 gpu_resources.cc:91] device: 0, cuDNN Version: 7.6.

4.模型训练

主要是batch size 选择,按比例调整,尽可能利用好显存。

# API说明:https://github.com/PaddlePaddle/PaddleX/blob/release/2.0.0/paddlex/cv/models/detector.py
# 各参数介绍与调整说明:https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html
model.train(
    num_epochs=300,  # 训练轮次
    train_dataset=train_dataset,  # 训练数据
    eval_dataset=eval_dataset,  # 验证数据
    train_batch_size=20,  # 批大小
    pretrain_weights='COCO',  # 预训练权重
    learning_rate=0.005 / 12,  # 学习率
    warmup_steps=500,  # 预热步数
    warmup_start_lr=0.0,  # 预热起始学习率
    save_interval_epochs=5,  # 每5个轮次保存一次,有验证数据时,自动评估
    lr_decay_epochs=[85, 135],  # step学习率衰减
    save_dir='output/yolov3_darknet53',  # 保存路径
    use_vdl=True)  # 其用visuadl进行可视化训练记录
2022-08-28 10:09:04 [INFO]	Downloading yolov3_darknet53_270e_coco.pdparams from https://paddledet.bj.bcebos.com/models/yolov3_darknet53_270e_coco.pdparams


100%|██████████| 242236/242236 [00:52<00:00, 4637.96KB/s] 


2022-08-28 10:09:57 [INFO]	Loading pretrained model from output/yolov3_darknet53/pretrain/yolov3_darknet53_270e_coco.pdparams
2022-08-28 10:09:57 [WARNING]	[SKIP] Shape of pretrained params yolo_head.yolo_output.0.weight doesn't match.(Pretrained: [255, 1024, 1, 1], Actual: [24, 1024, 1, 1])
2022-08-28 10:09:57 [WARNING]	[SKIP] Shape of pretrained params yolo_head.yolo_output.0.bias doesn't match.(Pretrained: [255], Actual: [24])
2022-08-28 10:09:57 [WARNING]	[SKIP] Shape of pretrained params yolo_head.yolo_output.1.weight doesn't match.(Pretrained: [255, 512, 1, 1], Actual: [24, 512, 1, 1])
2022-08-28 10:09:57 [WARNING]	[SKIP] Shape of pretrained params yolo_head.yolo_output.1.bias doesn't match.(Pretrained: [255], Actual: [24])
2022-08-28 10:09:57 [WARNING]	[SKIP] Shape of pretrained params yolo_head.yolo_output.2.weight doesn't match.(Pretrained: [255, 256, 1, 1], Actual: [24, 256, 1, 1])
2022-08-28 10:09:57 [WARNING]	[SKIP] Shape of pretrained params yolo_head.yolo_output.2.bias doesn't match.(Pretrained: [255], Actual: [24])
2022-08-28 10:09:57 [INFO]	There are 360/366 variables loaded into YOLOv3.
2022-08-28 10:10:22 [INFO]	[TRAIN] Epoch=1/200, Step=10/70, loss_xy=50.468498, loss_wh=53.456619, loss_obj=219.568649, loss_cls=37.263649, loss=360.757416, lr=0.000008, time_each_step=2.4s, eta=9:20:55
2022-08-28 10:10:45 [INFO]	[TRAIN] Epoch=1/200, Step=20/70, loss_xy=62.479233, loss_wh=51.094337, loss_obj=392.185303, loss_cls=43.888462, loss=549.647339, lr=0.000016, time_each_step=2.3s, eta=8:56:44
2022-08-28 10:11:08 [INFO]	[TRAIN] Epoch=1/200, Step=30/70, loss_xy=45.058094, loss_wh=26.950705, loss_obj=401.493713, loss_cls=31.457178, loss=504.959686, lr=0.000024, time_each_step=2.38s, eta=9:15:6
2022-08-28 10:11:31 [INFO]	[TRAIN] Epoch=1/200, Step=40/70, loss_xy=50.634811, loss_wh=23.337137, loss_obj=409.539001, loss_cls=34.183483, loss=517.694458, lr=0.000033, time_each_step=2.24s, eta=8:42:32
2022-08-28 10:11:53 [INFO]	[TRAIN] Epoch=1/200, Step=50/70, loss_xy=19.420666, loss_wh=7.536473, loss_obj=107.944473, loss_cls=13.795206, loss=148.696823, lr=0.000041, time_each_step=2.25s, eta=8:42:54
2022-08-28 10:12:19 [INFO]	[TRAIN] Epoch=1/200, Step=60/70, loss_xy=49.917320, loss_wh=20.835529, loss_obj=197.301407, loss_cls=33.253605, loss=301.307861, lr=0.000049, time_each_step=2.55s, eta=9:52:18
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。