ᕕ( ᐛ )ᕗ Wu555's blog

YOLOv5初体验

Before You Start

Clone repo and install requirements.txt in a Python>=3.7.0 environment, including PyTorch>=1.7Models and datasets download automatically from the latest YOLOv5 release. 我直接装最新的稳定版。

安装anacoda3

配置环境变量,安装在D盘,把D:\Anaconda\xx搞上 打开Anaconda Prompt(Anaconda3) 创建环境,得创建在D盘(C盘太小了,默认是C盘)

cd D:\\Anaconda\\envs 
conda create --prefix=/torch python=3.9

torch 为创建的环境名称 激活环境 activate D:\torch

安装pytorch

官网:Start Locally | PyTorch 不能选conda,会卡soving enviroment,网络问题,选pip安装,注意网络。

pip3 install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu116

![[Pasted image 20230313214802.png]]

安装YoloV5

git clone https://github.com/ultralytics/yolov5  # clone
cd yolov5
pip install -r requirements.txt  # install

具体见befor you start

![[Pasted image 20230313220956.png]]

安装requirements报错,原因win11不是uft-8,解决后重新安装(好像无关紧要)

训练数据

1.创建数据集

用yoloV5官方推荐的Roboflow,导出到电脑 ![[Pasted image 20230313223824.png]]

2.选择一个模式

![[Pasted image 20230313224008.png]]

3.训练

Train a YOLOv5s model on COCO128 by specifying dataset, batch-size, image size and either pretrained --weights yolov5s.pt (recommended), or randomly initialized --weights '' --cfg yolov5s.yaml (not recommended). Pretrained weights are auto-downloaded from the latest YOLOv5 release.

Train YOLOv5s on COCO128 for 3 epochs

$ python train.py –img 640 –batch 16 –epochs 3 –data coco128.yaml –weights yolov5s.pt

💡 ProTip: Add --cache ram or --cache disk to speed up training (requires significant RAM/disk resources).
💡 ProTip: Always train from a local dataset. Mounted or network drives like Google Drive will be very slow.

All training results are saved to runs/train/ with incrementing run directories, i.e. runs/train/exp2runs/train/exp3 etc. For more details see the Training section of our tutorial notebook.

WinError 1455页面文件太小,无法完成操作
在train.py 文件中搜索worker改为1 torch.cuda.OutOfMemoryError: CUDA out of memory. 换电脑!!

TENSORS

一个特殊的数据结构,像数组和矩阵一样,在pytorch里面时用来对模型的输入和输出进行一个处理,同时也对一些参数进行处理。理论上来说tensors和NumPy里面的数组是一个东西,除了tensors可以在cup或者gpu上训练。 Tensors — PyTorch Tutorials 2.0.0+cu117 documentation

TORCH.AUTOGRAD

是一个为神经网络提供动力的自动微分引擎。 神经网络就是对输入数据执行的套嵌函数的集合,这些函数由参数(权重和偏差)定义,被储存在Tensors里面。

训练一个神经网络需要两步

前向传播

就是说从NN的输入,经过一系列的神经元(套嵌函数),得到一个输出的过程。

反向传播

Backward Propagation: In backprop, the NN adjusts its parameters proportionate to the error in its guess. It does this by traversing backwards from the output, collecting the derivatives of the error with respect to the parameters of the functions (gradients), and optimizing the parameters using gradient descent. For a more detailed walkthrough of backprop, check out this video from 3Blue1Brown. 反向传播(Backpropagation ) - 知乎 (zhihu.com)

过于复杂,没必要!

神经网络

A typical training procedure for a neural network is as follows:

训练一个分类器

torchvision

针对视觉处理新建的一个python库,它具有用于 ImageNet、CIFAR10、MNIST 等常见数据集的数据加载器和用于图像的数据转换器,即 torchvision.datasets 和 torch.utils.data.DataLoader。