配置环境

1.配置新的虚拟环境

win+R输入cmd打开终端
输入:

conda create --name my_yolov11 python=3.10

2.下载库

torch官网:https://pytorch.org/
找到自己cuda对应的版本下载

下载其他的库

# YOLOv11 完整依赖列表
ultralytics>=8.0.100  # YOLO官方库核心功能
opencv-python>=4.8.0  # 图像处理基础库

# PyTorch及相关依赖(根据CUDA版本调整)
torch>=2.0.0  # 深度学习框架
torchvision>=0.15.0  # 计算机视觉扩展

# 数据处理与可视化
matplotlib>=3.3.0  # 图表绘制
numpy>=1.20.0  # 数值计算
pandas>=1.1.0  # 数据表格处理
seaborn>=0.11.0  # 高级统计可视化

# 工具与辅助库
tqdm>=4.41.0  # 训练进度条
scipy>=1.5.0  # 科学计算工具
requests>=2.23.0  # HTTP请求(模型下载等)
thop>=0.1.1  # 模型计算量(FLOPs)分析
psutil>=5.7.0  # 系统资源监控

# 配置与导出
pyyaml>=5.3.1  # YAML配置文件解析
tensorboard>=2.4.1  # 训练过程可视化
onnx>=1.10.0  # 模型导出为ONNX格式
onnxruntime>=1.10.0  # ONNX模型推理引擎
protobuf<=3.20.1  # 协议缓冲区(避免版本冲突)
packaging>=20.9  # 版本号解析工具

不用一个一个下载,做成requirements.txt放到项目文件夹里面打开终端激活环境输入指令pip install requirements.txt

2.下载模型

1.打开git搜索yolov11,下载数据集
网址:https://github.com/emptysoal/TensorRT-YOLO11

3.项目实战目标检测

1.整理数据集 打开X-Anylabeling自动标注,输入 python anylabeling/app.py

选择打开文件夹位置,选择自动保存,更改输出目录

选择自动标注

选择自动标注模型

转换为json脚本

import json
import os
from tqdm import tqdm

def convert_coco_to_yolo(json_path, output_dir, img_width=640, img_height=640):"""
    将COCO格式的JSON标注文件转换为YOLOv11需要的TXT格式
    
    参数:
    json_path (str): COCO格式的JSON文件路径
    output_dir (str): 输出TXT文件的目录
    img_width (int): 图像宽度,默认为640
    img_height (int): 图像高度,默认为640
    """# 确保输出目录存在
    os.makedirs(output_dir, exist_ok=True)# 读取COCO JSON文件with open(json_path, 'r') as f:
        coco_data = json.load(f)# 创建类别ID到索引的映射
    category_map = {cat['id']: i for i, cat in enumerate(coco_data['categories'])}# 按图像ID分组标注
    image_annotations = {}for ann in coco_data['annotations']:
        img_id = ann['image_id']if img_id not in image_annotations:
            image_annotations[img_id] = []
        image_annotations[img_id].append(ann)# 处理每个图像for img in tqdm(coco_data['images'], desc="Converting annotations"):
        img_id = img['id']
        img_name = img['file_name'].split('.')[0]  # 获取不带扩展名的文件名
        txt_path = os.path.join(output_dir, f"{img_name}.txt")# 如果该图像没有标注,创建空文件if img_id not in image_annotations:open(txt_path, 'w').close()continue# 处理该图像的所有标注with open(txt_path, 'w') as txt_file:for ann in image_annotations[img_id]:# 获取类别ID
                cat_id = ann['category_id']if cat_id not in category_map:continue  # 跳过未知类别
                class_idx = category_map[cat_id]# 获取边界框信息并转换为YOLO格式
                bbox = ann['bbox']  # [x, y, width, height]
                x_center = (bbox[0] + bbox[2]/2) / img_width
                y_center = (bbox[1] + bbox[3]/2) / img_height
                w = bbox[2] / img_width
                h = bbox[3] / img_height
                
                # 写入TXT文件(格式:class x_center y_center width height)
                txt_file.write(f"{class_idx} {x_center:.6f} {y_center:.6f} {w:.6f} {h:.6f}\n")print(f"转换完成!共处理{len(coco_data['images'])}张图像,TXT文件保存在: {output_dir}")if __name__ == "__main__":# 配置参数
    json_path = "path/to/your/coco_annotations.json"  # COCO格式的JSON文件路径
    output_dir = "path/to/output/labels"  # 输出TXT文件的目录
    img_width = 640  # 图像宽度,根据实际情况调整
    img_height = 640  # 图像高度,根据实际情况调整# 执行转换
    convert_coco_to_yolo(json_path, output_dir, img_width, img_height)

