关键词: 车辆分类 车辆目标检测 细粒度车辆分类与检测 车牌识别与车辆检测的整合 yolov8的使用
工具:pytorch + pycharm + anaconda
前言
作为正式开发的第一个深度学习相关项目(经历了从yolov5到v8的过渡),今天准备记录自己的学习笔记。这是我作为一个深度学习小白,探索的点点滴滴。同时,我也将为大家记录一下YOLOv8的使用细节,希望这篇文章能对小白们有所帮助。当然,大佬们轻喷哈,我也是在不断进步的路上,一起学习一起进步嘛!😎
一、前期准备
- yolov8
- 按照readme在pycharm中安装项目
- 在项目根目录下运行下列指令(配置解释器 个人喜欢用anaconda创建环境然后pycharm直接去找这个解释器 这样有些时候requirements中包的安装不完全或者需要别的时候我更习惯在anaconda的命令行中处理安装)
pip install ultralytics
pip install -r requirements.txt
二、正式使用
0. 关于yolov8下项目目录的介绍
- 主要介绍几个需要用到的
yolov8
├──datasets //自己创建一个存放数据集的文件夹)
....
├──runs //该文件夹下是运行后的结果存储位置
├──ultralytics(大部分重要文件)
├──cfg
├──datasets(原来的数据集yaml文件路径)
├──coco128.yaml
...
├──models
...
├──v8
├──yolov8.yaml(yolo的yaml)
...
...
大概介绍如上 我们用到的也就在上面
1. 数据集的处理
- 笔者是直接将v5的数据集复制过来也可以直接使用
- 关于转格式(voc xml)的文章csdn上也有很多了 在此不做赘述过多介绍(毕竟满足不了需求的话chatgpt也能解决😎)
yolov8
├──datasets //自己创建一个存放数据集的文件夹)
├──cars(数据集 自己命名)
├── images
├── train
├── xx.jpg
├── val
├── xx.jpg
├── labels
├── train
├── xx.txt
├── val
├── xx.txt
2. 配置文件的调整
- 笔者配置yaml的时候出现了不少问题 最后使用的方法如下
- 将数据集配置文件和yolo配置文件都放到数据集根目录下 然后在训练程序中加上路径名
数据集配置文件如下
# 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, ..]
path: E:\yolov8\datasets\cars # dataset root dir
train: images/train # train images (relative to 'path')
val: images/val # val images (relative to 'path')
test: # test images (optional)
# Classes
nc: 222 # number of classes
names:
0: 无
1: 小轿车
2: 越野车
3: 巴士
4: 公交车
5: 大货车
......
- 其中值得注意的是names中调整的txt文件中是以数字来标注的 且平移了一下(txt中类别也没有0直接从1开始)和大多往常的不太一样 所以我们的0号空了出来
- nc数对应总的类别数 但是yolov8必须一一对应 简单来说就是
names.length==nc
所以为了对应我们的标注规则 空闲的一些类别也要写上(如:无)
yolov8(s).yaml
# Ultralytics YOLO 🚀, AGPL-3.0 license
# YOLOv8 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect
# Parameters
nc: 222 # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
# [depth, width, max_channels]
n: [0.33, 0.25, 1024] # YOLOv8n summary: 225 layers, 3157200 parameters, 3157184 gradients, 8.9 GFLOPs
s: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPs
......
- 只需要修改一个部分
nc: 222 # number of classes
改成数据yaml中的类别数 - 值得注意的是yolov8的yaml有所改变
scales: # model compound scaling constants, i.e. 'model=yolov8n.yaml' will call yolov8.yaml with scale 'n'
这意味着你怎样命名就会怎样调用模型 如果你命名为yolov8s.yaml那么就会调用yolov8s n/m/l/x同理
3. 开始训练!✨✨✨
yolo task=detect mode=train data=E:\yolov8\datasets\carsTest.yaml model=yolov8s.yaml model=yolov8s.pt epochs=100 batch=8 device=0 workers=1
- 关于参数的设置可以根据自己设备自行选择
- 我的gpu是3060 6G(奈何笔记本阉割版 内存吃紧 所以调整的batch和workers都比较低)
- 放一个default.yaml(总体参数/yolo命令都可以参考如下) (在ultralytics目录下)
# Ultralytics YOLO 🚀, AGPL-3.0 license
# Default training settings and hyperparameters for medium-augmentation COCO training
task: detect # (str) YOLO task, i.e. detect, segment, classify, pose
mode: train # (str) YOLO mode, i.e. train, val, predict, export, track, benchmark
# Train settings -------------------------------------------------------------------------------------------------------
model: # (str, optional) path to model file, i.e. yolov8n.pt, yolov8n.yaml
data: # (str, optional) path to data file, i.e. coco128.yaml
epochs: 100 # (int) number of epochs to train for
patience: 50 # (int) epochs to wait for no observable improvement for early stopping of training
batch: 16 # (int) number of images per batch (-1 for AutoBatch)
imgsz: 640 # (int | list) input images size as int for train and val modes, or list[w,h] for predict and export modes
save: True # (bool) save train checkpoints and predict results
save_period: -1 # (int) Save checkpoint every x epochs (disabled if < 1)
cache: False # (bool) True/ram, disk or False. Use cache for data loading
device: # (int | str | list, optional) device to run on, i.e. cuda device=0 or device=0,1,2,3 or device=cpu
workers: 8 # (int) number of worker threads for data loading (per RANK if DDP)
project: # (str, optional) project name
name: # (str, optional) experiment name, results saved to 'project/name' directory
exist_ok: False # (bool) whether to overwrite existing experiment
pretrained: True # (bool | str) whether to use a pretrained model (bool) or a model to load weights from (str)
optimizer: auto # (str) optimizer to use, choices=[SGD, Adam, Adamax, AdamW, NAdam, RAdam, RMSProp, auto]
verbose: True # (bool) whether to print verbose output
seed: 0 # (int) random seed for reproducibility
deterministic: True # (bool) whether to enable deterministic mode
single_cls: False # (bool) train multi-class data as single-class
rect: False # (bool) rectangular training if mode='train' or rectangular validation if mode='val'
cos_lr: False # (bool) use cosine learning rate scheduler
close_mosaic: 10 # (int) disable mosaic augmentation for final epochs (0 to disable)
resume: False # (bool) resume training from last checkpoint
amp: True # (bool) Automatic Mixed Precision (AMP) training, choices=[True, False], True runs AMP check
fraction: 1.0 # (float) dataset fraction to train on (default is 1.0, all images in train set)
profile: False # (bool) profile ONNX and TensorRT speeds during training for loggers
freeze: None # (int | list, optional) freeze first n layers, or freeze list of layer indices during training
# Segmentation
overlap_mask: True # (bool) masks should overlap during training (segment train only)
mask_ratio: 4 # (int) mask downsample ratio (segment train only)
# Classification
dropout: 0.0 # (float) use dropout regularization (classify train only)
# Val/Test settings ----------------------------------------------------------------------------------------------------
val: True # (bool) validate/test during training
split: val # (str) dataset split to use for validation, i.e. 'val', 'test' or 'train'
save_json: False # (bool) save results to JSON file
save_hybrid: False # (bool) save hybrid version of labels (labels + additional predictions)
conf: # (float, optional) object confidence threshold for detection (default 0.25 predict, 0.001 val)
iou: 0.7 # (float) intersection over union (IoU) threshold for NMS
max_det: 300 # (int) maximum number of detections per image
half: False # (bool) use half precision (FP16)
dnn: False # (bool) use OpenCV DNN for ONNX inference
plots: True # (bool) save plots during train/val
# Prediction settings --------------------------------------------------------------------------------------------------
source: # (str, optional) source directory for images or videos
show: False # (bool) show results if possible
save_txt: False # (bool) save results as .txt file
save_conf: False # (bool) save results with confidence scores
save_crop: False # (bool) save cropped images with results
show_labels: True # (bool) show object labels in plots
show_conf: True # (bool) show object confidence scores in plots
vid_stride: 1 # (int) video frame-rate stride
stream_buffer: False # (bool) buffer all streaming frames (True) or return the most recent frame (False)
line_width: # (int, optional) line width of the bounding boxes, auto if missing
visualize: False # (bool) visualize model features
augment: False # (bool) apply image augmentation to prediction sources
agnostic_nms: False # (bool) class-agnostic NMS
classes: # (int | list[int], optional) filter results by class, i.e. classes=0, or classes=[0,2,3]
retina_masks: False # (bool) use high-resolution segmentation masks
boxes: True # (bool) Show boxes in segmentation predictions
# Export settings ------------------------------------------------------------------------------------------------------
format: torchscript # (str) format to export to, choices at https://docs.ultralytics.com/modes/export/#export-formats
keras: False # (bool) use Kera=s
optimize: False # (bool) TorchScript: optimize for mobile
int8: False # (bool) CoreML/TF INT8 quantization
dynamic: False # (bool) ONNX/TF/TensorRT: dynamic axes
simplify: False # (bool) ONNX: simplify model
opset: # (int, optional) ONNX: opset version
workspace: 4 # (int) TensorRT: workspace size (GB)
nms: False # (bool) CoreML: add NMS
# Hyperparameters ------------------------------------------------------------------------------------------------------
lr0: 0.01 # (float) initial learning rate (i.e. SGD=1E-2, Adam=1E-3)
lrf: 0.01 # (float) final learning rate (lr0 * lrf)
momentum: 0.937 # (float) SGD momentum/Adam beta1
weight_decay: 0.0005 # (float) optimizer weight decay 5e-4
warmup_epochs: 3.0 # (float) warmup epochs (fractions ok)
warmup_momentum: 0.8 # (float) warmup initial momentum
warmup_bias_lr: 0.1 # (float) warmup initial bias lr
box: 7.5 # (float) box loss gain
cls: 0.5 # (float) cls loss gain (scale with pixels)
dfl: 1.5 # (float) dfl loss gain
pose: 12.0 # (float) pose loss gain
kobj: 1.0 # (float) keypoint obj loss gain
label_smoothing: 0.0 # (float) label smoothing (fraction)
nbs: 64 # (int) nominal batch size
hsv_h: 0.015 # (float) image HSV-Hue augmentation (fraction)
hsv_s: 0.7 # (float) image HSV-Saturation augmentation (fraction)
hsv_v: 0.4 # (float) image HSV-Value augmentation (fraction)
degrees: 0.0 # (float) image rotation (+/- deg)
translate: 0.1 # (float) image translation (+/- fraction)
scale: 0.5 # (float) image scale (+/- gain)
shear: 0.0 # (float) image shear (+/- deg)
perspective: 0.0 # (float) image perspective (+/- fraction), range 0-0.001
flipud: 0.0 # (float) image flip up-down (probability)
fliplr: 0.5 # (float) image flip left-right (probability)
mosaic: 1.0 # (float) image mosaic (probability)
mixup: 0.0 # (float) image mixup (probability)
copy_paste: 0.0 # (float) segment copy-paste (probability)
# Custom config.yaml ---------------------------------------------------------------------------------------------------
cfg: # (str, optional) for overriding defaults.yaml
# Tracker settings ------------------------------------------------------------------------------------------------------
tracker: botsort.yaml # (str) tracker type, choices=[botsort.yaml, bytetrack.yaml]
- 训练结束后结果存储在
Results saved to runs\detect\train23
4. 开始测试
- 从结果中提取出best.pt
E:\yolov8\runs\detect\train23\weights
复制到yolov8根目录下
yolo task=detect mode=predict model=best.pt source=填写你想要测试的文件或文件夹 (置信度可选conf=0.25)
- 然后就可以在
Results saved to runs\detect\predict9
中愉快的查询结果了👏👏👏
5. 效果图
总结
-
以上主要介绍了YOLOv8的深度学习项目,分享了一些关于YOLOv8的使用经验。希望本文能为读者在YOLOv8项目中提供一些帮助和灵感
-
如果你还有其他问题或需要更多帮助,欢迎随时向我提问。继续加油,探索深度学习的奥秘吧!🚀🧠