PCB缺陷检测数据集 xml 可转yolo格式 ,共10688张图片

PCB缺陷检测数据集(yolov5,v7,v8) 数据集总共有两个文件夹,一个是pcb整体标注,一个是pcb部分截图。 整体标注有6个分类,开路,短路等都已经标注,标注格式为xml,每个文件夹下有100多张的图片,可转yolo格式,跑过效果很好,北京大学PCB数据集。 第二个是第一个的部分和增强,共10688张图片,多篇pcb论文用的是这个数据集(共6类),训练集和检测集总共有10688张,看最后一张图。标注格式为txt,可直接yolov5,v7,v8检测。

项目概述

本数据集是一个专门用于印刷电路板(PCB)缺陷检测的数据集,包含两个主要部分:一个是整体标注的PCB图像,另一个是部分截图和增强后的图像。整体标注部分有6个分类,包括开路、短路等常见缺陷,并且已经使用XML格式进行了标注。第二个部分是对第一个部分的部分截图和增强处理,共有10688张图像,标注格式为YOLO格式(txt文件),可以直接用于YOLOv5、YOLOv7和YOLOv8模型的训练和检测。

数据集特点

  • 高质量标注:所有标注数据经过处理,确保了标注质量。
  • 多样化类别:涵盖六类常见的PCB缺陷。
  • 多用途:适用于多种目标检测任务,特别是涉及PCB缺陷检测的应用。
  • 易于使用:提供了详细的说明文档和预处理好的标注文件,方便用户快速上手。
  • 学术认可:多篇PCB相关论文使用了该数据集,具有较高的学术价值和实际应用价值。

数据集结构

PCB_Defect_Detection_Dataset/
├── full_boards/                         # 整体标注的PCB图像
│   ├── images/                          # 图像文件夹
│   │   ├── train/                       # 训练集图像
│   │   ├── val/                         # 验证集图像
│   │   └── test/                        # 测试集图像
│   ├── annotations/                     # 标注文件夹 (XML格式)
│   │   ├── train/                       # 训练集标注
│   │   ├── val/                         # 验证集标注
│   │   └── test/                        # 测试集标注
├── partial_and_augmented/               # 部分截图和增强后的图像
│   ├── images/                          # 图像文件夹
│   │   ├── train/                       # 训练集图像
│   │   ├── val/                         # 验证集图像
│   │   └── test/                        # 测试集图像
│   ├── labels/                          # 标注文件夹 (YOLO格式)
│   │   ├── train/                       # 训练集标注
│   │   ├── val/                         # 验证集标注
│   │   └── test/                        # 测试集标注
├── README.md                            # 项目说明文档
└── data.yaml                            # 数据集配置文件

数据集内容

  • 总数据量
    • 整体标注的PCB图像:每个文件夹下约100多张图像。
    • 部分截图和增强后的图像:共10688张图像。
  • 标注格式
    • 整体标注:XML格式。
    • 部分截图和增强:YOLO格式(txt文件)。
  • 标注对象:各类PCB缺陷的位置。
  • 类别及数量
类别名标注个数
开路 (Open Circuit)具体数量
短路 (Short Circuit)具体数量
缺失元件 (Missing Component)具体数量
错误元件 (Wrong Component)具体数量
裂纹 (Crack)具体数量
污染 (Contamination)具体数量
  • 总计
    • 图像总数:整体标注约600张,部分截图和增强10688张
    • 标注总数:具体数量根据实际情况而定
    • 总类别数 (nc):6类

使用说明

  1. 环境准备

    • 确保安装了Python及其相关库(如torchopencv-pythonmatplotlib等)。
    • 下载并解压数据集到本地目录。
    • 安装YOLOv5、YOLOv7或YOLOv8所需的依赖项:
       
      git clone https://github.com/ultralytics/yolov5
      cd yolov5
      pip install -r requirements.txt
  2. 加载数据集

    • 可以使用常见的编程语言(如Python)来加载和处理数据集。
    • 示例代码如下:
import os
import xml.etree.ElementTree as ET
import pandas as pd
from pathlib import Path
from yolov5.utils.datasets import LoadImages, LoadImagesAndLabels
from yolov5.models.experimental import attempt_load
from yolov5.utils.general import non_max_suppression, scale_coords
from yolov5.utils.torch_utils import select_device
import cv2
import numpy as np

# 定义数据集路径
dataset_path = 'PCB_Defect_Detection_Dataset'

# 加载整体标注的图像和标注
def load_full_boards(folder):
    images_folder = os.path.join(dataset_path, 'full_boards', 'images', folder)
    annotations_folder = os.path.join(dataset_path, 'full_boards', 'annotations', folder)
    
    dataset = []
    for image_file in os.listdir(images_folder):
        if image_file.endswith('.jpg') or image_file.endswith('.png'):
            image_path = os.path.join(images_folder, image_file)
            annotation_path = os.path.join(annotations_folder, image_file.replace('.jpg', '.xml').replace('.png', '.xml'))
            
            tree = ET.parse(annotation_path)
            root = tree.getroot()
            labels = []
            for obj in root.findall('object'):
                name = obj.find('name').text
                bndbox = obj.find('bndbox')
                xmin = int(bndbox.find('xmin').text)
                ymin = int(bndbox.find('ymin').text)
                xmax = int(bndbox.find('xmax').text)
                ymax = int(bndbox.find('ymax').text)
                labels.append([name, xmin, ymin, xmax, ymax])
            
            dataset.append({
                'image_path': image_path,
                'labels': labels
            })
    
    return dataset

