小言_互联网的博客

Wider Face+YOLOV8人脸检测

849人阅读  评论(0)

YOLO系列的算法更新实在太快了,前些天刚学习完YOLOV7,YOLOV8就出来了。今天先理解模型的训练过程,后续再学习V8的网络结构等细节。

YOLOV8源码链接https://github.com/ultralytics/ultralytics

1 数据格式转换

Wider Face数据格式转YOLO数据格式可以参考我之前写的一篇博客:

https://blog.csdn.net/qq_38964360/article/details/128712287?spm=1001.2014.3001.5502

2 修改相关配置文件

首先是模型配置文件'ultralytics/models/v8/yolov8n.yaml',因为人脸检测是单目标检测,因此该配置文件里的nc应该改成1,部分代码如下:


   
  1. # Ultralytics YOLO 🚀, GPL-3.0 license
  2. # Parameters
  3. nc: 1 # number of classes
  4. depth_multiple: 0.33 # scales module repeats
  5. width_multiple: 0.25 # scales convolution channels

随后仿照 'yolov8/ultralytics/yolo/data/datasets/coco128.yaml' 文件,新建 'yolov8/ultralytics/yolo/data/datasets/wider_face.yaml' 文件,文件内容如下:


   
  1. # 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, ..]
  2. path: /kaxier01/projects/FAS/yolov8/datasets/wider_face # dataset root dir
  3. train: images/train # train images (relative to 'path') 12876 images
  4. val: images/val # val images (relative to 'path') 3226 images
  5. test: # test images (optional)
  6. # Classes
  7. names:
  8. 0: face
  9. # Download script/URL (optional)
  10. download:

数据集文件目录如下(images以及labels均由步骤1生成):

最后修改 'yolov8/ultralytics/yolo/configs/default.yaml' 文件中的参数,如:batch size、device、lr、损失权重等,代码如下:


   
  1. # Ultralytics YOLO 🚀, GPL-3.0 license
  2. # Default training settings and hyperparameters for medium-augmentation COCO training
  3. task: "detect" # choices=['detect', 'segment', 'classify', 'init'] # init is a special case. Specify task to run.
  4. mode: "train" # choices=['train', 'val', 'predict'] # mode to run task in.
  5. # Train settings -------------------------------------------------------------------------------------------------------
  6. model: null # i.e. yolov8n.pt, yolov8n.yaml. Path to model file
  7. data: null # i.e. coco128.yaml. Path to data file
  8. epochs: 300 # number of epochs to train for
  9. patience: 50 # TODO: epochs to wait for no observable improvement for early stopping of training
  10. batch: 32 # number of images per batch
  11. imgsz: 640 # size of input images
  12. save: True # save checkpoints
  13. cache: False # True/ram, disk or False. Use cache for data loading
  14. device: 0, 1, 2, 3 # cuda device, i.e. 0 or 0,1,2,3 or cpu. Device to run on
  15. workers: 16 # number of worker threads for data loading
  16. project: null # project name
  17. name: null # experiment name
  18. exist_ok: False # whether to overwrite existing experiment
  19. pretrained: False # whether to use a pretrained model
  20. optimizer: 'SGD' # optimizer to use, choices=['SGD', 'Adam', 'AdamW', 'RMSProp']
  21. verbose: False # whether to print verbose output
  22. seed: 0 # random seed for reproducibility
  23. deterministic: True # whether to enable deterministic mode
  24. single_cls: True # train multi-class data as single-class
  25. image_weights: False # use weighted image selection for training
  26. rect: False # support rectangular training
  27. cos_lr: False # use cosine learning rate scheduler
  28. close_mosaic: 10 # disable mosaic augmentation for final 10 epochs
  29. resume: False # resume training from last checkpoint
  30. # Segmentation
  31. overlap_mask: True # masks should overlap during training
  32. mask_ratio: 4 # mask downsample ratio
  33. # Classification
  34. dropout: 0.0 # use dropout regularization
  35. # Val/Test settings ----------------------------------------------------------------------------------------------------
  36. val: True # validate/test during training
  37. save_json: False # save results to JSON file
  38. save_hybrid: False # save hybrid version of labels (labels + additional predictions)
  39. conf: null # object confidence threshold for detection (default 0.25 predict, 0.001 val)
  40. iou: 0.7 # intersection over union (IoU) threshold for NMS
  41. max_det: 300 # maximum number of detections per image
  42. half: False # use half precision (FP16)
  43. dnn: False # use OpenCV DNN for ONNX inference
  44. plots: True # show plots during training
  45. # Prediction settings --------------------------------------------------------------------------------------------------
  46. source: null # source directory for images or videos
  47. show: False # show results if possible
  48. save_txt: False # save results as .txt file
  49. save_conf: False # save results with confidence scores
  50. save_crop: False # save cropped images with results
  51. hide_labels: False # hide labels
  52. hide_conf: False # hide confidence scores
  53. vid_stride: 1 # video frame-rate stride
  54. line_thickness: 3 # bounding box thickness (pixels)
  55. visualize: False # visualize results
  56. augment: False # apply data augmentation to images
  57. agnostic_nms: False # class-agnostic NMS
  58. retina_masks: False # use retina masks for object detection
  59. # Export settings ------------------------------------------------------------------------------------------------------
  60. format: torchscript # format to export to
  61. keras: False # use Keras
  62. optimize: False # TorchScript: optimize for mobile
  63. int8: False # CoreML/TF INT8 quantization
  64. dynamic: False # ONNX/TF/TensorRT: dynamic axes
  65. simplify: False # ONNX: simplify model
  66. opset: 17 # ONNX: opset version
  67. workspace: 4 # TensorRT: workspace size (GB)
  68. nms: False # CoreML: add NMS
  69. # Hyperparameters ------------------------------------------------------------------------------------------------------
  70. lr0: 0.02 # initial learning rate (SGD=1E-2, Adam=1E-3)
  71. lrf: 0.01 # final OneCycleLR learning rate (lr0 * lrf)
  72. momentum: 0.937 # SGD momentum/Adam beta1
  73. weight_decay: 0.0005 # optimizer weight decay 5e-4
  74. warmup_epochs: 3.0 # warmup epochs (fractions ok)
  75. warmup_momentum: 0.8 # warmup initial momentum
  76. warmup_bias_lr: 0.1 # warmup initial bias lr
  77. box: 7.5 # box loss gain
  78. cls: 0.5 # cls loss gain (scale with pixels)
  79. dfl: 1.5 # dfl loss gain
  80. fl_gamma: 0.0 # focal loss gamma (efficientDet default gamma=1.5)
  81. label_smoothing: 0.0
  82. nbs: 64 # nominal batch size
  83. hsv_h: 0.015 # image HSV-Hue augmentation (fraction)
  84. hsv_s: 0.7 # image HSV-Saturation augmentation (fraction)
  85. hsv_v: 0.4 # image HSV-Value augmentation (fraction)
  86. degrees: 0.0 # image rotation (+/- deg)
  87. translate: 0.1 # image translation (+/- fraction)
  88. scale: 0.5 # image scale (+/- gain)
  89. shear: 0.0 # image shear (+/- deg)
  90. perspective: 0.0 # image perspective (+/- fraction), range 0-0.001
  91. flipud: 0.0 # image flip up-down (probability)
  92. fliplr: 0.5 # image flip left-right (probability)
  93. mosaic: 1.0 # image mosaic (probability)
  94. mixup: 0.0 # image mixup (probability)
  95. copy_paste: 0.0 # segment copy-paste (probability)
  96. # Hydra configs --------------------------------------------------------------------------------------------------------
  97. cfg: null # for overriding defaults.yaml
  98. hydra:
  99. output_subdir: null # disable hydra directory creation
  100. run:
  101. dir: .
  102. # Debug, do not modify -------------------------------------------------------------------------------------------------
  103. v5loader: False # use legacy YOLOv5 dataloader

