当前位置:   article > 正文

基于yolov5的PCB缺陷检测_北京大学pcb数据集

北京大学pcb数据集

1.简述

基于图像识别技术实现自动印刷电路板(PCB)缺陷检测,具有高效、准确、可靠、自动化程度高和可扩展性强等优点,大大提高了检测效率,节省了人力成本。传统的检测方法通常需要大量的人力和时间,并且容易受到环境、工人技能和疲劳等因素的影响,而基于图像识别的缺陷检测方法可以实现自动化的缺陷检测,无需人工干预,提高了生产效率。

此外,图像识别技术可以通过不断的学习和训练,不断完善自身的识别能力,适应更多的缺陷类型,具有很强的可扩展性。

2.数据集介绍

使用北京大学公开的PCB数据集,包含1386张图像,具有6种缺陷类型分别为:漏孔、鼠咬、开路、短路、杂散、杂铜。 下载地址:北京大学智能机器人开放实验室

3.数据集构建

先把数据集划分为train, val ,处理代码 split_train_val.py如下:

  1. # -*- coding: utf-8 -*-
  2. import os
  3. import random
  4. import argparse
  5. parser = argparse.ArgumentParser()
  6. #xml文件的地址,根据自己的数据进行修改 xml一般存放在Annotations下
  7. parser.add_argument('--xml_path', default='Annotations', type=str, help='input xml label path')
  8. #数据集的划分,地址选择自己数据下的ImageSets/Main
  9. parser.add_argument('--txt_path', default='ImageSets/Main', type=str, help='output txt label path')
  10. opt = parser.parse_args()
  11. trainval_percent = 0.9
  12. train_percent = 0.8
  13. xmlfilepath = opt.xml_path
  14. txtsavepath = opt.txt_path
  15. total_xml = os.listdir(xmlfilepath)
  16. if not os.path.exists(txtsavepath):
  17. os.makedirs(txtsavepath)
  18. num = len(total_xml)
  19. list_index = range(num)
  20. tv = int(num * trainval_percent)
  21. tr = int(tv * train_percent)
  22. trainval = random.sample(list_index, tv)
  23. train = random.sample(trainval, tr)
  24. file_trainval = open(txtsavepath + '/trainval.txt', 'w')
  25. file_test = open(txtsavepath + '/test.txt', 'w')
  26. file_train = open(txtsavepath + '/train.txt', 'w')
  27. file_val = open(txtsavepath + '/val.txt', 'w')
  28. for i in list_index:
  29. name = total_xml[i][:-4] + '\n'
  30. if i in trainval:
  31. file_trainval.write(name)
  32. if i in train:
  33. file_train.write(name)
  34. else:
  35. file_val.write(name)
  36. else:
  37. file_test.write(name)
  38. file_trainval.close()
  39. file_train.close()
  40. file_val.close()
  41. file_test.close()

然后再把VOC的标签格式转换为yolov5的格式,代码如下:

  1. # -*- coding: utf-8 -*-
  2. import xml.etree.ElementTree as ET
  3. import os
  4. from os import getcwd
  5. sets = ['train', 'val']
  6. classes = ["missing_hole","mouse_bite","open_circuit","short","spur","spurious_copper"] # 改成自己的类别
  7. abs_path = os.getcwd()
  8. print(abs_path)
  9. def convert(size, box):
  10. dw = 1. / (size[0])
  11. dh = 1. / (size[1])
  12. x = (box[0] + box[1]) / 2.0 - 1
  13. y = (box[2] + box[3]) / 2.0 - 1
  14. w = box[1] - box[0]
  15. h = box[3] - box[2]
  16. x = x * dw
  17. w = w * dw
  18. y = y * dh
  19. h = h * dh
  20. return x, y, w, h
  21. def conver_annotation(image_id):
  22. in_file = open('Annotations/%s.xml' % (image_id), encoding='UTF-8')
  23. out_file = open('labels/%s.txt' % (image_id), 'w')
  24. tree = ET.parse(in_file)
  25. root = tree.getroot()
  26. size = root.find('size')
  27. w = int(size.find('width').text)
  28. h = int(size.find('height').text)
  29. for obj in root.iter('object'):
  30. difficult = obj.find('difficult').text
  31. #difficult = obj.find('Difficult').text
  32. cls = obj.find('name').text
  33. if cls not in classes or int(difficult) == 1:
  34. continue
  35. cls_id = classes.index(cls)
  36. xmlbox = obj.find('bndbox')
  37. b = (float(xmlbox.find('xmin').text), float(xmlbox.find('xmax').text), float(xmlbox.find('ymin').text), float(xmlbox.find('ymax').text))
  38. b1, b2, b3, b4 = b
  39. # 标注越界修正
  40. if b2 > w:
  41. b2 = w
  42. if b4 > h:
  43. b4 = h
  44. b = (b1, b2, b3, b4)
  45. bb = convert((w, h), b)
  46. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  47. wd = getcwd()
  48. for image_set in sets:
  49. if not os.path.exists('labels/'):
  50. os.makedirs('labels/')
  51. image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
  52. list_file = open('%s.txt' % (image_set), 'w')
  53. for image_id in image_ids:
  54. list_file.write(abs_path + '/images/%s.jpg\n' % (image_id))
  55. conver_annotation(image_id)
  56. list_file.close()

4.模型训练

(1)创建数据集配置文件

在 yolov5/data/ 目录下创建自己的数据集配置文件 pcb.yaml,内容如下,nc表示类别为6个分类

  1. # PCB data
  2. # Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
  3. train: ./data/PCB_DATASET/train.txt
  4. val: ./data/PCB_DATASET/val.txt
  5. nc: 6 # number of classes
  6. names: ['missing_hole', 'mouse_bite', 'open_circuit', 'short', 'spur', 'spurious_copper']

(2)创建模型文件

在 yolov5/models/ 目录下,拷贝一个版本模型作为自己的模型,这里我使用yolov5s,然后把文件中的类别数改为6即可:

cp yolov5s.yaml yolov5s_pcb.yaml
  1. # Parameters
  2. nc: 6 # number of classes
  3. depth_multiple: 0.33 # model depth multiple
  4. width_multiple: 0.50 # layer channel multiple
  5. anchors:
  6. - [10,13, 16,30, 33,23] # P3/8
  7. - [30,61, 62,45, 59,119] # P4/16
  8. - [116,90, 156,198, 373,326] # P5/32

(3)模型训练

执行以下命令进行训练:

python train.py --img 640 --batch 16 --epochs 100 --data ./data/pcb.yaml --weights yolov5s.pt

经过100 epochs的训练,效果如下:

mAP@0.5 达到了0.953,效果还是不错的。

(4)实测验证效果

 

 


参考:

基于yolov5的PCB缺陷检测,引入CVPR 2023 BiFormer:Vision Transformer with Bi-Level Routing Attention提升检测精度_pcb数据集_AI小怪兽的博客-CSDN博客

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/749306
推荐阅读
相关标签
  

闽ICP备14008679号