配电变压器检测图像数据集,总共3000张左右图片,标注为voc格式
配电变压器检测图像数据集(Distribution Transformer Detection Dataset, DTD)
摘要
配电变压器检测图像数据集(DTD)是一个专为配电变压器检测任务设计的数据集,包含大约3000张高质量的图像,每张图像都标注了配电变压器的位置。该数据集采用VOC格式进行标注,适用于目标检测任务。数据集旨在帮助研究人员和工程师开发更精确的配电变压器检测模型,应用于电力系统的自动化监控、维护和故障检测等领域。
数据集特点
- 单类别:数据集中只有一个类别,即
Transformer
。 - 高质量图像:图像分辨率高,清晰度好,能够准确反映配电变压器的情况。
- 详细的标注信息:每张图像都经过专业人员的手动标注,确保了标注框的准确性。
- 标准化格式:数据集采用PASCAL VOC格式进行标注,便于使用现有的目标检测框架。
- 易于使用:数据集已经划分好了训练集、验证集和测试集,可以直接用于训练目标检测模型。
数据集构成
- 总图像数量:约3000张
- 类别数:1类(
Transformer
) - 数据集划分:
- 训练集: 约2400张
- 验证集: 约300张
- 测试集: 约300张
- 数据量:具体大小取决于图像的分辨率,但通常在几GB左右。
数据集结构
数据集的文件结构如下:
distribution_transformer_dataset/
├── images/
│ ├── train/
│ ├── val/
│ └── test/
└── annotations/
├── train/
├── val/
└── test/
images/
目录下存放图像文件。annotations/
目录下存放对应的VOC格式的XML标注文件。
每个XML标注文件包含一个或多个<object>
标签,每个标签描述了一个配电变压器的边界框。例如:
<annotation>
<folder>train</folder>
<filename>000001.jpg</filename>
<size>
<width>800</width>
<height>600</height>
<depth>3</depth>
</size>
<object>
<name>Transformer</name>
<bndbox>
<xmin>100</xmin>
<ymin>150</ymin>
<xmax>300</xmax>
<ymax>400</ymax>
</bndbox>
</object>
</annotation>
示例代码
以下是一个详细的Python脚本示例,用于加载数据集中的一对图像-标签对,并可视化其中的标注信息。此外,还包括了如何使用常用的YOLOv5进行训练的基本步骤。
加载并可视化图像与标签
import os
import cv2
import xml.etree.ElementTree as ET
import matplotlib.pyplot as plt
from matplotlib.patches import Rectangle
# 数据集目录路径
data_dir = 'path/to/dtd_dataset'
train_image_dir = os.path.join(data_dir, 'images/train')
train_label_dir = os.path.join(data_dir, 'annotations/train')
# 选取一张训练图像及其对应标签
image_files = os.listdir(train_image_dir)
image_file = image_files[0] # 假设取第一张图
label_file = os.path.splitext(image_file)[0] + '.xml' # 假设是XML格式的标签
image_path = os.path.join(train_image_dir, image_file)
label_path = os.path.join(train_label_dir, label_file)
# 加载图像
image = cv2.imread(image_path, cv2.IMREAD_COLOR)
height, width, _ = image.shape
# 解析VOC格式标签
def parse_voc_label(label_path):
tree = ET.parse(label_path)
root = tree.getroot()
bboxes = []
for obj in root.findall('object'):
name = obj.find('name').text
bbox = obj.find('bndbox')
xmin = int(bbox.find('xmin').text)
ymin = int(bbox.find('ymin').text)
xmax = int(bbox.find('xmax').text)
ymax = int(bbox.find('ymax').text)
bboxes.append((name, xmin, ymin, xmax, ymax))
return bboxes
# 解析标签
bboxes = parse_voc_label(label_path)
# 可视化标注
fig, ax = plt.subplots(figsize=(10, 10))
ax.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
colors = ['#FFA500'] # 仅有一个类别,选择一个颜色
names = ['Transformer'] # 类别名称
for bbox, color_name in zip(bboxes, colors):
name, x_min, y_min, x_max, y_max = bbox
rect = Rectangle((x_min, y_min), x_max - x_min, y_max - y_min, linewidth=2, edgecolor=color_name, facecolor='none')
ax.add_patch(rect)
ax.text(x_min, y_min - 10, names[0], color=color_name, fontsize=8)
plt.title('Distribution Transformer Detection Dataset')
plt.axis('off')
plt.show()
使用YOLOv5进行训练
假设你已经安装了YOLOv5,并且配置文件(例如dtd.yaml
)已经准备好,以下是使用YOLOv5进行训练的基本步骤:
-
克隆YOLOv5仓库(如果尚未完成):
git clone https://github.com/ultralytics/yolov5 cd yolov5
-
安装依赖:
pip install -r requirements.txt
-
创建数据配置文件(如果尚未创建): 创建一个名为
dtd.yaml
的文件,内容如下:train: path/to/dtd_dataset/images/train val: path/to/dtd_dataset/images/val test: path/to/dtd_dataset/images/test nc: 1 # 类别数 names: ['Transformer']
-
转换VOC格式到YOLO格式: YOLOv5需要的是YOLO格式的标签文件,因此需要将VOC格式的标签转换为YOLO格式。可以使用以下脚本来实现这一转换:
import os import xml.etree.ElementTree as ET def convert_voc_to_yolo(voc_label_path, image_width, image_height): tree = ET.parse(voc_label_path) root = tree.getroot() yolo_labels = [] for obj in root.findall('object'): name = obj.find('name').text bbox = obj.find('bndbox') xmin = int(bbox.find('xmin').text) ymin = int(bbox.find('ymin').text) xmax = int(bbox.find('xmax').text) ymax = int(bbox.find('ymax').text) x_center = (xmin + xmax) / 2.0 / image_width y_center = (ymin + ymax) / 2.0 / image_height box_width = (xmax - xmin) / image_width box_height = (ymax - ymin) / image_height yolo_labels.append(f"0 {x_center} {y_center} {box_width} {box_height}") return yolo_labels def convert_voc_to_yolo_for_directory(voc_dir, image_dir, output_dir): if not os.path.exists(output_dir): os.makedirs(output_dir) for voc_file in os.listdir(voc_dir): if not voc_file.endswith('.xml'): continue voc_path = os.path.join(voc_dir, voc_file) image_file = os.path.splitext(voc_file)[0] + '.jpg' image_path = os.path.join(image_dir, image_file) image = cv2.imread(image_path, cv2.IMREAD_COLOR) height, width, _ = image.shape yolo_labels = convert_voc_to_yolo(voc_path, width, height) yolo_file = os.path.splitext(voc_file)[0] + '.txt' yolo_path = os.path.join(output_dir, yolo_file) with open(yolo_path, 'w') as f: f.write('\n'.join(yolo_labels)) # 转换训练集 convert_voc_to_yolo_for_directory( 'path/to/dtd_dataset/annotations/train', 'path/to/dtd_dataset/images/train', 'path/to/dtd_dataset/labels/train' ) # 转换验证集 convert_voc_to_yolo_for_directory( 'path/to/dtd_dataset/annotations/val', 'path/to/dtd_dataset/images/val', 'path/to/dtd_dataset/labels/val' )
-
开始训练:
python train.py --img 640 --batch 16 --epochs 100 --data dtd.yaml --weights yolov5s.pt
其中,
--img 640
指定输入图像大小,--batch 16
指定批量大小,--epochs 100
指定训练轮数,--data dtd.yaml
指定数据配置文件,--weights yolov5s.pt
指定预训练权重。 -
评估模型: 训练完成后,你可以使用以下命令来评估模型性能:深色版本
-
python detect.py --source path/to/your_test_images --weights runs/train/exp/weights/best.pt --save-txt --save-conf --save-crop
通过上述步骤,你可以轻松地使用这个数据集来训练一个高效的配电变压器检测模型。