当前位置:   article > 正文

nvidia TX2 CUDA yolov5环境搭建_loading weights: ../yolov5s.wts

loading weights: ../yolov5s.wts

本文记录笔者在 nvidia TX2 系统上搭建 yolov5 环境的过程。
注意说明的是,本文在文后的文章基础上进行实践,根据自己的经历进行描述和补充。由于能力有限,对本文涉及的知识和相关问题无法回答。
本文不涉及 yolo 深度学习方面的内容。

一、要点

本节对本文进行技术小结。

  • TX2 刷机后,PYthon版本为 3.7.1。
  • 保持T X2 联网,因为需要下载。
  • conda 创建环境,命令行前会有环境名称的提示。
  • 文后的尝试及记录,建议看看。

二、过程

准备工程源码

mkdir -p /opt/nvidia/deepstream/yolo
cd /opt/nvidia/deepstream/yolo
git clone https://github.com/DanaHan/Yolov5-in-Deepstream-5.0.git
git clone https://github.com/wang-xinyu/tensorrtx.git
git clone https://github.com/ultralytics/yolov5.git
  • 1
  • 2
  • 3
  • 4
  • 5

安装 conda

如无 conda,则安装之。到 https://github.com/Archiconda/build-tools/releases 下载。本文所用安装脚本为 Archiconda3-0.2.3-Linux-aarch64.sh 。下载到系统,再安装:

chmod +x Archiconda3-0.2.3-Linux-aarch64.sh

./Archiconda3-0.2.3-Linux-aarch64.sh

source ~/tx/.bashrc
  • 1
  • 2
  • 3
  • 4
  • 5

创建 conda 环境

使用如下命令创建名为 yolov5 的 conda 环境:

$ conda create -n yolov5 python
  • 1

此过程需要手动输入 y 以安装软件包。安装时长取决于联网速度。

成功后,输入conda activate yolov5激活,使用conda deactivate退出。过程日志信息详见文后附录。
进入 conda 环境后,命令行前有环境名称 yolov5,如:

(yolov5) tx@tx-desktop:/opt/nvidia/deepstream/yolo$ 
  • 1

安装 yolo 依赖库

切换分支:

cd /opt/nvidia/deepstream/yolo/yolov5
git checkout -b v4.0 v4.0
  • 1
  • 2

安装:

pip install scikit-build
pip install -r requirements.txt

  • 1
  • 2
  • 3

执行后命令,提示:

ERROR: Could not find a version that satisfies the requirement torchvision>=0.8.1
ERROR: No matching distribution found for torchvision>=0.8.1
  • 1
  • 2

安装 torchvision:

conda install torchvision -c pytorch
  • 1

在实践中发现该命令无法从官方途径安装。故从源码安装。
到 https://github.com/pytorch/vision/releases 下载源码压缩包,本文所用版本 v0.9.0,下载,并安装:

tar xf vision-0.9.0.tar.gz
cd vision-0.9.0
python setup.py install
  • 1
  • 2
  • 3

安装过程可能提示没有 torch 模块:

ModuleNotFoundError: No module named 'torch'
  • 1

安装之:

pip install torch
  • 1

torchvision编译需要等待一段时间。

依赖包安装完成后提示:

Successfully installed Cython-0.29.22 PyYAML-5.4.1 absl-py-0.12.0 cachetools-4.2.1 chardet-4.0.0 cycler-0.10.0 google-auth-1.27.1 google-auth-oauthlib-0.4.3 grpcio-1.36.1 idna-2.10 kiwisolver-1.3.1 markdown-3.3.4 matplotlib-3.3.4 oauthlib-3.1.0 opencv-python-4.5.1.48 pandas-1.2.3 protobuf-3.15.6 pyasn1-0.4.8 pyasn1-modules-0.2.8 pycocotools-2.0.2 python-dateutil-2.8.1 pytz-2021.1 requests-2.25.1 requests-oauthlib-1.3.0 rsa-4.7.2 scipy-1.6.1 seaborn-0.11.1 six-1.15.0 tensorboard-2.4.1 tensorboard-plugin-wit-1.8.0 thop-0.0.31.post2005241907 tqdm-4.59.0 urllib3-1.26.3 werkzeug-1.0.1
  • 1

在yolov5目录下:

cd /opt/nvidia/deepstream/yolo/yolov5
  • 1

下载模型文件:

bash weights/download_weights.sh
  • 1

