如何使用YOLOv8进行训练——红外飞机数据集(图像微小追踪) 红外图像车辆追踪数据集 红外飞机车辆跟踪检测数据集 红外车辆数据集检测
红外图像飞机, part1,红外图像微小飞机追踪数据集,数据集获取场景涵盖了天空、地面等背景,共计22段数据、30条航迹、16177帧图像、16944个目标,每个目标对应一个标注位置,标注目标坐标位置。数据量800MB,
红外图像车辆追踪数据集,以车辆为探测目标,以地面场景为探测背景的图像序列数据集。本数据集共计21750帧图像,89174个目标,标注目标框,组成了87段视频序列、393段目标轨迹。数据量4.5Gb
好的,让我们详细介绍这两个红外图像追踪数据集,并提供使用YOLOv8进行训练的代码。我们将确保所有步骤都是一键的,以便你可以轻松地进行模型训练和评估。
数据集介绍
Part 1: 红外图像微小飞机追踪数据集
- 数据集名称 :Infrared Small Aircraft Tracking Dataset (ISATD)
- 数据类型 :红外图像
- 目标类别 :飞机
- 数据集描述 :
- 获取场景:天空、地面等背景
- 数据量:22段数据、30条航迹、16177帧图像、16944个目标
- 标注格式:每个目标对应一个标注位置,标注目标坐标位置
- 数据量:800MB
Part 2: 红外图像车辆追踪数据集
- 数据集名称 :Infrared Vehicle Tracking Dataset (IVTD)
- 数据类型 :红外图像
- 目标类别 :车辆
- 数据集描述 :
- 探测目标:车辆
- 探测背景:地面场景
- 数据量:21750帧图像、89174个目标
- 标注格式:标注目标框
- 视频序列:87段视频序列、393段目标轨迹
- 数据量:4.5GB
数据集目录结构
为了方便管理,我们可以将两个数据集分别存储在不同的目录中。
Infrared_Tracking_Dataset/
├── ISATD/
│ ├── images/
│ │ ├── train/
│ │ └── val/
│ ├── labels/
│ │ ├── train/
│ │ └── val/
│ └── data.yaml
├── IVTD/
│ ├── images/
│ │ ├── train/
│ │ └── val/
│ ├── labels/
│ │ ├── train/
│ │ └── val/
│ └── data.yaml

数据集配置文件
ISATD 数据集配置文件
创建一个data.yaml文件,配置ISATD数据集的路径和类别信息:
path: ./Infrared_Tracking_Dataset/ISATD # 数据集路径
train: images/train # 训练集图像路径
val: images/val # 验证集图像路径
nc: 1 # 类别数
names: ['Aircraft'] # 类别名称
yaml
IVTD 数据集配置文件
创建一个data.yaml文件,配置IVTD数据集的路径和类别信息:
path: ./Infrared_Tracking_Dataset/IVTD # 数据集路径
train: images/train # 训练集图像路径
val: images/val # 验证集图像路径
nc: 1 # 类别数
names: ['Vehicle'] # 类别名称
yaml
转换标注格式
假设标注文件是VOC格式的XML文件,我们需要将它们转换为YOLO格式的TXT文件。
转换脚本
import xml.etree.ElementTree as ET
import os
def convert_voc_to_yolo(voc_file, yolo_file, class_names):
tree = ET.parse(voc_file)
root = tree.getroot()
width = int(root.find('size/width').text)
height = int(root.find('size/height').text)
with open(yolo_file, 'w') as f:
for obj in root.findall('object'):
class_name = obj.find('name').text
if class_name not in class_names:
continue
class_id = class_names.index(class_name)
bbox = obj.find('bndbox')
x_min = float(bbox.find('xmin').text)
y_min = float(bbox.find('ymin').text)
x_max = float(bbox.find('xmax').text)
y_max = float(bbox.find('ymax').text)
x_center = (x_min + x_max) / 2.0 / width
y_center = (y_min + y_max) / 2.0 / height
w = (x_max - x_min) / width
h = (y_max - y_min) / height
f.write(f"{class_id} {x_center} {y_center} {w} {h}\n")
def convert_all_voc_to_yolo(voc_dir, yolo_dir, class_names):
os.makedirs(yolo_dir, exist_ok=True)
for filename in os.listdir(voc_dir):
if filename.endswith('.xml'):
voc_file = os.path.join(voc_dir, filename)
yolo_file = os.path.join(yolo_dir, filename.replace('.xml', '.txt'))
convert_voc_to_yolo(voc_file, yolo_file, class_names)
if __name__ == "__main__":
class_names = ['Aircraft'] # 对于ISATD
voc_train_dir = 'Infrared_Tracking_Dataset/ISATD/labels_voc/train'
yolo_train_dir = 'Infrared_Tracking_Dataset/ISATD/labels/train'
convert_all_voc_to_yolo(voc_train_dir, yolo_train_dir, class_names)
voc_val_dir = 'Infrared_Tracking_Dataset/ISATD/labels_voc/val'
yolo_val_dir = 'Infrared_Tracking_Dataset/ISATD/labels/val'
convert_all_voc_to_yolo(voc_val_dir, yolo_val_dir, class_names)
# 对于IVTD
class_names = ['Vehicle']
voc_train_dir = 'Infrared_Tracking_Dataset/IVTD/labels_voc/train'
yolo_train_dir = 'Infrared_Tracking_Dataset/IVTD/labels/train'
convert_all_voc_to_yolo(voc_train_dir, yolo_train_dir, class_names)
voc_val_dir = 'Infrared_Tracking_Dataset/IVTD/labels_voc/val'
yolo_val_dir = 'Infrared_Tracking_Dataset/IVTD/labels/val'
convert_all_voc_to_yolo(voc_val_dir, yolo_val_dir, class_names)
python

