YOLO训练最终版

史上最详细yolov5环境配置搭建+配置所需文件_yolov5环境搭建-CSDN博客

Yolov5训练自己的数据集(详细完整版)_yolov5缔宇-CSDN博客

1.yolov5的源码下载

Yolov5 github官网:https://github.com/ultralytics/yolov5

将其解压到一个不带中文字符的文件夹下

2.下载预训练模型

一般使用YOLOv5s

3.安装anaconda与pycharm

从官网下载pycharm,解压安装完成后创建工程,路径选择到之前解压源码的无中文字符路径,注意!!!!,创建完成后此处有红框标记的文件即为路径正确(也可先创建再解压到工程文件中)

之后进入pycharm中选择工程所需的interpreter

4.安装cuda11.6和cudnn

5.安装pytorch

1
2
# CUDA 11.7
pip install torch==1.13.1+cu117 torchvision==0.14.1+cu117 torchaudio==0.13.1 --extra-index-url https://download.pytorch.org/whl/cu117

其他库的安装建议根据requirement.txt文件逐个安装。。。清华源太容易崩了
下面给出requirement.txt的内容

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
# 这个# pip install -U -r requirements.txt==
Cython
numpy>=1.18.5
opencv-python
torch>=1.5.1
matplotlib
pillow
tensorboard
PyYAML>=5.3
torchvision>=0.6
scipy
tqdm
# pycocotools>=2.0

# Nvidia Apex (optional) for mixed precision training --------------------------
# git clone https://github.com/NVIDIA/apex && cd apex && pip install -v --no-cache-dir --global-option="--cpp_ext" --global-option="--cuda_ext" . --user && cd .. && rm -rf apex

# Conda commands (in place of pip) ---------------------------------------------
# conda update -yn base -c defaults conda
# conda install -yc anaconda numpy opencv matplotlib tqdm pillow ipython
# conda install -yc conda-forge scikit-image pycocotools tensorboard
# conda install -yc spyder-ide spyder-line-profiler
# conda install -yc pytorch pytorch torchvision
# conda install -yc conda-forge protobuf numpy && pip install onnx==1.6.0 # https://github.com/onnx/onnx#linux-and-macos

可以根据下面的指令,依次输入以下命令安装相应的库:
pip install Cython
pip install numpy
pip install opencv-python
pip install matplotlib
pip install pillow
pip install tensorboard
pip install PyYAML
pip install torchvision
pip install scipy
pip install tqdm

6.模型训练

打标,labelimg-master,在虚拟环境中进行

1
2
3
pip install SIP
pip install pyqt5
pip install lxml

python train.py —weights weights/yolov5s.pt —cfg models/yolov5s.yaml —data data/myvoc.yaml —epoch 200 —batch-size 8 —img 640 —device 0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
import torch

# 检查是否有可用的 GPU
if torch.cuda.is_available():
# 打印 GPU 设备数量
print(f"Number of GPUs available: {torch.cuda.device_count()}")

# 打印当前使用的 GPU 设备的索引
current_device_index = torch.cuda.current_device()
print(f"Current GPU device index: {current_device_index}")

# 打印当前使用的 GPU 设备的名称
current_device_name = torch.cuda.get_device_name(current_device_index)
print(f"Current GPU device name: {current_device_name}")
else:
print("No GPU available, using CPU.")

测试:python detect.py —weights runs/train/exp2/weights/best.pt —source 0