默认下载到yolov5目录,有文件:yolov5l.pt yolov5m.pt yolov5s.pt yolov5x.pt。可根据实际情况选,本文使用默认值,即yolov5s.pt文件。拷贝到 weights 目录。

继续在 yolov5 目录下,拷贝 gen_wts.py。

cp ../tensorrtx/yolov5/gen_wts.py ./
  • 1

注意,可修改 gen_wts.py 文件,主要修改'weights/yolov5x.pt' 'yolov5x.wts'名称。

生成wts文件:

python gen_wts.py 
  • 1

最终生成文件:yolov5x.wts。将其拷贝到 Yolov5-in-Deepstream-5.0 目录,并切换到该目录。

cp yolov5s.wts ../Yolov5-in-Deepstream-5.0
cd ../Yolov5-in-Deepstream-5.0
  • 1
  • 2

根据实际情况,修改 yolov5.cpp 文件,将 NET 宏改成自己对应的模型。本文使用默认值 s,无须改动。

#define NET s  // s m l x
  • 1

编译:

mkdir build
cd build
cmake ..
make
  • 1
  • 2
  • 3
  • 4

生成 yolov5 文件,后面将用该文件生成模型和测试。

生成engine文件:

sudo ./yolov5 -s
  • 1

注意:程序使用了…/yolov5x.wts,所以 yolov5x.wts 要拷贝 Yolov5-in-Deepstream-5.0 目录。成功输出如下信息:

$sudo ./yolov5 -s
Loading weights: ../yolov5s.wts
Building engine, please wait for a while...
Build engine successfully!
  • 1
  • 2
  • 3
  • 4

测试:

mkdir ../samples
  • 1

从网上下载两张 coco 数据集图片,放到 samples 上。

sudo ./yolov5 -d  ../samples
  • 1

在build可看到有图片文件生成,且标注了,说明成功。

将 libmyplugins.so yolov5s.engine 拷贝到 Deepstream 5.0 目录。

cp yolov5x.engine ../Deepstream\ 5.0/
cp libmyplugins.so ../Deepstream\ 5.0/
  • 1
  • 2

Deepstream 测试

在 Yolov5-in-Deepstream-5.0\Deepstream 5.0\nvdsinfer_custom_impl_Yolo 目录执行make,生成libnvdsinfer_custom_impl_Yolo.so文件。进入 Yolov5-in-Deepstream-5.0\Deepstream 5.0 目录中,修改 config_infer_primary_yoloV5.tx 文件。主要修改:

model-engine-file=yolov5s.engine --> model-engine-file=yolov5x.engine

custom-lib-path=objectDetector_Yolo_V5/nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so --> custom-lib-path=nvdsinfer_custom_impl_Yolo/libnvdsinfer_custom_impl_Yolo.so
  • 1
  • 2
  • 3

将 DeepStream 的 label.txt 拷贝到当前目录(本文使用的是 /opt/nvidia/deepstream/deepstream-5.0/sources/objectDetector_Yolo/labels.txt )。修改 deepstream_app_config_yoloV5.txt 文件的视频地址(本文修改为 uri=file:/opt/nvidia/deepstream/deepstream-5.0/samples/streams/sample_720p.mp4)。使用官方 deepstream-app 程序测试,执行:

LD_PRELOAD=./libmyplugins.so deepstream-app -c deepstream_app_config_yoloV5.txt
  • 1

可得到识别的结果。

三、参考资源

DeepStream5.0系列之yolov5使用: https://blog.csdn.net/zong596568821xp/article/details/109444343
pytorch源码:https://github.com/pytorch/vision/releases

附1:尝试

一般错误

ModuleNotFoundError: No module named 'Cython'
--> python3.7 -m pip install cython

Could not find a version that satisfies the requirement cmake

--> python3.7 -m pip install cmake

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

torchvision

找不到 torchvision 模块提示如下:

ERROR: Could not find a version that satisfies the requirement torchvision>=0.8.1
ERROR: No matching distribution found for torchvision>=0.8.1
  • 1
  • 2

选按通用安装方式:

python3.7 -m pip install torchvision 
  • 1

但错误提示依旧。按网上说法使用如下命令安装:

pip3 install torchvision==0.8.1 -f https://download.pytorch.org/whl/torch_stable.html
  • 1

但依然失败。再用官方 提供的命令:

conda install torchvision -c pytorch
  • 1

还是失败。究其原因,可能是没有 aarch64 版本的缘故。所以用源码安装。

