import torch
import torch.nn as nn
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.Mish() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
self.bn2 = nn.BatchNorm2d(c2)
self.act2 = nn.Mish() if act is True else (act if isinstance(act, nn.Module) else nn.Identity())
def forward(self, x):
x = self.act1(self.bn1(self.conv1(x)))
offset = self.conv2_offset(x)
x = self.act2(self.bn2(self.conv2(x, offset)))
return x
# Parallel deformable convolutional network
class DCN(nn.Module):
# ch_in, ch_out, kernel, stride, padding, groups
def __init__(self, c1, c2):
super().__init__()
self.c_1 = int(c1)
self.c_2 = int(c2)
# deformable convolution
self.dc = DCNConv(self.c_1, self.c_2)
def forward(self, x):
x = self.dc(x)
return x
def main(inputs):
model_1 = DCN(3, 6)
model_2 = DCN(3, 3)
output_1 = model_1(inputs)
output_2 = model_2(inputs)
print(output_1.shape) # torch.Size([1, 6, 2, 2])
print(output_2.shape) # torch.Size([1, 3, 2, 2])
if __name__ == "__main__":
tensor = torch.rand(1, 3, 4, 4)
main(tensor)
可以尝试更改DCN结构来调整下采样倍率(默认两倍下采样)