YOLO诞生之前,目标检测研究领域主要以Faster-RCNN 为首的two-stage算法为主。two-stage算法通过卷积神经网络提取图片特征,再用RPN网络预测目标位置,最后再通过分类和回归来完成对候选目标位置和类别的判定,检测速度较慢但精度相对较高。而one-stage算法直接通过主干网络计算候选目标的类别和位置信息,其相较于two-stage 算法精度较低,但是网络运行的速度更快,内存占用更小,非常适合便携式设备。
YOLO系列是目前最热门的目标检测算法,那就拿它“开刀”了。YOLO目前已经更新到了YOLOv5,由于YOLOv5太新了,目前TensorFlow2版本还未出来(网上存在一些tf版本的,但是大概率不靠谱,就不踩坑了),那就用PyTorch吧,反正PyTorch迟早也是要学的嘛。
PyTorch与TensorFlow的区别如下:
TensorFlow:倾向于工程人员,落地比较好。
PyTorch: 比较倾向于研究人员,前沿算法用得比较多。
YOLOv5又分为Yolov5s、Yolov5m、Yolov5I、Yolov5x四个版本,我这里以Yolov5s为例。
YOLOv5 模型是 Ultralytics 公司于 2020 年 6 月 9 日公开发布的。它包括骨干网络、颈部和头部和输出.
(1)输入端:Mosaic数据增强、自适应锚框计算、自适应图片缩放
(2)Backbone:Focus结构,CSP结构
(3)Neck:FPN+PAN结构
(4)Prediction:GIOU_Loss
可以去github官网下载https://github.com/ultralytics/yolov5
本机在服务器Linux环境下安装。
cd yolov5-master/yolov5-master
/home/deep learning/Y1/yolov5-master/yolov5-master
ls
benchmarks.py detect.py [0m[01;34mmodels[0m/ setup.cfg val.py
[01;34mclassify[0m/ export.py README.md train.py
CONTRIBUTING.md hubconf.py requirements.txt tutorial.ipynb
[01;34mdata[0m/ LICENSE [01;34msegment[0m/ [01;34mutils[0m/
!pip install -r requirements.txt # install
需要的环境
Python>=3.6.0.
PyTorch> =1.7
以同样的方法打开cmd,输入命令: python path/to/detect.py --source path/to/img.jpg --weights yolov5s.pt --img 640,注意需要将图片路径替换成你的,
例如: python detect.py --source data\images\zidane.jpg --weights yolov5s.pt --img 640。这里我们使用的是官方已经帮我们训练好的模型。
在这个过程中,你中途可能会遇到这两个问题,不用担心,这是网络问题多试几次就好了。
TimeoutError: [winError 10060]由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。
TypeError: unlink( ) got an unexpected keyword argument 'missing_ok ’
如果你还是一直这样,将yolov5s.pt(提取码: bum6)文件放到项目的主目录下就ok了,上面的两个错误都是由于下载这个文件、下载失败缺少这个文件导致的。
命令运行成功后,你将会得到如下的反馈:
啊
import torch
# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s') # or yolov5n - yolov5x6, 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.
Using cache found in /home/liangjie/.cache/torch/hub/ultralytics_yolov5_master
[31m[1mrequirements:[0m YOLOv5 requirement "requests>=2.23.0" not found, attempting AutoUpdate...
Requirement already satisfied: requests>=2.23.0 in /home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages (2.28.1)
Requirement already satisfied: certifi>=2017.4.17 in /home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages (from requests>=2.23.0) (2022.6.15)
Requirement already satisfied: urllib3<1.27,>=1.21.1 in /home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages (from requests>=2.23.0) (1.24.3)
Requirement already satisfied: charset-normalizer<3,>=2 in /home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages (from requests>=2.23.0) (2.1.0)
Requirement already satisfied: idna<4,>=2.5 in /home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages (from requests>=2.23.0) (2.8)
[31m[1mrequirements:[0m 1 package updated per /home/liangjie/.cache/torch/hub/ultralytics_yolov5_master/requirements.txt
[31m[1mrequirements:[0m ⚠️ [1mRestart runtime or rerun command for updates to take effect[0m
YOLOv5 2022-12-2 Python-3.9.12 torch-1.13.0+cu117 CPU
Downloading https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt to yolov5s.pt...
ERROR:
Re-attempting https://github.com/ultralytics/yolov5/releases/download/v7.0/yolov5s.pt to yolov5s.pt...
######################################################################## 100.0%
Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
Adding AutoShape...
image 1/1: 720x1280 2 persons, 2 ties
Speed: 1066.8ms pre-process, 77.1ms inference, 33.3ms NMS per image at shape (1, 3, 384, 640)
results.show()
!python detect.py --source MyVideo_2.mp4
Traceback (most recent call last):
File "/home/liangjie/test/Modelwhale/deep learning/Y1/yolov5-master/yolov5-master/detect.py", line 43, in
from models.common import DetectMultiBackend
File "/home/liangjie/test/Modelwhale/deep learning/Y1/yolov5-master/yolov5-master/models/common.py", line 20, in
import pandas as pd
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/__init__.py", line 48, in
from pandas.core.api import (
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/api.py", line 48, in
from pandas.core.groupby import (
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/groupby/__init__.py", line 1, in
from pandas.core.groupby.generic import (
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/groupby/generic.py", line 70, in
from pandas.core.frame import DataFrame
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/frame.py", line 157, in
from pandas.core.generic import NDFrame
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/generic.py", line 152, in
from pandas.core.window import (
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/window/__init__.py", line 1, in
from pandas.core.window.ewm import ( # noqa:F401
File "/home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/core/window/ewm.py", line 12, in
import pandas._libs.window.aggregations as window_aggregations
ImportError: /lib64/libstdc++.so.6: version `GLIBCXX_3.4.29' not found (required by /home/liangjie/anaconda3/envs/newcdo/lib/python3.9/site-packages/pandas/_libs/window/aggregations.cpython-39-x86_64-linux-gnu.so)
这个问题是GLIBCXX_3.4.29’ not found ,确实没有3.4.29,需要更新到3.4.29z在运行