# 加载部分截图和增强后的图像和标注
def load_partial_and_augmented(folder):
    images_folder = os.path.join(dataset_path, 'partial_and_augmented', 'images', folder)
    labels_folder = os.path.join(dataset_path, 'partial_and_augmented', 'labels', folder)
    
    dataset = []
    for image_file in os.listdir(images_folder):
        if image_file.endswith('.jpg') or image_file.endswith('.png'):
            image_path = os.path.join(images_folder, image_file)
            label_path = os.path.join(labels_folder, image_file.replace('.jpg', '.txt').replace('.png', '.txt'))
            
            with open(label_path, 'r') as f:
                labels = [line.strip().split() for line in f.readlines()]
            
            dataset.append({
                'image_path': image_path,
                'labels': labels
            })
    
    return dataset

# 示例:加载整体标注的训练集
full_boards_train_dataset = load_full_boards('train')
print(f"Number of training images (full boards): {len(full_boards_train_dataset)}")

# 示例:加载部分截图和增强后的训练集
partial_and_augmented_train_dataset = load_partial_and_augmented('train')
print(f"Number of training images (partial and augmented): {len(partial_and_augmented_train_dataset)}")
  1. 模型训练
    • 使用预训练的YOLOv5、YOLOv7或YOLOv8模型进行微调,或者从头开始训练。
    • 示例代码如下(以YOLOv5为例):
# 设置设备
device = select_device('')

# 加载预训练模型或从头开始训练
model = attempt_load('yolov5s.pt', map_location=device)  # 或者 'path/to/custom_model.pt'
model.train()

# 数据集配置文件
data_yaml = 'PCB_Defect_Detection_Dataset/data.yaml'

# 训练参数
hyp = 'yolov5/data/hyps/hyp.scratch.yaml'  # 超参数配置文件
epochs = 100
batch_size = 16
img_size = 640

# 开始训练
%cd yolov5
!python train.py --img {img_size} --batch {batch_size} --epochs {epochs} --data {data_yaml} --weights yolov5s.pt
  1. 模型推理
    • 使用训练好的模型进行推理,并在图像上绘制检测结果。
    • 示例代码如下:
def detect(image_path, model, device, img_size=640):
    img0 = cv2.imread(image_path)
    img = letterbox(img0, new_shape=img_size)[0]
    img = img[:, :, ::-1].transpose(2, 0, 1)  # BGR to RGB, to 3x416x416
    img = np.ascontiguousarray(img)

    img = torch.from_numpy(img).to(device)
    img = img.half() if half else img.float()  # uint8 to fp16/32
    img /= 255.0  # 0 - 255 to 0.0 - 1.0
    if img.ndimension() == 3:
        img = img.unsqueeze(0)

    # 推理
    with torch.no_grad():
        pred = model(img, augment=False)[0]

    # NMS
    pred = non_max_suppression(pred, 0.4, 0.5, classes=None, agnostic=False)
    for i, det in enumerate(pred):  # 每个图像的检测结果
        if det is not None and len(det):
            det[:, :4] = scale_coords(img.shape[2:], det[:, :4], img0.shape).round()
            for *xyxy, conf, cls in reversed(det):
                label = f'{model.names[int(cls)]} {conf:.2f}'
                plot_one_box(xyxy, img0, label=label, color=(0, 255, 0), line_thickness=3)

    return img0

# 示例:检测单张图像
result_img = detect('path/to/image.jpg', model, device)
cv2.imshow('Detection Result', result_img)
cv2.waitKey(0)
cv2.destroyAllWindows()
  1. 性能评估
    • 使用测试集进行性能评估,计算mAP、召回率、精确率等指标。
    • 可以使用YOLOv5自带的评估脚本:
       bash 

      深色版本

      python val.py --data PCB_Defect_Detection_Dataset/data.yaml --weights best.pt --img 640

注意事项

  • 数据格式:确保图像文件和标注文件的命名一致,以便正确匹配。
  • 硬件要求:建议使用GPU进行训练和推理,以加快处理速度。如果没有足够的计算资源,可以考虑使用云服务提供商的GPU实例。
  • 超参数调整:根据实际情况调整网络架构、学习率、批次大小等超参数,以获得更好的性能。

应用场景

  • PCB制造:自动检测PCB上的缺陷,提高生产效率和产品质量。
  • 智能监控:结合自动化生产线,实现对PCB的实时监控和预警。
  • 科研教育:用于PCB缺陷检测研究和教学,提高学生和工程师的专业技能。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

QQ_1309399183

一角两角不嫌少

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值