YOLOv8训练代码
安装YOLOv8库和依赖项 :
git clone https://github.com/ultralytics/ultralytics.git
cd ultralytics
pip install -r requirements.txt
bash
训练模型 :
from ultralytics import YOLO
def train_model(data_yaml_path, model_config, epochs, batch_size, img_size):
# 加载模型
model = YOLO(model_config)
# 训练模型
results = model.train(
data=data_yaml_path,
epochs=epochs,
batch=batch_size,
imgsz=img_size
)
# 保存模型
model.save("runs/train/infrared_tracking_detection/best.pt")
if __name__ == "__main__":
data_yaml_path = 'Infrared_Tracking_Dataset/ISATD/data.yaml' # 或者 'Infrared_Tracking_Dataset/IVTD/data.yaml'
model_config = 'yolov8s.yaml'
epochs = 100
batch_size = 16
img_size = 640
train_model(data_yaml_path, model_config, epochs, batch_size, img_size)
python

详细解释
安装YOLOv8和依赖项 :
* 克隆YOLOv8仓库并安装所有必要的依赖项。
训练模型 :
* 导入YOLOv8库。
* 加载模型配置文件。
* 调用`model.train`方法进行训练。
* 保存训练后的最佳模型。
训练脚本
将上述脚本保存为一个Python文件(例如train_yolov8_infrared.py),然后它。
python train_yolov8_infrared.py
bash
评估模型
- 评估模型 :
from ultralytics import YOLO
def evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold):
# 加载模型
model = YOLO(weights_path)
# 评估模型
results = model.val(
data=data_yaml_path,
imgsz=img_size,
conf=conf_threshold
)
# 打印评估结果
print(results)
if __name__ == "__main__":
data_yaml_path = 'Infrared_Tracking_Dataset/ISATD/data.yaml' # 或者 'Infrared_Tracking_Dataset/IVTD/data.yaml'
weights_path = 'runs/train/infrared_tracking_detection/best.pt'
img_size = 640
conf_threshold = 0.4
evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold)
python

