CCPD数据集解析:从数据标注到模型训练的全流程实战

1次阅读
没有评论

共计 1905 个字符,预计需要花费 5 分钟才能阅读完成。

image.webp

背景与痛点

CCPD(Chinese City Parking Dataset)是目前国内最常用的车牌识别开源数据集,包含超过 30 万张带标注的车牌图像。数据覆盖不同天气、光照条件和车牌类型(蓝牌、黄牌、新能源等),但实际使用中会遇到几个典型问题:

CCPD 数据集解析:从数据标注到模型训练的全流程实战

  • 标注不一致:部分边界框未紧密贴合车牌边缘,字符级标注存在漏标或错标(如将 ’0’ 标为 ’O’)
  • 样本分布不均:新能源车牌占比不足 5%,偏远省份车牌样本稀缺
  • 噪声干扰:约 3% 的图片存在严重摩尔纹或运动模糊

数据预处理实战

1. 数据清洗

首先过滤无效样本,以下代码演示如何检查标注完整性:

import os
import json
from pathlib import Path

def validate_annotations(annot_dir, img_dir):
    invalid_count = 0
    for annot_file in Path(annot_dir).glob('*.json'):
        with open(annot_file) as f:
            data = json.load(f)

        # 检查图像文件是否存在
        img_path = Path(img_dir) / data['imagePath']
        if not img_path.exists():
            invalid_count += 1
            continue

        # 检查关键字段完整性
        required_fields = ['shapes', 'imageHeight', 'imageWidth']
        if not all(field in data for field in required_fields):
            invalid_count += 1

    print(f'Invalid samples: {invalid_count}/{len(list(Path(annot_dir).glob("*.json")))}')

2. 标注标准化

将不同格式的标注统一为 YOLO 格式:

def ccpd_to_yolo(ccpd_bbox, img_width, img_height):
    """将 CCPD 的 [x1,y1,x2,y2] 格式转为 YOLO 的 [center_x, center_y, w, h] 相对坐标"""
    x_center = ((ccpd_bbox[0] + ccpd_bbox[2]) / 2) / img_width
    y_center = ((ccpd_bbox[1] + ccpd_bbox[3]) / 2) / img_height
    width = (ccpd_bbox[2] - ccpd_bbox[0]) / img_width
    height = (ccpd_bbox[3] - ccpd_bbox[1]) / img_height
    return [x_center, y_center, width, height]

3. 数据增强策略

针对样本不平衡问题,使用 imbalanced-learn 库进行过采样:

from imblearn.over_sampling import RandomOverSampler
import numpy as np

# 假设 features 是特征矩阵,labels 是省份类别
oversample = RandomOverSampler(sampling_strategy='minority')
X_resampled, y_resampled = oversample.fit_resample(features, labels)

模型训练对比

YOLOv5 适配要点

修改 models/yolov5s.yaml 中的 anchor 配置:

anchors:
  - [4,5, 8,10, 13,16]  # 适合车牌的细长型 anchor
  - [23,29, 43,55, 73,105]
  - [146,217, 231,300, 335,433]

Faster R-CNN 关键调整

修改 ROI Head 的输出维度以匹配车牌字符数:

from torchvision.models.detection import fasterrcnn_resnet50_fpn

model = fasterrcnn_resnet50_fpn(pretrained=True)
model.roi_heads.box_predictor.cls_score.out_features = 7  # 车牌字符数 + 背景类

避坑指南

  1. 测试集污染:避免使用同一车辆的多个角度图片分别出现在训练 / 测试集
  2. 过拟合对策:在数据增强中添加随机灰度化(车牌颜色信息不重要)
  3. 评估陷阱:新能源车牌需要单独计算指标,避免被多数类掩盖问题

延伸思考

可以尝试以下混合数据集方案:

  • 阶段式训练:先用 CCPD 训练基础模型,再用 PKUData 微调
  • 联合标注:将两个数据集的标注格式统一后合并训练
  • 迁移学习:用 CCPD 训练检测器,PKUData 训练字符识别模块
正文完
 0
评论(没有评论)