(6)机器学习小白入门 YOLOv:图片的数据预处理
(1)机器学习小白入门YOLOv :从概念到实践
(2)机器学习小白入门 YOLOv:从模块优化到工程部署
(3)机器学习小白入门 YOLOv: 解锁图片分类新技能
(4)机器学习小白入门YOLOv :图片标注实操手册
(5)机器学习小白入门 YOLOv:数据需求与图像不足应对策略
(6)机器学习小白入门 YOLOv:图片的数据预处理
在使用 YOLOv 模型进行目标检测前,图片的数据预处理是非常重要的一环,它决定了你训练出来的模型效果好不好、能不能泛化到实际场景中。下面我为你详细介绍 YOLOv 的数据预处理技术与步骤,帮助你更好地准备用于训练的数据。
一、YOLO 数据集结构要求
1. 常见文件夹结构(如使用 darknet 格式):
yolov_dataset/
│
├── images/ # 放置图片
│ ├── train/
│ └── val/
│
└── labels/ # 对应的标签文件,即 label.txt 文件├── train/└── val/
2. 图片命名要求:
- 所有图片以
.jpg
或者.png
格式存储; train/val
中的图像名称要一致,如:images/train/1.jpg labels/train/1.txtimages/train/2.jpg labels/train/2.txt
3. label.txt 文件格式:
每张图片对应的 label.txt
包含若干行(对应图像中有多少个目标),每一行为如下结构:
class_id x_center y_center width height
x_center, y_center, width, height
:归一化的坐标,范围 0~1。class_id
:目标类别在类列表中的索引编号(从 0 开始)。
🛠️ 二、YOLO 数据预处理技术与步骤
1. 图像标准化(Normalize Image)
将图片尺寸统一到模型训练时使用的大小,例如:
from PIL import Imagedef resize_image(img, target_size=(640, 640)): # YOLOv5 常用输入分辨率return img.resize(target_size)
2. 图像归一化(Normalize Pixel)
YOLO 训练过程中,一般使用以下方式进行图像归一:
import numpy as npdef normalize(img):# 将图片转换为 np.arrayimg_array = np.array(img) / 255.0 # 归一到 [0,1] 区间return img_array
3. 标签标准化处理(Label Normalization)
将标注文件中的 x_center, y_center, width, height
按图像尺寸进行归一化,例如:
def normalize_label(label_path, image_width, image_height):labels = []with open(label_path, 'r') as f:lines = f.readlines()for line in lines:parts = line.strip().split()class_id, x_center, y_center, width, height = map(float, parts)# 归一化到 0~1x_center_norm = x_center / image_widthy_center_norm = y_center / image_heightwidth_norm = width / image_widthheight_norm = height / image_heightlabels.append(f"{int(class_id)} {x_center_norm:.6f} {y_center_norm:.6f} {width_norm:.6f} {height_norm:.6f}")return labels
4. 图像增强(Image Augmentation)(可选,但推荐使用)
图像增强是提高模型泛化能力的利器。你可以采用以下方式进行增强:
使用 albumentations
进行数据增强:
import albumentations as Atransform = A.Compose([A.HorizontalFlip(p=0.5),A.RandomBrightnessContrast(p=0.2),A.Rotate(limit=15, p=0.5),A.Cutout(num_holes=4, max_height=8, max_width=8, fill_value=0, p=0.3)
])def augment_image(img):return transform(image=np.array(img))['image']
5. 分割数据集(Train/Val/Test)
使用 sklearn
或自定义方式划分训练集与验证集:
from sklearn.model_selection import train_test_split# 假设 images_list 是你的图像文件名列表
train_files, val_files = train_test_split(images_list, test_size=0.2, random_state=42)
三、使用 LabelImg 等工具生成标签(可选)
你还可以编写脚本将 .xml
转换为 YOLO 可读的 label.txt
文件:
import xml.etree.ElementTree as ETdef convert_xml_to_yolo(xml_path, img_w, img_h):tree = ET.parse(xml_path)root = tree.getroot()labels = []for obj in root.findall('object'):class_name = obj.find('name').textclass_id = 0 # 根据你自己的类定义填写 class_idbox = obj.find('bndbox')x_min = int(box.find('xmin').text)y_min = int(box.find('ymin').text)x_max = int(box.find('xmax').text)y_max = int(box.find('ymax').text)width = x_max - x_minheight = y_max - y_minxc = (x_min + x_max) / 2 / img_wyc = (y_min + y_max) / 2 / img_hw = width / img_wh = height / img_hlabels.append(f"{class_id} {xc:.6f} {yc:.6f} {w:.6f} {h:.6f}")return labels
四、预处理完整流程图(可选)
你可以使用如下结构进行训练数据的预处理:
五、小结
步骤 | 内容说明 |
---|---|
图像标准化 | 调整图片大小为统一尺寸 |
标签处理 | 将 label.txt 中的坐标归一化到 [0,1] |
数据增强(可选) | 通过旋转、翻转、亮度变换等方式增强多样性 |
分割训练/验证集 | 提升模型泛化能力和评估性能 |