详细解释
- 评估模型 :
- 导入YOLOv8库。
- 加载训练好的模型权重。
- 调用
model.val方法进行评估。 - 打印评估结果。
评估脚本
将上述脚本保存为一个Python文件(例如evaluate_yolov8_infrared.py),然后它。
python evaluate_yolov8_infrared.py
bash
一键脚本
为了实现一键,可以将训练和评估脚本合并到一个主脚本中,并添加命令行参数来控制模式。
import argparse
from ultralytics import YOLO
def convert_voc_to_yolo(voc_file, yolo_file, class_names):
tree = ET.parse(voc_file)
root = tree.getroot()
width = int(root.find('size/width').text)
height = int(root.find('size/height').text)
with open(yolo_file, 'w') as f:
for obj in root.findall('object'):
class_name = obj.find('name').text
if class_name not in class_names:
continue
class_id = class_names.index(class_name)
bbox = obj.find('bndbox')
x_min = float(bbox.find('xmin').text)
y_min = float(bbox.find('ymin').text)
x_max = float(bbox.find('xmax').text)
y_max = float(bbox.find('ymax').text)
x_center = (x_min + x_max) / 2.0 / width
y_center = (y_min + y_max) / 2.0 / height
w = (x_max - x_min) / width
h = (y_max - y_min) / height
f.write(f"{class_id} {x_center} {y_center} {w} {h}\n")
def convert_all_voc_to_yolo(voc_dir, yolo_dir, class_names):
os.makedirs(yolo_dir, exist_ok=True)
for filename in os.listdir(voc_dir):
if filename.endswith('.xml'):
voc_file = os.path.join(voc_dir, filename)
yolo_file = os.path.join(yolo_dir, filename.replace('.xml', '.txt'))
convert_voc_to_yolo(voc_file, yolo_file, class_names)
def train_model(data_yaml_path, model_config, epochs, batch_size, img_size):
# 加载模型
model = YOLO(model_config)
# 训练模型
results = model.train(
data=data_yaml_path,
epochs=epochs,
batch=batch_size,
imgsz=img_size
)
# 保存模型
model.save("runs/train/infrared_tracking_detection/best.pt")
def evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold):
# 加载模型
model = YOLO(weights_path)
# 评估模型
results = model.val(
data=data_yaml_path,
imgsz=img_size,
conf=conf_threshold
)
# 打印评估结果
print(results)
def main(mode, dataset):
if dataset == 'ISATD':
data_yaml_path = 'Infrared_Tracking_Dataset/ISATD/data.yaml'
class_names = ['Aircraft']
elif dataset == 'IVTD':
data_yaml_path = 'Infrared_Tracking_Dataset/IVTD/data.yaml'
class_names = ['Vehicle']
else:
print("Invalid dataset. Use 'ISATD' or 'IVTD'.")
return
model_config = 'yolov8s.yaml'
epochs = 100
batch_size = 16
img_size = 640
conf_threshold = 0.4
if mode == 'convert':
voc_train_dir = f'Infrared_Tracking_Dataset/{dataset}/labels_voc/train'
yolo_train_dir = f'Infrared_Tracking_Dataset/{dataset}/labels/train'
convert_all_voc_to_yolo(voc_train_dir, yolo_train_dir, class_names)
voc_val_dir = f'Infrared_Tracking_Dataset/{dataset}/labels_voc/val'
yolo_val_dir = f'Infrared_Tracking_Dataset/{dataset}/labels/val'
convert_all_voc_to_yolo(voc_val_dir, yolo_val_dir, class_names)
elif mode == 'train':
train_model(data_yaml_path, model_config, epochs, batch_size, img_size)
elif mode == 'eval':
weights_path = 'runs/train/infrared_tracking_detection/best.pt'
evaluate_model(data_yaml_path, weights_path, img_size, conf_threshold)
else:
print("Invalid mode. Use 'convert', 'train', or 'eval'.")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Convert, train, or evaluate YOLOv8 on the infrared tracking datasets.")
parser.add_argument('mode', type=str, choices=['convert', 'train', 'eval'], help="Mode: 'convert', 'train', or 'eval'")
parser.add_argument('dataset', type=str, choices=['ISATD', 'IVTD'], help="Dataset: 'ISATD' or 'IVTD'")
args = parser.parse_args()
main(args.mode, args.dataset)
python

详细解释
命令行参数 :
* 使用`argparse`库添加命令行参数,控制脚本的模式(转换、训练或评估)和选择的数据集(ISATD或IVTD)。
主函数 :
* 根据传入的模式和数据集参数,调用相应的转换、训练或评估函数。
主脚本
将上述脚本保存为一个Python文件(例如main_yolov8_infrared.py),然后它。
转换标注格式
python main_yolov8_infrared.py convert ISATD
python main_yolov8_infrared.py convert IVTD
bash
训练模型
python main_yolov8_infrared.py train ISATD
python main_yolov8_infrared.py train IVTD
bash
评估模型
python main_yolov8_infrared.py eval ISATD
python main_yolov8_infrared.py eval IVTD
bash
总结
通过以上步骤,你可以准备好红外图像飞机和车辆追踪数据集,并使用YOLOv8进行训练和评估。希望这些信息对你有帮助!如果你有任何其他问题或需要进一步的帮助,请随时告诉我。
