YOLOV5源码的详细解读

YOLOV5源码的详细解读
⽬录结构
├── data:主要是存放⼀些超参数的配置⽂件(这些⽂件(yaml⽂件)是⽤来配置训练集和测试集还有验证集的路径的,其中还包括⽬标检测的种类数和种类的名称);还有⼀些官⽅提供测试的图⽚。如果是训练⾃⼰的数据集的话,那么就需要修改其中的yaml⽂件。但是⾃⼰的数据集不建议放在这个路径下⾯,⽽是建议把数据集放到yolov5项⽬的同级⽬录下⾯。
|——dataset :存放⾃⼰的数据集,分为images和labels两部分
├── models:⾥⾯主要是⼀些⽹络构建的配置⽂件和函数,其中包含了该项⽬的四个不同的版本,分别为是s、m、l、x。从名字就可以看出,这⼏个版本的⼤⼩。他们的检测测度分别都是从快到慢,但是精确度分别是从低到⾼。这就是所谓的鱼和熊掌不可兼得。如果训练⾃⼰的数据集的话,就需要修改这⾥⾯相对应的yaml⽂件来训练⾃⼰模型。
├── utils:存放的是⼯具类的函数,⾥⾯有loss函数,metrics函数,plots函数等等。
├── weights:放置训练好的权重参数pt⽂件。
├── detect.py:利⽤训练好的权重参数进⾏⽬标检测,可以进⾏图像、视频和摄像头的检测。
├── train.py:训练⾃⼰的数据集的函数。
六氯苯├── test.py:测试训练的结果的函数。
|—— hubconf.py:pytorch hub 相关代码
|—— sotabench.py: coco数据集测试脚本
量化分析
|—— tutorial.ipynb: jupyter notebook 演⽰⽂件
├──:这是⼀个⽂本⽂件,⾥⾯写着使⽤yolov5项⽬的环境依赖包的⼀些版本,可以利⽤该⽂本导⼊相应版本的包。|----run⽇志⽂件,每次训练的数据,包含权重⽂件,训练数据,直⽅图等
|——LICENCE 版权⽂件
以上就是yolov5项⽬代码的整体介绍。我们训练和测试⾃⼰的数据集基本就是利⽤到如上的代码。
data ⽂件夹yaml多种数据集的配置⽂件,如coco,coco128,pascalvoc等hyps 超参数微调配置⽂件
scripts⽂件夹存放着下载数据集额shell命令
在利⽤⾃⼰的数据集进⾏训练时,需要将配置⽂件中的路径进⾏修改,改成⾃⼰对应的数据集所在⽬录,最好复制+重命名。dataset ⽂件夹
存放着⾃⼰的数据集,但应按照image和label分开,同时每⼀个⽂件夹下,⼜应该分为train,val。
2-溴芴.cache⽂件为缓存⽂件,将数据加载到内存中,⽅便下次调⽤快速。
model
⽂件夹
common.py ⽹络组件模块train: E:/project/yolov5/yolov5-master/dataset/images/train # train images val: E:/project/yolov5/yolov5-master/dataset/images/val  # val images
国外农业网站1
2
# YOLOv5  by Ultralytics, GPL-3.0 license """Common modules """import logging import math import warnings from copy import copy from pathlib import Path import numpy as np import pandas as pd import requests import torch as nn from PIL import Image from torch.cuda import amp from utils.datasets import exif_transpose, letterbox al import colorstr, increment_path, make_divisible, non_max_suppression, save_one_box, \    scale_coords, xyxy2xywh from utils.plots import Annotator, colors h_utils import time_sync LOGGER = Logger(__name__)#为same 卷积或same 池化⾃动扩充def autopad(k, p=None):  # kernel, padding    # Pad to 'same'    if p is None:        p = k // 2 if isinstance(k, int) else [x // 2 for x in k]  # auto-pad    return p    #标准的卷积 conv+BN+hardswish class Conv(nn.Module):    # Standard convolution    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, 卷积核kernel, 步长stride, padding, groups        super().__init__()        v = nn.Conv2d(c1, c2, k, s, autopad(k, p), groups=g, bias=False)        self.bn = nn.BatchNorm2d(c2)        self.act = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())    def forward(self, x):#正向传播        return self.act(self.v(x)))    def forward_fuse(self, x):        return self.v(x))#深度可分离卷积⽹络class DWConv(Conv):    # Depth-wise convolution class    def __init__(self, c1, c2,
k=1, s=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groups        super().__init__(c1, c2, k, s, d(c1, c2), act=act)class TransformerLayer(nn.Module):    # Transformer layer /abs/2010.11929 (LayerNorm layers removed for better performance)    def __init__(self, c, num_heads):        super().__init__()        self.q = nn.Linear(c, c, bias=False)        self.k = nn.Linear(c, c, bias=False)        self.v = nn.Linear(c, c, bias=False)        self.ma = nn.MultiheadAttention(embed_dim=c, num_heads=num_heads)        self.fc1 = nn.Linear(c, c, bias=False)
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
提出教师节的人
self.fc1 = nn.Linear(c, c, bias=False)        self.fc2 = nn.Linear(c, c, bias=False)    def forward(self, x):        x = self.ma(self.q(x), self.k(x), self.v(x))[0] + x        x = self.fc2(self.fc1(x)) + x        return x class TransformerBlock(nn.Module):    # Vision Transformer /abs/2010.11929    def __init__(self, c1, c2, num_heads, num_layers):        super().__init__()        v = None        if c1 != c2:            v = Conv(c1, c2)        self.linear = nn.Linear(c2, c2)  # learnable position embedding        = nn.Sequential(*[TransformerLayer(c2, num_heads) for _ in range(num_layers)])        self.c2 = c2    def forward(self, x):        v is not None:            x = v(x)        b, _, w, h = x.shape        p = x.flatten(2).unsqueeze(0).transpose(0, 3).squeeze(3)   
    (p + self.linear(p)).unsqueeze(3).transpose(0, 3).reshape(b, self.c2, w, h)class Bottleneck(nn.Module):    # Standard bottleneck    def __init__(self, c1, c2, shortcut=True, g=1, e=0.5):  # ch_in, ch_out, shortcut, groups, expansion        super().__init__()        c_ = int(c2 * e)  # hidden channels        self.cv1 = Conv(c1, c_, 1, 1)        self.cv2 = Conv(c_, c2, 3, 1, g=g)        self.add = shortcut and c1 == c2    def forward(self, x):        return x + self.cv2(self.cv1(x)) if self.add else self.cv2(self.cv1(x))class BottleneckCSP(nn.Module):    # CSP Bottleneck github/WongKinYiu/CrossStagePartialNetworks    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):  # ch_in, ch_out, number, shortcut, groups, expansion        super().__init__()        c_ = int(c2 * e)  # hidden channels        self.cv1 = Conv(c1, c_, 1, 1)        self.cv2 = nn.Conv2d(c1, c_, 1, 1, bias=False)        self.cv3 = nn.Conv2d(c_, c_, 1, 1, bias=False)        self.cv4 = Conv(2 * c_, c2, 1, 1)        self.bn = nn.BatchNorm2d(2 * c_)  # applied to cat(cv2, cv3)        self.act = nn.LeakyReLU(0.1, inplace=True)        #*把list 拆分为⼀个个元素        self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])    def forward(self, x):        y1 = self.cv3(self.m(self.cv1(x)))        y2 = self.cv2(x)        return self.cv4(self.act(self.bn(torch.cat((y1, y2), dim=1))))class C3(nn.Module):    # CSP Bottleneck with 3 convolutions    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):  # ch_in, ch_out, number, shortcut, groups, expansion        super().__init__()        c_ = int(c2 * e)  # hidden channels