3 训练及验证

参考源码中的README.md 文件,安装相关依赖库,

pip install ultralytics

模型的训练、验证及预测都有两种实现方式:

1)使用Command Line Interface (CLI)。指令如下:


   
  1. # 单卡训练
  2. yolo task=detect mode=train model=yolov8n.pt data=coco128.yaml device= 0
  3. # 多卡训练
  4. yolo task=detect mode=train model=yolov8n.pt data=coco128.yaml device=\ '0,1,2,3\'
  5. # Syntax
  6. yolo task=detect mode=train model=yolov8n.yaml args...
  7. classify predict yolov8n-cls.yaml args...
  8. segment val yolov8n-seg.yaml args...
  9. export yolov8n.pt format=onnx args...

2)使用Python。新建一个脚本,代码如下:


   
  1. # filename: python_example.py
  2. # dir: yolov8/python_example.py
  3. from ultralytics import YOLO
  4. ## 以下模型初始化指令选一个就行
  5. model = YOLO( "yolov8/ultralytics/models/v8/yolov8n.yaml") # 从头开始训练
  6. model = YOLO( "yolov8/weights/yolov8n.pt") # 或者加载预训练好的模型
  7. # 模型训练
  8. results = model.train(data= "yolov8/ultralytics/yolo/data/datasets/wider_face.yaml", epochs= 300)
  9. # 模型验证
  10. results = model.val()
  11. # 模型导出
  12. success = model.export( format= "onnx")

使用以下指令便可多卡训练模型(我这里用了4卡训练,把default.yaml的device值改为0,1,2,3):

python -m torch.distributed.launch --nproc_per_node 4 --master_port 9527 yolov8/python_example.py

数据集加载相关文件:'yolov8/ultralytics/yolo/data/dataloaders/v5loader.py'

数据增强相关文件:'yolov8/ultralytics/yolo/data/dataloaders/v5augmentations.py'

模型定义相关文件:'yolov8/ultralytics/yolo/engine/model.py'

模型训练相关文件:'yolov8/ultralytics/yolo/engine/trainer.py'

模型训练过程

模型验证过程

测试结果


转载:https://blog.csdn.net/qq_38964360/article/details/128728145
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场