当前位置:网站首页>Yolov5 model environment construction and Google lab training
Yolov5 model environment construction and Google lab training
2022-04-21 08:02:00 【kuokay】
Environment building
Environmental Science
- ubuntu 18.04 64bit
- GTX 1070Ti
- anaconda with python 3.8
- pytorch 1.7.1
- cuda 10.1
- yolov5 5.0.9
For ease of use yolov5
object detection , Some netizens have made it into a library , Submitted to the official index library pypi
On , such , We can use it directly pip To install , Its project address : https://github.com/fcakyon/yolov5-pip
install
First create a clean python
A virtual environment
conda create -n yolov5pip python=3.8
conda activate yolov5pip
If python
The version is greater than 3.7, Use it directly pip
install
pip install yolov5
If python
The version is 3.6 Words , You need to install the specified version of numpy
and matplotlib
pip install "numpy>=1.18.5,<1.20" "matplotlib>=3.2.2,<4"
pip install yolov5
Use of the command line
yolov5
After the library is installed , At the same time, it will generate yolov5
Command line tools for , Its use method is the same as that in the source code detect.py
Very similar , as follows
yolov5 detect --source 0 # webcam
file.jpg # image
file.mp4 # video
path/ # directory
path/*.jpg # glob
rtsp://170.93.143.139/rtplive/470011e600ef003a004ee33696235daa # rtsp stream
rtmp://192.168.1.105/live/test # rtmp stream
http://112.50.243.8/PLTV/88888888/224/3221225900/1.m3u8 # http stream
Tests found ,cpu Under the environment of , picture 、 No problem with video detection , But if you use gpu Words , It's going to be a mistake , This may be the current version of bug
YOLOv5 2021-9-15 torch 1.7.1+cu101 CUDA:0 (NVIDIA GeForce GTX 1070 Ti, 8116.4375MB)
Fusing layers…
Model Summary: 283 layers, 7276605 parameters, 0 gradients
Traceback (most recent call last):
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/bin/yolov5”, line 8, in
sys.exit(app())
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/lib/python3.8/site-packages/yolov5/cli.py”, line 11, in app
fire.Fire(
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/lib/python3.8/site-packages/fire/core.py”, line 141, in Fire
component_trace = _Fire(component, args, parsed_flag_args, context, name)
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/lib/python3.8/site-packages/fire/core.py”, line 466, in _Fire
component, remaining_args = _CallAndUpdateTrace(
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/lib/python3.8/site-packages/fire/core.py”, line 681, in _CallAndUpdateTrace
component = fn(*varargs, **kwargs)
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/lib/python3.8/site-packages/torch/autograd/grad_mode.py”, line 26, in decorate_context
return func(*args, **kwargs)
File “/home/xugaoxiang/anaconda3/envs/yolov5pip/lib/python3.8/site-packages/yolov5/detect.py”, line 120, in run
model(torch.zeros(1, 3, *imgsz).to(device).type_as(next(model.parameters()))) # run once
TypeError: zeros() argument after * must be an iterable, not int
except detect Beyond detection ,yolov5 And support train、val and export
The commands for model training are as follows
yolov5 train --data coco.yaml --cfg yolov5s.yaml --weights '' --batch-size 64
yolov5m 40
yolov5l 24
yolov5x 16
On the other val and export Use of commands , Can pass --help To view the specific supported parameters , Such as
yolov5 export --help
stay Python Use in
The library is installed , You can go to python Called in , Let's look at the simplest example
# The import module
import yolov5
# Load model
model = yolov5.load('yolov5s.pt')
# Pictures to be tested
img = 'https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg'
# Reasoning , Default parameters
# results = model(img)
# Use specific dimensions for reasoning
# results = model(img, size=1280)
# Data to enhance , Can detect more targets , Of course, there may also be false detection
results = model(img, augment=True)
# Analysis of test result data , Category 、 Degree of confidence 、 Target location information
predictions = results.pred[0]
boxes = predictions[:, :4] # x1, x2, y1, y2
scores = predictions[:, 4]
categories = predictions[:, 5]
# Show test results
results.show()
# Save the test result picture
results.save(save_dir='results/')
Will be officially provided by yolov5s Download the model , Then execute the above script , The test results are as follows

Due to the use of augment Parameters , There are more test results person, The detection results of the default parameters are the same as the original yolov5 The test results are consistent
Use GPU Accelerated testing
Installation is preferred gpu Version of pytorch, Choose here 1.7.1
pip install torch==1.7.1+cu101 torchvision==0.8.2+cu101 torchaudio==0.7.2 -f https://download.pytorch.org/whl/torch_stable.html
Just like the original , adopt device Parameter to specify cuda Speed up
from yolov5 import YOLOv5
# Model path
model_path = 'yolov5s.pt'
device = "cuda" # or "cpu"
# Model initialization
yolov5 = YOLOv5(model_path, device)
# Pictures to be tested
image = 'https://github.com/ultralytics/yolov5/raw/master/data/images/zidane.jpg'
# Reasoning , Default parameters
results = yolov5.predict(image)
# Use specific dimensions for reasoning
results = yolov5.predict(image, size=1280)
# Data to enhance , Can detect more targets , Of course, there may also be false detection
# results = yolov5.predict(image, augment=True)
# Use multiple parameters together
# results = yolov5.predict(image, size=1280, augment=True)
# If you need to detect multiple pictures , You can use the list
# results = yolov5.predict([image1, image2], size=1280, augment=True)
# Analysis of test result data , Category 、 Degree of confidence 、 Target location information
predictions = results.pred[0]
boxes = predictions[:, :4] # x1, x2, y1, y2
scores = predictions[:, 4]
categories = predictions[:, 5]
# Show test results
results.show()
# Save the test results
results.save(save_dir='results/')
Use google colab Training YOLOv5 Model
colab What is it?
colab yes google One of the jupyter notebook Tools , Support google drive、tensorflow Inside google Family bucket , It is mainly used in the development and research of machine learning .colab The biggest benefit is to the general public AI Developers offer free gpu resources , It can be run very easily on it, such as tensorflow、pytorch、keras Equal depth learning framework .
YOLOv5 model training
Came to google drive, Click on the top left New

Create a new folder colab

Next , Upload the prepared mask data set to colab In the folder , This dataset , We were in front of YOLOv5 Used in model training , You can download it at the following address
Original link https://public.roboflow.ai/object-detection/mask-wearing
Next create colab, Click on New --> More --> Google Colaboratory

Create good notebook after , Need to come modify --> Notebook settings Set up gpu Speed up

Hardware accelerator , choice GPU, preservation

Click on the Connect , choice Connect to managed code executor .

GPU After setting the environment , We can do that notebook View in colab Provided gpu Resources , Use !nvidia-smi command

You can see google The hardware provided is tesla P100, Explicit memory is 16G. It seems that every time colab The distribution of gpu It's different , Sometimes it's P100, Sometimes it's T4
Let's see below. pytorch Installation of , perform
import torch
torch.__version__
You can see that the platform has been installed by default , And the version is 1.6,CUDA The version is 10.1


If you need to install a third-party library , You can install... Directly in the cell , Such as !pip3 install torchvision

Next, I'll put google drive Mount here , So you can do it colab Use in google drive Resources in
import os
from google.colab import drive
drive.mount('/content/drive')
path = "/content/drive/My Drive"
os.chdir(path)
os.listdir(path)
Execute the code in the above cell , You will be asked to enter the verification code


google drive Will be mounted to the directory /content/drive, You can do it later google drive The files in

The preparations are done , We can download YOLOv5 The source of the , Execute... In cells
git clone https://github.com/ultralytics/yolov5.git
And then switch to google drive, modify yolov5/models/yolov5s.yaml, The original nc: 80 Change it to nc: 2

Then you can come to colab, Enter into yolov5 Catalog , Execute the training command in the cell
python train.py --data ../mask/data.yaml --cfg models/yolov5s.yaml --weights '' --batch-size 64
During training , There is pyyaml An error in the module , This is because pyyaml Why the version is too low , We can solve it by upgrading
pip install -U pyyaml

Keep training


版权声明
本文为[kuokay]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210703031796.html
边栏推荐
- You cannot set a form field before rendering a field associated with the value
- go语言记录日志uber-go/zap/lumberjack的用法
- [Ethernet switching security] - port security and MAC address drift prevention and detection
- Playwright, selenium, operation ifram element
- Apache solr 远程代码执行漏洞(CVE-2019-0193)复现
- Common SQL
- Couchdb-垂直越权
- The interface is not restored after Fiddler changes the font
- Nail custom robot docking source code
- Why is the primary key matching setting in MySQL showing that I am wrong?
猜你喜欢

Why the existing modules in PIP list cannot be used in the new project has been solved

2022g3 boiler water treatment examination question bank and answers

How can transformer break into the CV world and kill CNN?

The listview column in C automatically adapts to the perfect effect of scaling

路由器设备选型参照天梯

实战JDBC连接Mysql数据库

Acrobat Pro DC 教程::如何使用文本和图片文件创建 PDF?

【以太网交换安全】--- 端口隔离运行原理及二层隔离三层通信实例配置讲解

Basic concept and storage structure of tree

如何修改 Rancher Server 的 IP 地址
随机推荐
2022起重机械指挥特种作业证考试题库及模拟考试
无意中发现了一位清华妹子的资料库!
VMware16 新安装Win11专业版 ,无法读取ISO镜像无法启动安装程序
IIC bus design ① - IIC communication protocol
Usage of go language log Uber go / zap / lumberjack
Implementing AES encryption and decryption with PHP
【知行】浅谈Switch与If
MongoDB 实验——数据备份和恢复和数据库优化
显示器选购参考天梯
Accidentally found a Tsinghua sister's database!
可以下载到跑LOAM的数据集
Laravel print SQL
Zephyr IOT operating system column summary
Laravel EXECL import time becomes digital
从源码角度剖析redis分布式锁
2022-4-20作业
Return value iresult of standard function
Usage of go ini
Introduction to QT 5.12 actual combat
kubesphere3. 0 forgot admin password