生成 egine 文件时提示:

Loading weights: ../yolov5x.wts
[03/10/2021-01:00:59] [E] [TRT] (Unnamed Layer* 14) [Convolution]: kernel weights has count 0 but 12800 was expected
[03/10/2021-01:00:59] [E] [TRT] (Unnamed Layer* 14) [Convolution]: count of 0 weights in kernel, but kernel dimensions (1,1) with 160 input channels, 80 output channels and 1 groups were specified. Expected Weights count is 160 * 1*1 * 80 / 1 = 12800
[03/10/2021-01:00:59] [E] [TRT] Parameter check failed at: ../builder/Network.cpp::addScale::482, condition: shift.count > 0 ? (shift.values != nullptr) : (shift.values == nullptr)
yolov5: /opt/nvidia/deepstream/yolo-in-Deepstream-5.0/common.hpp:190: nvinfer1::IScaleLayer* addBatchNorm2d(nvinfer1::INetworkDefinition*, std::map<std::__cxx11::basic_string<char>, nvinfer1::Weights>&, nvinfer1::ITensor&, std::__cxx11::string, float): Assertion `scale_1' failed.
Aborted
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6

原因是使用 yolov5x 版本,改为默认的 yolov5s 不再出现。
使用 yolov5s.wts 在 AGX Xavier 平台上出现的错误:

 sudo ./yolov5 -s
Loading weights: ../yolov5s.wts
[03/12/2021-16:06:07] [E] [TRT] (Unnamed Layer* 14) [Convolution]: kernel weights has count 0 but 2048 was expected
[03/12/2021-16:06:07] [E] [TRT] (Unnamed Layer* 14) [Convolution]: count of 0 weights in kernel, but kernel dimensions (1,1) with 64 input channels, 32 output channels and 1 groups were specified. Expected Weights count is 64 * 1*1 * 32 / 1 = 2048
[03/12/2021-16:06:07] [E] [TRT] Parameter check failed at: ../builder/Network.cpp::addScale::482, condition: shift.count > 0 ? (shift.values != nullptr) : (shift.values == nullptr)
yolov5: /opt/nvidia/deepstream/yolo/Yolov5-in-Deepstream-5.0/common.hpp:190: nvinfer1::IScaleLayer* addBatchNorm2d(nvinfer1::INetworkDefinition*, std::map<std::__cxx11::basic_string<char>, nvinfer1::Weights>&, nvinfer1::ITensor&, std::__cxx11::string, float): Assertion `scale_1' failed.
Aborted
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

使用不同平台的 engine 文件的错误(TX2生成,AGX Xavier运行):

