YOLOv5目标检测

YOLOv5目标检测

一、环境

  • ubuntu 18.04 64bit
  • GTX 1070Ti
  • anaconda with python 3.8
  • pytorch 1.7
  • cuda 10.1

二、前言

YOLOV5最近更新了v6.0版本,v5.0与v6.0有那些区别:

这个新版本在 V5.0 的基础上集成了很多的新特性,而且在网络结构上也做了微调,引入了全新的更小( Nano )的模型 P5(YOLOv5n) P6(YOLOv5n6)。Nano 模型保持了 yolov5s 模型的深度( depth ),宽度( width ) 则是从0.5降到了0.25,经过这个操作后,总参数减少了 75%,从 7.5M 缩小到了 1.9M,这样的话,就非常适合于移动端或者是 CPU 的环境。

yolov5 v6.0

  • 新特性

  • 整合了 Roboflow

    roboflow 前面我们提过了,它公开了很多非常有用的数据集,在 v6.0 上可以直接使用他们的数据集,参考 github.com/ultralytics…,非常的方便

  • 支持 tensorflowkeras模型的导出

    使用 python export.py --include saved_model pb tflite tfjs 就可以完成 tensorFlowkerastflitetf.js 模型的导出

  • 同时支持 OpenCV DNNONNX Runtime

    导出的 onnx 同时支持 opencv dnnonnx runtime

    python export --weights yolov5s.pt --include onnx --opset 12 --dynamic
    复制代码
    

    在检测的时候也可以使用指定的 onnxpython detect.py --weights yolov5s.onnx --dnn

  • 模型结构

    • Conv(k=6, s=2, p=2) 代替 Focus 层,主要是为了方便模型导出
    • 使用 SPPF 代替 SPP
    • 减少 P3 主干层 C3
    • SPPF 放在主干的后面
    • 在最后一个 C3 主干层中重新引入快捷方式
    • 更新超参数
  • 增加了 Flask REST API

    提供了 web api 的支持,远端测试非常方便,常见的开放平台都是这么做的

三、项目开发

1.YOLOV5项目下载

现在已经更新到v6.0版本,根据自己项目的特性,选择版本

下载网址

2.解压YOLOV5并部署环境

  • 解压后,在PyCharm中打开

  • Pytorch环境配置

在这里插入图片描述

  • 下载依赖包
    在这里插入图片描述

3.YOLOV5详解

3.1 YOLOV5预训练模型比较
Model size (pixels) mAPval 0.5:0.95 mAPval 0.5 Speed CPU b1 (ms) Speed V100 b1 (ms) Speed V100 b32 (ms) params (M) FLOPs @640 (B)
YOLOv5n 640 28.4 46.0 45 6.3 0.6 1.9 4.5
YOLOv5s 640 37.2 56.0 98 6.4 0.9 7.2 16.5
YOLOv5m 640 45.2 63.9 224 8.2 1.7 21.2 49.0
YOLOv5l 640 48.8 67.2 430 10.1 2.7 46.5 109.1
YOLOv5x 640 50.7 68.9 766 12.1 4.8 86.7 205.7
YOLOv5n6 1280 34.0 50.7 153 8.1 2.1 3.2 4.6
YOLOv5s6 1280 44.5 63.0 385 8.2 3.6 16.8 12.6
YOLOv5m6 1280 51.0 69.0 887 11.1 6.8 35.7 50.0
YOLOv5l6 1280 53.6 71.6 1784 15.8 10.5 76.8 111.4
YOLOv5x6 + TTA 1280 1536 54.7 55.4 72.4 72.3 3136 - 26.2 - 19.4 - 140.7 - 209.8 -

img

img

3.2 通过指令运行项目
  • nstall下载依赖

Python>=3.6.0 is required with all requirements.txt installed including PyTorch>=1.7:

$ git clone https://github.com/ultralytics/yolov5
$ cd yolov5
$ pip install -r requirements.txt
  • Inference推理

使用 YOLOv5 和 PyTorch Hub 进行推理。模型会自动从最新的 YOLOv5 版本下载。

import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')  # or yolov5m, yolov5l, yolov5x, custom
# Images
img = 'https://ultralytics.com/images/zidane.jpg'  # or file, Path, PIL, OpenCV, numpy, list
# Inference
results = model(img)
# Results
results.print()  # or .show(), .save(), .crop(), .pandas(), etc.

要对数据/图像中的示例图像运行推理,请执行以下操作:

# --weights yolov5s.pt指定yolov5s.pt为项目的权重;
# --conf 0.25指定0.25为失信度;
# --source data/images指定文件路径
$ python detect.py --source data/images --weights yolov5s.pt --conf 0.25

# 运行结果如下
Namespace(agnostic_nms=False, augment=False, classes=None, conf_thres=0.25, device='', exist_ok=False, img_size=640, iou_thres=0.45, name='exp', project='runs/detect', save_conf=False, save_txt=False, source='data/images/', update=False, view_img=False, weights=['yolov5s.pt'])
YOLOv5 v4.0-96-g83dc1b4 torch 1.7.0+cu101 CUDA:0 (Tesla V100-SXM2-16GB, 16160.5MB)

Fusing layers... 
Model Summary: 224 layers, 7266973 parameters, 0 gradients, 17.0 GFLOPS
image 1/2 /content/yolov5/data/images/bus.jpg: 640x480 4 persons, 1 bus, Done. (0.010s)
image 2/2 /content/yolov5/data/images/zidane.jpg: 384x640 2 persons, 1 tie, Done. (0.011s)
Results saved to runs/detect/exp2
Done. (0.103s)
  • Inference with detect.py 通过 detect.py 推理

detect.py 在各种源上运行推理,从最新的YOLOv5版本自动下载模型,并将结果保存到运行/检测(runs/detect.)。

$ python detect.py --source 0  # webcam
                            file.jpg  # image 
                            file.mp4  # video
                            path/  # directory
                            path/*.jpg  # glob
                            'https://youtu.be/NUsoVlDFqZg'  # YouTube
                            'rtsp://example.com/media.mp4'  # RTSP, RTMP, HTTP stream 通过传输协议可以实现实时检测    
  • Training 训练模型

运行以下命令以重现 COCO 数据集上的结果(首次使用时会自动下载数据集)。在单个 V100 上,YOLOv5s/m/l/x 的训练时间为 2/4/6/8 天(多 GPU 时间更快)。使用 GPU 允许的最大批大小(显示 16 GB 设备的批大小)。

$ python train.py --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 64
                                         yolov5m                                40
                                         yolov5l                                24
                                         yolov5x                                16

img

3.3 YOLOV5参数讲解
  • detect.py文件中main函数详解
# --weights  default='yolov5s.pt'权重文件的路径地址
   parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
# --source   default='data/images'给神经网络指定数据输入,可以用来实时检测和验证模型使用,输入0是使用电脑自带摄像
    parser.add_argument('--source', type=str, default='data/images', help='source')  # file/folder, 0 for webcam
# --img-size   default=640指定输入图片的分辨率,输出的图片格式不会被改变(只是在网络运算过程中把图片进行一个缩放或扩大方便运算)
    parser.add_argument('--img-size', type=int, default=640, help='inference size (pixels)')
# --conf-thres  default=0.25置信度(只要大于0.25,就显示出目标)
    parser.add_argument('--conf-thres', type=float, default=0.25, help='object confidence threshold')
# --iou-thres  default=0.45目标上的框,iou是选出
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

石先森很疯狂

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值