赞
踩
HorNet论文地址:https://arxiv.org/pdf/2207.14284.pdf
HorNet是在Swin transformer结构的基础上,结合大核思想提出的新的网络结构模块,使用该模块,作者在ImageNet-1k数据集上做分类,分割以及检测任务都在当时达到了SOTA的效果,是一个能有效增强各种网络的性能而不会引入太大参数量的一种改进思路,已经有很多博主提出将该模块用于yolo系列网络中,以期望达到更好的效果。本文主要是针对YoloV5系列的网络进行C3模块的替换,替换成HorNet模块。
HorNet模块的结构如下图所示:
该图来源于论文中。从图中我们可以清晰的看到,HorNet模块和Swin transformer模块有着相似的结构,不同的是HorNet中使用到了GnConv这样一个新的模块,GnConv的结构也在上图中给出来了。
下面进行yolo网络的修改。
首先在common.py文件中添加如下代码:
- class HorLayerNorm(nn.Module):
- def __init__(self, normalized_shape, eps=1e-6, data_format="channels_last"):
- super().__init__()
- self.weight = nn.Parameter(torch.ones(normalized_shape))
- self.bias = nn.Parameter(torch.zeros((normalized_shape)))
- self.eps = eps
- self.data_format = data_format
- if self.data_format not in ["channels_last", "channels_first"]:
- raise NotImplementedError #by iscyy/air
- self.normalized_shape = (normalized_shape,)
-
- def forward(self, x):
- if self.data_format == "channels_last":
- return F.layer_norm(x, self.normalized_shape, self.weight, self.bias, self.eps)
- elif self.data_format == "channels_first":
- u = x.mean(1, keepdim=True)
- s = (x - u).pow(2).mean(1, keepdim=True)
- x = (x - u)/torch.sqrt(s + self.eps)
- x = self.weight[:, None, None] * x + self.bias[:, None, None]
- return x
-
- class GlobalLocalFilter(nn.Module):
- def __init__(self, dim, h=14, w=8):
- super().__init__()
- self.dw = nn.Conv2d(dim // 2, dim // 2, kernel_size=3, padding=1, bias=False, groups=dim // 2)
- self.complex_weight = nn.Parameter(torch.randn(dim // 2, h, w, 2, dtype=torch.float32) * 0.02)
- trunc_normal_(self.complex_weight, std=0.02)
- self.pre_norm = HorLayerNorm(dim, eps=1e-6, data_format='channels_first')
- self.post_norm = HorLayerNorm(dim, eps=1e-6, data_format='channels_first')
-
- def forward(self, x):
- x = self.pre_norm(x)
- x1, x2 = torch.chunk(x, 2, dim=1)
- x1 = self.dw(x1)
-
- x2 = x2.to(torch.float32)
- B, C, a, b = x2.shape
- x2 = torch.fft.rfft2(x2, dim=(2, 3), norm='ortho')
- weight = self.complex_weight
- if not weight.shape[1:3] == x2.shape[2:4]:
- weight = F.interpolate(weight.permute(3, 0, 1, 2), size=x2.shape[2:4], mode='bilinear', align_corners=True).permute(1,2,3,0)
-
- weight = torch.view_as_complex(weight.contiguous())
-
- x2 = x2*weight
- x2 = torch.fft.irfft2(x2, s=(a, b), dim=(2,3), norm='ortho')
-
- x = torch.cat([x1.unsqueeze(2), x2.unsqueeze(2)], dim=2).reshape(B,2*C,a,b)
- x = self.post_norm(x)
- return x
-
- class gnconv(nn.Module):
- def __init__(self, dim, order=5, gflayer=None, h=14, w=8, s=1.0):
- super().__init__()
- self.order = order
- self.dims = [dim//2**i for i in range(order)]
- self.dims.reverse()
- self.proj_in = nn.Conv2d(dim, 2*dim, 1)
-
- if gflayer is None:
- self.dwconv = get_dwconv(sum(self.dims), 7, True)
- else:
- self.dwconv = gflayer(sum(self.dims), h=h, w=w)
-
- self.proj_out = nn.Conv2d(dim, dim, 1)
- self.pws = nn.ModuleList(
- [nn.Conv2d(self.dims[i], self.dims[i+1], 1) for i in range(order-1)]
- )
- self.scale = s
-
- def forward(self, x, mask=None, dummy=False):
- # B, C, H, W = x.shape gnconv [512]by iscyy/air
- fused_x = self.proj_in(x)
- pwa, abc = torch.split(fused_x, (self.dims[0], sum(self.dims)), dim=1)
- dw_abc = self.dwconv(abc) * self.scale
- dw_list = torch.split(dw_abc, self.dims, dim=1)
- x = pwa* dw_list[0]
- for i in range(self.order-1):
- x = self.pws[i](x) * dw_list[i+1]
- x = self.proj_out(x)
-
- return x
-
- def get_dwconv(dim, kernel, bias):
- return nn.Conv2d(dim, dim, kernel_size=kernel, padding=(kernel-1)//2, bias=bias, groups=dim)
-
-
- class HorBlock(nn.Module):
- def __init__(self, dim, drop_path=0., layer_scale_init_value=1e-6, gnconv=gnconv):
- super().__init__()
- self.norm1 = HorLayerNorm(dim, eps=1e-6, data_format='channels_first')
- self.gnconv = gnconv(dim)
- self.norm2 = HorLayerNorm(dim, eps=1e-6)
- self.pwconv1 = nn.Linear(dim, 4*dim)
- self.act = nn.GELU()
- self.pwconv2 = nn.Linear(4 * dim, dim)
-
- self.gamma1 = nn.Parameter(layer_scale_init_value * torch.ones(dim),
- requires_grad=True) if layer_scale_init_value >0 else None
-
- self.gamma2 = nn.Parameter(layer_scale_init_value * torch.ones(dim),
- requires_grad=True) if layer_scale_init_value > 0 else None
- self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
-
- def forward(self, x):
- B, C, H, W = x.shape
- if self.gamma1 is not None:
- gamma1 = self.gamma1.view(C, 1, 1)
- else:
- gamma1 = 1
- x = x + self.drop_path(gamma1 * self.gnconv(self.norm1(x)))
-
- input = x
- x = x.permute(0, 2, 3, 1)
- x = self.norm2(x)
- x = self.pwconv1(x)
- x = self.act(x)
- x = self.pwconv2(x)
- if self.gamma2 is not None:
- x = self.gamma2 * x
- x = x.permute(0, 3, 1, 2)
- x = input + self.drop_path(x)
- return x
然后在yolo.py中找到parse_model函数,对HorBlock类进行声明。声明的位置如下图所示。可通过Ctrl+F的形式找关键词找到这部分代码。
最后就是针对配置文件.yaml进行修改。修改后的代码如下:
- # parameters
- nc: 2 # number of classes
- depth_multiple: 0.33 # model depth multiple
- width_multiple: 0.50 # layer channel multiple
-
- # anchors
- anchors:
- - [10,13, 16,30, 33,23] # P3/8
- - [30,61, 62,45, 59,119] # P4/16
- - [116,90, 156,198, 373,326] # P5/32
-
- # 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, HorBlock, [128]],
- [-1, 1, Conv, [256, 3, 2]], # 3-P3/8
- [-1, 6, HorBlock, [256]],
- [-1, 1, Conv, [512, 3, 2]], # 5-P4/16
- [-1, 9, HorBlock, [512]],
- [-1, 1, Conv, [1024, 3, 2]], # 7-P5/32
- [-1, 3, HorBlock, [1024]],
- [-1, 1, SPPF, [1024, 5]], # 9
- ]
-
- # YOLOv5 v6.0 head
- head:
- [[-1, 1, Conv, [512, 1, 1]],
- [-1, 1, nn.Upsample, [None, 2, 'nearest']],
- [[-1, 6], 1, Concat, [1]], # cat backbone P4
- [-1, 3, C3, [512, False]], # 13
-
- [-1, 1, Conv, [256, 1, 1]],
- [-1, 1, nn.Upsample, [None, 2, 'nearest']],
- [[-1, 4], 1, Concat, [1]], # cat backbone P3
- [-1, 3, C3, [256, False]], # 17 (P3/8-small)
-
- [-1, 1, Conv, [256, 3, 2]],
- [[-1, 14], 1, Concat, [1]], # cat head P4
- [-1, 3, C3, [512, False]], # 20 (P4/16-medium)
-
- [-1, 1, Conv, [512, 3, 2]],
- [[-1, 10], 1, Concat, [1]], # cat head P5
- [-1, 3, C3, [1024, False]], # 23 (P5/32-large)
-
- [[17, 20, 23], 1, Detect, [nc, anchors]], # Detect(P3, P4, P5)
- ]
其中的nc值得根据你任务的类别来进行修改。
以上就是整体的修改过程,整体网络的结构如下:
在一个小数据集上跑了150轮后的结果如下,单从这个结果上来看效果并不好,不如原始的网络的效果好。从结果反推原因,可能是将所有的C3模块都进行替换并不是一个好的选择。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。