1.深度学习环境配置
- 英伟达显卡驱动&cuda+cudnn
- anaconda/miniconda
- pycharm
- pytorch
安装命令:以cuda12.4版本为例:下面给出pytorch官方网址
https://pytorch.org/get-started/previous-versions/
conda install pytorch2.5.1 torchvision0.20.1 torchaudio==2.5.1
pytorch-cuda=12.4 -c pytorch -c nvidia
conda create -n yolo12 python=3.11
conda activate yolo12
2.下载yolo12源码
https://github.com/CVPR-engineer/yolo12
3.制作数据集
4.定义配置文件
4.1 数据集配置
publicRetailDatas.yaml
# Ultralytics YOLO 🚀, AGPL-3.0 license
# Signature dataset by Ultralytics
# Documentation: https://docs.ultralytics.com/datasets/detect/signature/
# Example usage: yolo train data=signature.yaml
# parent
# ├── ultralytics
# └── datasets
# └── signature ← downloads here (11.2 MB)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
train: E:/pycharmProjects/pythonProject11/paperData/DataDivideCenter/YOLO/images/train
val: E:/pycharmProjects/pythonProject11/paperData/DataDivideCenter/YOLO/images/val
# val images (relative to 'path') 35 images
test: E:/pycharmProjects/pythonProject11/paperData/DataDivideCenter/YOLO/images/test
# Classes
names:
0: asm
1: kouxiangtang
2: lvjian
3: maidong
4: monilvcha
5: redbull
nc: 6
4.2 网络模型配置
# YOLOv12 🚀, AGPL-3.0 license
# YOLOv12 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# CFG file for YOLOv12-turbo
# Parameters
nc: 6 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov12n.yaml' will call yolov12.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.50, 0.25, 1024] # summary: 497 layers, 2,553,904 parameters, 2,553,888 gradients, 6.2 GFLOPs
s: [0.50, 0.50, 1024] # summary: 497 layers, 9,127,424 parameters, 9,127,408 gradients, 19.7 GFLOPs
m: [0.50, 1.00, 512] # summary: 533 layers, 19,670,784 parameters, 19,670,768 gradients, 60.4 GFLOPs
l: [1.00, 1.00, 512] # summary: 895 layers, 26,506,496 parameters, 26,506,480 gradients, 83.3 GFLOPs
x: [1.00, 1.50, 512] # summary: 895 layers, 59,414,176 parameters, 59,414,160 gradients, 185.9 GFLOPs
# YOLO12-turbo backbone
backbone:
# [from, repeats, module, args]
- [-1, 1, Conv, [64, 3, 2]] # 0-P1/2
- [-1, 1, Conv, [128, 3, 2, 1, 2]] # 1-P2/4
- [-1, 2, C3k2, [256, False, 0.25]]
- [-1, 1, Conv, [256, 3, 2, 1, 4]] # 3-P3/8
- [-1, 2, C3k2, [512, False, 0.25]]
- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16
- [-1, 4, A2C2f, [512, True, 4]]
- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32
- [-1, 4, A2C2f, [1024, True, 1]] # 8
# YOLO12-turbo head
head:
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 6], 1, Concat, [1]] # cat backbone P4
- [-1, 2, A2C2f, [512, False, -1]] # 11
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [[-1, 4], 1, Concat, [1]] # cat backbone P3
- [-1, 2, A2C2f, [256, False, -1]] # 14
- [-1, 1, Conv, [256, 3, 2]]
- [[-1, 11], 1, Concat, [1]] # cat head P4
- [-1, 2, A2C2f, [512, False, -1]] # 17
- [-1, 1, Conv, [512, 3, 2]]
- [[-1, 8], 1, Concat, [1]] # cat head P5
- [-1, 2, C3k2, [1024, True]] # 20 (P5/32-large)
- [[14, 17, 20], 1, Detect, [nc]] # Detect(P3, P4, P5)
注意修改nc即可
4.3 创建train.py
# @File: train_originalModel.py
# @Author: chen_song
# @Time: 2025-03-13 18:36
from ultralytics import YOLO
model = YOLO(r'F:\paperDatas\githubCode\yolo12\yolov12s.pt')
# Train the model
results = model.train(
data=r'F:\paperDatas\githubCode\yolo12\ultralytics\cfg\datasets\publicRetailDatas.yaml',
epochs=100,
batch=8,
imgsz=640,
scale=0.5, # S:0.9; M:0.9; L:0.9; X:0.9
mosaic=1.0,
mixup=0.0, # S:0.05; M:0.15; L:0.15; X:0.2
copy_paste=0.1, # S:0.15; M:0.4; L:0.5; X:0.6
)
# Evaluate model performance on the validation set
metrics = model.val()
# Perform object detection on an image
results = model("runs/predict/image.jpg")
results[0].show()