6566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
c_ = int(c2 * e)  # hidden channels        self.cv1 = Conv(c1, c_, 1, 1)        self.cv2 = Conv(c1, c_, 1, 1)        self.cv3 = Conv(2 * c_, c2, 1)  # act=FReLU(c2)        self.m = nn.Sequential(*[Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)])        # self.m = nn.Sequential(*[CrossConv(c_, c_, 3, 1, g, 1.0, shortcut) for _ in range(n)])    def forward(self, x):        return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), dim=1))class C3TR(C3):    # C3 module with TransformerBlock()    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):        super().__init__(c1, c2, n, shortcut, g, e)        c_ = int(c2 * e)        self.m = TransformerBlock(c_, c_, 4, n)class C3SPP(C3):    # C3 module with SPP()    def __init__(self, c1, c2, k=(5, 9, 13), n=1, shortcut=True, g=1, e=0.5):        super().__init__(c1, c2, n, shortcut, g, e)        c_ = int(c2 * e)        self.m = SPP(c_, c_, k)class C3Ghost(C3):    # C3 module with GhostBottleneck()    def __init__(self, c1, c2, n=1, shortcut=True, g=1, e=0.5):        super().__init__(c1, c2, n, shortcut, g, e)        c_ = int(c2 * e)  # hidden channels        self.m = nn.Sequential(*[GhostBottleneck(c_, c_) for _ in range(n)])#⾦字塔池化class SPP(nn.Module):    # Spatial Pyramid Pooling (SPP) layer /abs/1406.4729    def __init__(self, c1, c2, k=(5, 9, 13)):        super().__init__()        c_ = c1 // 2  # hidden channels        self.cv1 = Conv(c1, c_, 1, 1)        self.cv2 = Conv(c_ * (len(k) + 1), c
2, 1, 1)        self.m = nn.ModuleList([nn.MaxPool2d(kernel_size=x, stride=1, padding=x // 2) for x in k])    def forward(self, x):        x = self.cv1(x)        with warnings.catch_warnings():            warnings.simplefilter('ignore')  # suppress torch 1.9.0 max_pool2d() warning            return self.cv2(torch.cat([x] + [m(x) for m in self.m], 1))class SPPF(nn.Module):    # Spatial Pyramid Pooling - Fast (SPPF) layer for YOLOv5 by Glenn Jocher    def __init__(self, c1, c2, k=5):  # equivalent to SPP(k=(5, 9, 13))        super().__init__()        c_ = c1 // 2  # hidden channels        self.cv1 = Conv(c1, c_, 1, 1)        self.cv2 = Conv(c_ * 4, c2, 1, 1)        self.m = nn.MaxPool2d(kernel_size=k, stride=1, padding=k // 2)    def forward(self, x):        x = self.cv1(x)        with warnings.catch_warnings():            warnings.simplefilter('ignore')  # suppress torch 1.9.0 max_pool2d() warning            y1 = self.m(x)            y2 = self.m(y1)
龚绮130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
y2 = self.m(y1)            return self.cv2(torch.cat([x, y1, y2, self.m(y2)], 1))#把宽和⾼填充整合到c 空间中class Focus(nn.Module):    # Focus wh information into c-space    def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):  # ch_in, ch_out, kernel, stride, padding, groups        super().__init__()        v = Conv(c1 * 4, c2, k, s, p, g, act)        # act = Contract(gain=2)    def forward(self, x):  # x(b,c,w,h) -> y(b,4c,w/2,h/2)        v(torch.cat([x[..., ::2, ::2], x[..., 1::2, :
:2], x[..., ::2, 1::2], x[..., 1::2, 1::2]], 1))        # act(x))class GhostConv(nn.Module):    # Ghost Convolution github/huawei-noah/ghostnet    def __init__(self, c1, c2, k=1, s=1, g=1, act=True):  # ch_in, ch_out, kernel, stride, groups        super().__init__()        c_ = c2 // 2  # hidden channels        self.cv1 = Conv(c1, c_, k, s, None, g, act)        self.cv2 = Conv(c_, c_, 5, 1, None, c_, act)    def forward(self, x):        y = self.cv1(x)        return torch.cat([y, self.cv2(y)], 1)class GhostBottleneck(nn.Module):    # Ghost Bottleneck github/huawei-noah/ghostnet    def __init__(self, c1, c2, k=3, s=1):  # ch_in, ch_out, kernel, stride        super().__init__()        c_ = c2 // 2        v = nn.Sequential(GhostConv(c1, c_, 1, 1),  # pw                                  DWConv(c_, c_, k, s, act=False) if s == 2 else nn.Identity(),  # dw                                  GhostConv(c_, c2, 1, 1, act=False))  # pw-linear        self.shortcut = nn.Sequential(DWConv(c1, c1, k, s, act=False),                                      Conv(c1, c2, 1, 1, act=False)) if s == 2 else nn.Identity()    def forward(self, x):        v(x) + self.shortcut(x)class Contract(nn.Module):    # Contract width-height into channels, i.e. x(1,64,80,80) to x(1,256,40,40)    def __init__(self, gain=2):        super().__init__()        self.gain = gain    def forward(self, x):        b, c, h, w = x.size()  # assert (h / s == 0) and (W / s == 0), 'Indivisible gain'        s = self.gain        x = x.view(b, c, h // s, s, w // s, s)  # x(1,64,40,2,40,2)        x = x.permute(0, 3, 5, 1, 2, 4).contiguous()  # x(1,2,2,64,40,40)        return x.view(b, c * s * s, h // s, w // s)  # x(1,256,40,40)class
Expand(nn.Module):    # Expand channels into width-height, i.e. x(1,64,80,80) to x(1,16,160,160)    def __init__(self, gain=2):        super().__init__()        self.gain = gain    def forward(self, x):        b, c, h, w = x.size()  # assert C / s ** 2 == 0, 'Indivisible gain'195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260

本文发布于:2024-09-21 00:33:52,感谢您对本站的认可!

本文链接:https://www.17tex.com/xueshu/473478.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:数据   训练   测试   检测   卷积   配置   函数   整合
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议