2.配置数据集

datasets/
├── images/          # 图像数据总目录
│   ├── train/       # 训练集图像
│   ├── val/         # 验证集图像
└── labels/          # 标签数据总目录
    ├── train/       # 训练集标签
    ├── val/         # 验证集标签

txt格式

0 0.45 0.32 0.12 0.25  # 类别ID 中心x 中心y 宽度 高度 (归一化坐标)
2 0.78 0.61 0.05 0.07

3.配置yaml文件

路径:ultralytics-main\ultralytics\cfg\datasets\coco8.yaml
复制更改成my_coco.yaml

path: dataset # dataset root dir
train: images/train # train images (relative to 'path') 4 images
val: images/val # val images (relative to 'path') 4 images
test: # test images (optional)

# Classes
names:
  0: person
  1: bicycle
  2: car
  3: motorcycle
  4: airplane
  5: bus
  6: train

4. 修改模型配置文件(关键步骤)

编辑项目中的.yaml 文件
路径:ultralytics-main\ultralytics\cfg\models\11\yolo11.yaml

# Ultralytics   AGPL-3.0 License - https://ultralytics.com/license

# Ultralytics YOLO11 object detection model with P3/8 - P5/32 outputs
# Model docs: https://docs.ultralytics.com/models/yolo11
# Task docs: https://docs.ultralytics.com/tasks/detect

# Parameters
nc: 6# number of classes
scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
  # [depth, width, max_channels]
  n: [0.50, 0.25, 1024] # summary: 181 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
  s: [0.50, 0.50, 1024] # summary: 181 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
  m: [0.50, 1.00, 512] # summary: 231 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
  l: [1.00, 1.00, 512] # summary: 357 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
  x: [1.00, 1.50, 512] # summary: 357 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs

# YOLO11n backbone
backbone:
  # [from, repeats, module, args]
  - [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
  - [-1, 1, Conv, [128, 3, 2]] # 1-P2/4
  - [-1, 2, C3k2, [256, False, 0.25]]
  - [-1, 1, Conv, [256, 3, 2]] # 3-P3/8
  - [-1, 2, C3k2, [512, False, 0.25]]
  - [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
  - [-1, 2, C3k2, [512, True]]
  - [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
  - [-1, 2, C3k2, [1024, True]]
  - [-1, 1, SPPF, [1024, 5]] # 9
  - [-1, 2, C2PSA, [1024]] # 10

# YOLO11n head
head:
  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 6], 1, Concat, [1]] # cat backbone P4
  - [-1, 2, C3k2, [512, False]] # 13

  - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
  - [[-1, 4], 1, Concat, [1]] # cat backbone P3
  - [-1, 2, C3k2, [256, False]] # 16 (P3/8-small)

  - [-1, 1, Conv, [256, 3, 2]]
  - [[-1, 13], 1, Concat, [1]] # cat head P4
  - [-1, 2, C3k2, [512, False]] # 19 (P4/16-medium)

  - [-1, 1, Conv, [512, 3, 2]]
  - [[-1, 10], 1, Concat, [1]] # cat head P5
  - [-1, 2, C3k2, [1024, True]] # 22 (P5/32-large)

  - [[16, 19, 22], 1, Detect, [nc]] # Detect(P3, P4, P5)

4.开启训练

1.启动训练
训练指令

从 YAML 配置新建模型并从头训练
yolo detect train data=coco8.yaml model=yolo11n.yaml epochs=100 imgsz=640

从预训练模型(.pt)直接微调
yolo detect train data=coco8.yaml model=yolo11n.pt epochs=100 imgsz=640

结合 YAML 配置与预训练权重
yolo detect train data=coco8.yaml model=yolo11n.yaml pretrained=yolo11n-seg epochs=100 imgsz=640
Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