轻松搭建Yolov5 GPU运行环境
转载请注明出处 2022年5月21日 @家有一亩三分地
轻松搭建Yolov5 GPU运行环境
下载yolov5程序
https://github.com/ultralytics/yolov5.git
PS D:\ptwork\git> git clone https://github.com/ultralytics/yolov5.git
Cloning into 'yolov5'...
remote: Enumerating objects: 13641, done.
remote: Counting objects: 100% (165/165), done.
remote: Compressing objects: 100% (75/75), done.
remote: Total 13641 (delta 114), reused 131 (delta 90), pack-reused 13476
Receiving objects: 100% (13641/13641), 12.18 MiB | 1.05 MiB/s, done.
Resolving deltas: 100% (9514/9514), done.
conda 配置环境
需要先安装conda环境,本机使用miniconda3
创建虚拟环境
conda create -n yolov5 python==3.9 -y
激活环境
(yolov5) D:\ptwork\git\yolov5>conda activate yolov5
(yolov5) D:\ptwork\git\yolov5>
安装gpu版本pytorch
查看本机gpu,
(yolov5) D:\ptwork\git\yolov5>nvidia-smi
Sat May 21 16:56:49 2022
+-----------------------------------------------------------------------------+
| NVIDIA-SMI 511.69 Driver Version: 511.69 CUDA Version: 11.6 |
|-------------------------------+----------------------+----------------------+
| GPU Name TCC/WDDM | Bus-Id Disp.A | Volatile Uncorr. ECC |
| Fan Temp Perf Pwr:Usage/Cap| Memory-Usage | GPU-Util Compute M. |
| | | MIG M. |
|===============================+======================+======================|
| 0 NVIDIA GeForce ... WDDM | 00000000:2B:00.0 Off | N/A |
| N/A 46C P3 N/A / N/A | 0MiB / 2048MiB | 3% Default |
| | | N/A |
+-------------------------------+----------------------+----------------------+
显示使用笔记本的cuda版本是11.6,需要去pytorch下载对应的版本https://pytorch.org/get-started/locally/
因为官网没有对应的cuda版本,这里采用了最新的版本
conda install pytorch torchvision torchaudio cudatoolkit=11.3 -c pytorch
安装完成,测试GPU是否可用
(yolov5) D:\ptwork\git\yolov5>python
Python 3.9.0 (default, Nov 15 2020, 08:30:55) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import torch
>>> torch.cuda.device_count()
1
>>> torch.cuda.is_available()
True
显示有1个GPU,并且可用
安装yolov5 依赖库
修改安装依赖
因为预先安装了pytorch,需要修改依赖文件。打开刚刚下载的yolov5/requirements
文件。并修改如下
注释掉torch
和torchvision
。
#torch>=1.7.0
#torchvision>=0.8.1
使用清华源下载,速度嗷嗷快。
-i https://pypi.tuna.tsinghua.edu.cn/simple
修改完成的文件如下:
# pip install -r requirements.txt
# Base ----------------------------------------
matplotlib>=3.2.2
numpy>=1.18.5
opencv-python>=4.1.1
Pillow>=7.1.2
PyYAML>=5.3.1
requests>=2.23.0
scipy>=1.4.1 # Google Colab version
#torch>=1.7.0
#torchvision>=0.8.1
tqdm>=4.41.0
# Logging -------------------------------------
tensorboard>=2.4.1
# wandb
# Plotting ------------------------------------
pandas>=1.1.4
seaborn>=0.11.0
# Export --------------------------------------
# coremltools>=4.1 # CoreML export
# onnx>=1.9.0 # ONNX export
# onnx-simplifier>=0.3.6 # ONNX simplifier
# scikit-learn==0.19.2 # CoreML quantization
# tensorflow>=2.4.1 # TFLite export
# tensorflowjs>=3.9.0 # TF.js export
# openvino-dev # OpenVINO export
# Extras --------------------------------------
# albumentations>=1.0.3
# Cython # for pycocotools https://github.com/cocodataset/cocoapi/issues/172
# pycocotools>=2.0 # COCO mAP
# roboflow
thop # FLOPs computation
-i https://pypi.tuna.tsinghua.edu.cn/simple
安装依赖
执行安装命令
pip install -r requirements.txt
等待安装结束
(yolov5) D:\ptwork\git\yolov5>pip install -r requirements.txt
WARNING: Ignore distutils configs in setup.cfg due to encoding errors.
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting matplotlib>=3.2.2
......
Successfully installed PyYAML-6.0 absl-py-1.0.0 cachetools-5.1.0 colorama-0.4.4 cycler-0.11.0 fonttools-4.33.3 google-auth-2.6.6 google-auth-oauthlib-0.4.6 grpcio-1.46.3 importlib-metadata-4.11.3 kiwisolver-1.4.2 markdown-3.3.7 matplotlib-3.5.2 oauthlib-3.2.0 opencv-python-4.5.5.64 packaging-21.3 pandas-1.4.2 protobuf-3.20.1 pyasn1-0.4.8 pyasn1-modules-0.2.8 pyparsing-3.0.9 python-dateutil-2.8.2 pytz-2022.1 requests-oauthlib-1.3.1 rsa-4.8 scipy-1.8.1 seaborn-0.11.2 tensorboard-2.9.0 tensorboard-data-server-0.6.1 tensorboard-plugin-wit-1.8.1 thop-0.0.31.post2005241907 tqdm-4.64.0 werkzeug-2.1.2 zipp-3.8.0
测试
运行测试命令
(yolov5) D:\ptwork\git\yolov5>python detect.py
detect: weights=yolov5s.pt, source=data\images, data=data\coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
Parse error at "'-i https'": Expected W:(abcd...)
YOLOv5 v6.1-207-g5774a15 Python-3.9.0 torch-1.11.0 CUDA:0 (NVIDIA GeForce MX350, 2048MiB)
Downloading https://github.com/ultralytics/yolov5/releases/download/v6.1/yolov5s.pt to yolov5s.pt...
第一次运行会下载yolov5s.pt文件,下载可能会很慢,可以自行下载,放置在yolov5/
文件夹下即可,也可以创建一个权重文件夹,yolov5/weights/yolov5s.pt
(git上还有其他预训练好的模型,可以自行下载)。
可以使用如下命令
(yolov5) D:\ptwork\git\yolov5>python detect.py --weights weights/yolov5s.pt
detect: weights=['weights/yolov5s.pt'], source=data\images, data=data\coco128.yaml, imgsz=[640, 640], conf_thres=0.25, iou_thres=0.45, max_det=1000, device=, view_img=False, save_txt=False, save_conf=False, save_crop=False, nosave=False, classes=None, agnostic_nms=False, augment=False, visualize=False, update=False, project=runs\detect, name=exp, exist_ok=False, line_thickness=3, hide_labels=False, hide_conf=False, half=False, dnn=False
Parse error at "'-i https'": Expected W:(abcd...)
YOLOv5 v6.1-207-g5774a15 Python-3.9.0 torch-1.11.0 CUDA:0 (NVIDIA GeForce MX350, 2048MiB)
Fusing layers...
YOLOv5s summary: 213 layers, 7225885 parameters, 0 gradients
image 1/2 D:\ptwork\git\yolov5\data\images\bus.jpg: 640x480 4 persons, 1 bus, Done. (0.034s)
image 2/2 D:\ptwork\git\yolov5\data\images\zidane.jpg: 384x640 2 persons, 2 ties, Done. (0.025s)
Speed: 1.5ms pre-process, 29.5ms inference, 3.5ms NMS per image at shape (1, 3, 640, 640)
Results saved to runs\detect\exp2
运行的结果在runs\detect\exp2
中。
在这里插入图片描述
来源:家有一亩三分地