共计 2096 个字符,预计需要花费 6 分钟才能阅读完成。
数据特性分析
CARPK 数据集包含大量俯视角拍摄的停车场车辆图像,主要挑战来自三个方面:小目标密集、遮挡严重以及光照变化。通过 Matplotlib 可视化分析,我们可以更直观地理解这些特点。

- 标注分布分析
- 使用 Pandas 加载标注文件后,统计显示 60% 的车辆目标宽度在 20~50 像素之间(小目标)
-
绘制 bbox 宽高散点图可见明显聚类现象(适合 K -means++ 生成 Anchor)
-
尺度变化示例
plt.figure(figsize=(12,6)) plt.scatter(df['width'], df['height'], alpha=0.3) plt.title('CARPK BBox Distribution') plt.xlabel('Width(pixels)') plt.ylabel('Height(pixels)') -
遮挡情况统计
- 约 35% 的车辆存在 20% 以上的遮挡
- 采用可视化方法标注遮挡程度(红色半透明蒙版)
技术方案对比
在 CARPK 验证集上的 benchmark 对比(输入尺寸 640×640):
| 模型 | mAP@0.5 | FPS | 显存占用 |
|---|---|---|---|
| Faster R-CNN | 78.2 | 8.7 | 5.2GB |
| RetinaNet | 82.1 | 14.3 | 4.1GB |
| YOLOv5s | 84.6 | 45.6 | 2.8GB |
| YOLOv5x | 89.3 | 23.4 | 6.5GB |
实际测试表明,单阶段检测器更适合密集小目标场景。YOLOv5 在精度和速度的平衡上表现最佳。
核心实现细节
改进的 Anchor 生成
传统 K -means 对 CARPK 效果不佳,改进方案:
def kmeans_plusplus(boxes: np.ndarray, k: int) -> np.ndarray:
"""
改进的 Anchor 生成算法
:param boxes: (N,2) 宽高数组
:param k: 聚类中心数
:return: (k,2) Anchor 尺寸
"""
# 1. 基于最大最小距离初始化中心点
centers = [boxes[np.random.randint(len(boxes))]]
for _ in range(1, k):
dists = np.min([np.linalg.norm(boxes - c, axis=1) for c in centers], axis=0)
new_center = boxes[np.argmax(dists)]
centers.append(new_center)
# 2. 标准 K -means 迭代
while True:
clusters = [[] for _ in range(k)]
for box in boxes:
distances = [np.linalg.norm(box - c) for c in centers]
cluster_idx = np.argmin(distances)
clusters[cluster_idx].append(box)
new_centers = [np.mean(c, axis=0) if c else centers[i]
for i, c in enumerate(clusters)]
if np.allclose(centers, new_centers, atol=1e-6):
break
centers = new_centers
return np.array(centers)
数据增强策略
使用 Albumentations 组合光照和遮挡增强:
aug = Compose([RandomBrightnessContrast(p=0.5),
RandomShadow(p=0.3),
GridDropout(ratio=0.1, random_offset=True, p=0.5),
HueSaturationValue(hue_shift_limit=10, sat_shift_limit=20, val_shift_limit=10, p=0.5),
], bbox_params=BboxParams(format='pascal_voc'))
检测头改进
针对小目标优化 YOLOv5 结构:
- 在 Backbone 末层增加 160×160 的检测头
- 采用 BiFPN 替换原 PANet
- 使用深度可分离卷积减少参数量
性能验证
在 Tesla T4 上的性能测试(batch_size=8):
| 输入尺寸 | mAP@0.5 | FPS |
|---|---|---|
| 320×320 | 82.1 | 68.3 |
| 640×640 | 89.3 | 31.7 |
| 1024×1024 | 91.2 | 14.5 |
推荐实际部署使用 640 尺寸,精度和速度达到最佳平衡。
避坑指南
- COCO 格式转换
- CARPK 原始标注的 class_id 从 1 开始,需映射到 0 -based
-
注意 bbox 格式转换(xywh→xyxy)时的数值归一化
-
显存优化
- 使用梯度累积:设置
--accumulate 2 - 启用混合精度训练:
--fp16 -
多尺度训练时限制最小尺寸:
--img-size 640 --multi-scale --ms-min 512 -
评估指标
- 车辆计数任务优先看 AP50
- 定位精度要求高时参考 AP75
开放性问题
无人机视角下车辆投影重叠会导致特征混淆,可能的解决方案:
- 引入高度估计分支
- 使用 3D 检测框表示
- 结合时序信息进行轨迹去重
当前方案在测试集达到 92.1% AP50,但实际部署还需考虑嵌入式设备优化。完整代码已开源在 GitHub,包含预训练模型和转换脚本。
正文完
