本周任务:将YOLOv5s网络模型中的C3模块按照下图方式修改形成C2模块,并将C2模块插入第2层与第3层之间,且跑通YOLOv5s。
任务提示:
提示1:需要修改common.yaml、yolo.py、yolov5s.yaml文件。
提示2:C2模块与C3模块是非常相似的两个模块,我们要插入C2到模型当中,只需要找到哪里有C3模块,然后在其附近加上C2即可。
# YOLOv5 by Ultralytics, GPL-3.0 license
"""
YOLO-specific modules
Usage:
$ python models/yolo.py --cfg yolov5s.yaml
"""
import argparse
import contextlib
import os
import platform
import sys
from copy import deepcopy
from pathlib import Path
FILE = Path(__file__).resolve()
ROOT = FILE.parents[1] # YOLOv5 root directory
if str(ROOT) not in sys.path:
sys.path.append(str(ROOT)) # add ROOT to PATH
if platform.system() != 'Windows':
ROOT = Path(os.path.relpath(ROOT, Path.cwd())) # relative
from models.common import *
from models.experimental import *
from utils.autoanchor import check_anchor_order
from utils.general import LOGGER, check_version, check_yaml, make_divisible, print_args
from utils.plots import feature_visualization
from utils.torch_utils import (fuse_conv_and_bn, initialize_weights, model_info, profile, scale_img, select_device,
time_sync)
try:
import thop # for FLOPs computation
except ImportError:
thop = None
这个函数用于将模型的模块拼接起来,搭建完成的网络模型。后续如果需要动模型框架的话,你需要对这个函数做相应的改动。
def parse_model(d, ch): # model_dict, input_channels(3)
# Parse a YOLOv5 model.yaml dictionary
LOGGER.info(f"\n{'':>3}{'from':>18}{'n':>3}{'params':>10} {'module':<40}{'arguments':<30}")
anchors, nc, gd, gw, act = d['anchors'], d['nc'], d['depth_multiple'], d['width_multiple'], d.get('activation')
if act:
Conv.default_act = eval(act) # redefine default activation, i.e. Conv.default_act = nn.SiLU()
LOGGER.info(f"{colorstr('activation:')} {act}") # print
na = (len(anchors[0]) // 2) if isinstance(anchors, list) else anchors # number of anchors
no = na * (nc + 5) # number of outputs = anchors * (classes + 5)
layers, save, c2 = [], [], ch[-1] # layers, savelist, ch out
for i, (f, n, m, args) in enumerate(d['backbone'] + d['head']): # from, number, module, args
m = eval(m) if isinstance(m, str) else m # eval strings
for j, a in enumerate(args):
with contextlib.suppress(NameError):
args[j] = eval(a) if isinstance(a, str) else a # eval strings
n = n_ = max(round(n * gd), 1) if n > 1 else n # depth gain
if m in {
Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x}:
c1, c2 = ch[f], args[0]
if c2 != no: # if not output
c2 = make_divisible(c2 * gw, 8)
args = [c1, c2, *args[1:]]
if m in {BottleneckCSP, C3, C3TR, C3Ghost, C3x}:
args.insert(2, n) # number of repeats
n = 1
elif m is nn.BatchNorm2d:
args = [ch[f]]
elif m is Concat:
c2 = sum(ch[x] for x in f)
# TODO: channel, gw, gd
elif m in {Detect, Segment}:
args.append([ch[x] for x in f])
if isinstance(args[1], int): # number of anchors
args[1] = [list(range(args[1] * 2))] * len(f)
if m is Segment:
args[3] = make_divisible(args[3] * gw, 8)
elif m is Contract:
c2 = ch[f] * args[0] ** 2
elif m is Expand:
c2 = ch[f] // args[0] ** 2
else:
c2 = ch[f]
m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args) # module
t = str(m)[8:-2].replace('__main__.', '') # module type
np = sum(x.numel() for x in m_.parameters()) # number params
m_.i, m_.f, m_.type, m_.np = i, f, t, np # attach index, 'from' index, type, number params
LOGGER.info(f'{i:>3}{str(f):>18}{n_:>3}{np:10.0f} {t:<40}{str(args):<30}') # print
save.extend(x % i for x in ([f] if isinstance(f, int) else f) if x != -1) # append to savelist
layers.append(m_)
if i == 0:
ch = []
ch.append(c2)
return nn.Sequential(*layers), sorted(save)
Detect模块是用来构建Detect层的,将输入featuremap通过一个卷积操作和公式计算到我们想要的shape,为后面的计算损失或者NMS作准备。
Detect模块代码:
class Detect(nn.Module):
# YOLOv5 Detect head for detection models
stride = None # strides computed during build
dynamic = False # force grid reconstruction
export = False # export mode
def __init__(self, nc=80, anchors=(), ch=(), inplace=True): # detection layer
super().__init__()
self.nc = nc # number of classes
self.no = nc + 5 # number of outputs per anchor
self.nl = len(anchors) # number of detection layers
self.na = len(anchors[0]) // 2 # number of anchors
self.grid = [torch.empty(0) for _ in range(self.nl)] # init grid
self.anchor_grid = [torch.empty(0) for _ in range(self.nl)] # init anchor grid
self.register_buffer('anchors', torch.tensor(anchors).float().view(self.nl, -1, 2)) # shape(nl,na,2)
self.m = nn.ModuleList(nn.Conv2d(x, self.no * self.na, 1) for x in ch) # output conv
self.inplace = inplace # use inplace ops (e.g. slice assignment)
def forward(self, x):
z = [] # inference output
for i in range(self.nl):
x[i] = self.m[i](x[i]) # conv
bs, _, ny, nx = x[i].shape # x(bs,255,20,20) to x(bs,3,20,20,85)
x[i] = x[i].view(bs, self.na, self.no, ny, nx).permute(0, 1, 3, 4, 2).contiguous()
if not self.training: # inference
if self.dynamic or self.grid[i].shape[2:4] != x[i].shape[2:4]:
self.grid[i], self.anchor_grid[i] = self._make_grid(nx, ny, i)
if isinstance(self, Segment): # (boxes + masks)
xy, wh, conf, mask = x[i].split((2, 2, self.nc + 1, self.no - self.nc - 5), 4)
xy = (xy.sigmoid() * 2 + self.grid[i]) * self.stride[i] # xy
wh = (wh.sigmoid() * 2) ** 2 * self.anchor_grid[i] # wh
y = torch.cat((xy, wh, conf.sigmoid(), mask), 4)
else: # Detect (boxes only)
xy, wh, conf = x[i].sigmoid().split((2, 2, self.nc + 1), 4)
xy = (xy * 2 + self.grid[i]) * self.stride[i] # xy
wh = (wh * 2) ** 2 * self.anchor_grid[i] # wh
y = torch.cat((xy, wh, conf), 4)
z.append(y.view(bs, self.na * nx * ny, self.no))
return x if self.training else (torch.cat(z, 1),) if self.export else (torch.cat(z, 1), x)
def _make_grid(self, nx=20, ny=20, i=0, torch_1_10=check_version(torch.__version__, '1.10.0')):
d = self.anchors[i].device
t = self.anchors[i].dtype
shape = 1, self.na, ny, nx, 2 # grid shape
y, x = torch.arange(ny, device=d, dtype=t), torch.arange(nx, device=d, dtype=t)
yv, xv = torch.meshgrid(y, x, indexing='ij') if torch_1_10 else torch.meshgrid(y, x) # torch>=0.7 compatibility
grid = torch.stack((xv, yv), 2).expand(shape) - 0.5 # add grid offset, i.e. y = 2.0 * x - 0.5
anchor_grid = (self.anchors[i] * self.stride[i]).view((1, self.na, 1, 1, 2)).expand(shape)
return grid, anchor_grid
这个模块是整个模型的搭建模块。且yolov5的作者将这个模块的功能写的很全,不光包含模型的搭建,还扩展了很多功能如:特征可视化,打印模型信息、TTA推理增强、融合Conv+Bn加速推理、模型搭载nms功能、autoshape函数:模型包含前处理、推理、后处理的模块(预处理+推理+nms)。感兴趣的可以仔细看看,不感兴趣的可以直接看init和forward两个函数即可。
BaseModel模块代码:
class BaseModel(nn.Module):
# YOLOv5 base model
def forward(self, x, profile=False, visualize=False):
return self._forward_once(x, profile, visualize) # single-scale inference, train
def _forward_once(self, x, profile=False, visualize=False):
y, dt = [], [] # outputs
for m in self.model:
if m.f != -1: # if not from previous layer
x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f] # from earlier layers
if profile:
self._profile_one_layer(m, x, dt)
x = m(x) # run
y.append(x if m.i in self.save else None) # save output
if visualize:
feature_visualization(x, m.type, m.i, save_dir=visualize)
return x
def _profile_one_layer(self, m, x, dt):
c = m == self.model[-1] # is final layer, copy input as inplace fix
o = thop.profile(m, inputs=(x.copy() if c else x,), verbose=False)[0] / 1E9 * 2 if thop else 0 # FLOPs
t = time_sync()
for _ in range(10):
m(x.copy() if c else x)
dt.append((time_sync() - t) * 100)
if m == self.model[0]:
LOGGER.info(f"{'time (ms)':>10s} {'GFLOPs':>10s} {'params':>10s} module")
LOGGER.info(f'{dt[-1]:10.2f} {o:10.2f} {m.np:10.0f} {m.type}')
if c:
LOGGER.info(f"{sum(dt):10.2f} {'-':>10s} {'-':>10s} Total")
def fuse(self): # fuse model Conv2d() + BatchNorm2d() layers
LOGGER.info('Fusing layers... ')
for m in self.model.modules():
if isinstance(m, (Conv, DWConv)) and hasattr(m, 'bn'):
m.conv = fuse_conv_and_bn(m.conv, m.bn) # update conv
delattr(m, 'bn') # remove batchnorm
m.forward = m.forward_fuse # update forward
self.info()
return self
def info(self, verbose=False, img_size=640): # print model information
model_info(self, verbose, img_size)
def _apply(self, fn):
# Apply to(), cpu(), cuda(), half() to model tensors that are not parameters or registered buffers
self = super()._apply(fn)
m = self.model[-1] # Detect()
if isinstance(m, (Detect, Segment)):
m.stride = fn(m.stride)
m.grid = list(map(fn, m.grid))
if isinstance(m.anchor_grid, list):
m.anchor_grid = list(map(fn, m.anchor_grid))
return self
复制c3生成c2
class C2(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
self.cv1 = Conv(c1, c_, 1, 1)
self.cv2 = Conv(c1, c_, 1, 1)
self.cv3 = Conv(2 * c_, c2, 1) # optional act=FReLU(c2)
self.m = nn.Sequential(*(Bottleneck(c_, c_, shortcut, g, e=1.0) for _ in range(n)))
def forward(self, x):
return self.cv3(torch.cat((self.m(self.cv1(x)), self.cv2(x)), 1))
if m in {
Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
BottleneckCSP, C2, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x}:
c1, c2 = ch[f], args[0]
if c2 != no: # if not output
c2 = make_divisible(c2 * gw, 8)
args = [c1, c2, *args[1:]]
if m in {BottleneckCSP, C2, C3, C3TR, C3Ghost, C3x}:
args.insert(2, n) # number of repeats
n = 1
# YOLOv5 v6.0 backbone
backbone:
# [from, number, module, args]
[[-1, 1, Conv, [64, 6, 2, 2]], # 0-P1/2
[-1, 1, Conv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 3, C2, [128]], # todo
[-1, 1, Conv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, Conv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [1024]],
[-1, 1, SPPF, [1024, 5]], # 9
]
python train.py --img 900 --batch 2 --epoch 100 --data paper_data/ab.yaml --cfg models/yolov5s.yaml --weights yolov5s.pt
(k39) PS E:\doc\1.学院\3.学习培训\21.365深度学习训练营\y2\yolov5-master> python train.py --img 900 --batch 2 --epoch 2 --data paper_data/ab.yaml --cfg models/yolov5s.yaml --weights yolov5s.pt
train: weights=yolov5s.pt, cfg=models/yolov5s.yaml, data=paper_data/ab.yaml, hyp=data\hyps\hyp.scratch-low.yaml, epochs=2, batch_size=2, imgsz=900, rect=False, resume=False, nosave=False, noval=False, noautoanchor=False, noplots=False, evolve=None, bucket=, cache=None, image_weights=False, device=, multi_scale=False, single_cls=False, optimizer=SGD, sync_bn=False, workers=8, project=runs\train, name=exp, exist_ok=False, quad=False, cos_lr=False, label_smoothing=0.0, patience=100, freeze=[0], save_period=-1, seed=0, local_rank=-1, entity=None, upload_dataset=False, bbox_interval=-1, artifact_alias=latest
github: skipping check (not a git repository), for updates see https://github.com/ultralytics/yolov5
YOLOv5 2022-12-7 Python-3.9.15 torch-1.13.0+cpu CPU
hyperparameters: lr0=0.01, lrf=0.01, momentum=0.937, weight_decay=0.0005, warmup_epochs=3.0, warmup_momentum=0.8, warmup_bias_lr=0.1, box=0.05, cls=0.5, cls_pw=1.0, obj=1.0, obj_pw=1.
0, iou_t=0.2, anchor_t=4.0, fl_gamma=0.0, hsv_h=0.015, hsv_s=0.7, hsv_v=0.4, degrees=0.0, translate=0.1, scale=0.5, shear=0.0, perspective=0.0, flipud=0.0, fliplr=0.5, mosaic=1.0, mixup=0.0, copy_paste=0.0
ClearML: run 'pip install clearml' to automatically track, visualize and remotely train YOLOv5 in ClearML
Comet: run 'pip install comet_ml' to automatically track and visualize YOLOv5 runs in Comet
TensorBoard: Start with 'tensorboard --logdir runs\train', view at http://localhost:6006/
Overriding model.yaml nc=80 with nc=4
from n params module arguments
0 -1 1 3520 models.common.Conv [3, 32, 6, 2, 2]
1 -1 1 18560 models.common.Conv [32, 64, 3, 2]
2 -1 1 18816 models.common.C3 [64, 64, 1]
3 -1 1 18816 models.common.C2 [64, 64, 1]
4 -1 1 73984 models.common.Conv [64, 128, 3, 2]
5 -1 2 115712 models.common.C3 [128, 128, 2]
6 -1 1 295424 models.common.Conv [128, 256, 3, 2]
7 -1 3 625152 models.common.C3 [256, 256, 3]
8 -1 1 1180672 models.common.Conv [256, 512, 3, 2]
9 -1 1 1182720 models.common.C3 [512, 512, 1]
10 -1 1 656896 models.common.SPPF [512, 512, 5]
11 -1 1 131584 models.common.Conv [512, 256, 1, 1]
12 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
13 [-1, 6] 1 0 models.common.Concat [1]
14 -1 1 361984 models.common.C3 [512, 256, 1, False]
15 -1 1 33024 models.common.Conv [256, 128, 1, 1]
16 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
17 [-1, 4] 1 0 models.common.Concat [1]
18 -1 1 90880 models.common.C3 [256, 128, 1, False]
19 -1 1 147712 models.common.Conv [128, 128, 3, 2]
20 [-1, 14] 1 0 models.common.Concat [1]
21 -1 1 329216 models.common.C3 [384, 256, 1, False]
22 -1 1 590336 models.common.Conv [256, 256, 3, 2]
23 [-1, 10] 1 0 models.common.Concat [1]
24 -1 1 1313792 models.common.C3 [768, 512, 1, False]
25 [17, 20, 23] 1 38097 models.yolo.Detect [4, [[10, 13, 16, 30, 33, 23], [30, 61, 62, 45, 59, 119], [116, 90, 156, 198, 373, 326]], [256, 384, 768]]
YOLOv5s summary: 232 layers, 7226897 parameters, 7226897 gradients, 17.2 GFLOPs
Transferred 66/379 items from yolov5s.pt
WARNING --img-size 900 must be multiple of max stride 32, updating to 928
optimizer: SGD(lr=0.01) with parameter groups 62 weight(decay=0.0), 65 weight(decay=0.0005), 65 bias
train: Scanning E:\doc\1.学院\3.学习培训\21.365深度学习训练营\y2\yolov5-master\paper_data\train... 180 images, 0 backgrounds, 0 corrupt: 100%|██████████| 180/180 [00:04<00:00, 36.18i
train: WARNING Cache directory E:\doc\1.\3.\21.365\y2\yolov5-master\paper_data is not writeable: [WinError 183] : 'E:\\doc\\1.\\3.\\21.365\\y2\\yolov5-master\\paper_data\\train.cache.npy' -> 'E:\\doc\\1.\\3.\\21.365\\y2\\yolov5-master\\paper_data\\train.cache'
val: Scanning E:\doc\1.学院\3.学习培训\21.365深度学习训练营\y2\yolov5-master\paper_data\val.cache... 20 images, 0 backgrounds, 0 corrupt: 100%|██████████| 20/20 [00:00<?, ?it/s]
AutoAnchor: 4.82 anchors/target, 1.000 Best Possible Recall (BPR). Current anchors are a good fit to dataset
Plotting labels to runs\train\exp19\labels.jpg...
Image sizes 928 train, 928 val
Using 2 dataloader workers
Logging results to runs\train\exp19
Starting training for 2 epochs...
Epoch GPU_mem box_loss obj_loss cls_loss Instances Size
0/1 0G 0.1086 0.06274 0.04353 9 928: 0%| | 0/90 [00:02<?, ?it/s]WARNING TensorBoard graph visualization failure Sizes of tensors must match except in dimension 1. Expected size 58 but got size 57 for tensor number 1 in the list.
0/1 0G 0.1054 0.06577 0.0437 11 928: 100%|██████████| 90/90 [03:21<00:00, 2.23s/it]
Class Images Instances P R mAP50 mAP50-95: 100%|██████████| 5/5 [00:06<00:00, 1.31s/it]
all 20 60 0.000787 0.109 0.000597 7.26e-05
Epoch GPU_mem box_loss obj_loss cls_loss Instances Size
1/1 0G 0.1004 0.06878 0.04284 5 928: 100%|██████████| 90/90 [03:17<00:00, 2.20s/it]
Class Images Instances P R mAP50 mAP50-95: 40%|████ | 2/5 [00:03<00:04, 1.65s/it]WARNING NMS time limit 0.700s exceeded
Class Images Instances P R mAP50 mAP50-95: 80%|████████ | 4/5 [00:07<00:01, 1.82s/it]WARNING NMS time limit 0.700s exceeded
Class Images Instances P R mAP50 mAP50-95: 100%|██████████| 5/5 [00:08<00:00, 1.80s/it]
all 20 60 0.00232 0.251 0.00263 0.000611
2 epochs completed in 0.115 hours.
Optimizer stripped from runs\train\exp19\weights\last.pt, 15.0MB
Optimizer stripped from runs\train\exp19\weights\best.pt, 15.0MB
Validating runs\train\exp19\weights\best.pt...
Fusing layers...
YOLOv5s summary: 170 layers, 7217201 parameters, 0 gradients, 17.0 GFLOPs
Class Images Instances P R mAP50 mAP50-95: 40%|████ | 2/5 [00:02<00:04, 1.53s/it]WARNING NMS time limit 0.700s exceeded
Class Images Instances P R mAP50 mAP50-95: 80%|████████ | 4/5 [00:06<00:01, 1.71s/it]WARNING NMS time limit 0.700s exceeded
Class Images Instances P R mAP50 mAP50-95: 100%|██████████| 5/5 [00:08<00:00, 1.68s/it]
all 20 60 0.00196 0.213 0.0024 0.000587
banana 20 16 0.00292 0.312 0.00268 0.000533
snake fruit 20 20 0 0 0 0
dragon fruit 20 11 0 0 0 0
pineapple 20 13 0.00494 0.538 0.00693 0.00181
Results saved to runs\train\exp19