当前位置:   article > 正文

基于yolov5的PCB缺陷检测,引入CVPR 2023 BiFormer:Vision Transformer with Bi-Level Routing Attention提升检测精度_yolo缺陷数据集

yolo缺陷数据集

目录

1.PCB数据集介绍

1.1 通过split_train_val.py得到trainval.txt、val.txt、test.txt

 1.2 通过voc_label.py得到适合yolov5训练需要的

2.基于Yolov5 的PCB缺陷识别

2.1配置 pcb.yaml

 2.2 修改yolov5s_pcb.yaml

2.3 超参数修改train.py

3.实验结果分析

3.1  CVPR 2023 BiFormer: 基于动态稀疏注意力构建高效金字塔网络架构


1.PCB数据集介绍

PCB是最具竞争力的产业之一,其产品的优良则关系到企业的发展。由于产品外观缺陷的种类非常广泛,所以较一般电子零部件的缺陷检测更加困难。PCB 板缺陷包括短路、多铜及少铜、断路、缺口、毛刺等。利用深度学习技术采用人工智能学习PCB图像,可以分析复杂的图像,大幅提升自动化视觉检测的图像判读能力和准确度,并可将缺陷进行分类。针对不同产品不同的缺陷标准,智能系统能够灵活应对。

PCB数据集共有六种缺陷,分别是"missing_hole","mouse_bite","open_circuit","short","spur","spurious_copper",缺陷属于小目标缺陷检测

下图为每个类别的数据量、标签,center xy, labels 标签的长和宽

1.1 通过split_train_val.py得到trainval.txt、val.txt、test.txt

  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()

 1.2 通过voc_label.py得到适合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 convert_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),
  38. float(xmlbox.find('ymax').text))
  39. b1, b2, b3, b4 = b
  40. # 标注越界修正
  41. if b2 > w:
  42. b2 = w
  43. if b4 > h:
  44. b4 = h
  45. b = (b1, b2, b3, b4)
  46. bb = convert((w, h), b)
  47. out_file.write(str(cls_id) + " " + " ".join([str(a) for a in bb]) + '\n')
  48. wd = getcwd()
  49. for image_set in sets:
  50. if not os.path.exists('labels/'):
  51. os.makedirs('labels/')
  52. image_ids = open('ImageSets/Main/%s.txt' % (image_set)).read().strip().split()
  53. list_file = open('%s.txt' % (image_set), 'w')
  54. for image_id in image_ids:
  55. list_file.write(abs_path + '/images/%s.jpg\n' % (image_id))
  56. convert_annotation(image_id)
  57. list_file.close()

2.基于Yolov5 的PCB缺陷识别

2.1配置 pcb.yaml

 2.2 修改yolov5s_pcb.yaml

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