本文介绍用目标检测算法替代人工进行PCB电路板故障检测。先安装PaddleDetection并准备数据,用分层采样拆分训练集和验证集。训练时可选多种算法,以yolov3为例,还可边训练边测试并可视化。之后用infer.py推理,也能导出模型部署。
☞☞☞AI 智能聊天, 问答助手, AI 智能搜索, 免费无限量使用 DeepSeek R1 模型☜☜☜

PCB质检通常由产线工人使用显微放大镜在强光下目检,效率较低,且长期在强光显微镜下观察PCB板容易对眼睛产生伤害,同时也容易疲劳出错。通过下图可以看出这个检测空间非常狭小,人工检测效率效率低不说,工人也非常辛苦!可以让计算机视觉去执行的任务何必耗费人力,大伙们说是吧!
因此,我们提出用目标检测算法去代替人工检测,简单来说,就是要使用PaddleDetection去检测PCB电路板生产时可能会出现的问题,一共有六种,数据集已经由官方标注好。

# 通过git安装PaddleDetection# github: https://github.com/PaddlePaddle/PaddleDetection.git# gitee: https://gitee.com/paddlepaddle/PaddleDetection.git!cd ~/work/ && git clone https://gitee.com/paddlepaddle/PaddleDetection.git
Cloning into 'PaddleDetection'... remote: Enumerating objects: 14575, done. remote: Counting objects: 100% (14575/14575), done. remote: Compressing objects: 100% (6264/6264), done. remote: Total 14575 (delta 10731), reused 11458 (delta 8175), pack-reused 0 Receiving objects: 100% (14575/14575), 132.71 MiB | 4.95 MiB/s, done. Resolving deltas: 100% (10731/10731), done. Checking connectivity... done.
# 准备数据,将数据解压到PaddleDetection工程中!cd ~/data/data73416/ && unzip -o PCB_DEFECT.zip -d ~/work/PaddleDetection/dataset/
# 使用分层采样方法拆分数据集分别用来训练和验证import randomimport os# 对损坏类型分类total = 'work/PaddleDetection/dataset/voc/total.txt'with open(total, 'r') as origin:
lines = origin.readlines() # 创建分类字典
classify_dict = {}
classify_dict['Missing_hole'] = [line for line in lines if 'Missing_hole' in line]
classify_dict['Mouse_bite'] = [line for line in lines if 'Mouse_bite' in line]
classify_dict['Open_circuit'] = [line for line in lines if 'Open_circuit' in line]
classify_dict['Short'] = [line for line in lines if 'Short' in line]
classify_dict['Spur'] = [line for line in lines if 'Spurious_copper' in line]
classify_dict['Spurious_copper'] = [line for line in lines if 'Spur' in line and 'Spurious_copper' not in line]#从每种损坏类别随机选80%作为训练集,20%为测试集train_file = 'work/PaddleDetection/dataset/voc/trainval.txt'eval_file = 'work/PaddleDetection/dataset/voc/test.txt'train_f = open(train_file, 'w')
eval_f = open(eval_file, 'w')for damage_type in classify_dict:
data = classify_dict[damage_type]
length = len(data)
random.shuffle(data)
eval_num = length // 5
train_f.writelines(data[eval_num:])
eval_f.writelines(data[:eval_num])
train_f.close()
eval_f.close()训练之前先来看看PaddleDetection涵盖的基本算法
从图中可以看出从精度和性能综合考虑,最强的是PP-YOLO v2,但是作者这里先不用他,希望由读者来亲自体验他的强大。
使用PaddleDetection/tools下的train.py来训练文件
常见训练参数:
| FLAG | 用途 | 备注 |
|---|---|---|
| -c | 指定配置文件 | 必选,例如-c configs/yolov3/yolov3_darknet53_270e_voc.yml |
| -o | 设置或更改配置文件里的参数内容 | 相较于-c设置的配置文件有更高优先级,例如:-o use_gpu=False |
| --eval | 是否边训练边测试 | 如需指定,直接--eval即可 |
| -r/--resume_checkpoint | 恢复训练加载的权重路径 | 例如:-r output/faster_rcnn_r50_1x_coco/10000 |
| --use_vdl | 是否使用VisualDL记录数据,进而在VisualDL面板中显示 | VisualDL需Python>=3.5 |
关于配置文件参数,用户可根据自己喜好调整参数,参数说明:https://gitee.com/paddlepaddle/PaddleDetection/blob/release/2.0/docs/tutorials/config_annotation/ppyolo_r50vd_dcn_1x_coco_annotation.md
# 开始训练,这里作者没用最强的PP-YOLO v2,读者可以自行更改体验一下PP-YOLO v2的强大# configs中提供多种不同的配置,可以根据文档尝试使用!cd ~/work/PaddleDetection/ && python tools/train.py -c configs/yolov3/yolov3_darknet53_270e_voc.yml --eval --use_vdl=True
vdl训练数据可视化:
检测用的脚本文件infer.py就在PaddleDetection下的tools中
# 尝试一下用自己训练出来的模型推理一下指定图片!cd ~/work/PaddleDetection/ && python tools/infer.py -c configs/yolov3/yolov3_darknet53_270e_voc.yml --infer_img=dataset/voc/rotation/Missing_hole_rotation/06_missing_hole_01.jpg
/opt/conda/envs/python35-paddle120-env/lib/python3.7/site-packages/paddle/tensor/creation.py:125: DeprecationWarning: `np.object` is a deprecated alias for the builtin `object`. To silence this warning, use `object` by itself. Doing this will not modify any behavior and is safe. Deprecated in NumPy 1.20; for more details and guidance: https://numpy.org/devdocs/release/1.20.0-notes.html#deprecations if data.dtype == np.object: W0607 12:56:04.457180 10141 device_context.cc:404] Please NOTE: device: 0, GPU Compute Capability: 7.0, Driver API Version: 10.1, Runtime API Version: 10.1 W0607 12:56:04.461891 10141 device_context.cc:422] device: 0, cuDNN Version: 7.6. [06/07 12:56:07] ppdet.utils.checkpoint INFO: Finish loading model weights: output/yolov3_mobilenet_v1_270e_voc/5.pdparams [06/07 12:56:08] ppdet.engine INFO: Detection bbox results save in output/06_missing_hole_01.jpg
推理出来的图片存放路径output/06_missing_hole_01.jpg,来看看自己训练出来的模型检测目标的效果如何:
# 如果想导出模型到端上部署,可以执行这个文件:!cd ~/work/PaddleDetection/ && python tools/export_model.py -c configs/yolov3/yolov3_mobilenet_v1_270e_voc.yml --output_dir=./inference_model
以上就是软件杯高职组赛题:PCB电路板故障检测的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号