更新,之前的deformconv2d没加调制参数,应该是dcnv1
现在的才是dcnv2
from torchvision.ops import DeformConv2d
class DCNConv(nn.Module):
# Standard convolution
def __init__(self,
in_channels,
out_channels,
kernel_size,
stride=1,
padding=0,
dilation=1, act=True):
# 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__()
# self.conv1 = nn.Conv2d(c1, c2, 3, 2, 1, groups=g, bias=False)
deformable_groups = 1
offset_channels = 18
self.conv2_offset = nn.Conv2d(in_channels, deformable_groups * offset_channels, kernel_size=3, padding=1)
self.modulator_conv = nn.Conv2d(in_channels,
offset_channels//2,
kernel_size=3,
stride=1,
padding=1,
bias=True)
self.conv2 = DeformConv2d(in_channels, out_channels, kernel_size=3, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.act1 = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
self.bn2 = nn.BatchNorm2d(out_channels)
self.act2 = nn.ReLU(inplace=True) if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
def forward(self, x):
# print(x.shape)
# print('-'*50)
# x = self.act1(self.bn1(self.conv1(x)))
# print(x.shape)
offset = self.conv2_offset(x)
modulator = self.modulator_conv(x)
x = self.act2(self.bn2(self.conv2(x,offset,modulator)))
return x
修改common.py 文件
from torchvision.ops import DeformConv2d
class DCNConv(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__()
self.conv1 = nn.Conv2d(c1, c2, 3, 2, 1, groups=g, bias=False)
deformable_groups = 1
offset_channels = 18
self.conv2_offset = nn.Conv2d(c2, deformable_groups * offset_channels, kernel_size=3, padding=1)
self.conv2 = DeformConv2d(c2, c2, kernel_size=3, padding=1, bias=False)
# self.conv2 = DeformableConv2d(c2, c2, k, s, autopad(k, p), groups=g, bias=False)
self.bn1 = nn.BatchNorm2d(c2)
self.act1 = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
self.bn2 = nn.BatchNorm2d(c2)
self.act2 = nn.SiLU() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
def forward(self, x):
# print(x.shape)
# print('-'*50)
x = self.act1(self.bn1(self.conv1(x)))
# print(x.shape)
offset = self.conv2_offset(x)
x = self.act2(self.bn2(self.conv2(x,offset)))
# print('-'*50)
# print(x.shape)
return x
修改yolo.p文件
找到parse_model函数,把DCNConv加入进去
if m in (Conv, GhostConv, Bottleneck, GhostBottleneck, SPP, SPPF, DWConv, MixConv2d, Focus, CrossConv,
BottleneckCSP, C3, C3TR, C3SPP, C3Ghost, nn.ConvTranspose2d, DWConvTranspose2d, C3x, CoordAtt, DCNConv):
修改yolov5s.yaml文件
# YOLOv5 🚀 by Ultralytics, GPL-3.0 license
# Parameters
nc: 1 # number of classes
depth_multiple: 0.33 # model depth multiple
width_multiple: 0.50 # layer channel multiple
anchors:
- [10,13, 16,30, 33,23]
- [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, DCNConv, [128, 3, 2]], # 1-P2/4
[-1, 3, C3, [128]],
[-1, 1, DCNConv, [256, 3, 2]], # 3-P3/8
[-1, 6, C3, [256]],
[-1, 1, DCNConv, [512, 3, 2]], # 5-P4/16
[-1, 9, C3, [512]],
[-1, 1, DCNConv, [1024, 3, 2]], # 7-P5/32
[-1, 3, C3, [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)
]
运行下面命令查看网络结构文章来源:https://www.toymoban.com/news/detail-536094.html
python models/yolo.py --cfg models/yolov5s.yaml
在自己的数据集上,map50提升了5%文章来源地址https://www.toymoban.com/news/detail-536094.html
到了这里,关于yolov5 加入可形变卷积的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!