物联网技术应用专业是学什么的,广州seo代理计费,安装网站模板,青岛政务网官网首页深度学习目标检测方法则是利用深度神经网络模型进行目标检测#xff0c;主要有以下几种#xff1a; R-CNN系列#xff1a;包括R-CNN、Fast R-CNN、Faster R-CNN等#xff0c;通过候选区域法生成候选目标区域#xff0c;然后使用卷积神经网络提取特征#xff0c;并通过分类… 深度学习目标检测方法则是利用深度神经网络模型进行目标检测主要有以下几种 R-CNN系列包括R-CNN、Fast R-CNN、Faster R-CNN等通过候选区域法生成候选目标区域然后使用卷积神经网络提取特征并通过分类器对每个候选区域进行分类。 SSDSingle Shot MultiBox Detector通过在特征图上利用不同大小和形状的卷积核进行目标检测同时预测目标的类别和位置。 YOLOYou Only Look Once将目标检测问题转化为回归问题通过将图像分割成网格单元并预测每个单元中是否存在目标以及目标的位置和类别。 RetinaNet通过引入Focal Loss解决目标检测中类别不平衡问题提高了小目标的检测效果。 YOLOv8是一种用于对象检测的深度学习模型它是YOLO系列模型的最新版本。本文介绍了基于Yolov8的任务的安全帽检测模型包括训练过程和数据准备过程同时提供了推理的代码。对准备计算机视觉相关的毕业设计的同学有着一定的帮助。
效果如下图带了安全帽的类别是helmet没带安全帽的head 示例2 一、yolov8安装
yolov8官方文档主页 - Ultralytics YOLOv8 文档
安装部分参考官方安装教程
二、数据集准备
本次使用的数据集是安全帽检测数据集其包含的示例图片如下 原数据集的格式为voc格式来自aistudio平台使用yolov8训练需要将voc格式转换为yolov8训练的格式本文提供转换好的数据集连接训练和验证图片、数据标签。
其中训练数据4000条验证数据1000条请将所有数据按照以下目录放置
|-images|--|-train|--|-val|-labels|--|-train|--|-val
三、模型训练
1、数据集配置文件
在ultralytics/ultralytics/cfg/datasets目录下添加anquanmao.yaml添加以下内容path修改为自己的路径
# Ultralytics YOLO , AGPL-3.0 license
# COCO 2017 dataset http://cocodataset.org by Microsoft
# Example usage: yolo train datacoco.yaml
# parent
# ├── ultralytics
# └── datasets
# └── coco ← downloads here (20.1 GB)# 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: datasets/helmet/HelmetDetection-yolov8 #改成你的数据集路径建议使用绝对路径
train: images/train
val: images/val
test: images/val # Classes
names:# 0: normal0: helmet1: head2: person
2、修改模型配置文件
在ultralytics/ultralytics/cfg/models/v8目录下添加yolov8n_helmet.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: 3 # number of classes
scales: # model compound scaling constants, i.e. modelyolov8n.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 GFLOPss: [0.33, 0.50, 1024] # YOLOv8s summary: 225 layers, 11166560 parameters, 11166544 gradients, 28.8 GFLOPsm: [0.67, 0.75, 768] # YOLOv8m summary: 295 layers, 25902640 parameters, 25902624 gradients, 79.3 GFLOPsl: [1.00, 1.00, 512] # YOLOv8l summary: 365 layers, 43691520 parameters, 43691504 gradients, 165.7 GFLOPsx: [1.00, 1.25, 512] # YOLOv8x summary: 365 layers, 68229648 parameters, 68229632 gradients, 258.5 GFLOPs# YOLOv8.0n 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, 3, C2f, [128, True]]- [-1, 1, Conv, [256, 3, 2]] # 3-P3/8- [-1, 6, C2f, [256, True]]- [-1, 1, Conv, [512, 3, 2]] # 5-P4/16- [-1, 6, C2f, [512, True]]- [-1, 1, Conv, [1024, 3, 2]] # 7-P5/32- [-1, 3, C2f, [1024, True]]- [-1, 1, SPPF, [1024, 5]] # 9# YOLOv8.0n head
head:- [-1, 1, nn.Upsample, [None, 2, nearest]]- [[-1, 6], 1, Concat, [1]] # cat backbone P4- [-1, 3, C2f, [512]] # 12- [-1, 1, nn.Upsample, [None, 2, nearest]]- [[-1, 4], 1, Concat, [1]] # cat backbone P3- [-1, 3, C2f, [256]] # 15 (P3/8-small)- [-1, 1, Conv, [256, 3, 2]]- [[-1, 12], 1, Concat, [1]] # cat head P4- [-1, 3, C2f, [512]] # 18 (P4/16-medium)- [-1, 1, Conv, [512, 3, 2]]- [[-1, 9], 1, Concat, [1]] # cat head P5- [-1, 3, C2f, [1024]] # 21 (P5/32-large)- [[15, 18, 21], 1, Detect, [nc]] # Detect(P3, P4, P5)3、训练模型
使用如下命令训练模型相关路径更改为自己的路径建议绝对路径
yolo detect train projectdeploy nameyolov8_helmet exist_okFalse optimizerauto valTrue ampTrue epochs100 imgsz640 modelultralytics/ultralytics/cfg/models/v8/yolov8_helmet.yaml dataultralytics/ultralytics/cfg/datasets/anquanmao.yaml4、验证模型
使用如下命令验证模型相关路径根据需要修改
yolo detect val imgsz640 modeldeploy/yolov8_helmet/weights/best.pt dataultralytics/ultralytics/cfg/datasets/anquanmao.yaml精度如下 四、推理
训练好了模型可以使用如下代码实现推理将权重放到同级目录
from PIL import Image
from ultralytics import YOLO# 加载预训练的YOLOv8n模型
model YOLO(best.pt)# 在bus.jpg上运行推理
image_path hard_hat_workers1.png
results model(image_path) # 结果列表# 展示结果
for r in results:im_array r.plot() # 绘制包含预测结果的BGR numpy数组im Image.fromarray(im_array[..., ::-1]) # RGB PIL图像im.show() # 显示图像im.save(results.jpg) # 保存图像五、相关资料下载
您可以在推理代码下载本文训练好的权重和推理代码。