$ sudo ./yolov5 -d  ../samples
[03/12/2021-16:12:13] [E] [TRT] INVALID_CONFIG: The engine plan file is generated on an incompatible device, expecting compute 7.2 got compute 6.2, please rebuild.
[03/12/2021-16:12:13] [E] [TRT] engine.cpp (1546) - Serialization Error in deserialize: 0 (Core engine deserialization failure)
[03/12/2021-16:12:13] [E] [TRT] INVALID_STATE: std::exception
[03/12/2021-16:12:13] [E] [TRT] INVALID_CONFIG: Deserialize the cuda engine failed.
yolov5: /opt/nvidia/deepstream/yolo/Yolov5-in-Deepstream-5.0/yolov5.cpp:534: int main(int, char**): Assertion `engine != nullptr' failed.
Aborted
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

测试时,如果 samples 目录存在非图片时,会提示:

740ms
terminate called after throwing an instance of 'cv::Exception'
  what():  OpenCV(4.1.1) /home/nvidia/host/build_opencv/nv_opencv/modules/imgcodecs/src/loadsave.cpp:662: error: (-2:Unspecified error) could not find a writer for the specified extension in function 'imwrite_'
  • 1
  • 2
  • 3

解决:图片后缀需正确,如jpg、png。

创建 yolo 环境输出日志

$ conda create -n yolov5 python
Solving environment: done

## Package Plan ##

  environment location: /home/tx/archiconda3/envs/yolov5

  added / updated specs: 
    - python

The following packages will be downloaded:

    package                    |            build
    ---------------------------|-----------------
    zlib-1.2.11                |       h7b6447c_2         118 KB  c4aarch64
    openssl-1.0.2p             |       h7b6447c_0         3.1 MB  c4aarch64
    sqlite-3.25.2              |       h7ce4240_0         2.2 MB  c4aarch64
    readline-7.0               |       h7ce4240_5         440 KB  c4aarch64
    pip-10.0.1                 |           py37_0         1.7 MB  c4aarch64
    wheel-0.32.1               |           py37_0          34 KB  c4aarch64
    libgcc-ng-7.3.0            |       h5c90dd9_0         5.9 MB  c4aarch64
    libstdcxx-ng-7.3.0         |       h5c90dd9_0         2.5 MB  c4aarch64
    setuptools-40.4.3          |           py37_0         601 KB  c4aarch64
    xz-5.2.4                   |       h7ce4240_4         345 KB  c4aarch64
    certifi-2018.10.15         |           py37_0         137 KB  c4aarch64
    ncurses-6.1                |       h71b71f5_0         1.0 MB  c4aarch64
    tk-8.6.8                   |       hbc83047_0         3.2 MB  c4aarch64
    ca-certificates-2018.03.07 |                0         123 KB  c4aarch64
    python-3.7.2               |       he90a169_0        36.1 MB  c4aarch64
    libffi-3.2.1               |       h71b71f5_5          51 KB  c4aarch64
    libedit-3.1.20170329       |       hc058e9b_2         188 KB  c4aarch64
    ------------------------------------------------------------
                                           Total:        57.8 MB

The following NEW packages will be INSTALLED:

    ca-certificates: 2018.03.07-0            c4aarch64
    certifi:         2018.10.15-py37_0       c4aarch64
    libedit:         3.1.20170329-hc058e9b_2 c4aarch64
    libffi:          3.2.1-h71b71f5_5        c4aarch64
    libgcc-ng:       7.3.0-h5c90dd9_0        c4aarch64
    libstdcxx-ng:    7.3.0-h5c90dd9_0        c4aarch64
    ncurses:         6.1-h71b71f5_0          c4aarch64
    openssl:         1.0.2p-h7b6447c_0       c4aarch64
    pip:             10.0.1-py37_0           c4aarch64
    python:          3.7.2-he90a169_0        c4aarch64
    readline:        7.0-h7ce4240_5          c4aarch64
    setuptools:      40.4.3-py37_0           c4aarch64
    sqlite:          3.25.2-h7ce4240_0       c4aarch64
    tk:              8.6.8-hbc83047_0        c4aarch64
    wheel:           0.32.1-py37_0           c4aarch64
    xz:              5.2.4-h7ce4240_4        c4aarch64
    zlib:            1.2.11-h7b6447c_2       c4aarch64

Proceed ([y]/n)? y

Preparing transaction: done
Verifying transaction: done
Executing transaction: done
#
# To activate this environment, use
#
#     $ conda activate yolov5
#
# To deactivate an active environment, use
#
#     $ conda deactivate

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68

附2:成果

1、文中涉及的软件包只在 TX2 测试通过,已整合为 aarch64 系统的 docker 镜像,并上传至阿里云仓库备份。
Dockerfile:

FROM busybox 
RUN mkdir /yolotools  
COPY vision-0.9.0.tar.gz  /yolotools
COPY yolo.tar.bz2  /yolotools
COPY Archiconda3-0.2.3-Linux-aarch64.sh  /yolotools
  • 1
  • 2
  • 3
  • 4
  • 5

创建并上传:

docker build -t registry.cn-hangzhou.aliyuncs.com/latelee/yoloenv:aarch64 .
docker push registry.cn-hangzhou.aliyuncs.com/latelee/yoloenv:aarch64
  • 1
  • 2

镜像说明:yolo.tar.bz2包含了本文的工程源码。运行后查看容器的 /yolotools 目录。

docker run -itd --name foobar registry.cn-hangzhou.aliyuncs.com/latelee/yoloenv:aarch64 sh
docker exec -it foobar ls /yolotools
  • 1
  • 2

在宿主机拷贝文件出来:

mkdir tools
docker cp foobar:/yolotools/vision-0.9.0.tar.gz tools
docker cp foobar:/yolotools/yolo.tar.bz2 tools
docker cp foobar:/yolotools/Archiconda3-0.2.3-Linux-aarch64.sh tools
  • 1
  • 2
  • 3
  • 4

再附

一段时间来,github 和 gitlab 官方经常打不开,在访问网站上较耗时。不知何故。

李迟 2021.3.12 周五

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/101429
推荐阅读
相关标签
  

闽ICP备14008679号