928 字
5 分钟
YOLO入门 5 DeepSort多目标追踪

前言#

这次尝试在 Yolov5 的基础上使用 DeepSort 进行多目标追踪。

使用的开源项目是 Yolov5_DeepSort_Pytorch

准备#

Yolov5_DeepSort_Pytorch#

使用作者给的指令,将项目和子模块一起 clone 下来:

Terminal window
git clone --recurse-submodules https://github.com/mikel-brostrom/Yolov5_DeepSort_Pytorch.git

进入项目,并根据 requirements.txt 安装需要的 Python 库:

Terminal window
pip3 install -r requirements.txt

Yolov5 模型#

推荐 YOLOv5s, 模型小,检测速度快,下载完成后把模型放在项目根目录里。

DeepSort 模型#

Model Zoo 下载需要的模型,但模型是存储在 Google Drive 上的,需要科学上网。

推荐下载 osnet_x0_25 ,该模型是 DeepSort 项目中的默认模型。

模型文件放到 ~/.cache/torch/checkpoints 中。

注意:这个路径可能不一定对,因为 Yolov5_DeepSort_Pytorch 的作者引用了另一个 github 上的项目 deep-person-reid ,是 DeepSort 的项目。 该项目应该是修改过模型路径,所以网上有人和我有不太一样的路径1。 如何判断具体的路径请跳转到 “报错” 一章中查看。

测试#

输入如下指令,进行目标检测:

Terminal window
python3 track.py --source 0 --yolo_model yolov5/yolov5s.pt --img 320

我使用的是 AMD Ryzen 7 PRO 4750U with Radeon Graphics

检测速度:

检测结果:

报错#

No module named ‘torchreid’#

No module named ‘xx’ 类型的错误基本上就是 Python 库缺失,但这个库无法通过 pip 指令进行安装。

方法1:在 git 项目时获取项目子模块

Terminal window
git clone --recurse-submodules https://github.com/mikel-brostrom/Yolov5_DeepSort_Pytorch.git

方法2:独立安装 torchreid 库

独立安装 torchreid 库,可以参考 torchreiddeep-person-reid 进行安装。

UserWarning: Cython evaluation …#

UserWarning: Cython evaluation (very fast so highly recommended) is unavailable, now use python evaluation.

这种 UserWarnging 错误,我见过很多次,本来只是报个错,结果直接项目卡死。

这里我们需要对报错代码进行注释2,打开如下文件:

Yolov5_DeepSort_Pytorch/deep_sort/deep/reid/torchreid/metrics/rank.py

注释掉以下内容:

warnings.warn(
'Cython evaluation (very fast so highly recommended) is '
'unavailable, now use python evaluation.'
)

… HTTPSConnectionPool(host=‘drive.google.com’, port=443): …#

requests.exceptions.ConnectionError: HTTPSConnectionPool(host=‘drive.google.com’, port=443): Max retries exceeded with url: /uc?id=1rb8UN5ZzPKRc_xvtHlyDh-cSz88YX9hs (Caused by NewConnectionError(‘<urllib3.connection.VerifiedHTTPSConnection object at 0x7f0d1e24f430>: Failed to establish a new connection: [Errno 101] Network is unreachable’))

该报错出现的原因是在指定文件没有模型,同时 DeepSort 的原作者希望在调用模型的时候检测模型是否存在,如果没有就从对应的网站下,但可惜的是国内访问 Google Drive 是无法访问的,我科学上网之后它又认为我是机器人不让下。

在这个项目中作者是通过例如 osnet_x0_25 、osnet_x0_5 等关键字,然后通过代码生成的路径,而不是通过指定路径来定位模型的。

# 这是作者给的参数, DeepSort 的模型默认是 osnet_x0_25)
parser.add_argument('--deep_sort_model', type=str, default='osnet_x0_25')

我看到的教程给出的步骤是把模型放到 Yolov5_DeepSort_Pytorch/deep_sort/deep/checkpoint 里,但有可能是项目的多次更新之后,作者修改了默认的路径,所以将模型放到教程的路径中后,仍然报错,然后我就尝试进行 Debug 。

我通过分析报错信息,直接找到调用爬虫的文件:

Yolov5_DeepSort_Pytorch/deep_sort/deep/reid/torchreid/models/osnet.py

Terminal window
File "/home/nono/DeepTraffic/Example/Yolov5_DeepSort_Pytorch/deep_sort/deep/reid/torchreid/models/osnet.py", line 480, in init_pretrained_weights
gdown.download(pretrained_urls[key], cached_file, quiet=False)

找到对应文件,发现了作者获取模型路径的代码,并且发现根本没有 checkpoint 的文件夹,而是 checkpoints:

torch_home = _get_torch_home()
model_dir = os.path.join(torch_home, 'checkpoints') ##这里有问题

尝试把 checkpoint 重命名为 checkpoints 进行尝试,发现报错依旧,于是我在该文件末尾增加了如下内容,以获取模型路径:

if __name__ == "__main__":
import os
import errno
import gdown
from collections import OrderedDict
def _get_torch_home():
ENV_TORCH_HOME = 'TORCH_HOME'
ENV_XDG_CACHE_HOME = 'XDG_CACHE_HOME'
DEFAULT_CACHE_DIR = '~/.cache'
torch_home = os.path.expanduser(
os.getenv(
ENV_TORCH_HOME,
os.path.join(
os.getenv(ENV_XDG_CACHE_HOME, DEFAULT_CACHE_DIR), 'torch'
)
)
)
return torch_home
torch_home = _get_torch_home()
model_dir = os.path.join(torch_home, 'checkpoints')
print(model_dir)

到这,我才得到了存放模型的正确位置:

/home/nono/.cache/torch/checkpoints/

Footnotes#

  1. YOLOV5+DeepSort行人统计

  2. 快速里德复制(market1501),fastreid,复现,Market1501

YOLO入门 5 DeepSort多目标追踪
https://fuwari.vercel.app/posts/人工智能/计算机视觉/yolo入门-5-deepsort多目标追踪/
作者
Asuwee
发布于
2022-01-20
许可协议
CC BY-NC-SA 4.0