VisDrone2019上训练YOLOv5(用ultralytics)

使用的package library: ultralytics

环境:python3.8, torch=1.7.0

把ultralytics代码git clone 到本地

git clone https://github.com/ultralytics/ultralytics/

本地新建一个train.py,内容写下面这些。

from ultralytics import YOLO

# Load a model
model = YOLO("yolov5n.yaml")  # build a new model from scratch

# Use the model
# model.train(data="VisDrone.yaml", epochs=1,batch=1)  # train the model
model.train(data="coco128.yaml", epochs=1,batch=1)

metrics = model.val()  # evaluate model performance on the validation set

在本地做测试用coco128这128张图片。跑通了,上云端服务器的饿时候在换成VisDrone.yaml这个我们要训练的数据集

40G显存,最佳batch size是25张, 显示的实时GPU占用仅仅是11.4G,但是显存占用最高的时候是95%.你需要预留一些空间reserve,防止内存爆掉。

yolov5n.yaml

yolov6n.yaml

### VisDrone2019 数据集YOLO 标签格式转换 VisDrone2019 是一个广泛用于无人机目标检测和跟踪的数据集,其标注信息通常以矩形框的形式提供。而 YOLO (You Only Look Once) 的标签格式则是一种特定的标准化坐标表示方式。为了将 VisDrone2019 数据集的标注转换YOLO 格式,可以按照以下方式进行处理。 #### 1. **VisDrone2019 原始标注格式** VisDrone2019 数据集中,每张图像对应的标注文件是一个 `.txt` 文件,其中每一行代表一个边界框及其属性。具体来说,每一行由逗号分隔的多个字段组成,如下所示: ``` <bbox_left>,<bbox_top>,<bbox_width>,<bbox_height>,<score>,<object_category>,<truncation>,<occlusion> ``` - `<bbox_left>` 和 `<bbox_top>` 表示边界框左上角相对于图像原点的位置。 - `<bbox_width>` 和 `<bbox_height>` 表示边界框的宽度和高度。 - `<object_category>` 表示对象类别编号(例如行人、汽车等)。[^1] #### 2. **YOLO 标签格式** YOLO 的标签格式要求每个边界框的信息存储在一个单独的 `.txt` 文件中,且该文件名应与对应图像文件相同。每一行描述一个边界框,格式如下: ``` <class_id> <x_center> <y_center> <width> <height> ``` - `<class_id>` 是类别的索引值(从 0 开始计数)。 - `<x_center>` 和 `<y_center>` 是边界框中心点相对于整幅图像宽高的归一化比例位置。 - `<width>` 和 `<height>` 是边界框宽高相对于整幅图像宽高的归一化比例尺寸。[^1] #### 3. **转换逻辑** 以下是实现从 VisDrone2019YOLO 标签格式转换的核心逻辑: - 对于每一个原始标注文件中的边界框: - 计算边界框中心点 `(x_center, y_center)`: \[ x_{\text{center}} = \frac{\text{bbox\_left} + (\text{bbox\_width}/2)}{\text{image\_width}} \] \[ y_{\text{center}} = \frac{\text{bbox\_top} + (\text{bbox\_height}/2)}{\text{image\_height}} \] - 归一化边界框宽高: \[ width = \frac{\text{bbox\_width}}{\text{image\_width}} \] \[ height = \frac{\text{bbox\_height}}{\text{image\_height}} \] - 将计算得到的结果写入对应的 YOLO 标签文件中。[^1] #### 4. **Python 实现代码** 下面是一段 Python 脚本,演示如何完成上述转换过程: ```python import os import cv2 def convert_visdrone_to_yolo(visdrone_label_path, image_folder, output_folder): """ Convert VisDrone labels to YOLO format. :param visdrone_label_path: Path to the folder containing VisDrone label files (.txt). :param image_folder: Path to the folder containing images corresponding to the labels. :param output_folder: Output folder where YOLO formatted labels will be saved. """ if not os.path.exists(output_folder): os.makedirs(output_folder) for label_file in os.listdir(visdrone_label_path): if not label_file.endswith('.txt'): continue # Load image dimensions image_name = label_file.replace('annotations', 'images').replace('.txt', '.jpg') image_path = os.path.join(image_folder, image_name) if not os.path.isfile(image_path): print(f"Image {image_name} not found.") continue image = cv2.imread(image_path) h, w = image.shape[:2] # Process each line of the annotation file with open(os.path.join(visdrone_label_path, label_file), "r") as f_in: lines = f_in.readlines() yolo_lines = [] for line in lines: fields = list(map(int, line.strip().split(','))) bbox_left, bbox_top, bbox_w, bbox_h, _, category, truncation, occlusion = fields if category == 0 or truncation >= 2 or occlusion >= 2: continue # Ignore ignored regions and highly truncated/occluded objects class_id = category - 1 # Adjusting from 1-based index to 0-based index x_center = ((bbox_left + bbox_w / 2) / w) y_center = ((bbox_top + bbox_h / 2) / h) width_norm = bbox_w / w height_norm = bbox_h / h yolo_line = f"{class_id} {x_center:.6f} {y_center:.6f} {width_norm:.6f} {height_norm:.6f}\n" yolo_lines.append(yolo_line) # Save converted annotations output_file = os.path.join(output_folder, label_file.split('.')[0] + ".txt") with open(output_file, "w") as f_out: f_out.writelines(yolo_lines) # Example usage convert_visdrone_to_yolo( visdrone_label_path="path/to/visdrone_labels", image_folder="path/to/images", output_folder="path/to/yolo_labels" ) ``` 此脚本会遍历指定目录下的所有 VisDrone 标注文件,并将其逐一转换YOLO 格式的标签文件。需要注意的是,在实际应用过程中可能还需要调整一些细节参数来适配具体的场景需求。[^1] --- ###
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

德彪稳坐倒骑驴

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值