自动驾驶路面分类数据集,按照不同的天气条件,即干燥、湿润、水上、新雪、融化的雪和冰。不同的道路材料属性包括沥青、混凝土、泥浆和碎石。不同的道路平整性分为光滑、轻微不平和严重不平。共27类,37万张图像,13GB数据
好的,让我们从数据集准备开始,逐步介绍如何使用深度学习框架(以YOLOv8为例)训练自动驾驶路面分类模型。我们将涵盖数据集准备、模型训练、模型评估等步骤,并提供详细的代码示例。
1. 数据集准备
1.1. 数据集结构
确保你的数据集按照以下结构组织:
road_classification/
│
├── images/
│ ├── train/
│ ├── val/
│ └── test/
│
├── labels/
│ ├── train/
│ ├── val/
│ └── test/
│
└── data.yaml
1.2. 创建数据配置文件 (data.yaml
)
train: ./road_classification/images/train
val: ./road_classification/images/val
test: ./road_classification/images/test
nc: 27 # 类别数量(27类)
names: ['Dry Asphalt', 'Dry Concrete', 'Dry Mud', 'Dry Gravel', 'Wet Asphalt', 'Wet Concrete', 'Wet Mud', 'Wet Gravel', 'Water Asphalt', 'Water Concrete', 'Water Mud', 'Water Gravel', 'New Snow Asphalt', 'New Snow Concrete', 'New Snow Mud', 'New Snow Gravel', 'Melting Snow Asphalt', 'Melting Snow Concrete', 'Melting Snow Mud', 'Melting Snow Gravel', 'Ice Asphalt', 'Ice Concrete', 'Ice Mud', 'Ice Gravel', 'Smooth Asphalt', 'Smooth Concrete', 'Smooth Mud', 'Smooth Gravel', 'Slightly Bumpy Asphalt', 'Slightly Bumpy Concrete', 'Slightly Bumpy Mud', 'Slightly Bumpy Gravel', 'Severely Bumpy Asphalt', 'Severely Bumpy Concrete', 'Severely Bumpy Mud', 'Severely Bumpy Gravel'] # 类别名称
# 下载数据集
download: ''
2. 安装YOLOv8
确保你已经安装了YOLOv8。YOLOv8是YOLO系列的最新版本,可以从Ultralytics的GitHub仓库中获取。
git clone https://github.com/ultralytics/yolov8.git
cd yolov8
pip install -r requirements.txt
3. 训练模型
3.1. 配置训练
使用YOLOv8进行路面分类。以下是一个示例命令:
python train.py --data ./road_classification/data.yaml --img 640 --batch 16 --epochs 100 --name yolov8_custom_road_classification --weights yolov8n.pt
4. 评估模型
4.1. 运行评估
在验证集上评估训练好的模型:
python val.py --data ./road_classification/data.yaml --weights runs/train/yolov8_custom_road_classification/weights/best.pt
4.2. 可视化结果
你可以使用val
命令的--save
标志来可视化结果:
python val.py --data ./road_classification/data.yaml --weights runs/train/yolov8_custom_road_classification/weights/best.pt --save
5. 示例代码
5.1. 数据预处理示例
import cv2
import os
def resize_images(input_dir, output_dir, size=(640, 640)):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith(('.jpg', '.png', '.jpeg')):
img_path = os.path.join(input_dir, filename)
img = cv2.imread(img_path)
img_resized = cv2.resize(img, size)
output_path = os.path.join(output_dir, filename)
cv2.imwrite(output_path, img_resized)
# 示例用法
resize_images('./road_classification/images/train', './road_classification/images/train_resized')
resize_images('./road_classification/images/val', './road_classification/images/val_resized')
resize_images('./road_classification/images/test', './road_classification/images/test_resized')
5.2. 训练模型
import torch
# 确保YOLOv8路径正确
YOLO_PATH = 'path/to/yolov8'
# 加载YOLOv8模型
model = torch.hub.load(YOLO_PATH, 'custom', path='runs/train/yolov8_custom_road_classification/weights/best.pt')
# 训练模型
model.train()
model.fit(data='road_classification/data.yaml', imgsz=640, batch=16, epochs=100)
5.3. 评估模型
# 加载训练好的模型
model = torch.hub.load(YOLO_PATH, 'custom', path='runs/train/yolov8_custom_road_classification/weights/best.pt')
# 评估模型
results = model.val(data='road_classification/data.yaml', weights='runs/train/yolov8_custom_road_classification/weights/best.pt', save=True)
print(results.metrics)
6. 其他建议
- 数据增强:使用数据增强技术来提高模型的鲁棒性。YOLOv8支持多种数据增强方法,如翻转、旋转、裁剪等。
- 模型集成:集成多个模型以提高性能。
- 模型量化:量化模型以适应边缘设备。
7. 示例代码
7.1. 数据预处理示例
import cv2
import os
def resize_images(input_dir, output_dir, size=(640, 640)):
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for filename in os.listdir(input_dir):
if filename.endswith(('.jpg', '.png', '.jpeg')):
img_path = os.path.join(input_dir, filename)
img = cv2.imread(img_path)
img_resized = cv2.resize(img, size)
output_path = os.path.join(output_dir, filename)
cv2.imwrite(output_path, img_resized)
# 示例用法
resize_images('./road_classification/images/train', './road_classification/images/train_resized')
resize_images('./road_classification/images/val', './road_classification/images/val_resized')
resize_images('./road_classification/images/test', './road_classification/images/test_resized')
8. 运行和调试
确保你的环境配置正确,并且所有依赖项都已安装。运行模型训练和评估时,确保模型文件路径正确,并且数据集路径正确。
标签:classification,train,分类,路面,13GB,dir,images,path,road From: https://blog.csdn.net/2401_88440984/article/details/144280255