| 55 | |
| 56 | |
| 57 | class ModulatedDeformConvWithOff(nn.Module): |
| 58 | |
| 59 | def __init__(self, in_channels, out_channels, |
| 60 | kernel_size=3, stride=1, padding=1, |
| 61 | dilation=1, deformable_groups=1): |
| 62 | super(ModulatedDeformConvWithOff, self).__init__() |
| 63 | self.offset_mask_conv = nn.Conv2d( |
| 64 | in_channels, |
| 65 | deformable_groups * 3 * kernel_size * kernel_size, |
| 66 | kernel_size=kernel_size, |
| 67 | stride=stride, |
| 68 | padding=padding, |
| 69 | ) |
| 70 | self.dcnv2 = ModulatedDeformConv( |
| 71 | in_channels, out_channels, kernel_size=kernel_size, |
| 72 | stride=stride, padding=padding, dilation=dilation, |
| 73 | deformable_groups=deformable_groups, |
| 74 | ) |
| 75 | |
| 76 | def forward(self, input): |
| 77 | x = self.offset_mask_conv(input) |
| 78 | o1, o2, mask = torch.chunk(x, 3, dim=1) |
| 79 | offset = torch.cat((o1, o2), dim=1) |
| 80 | mask = torch.sigmoid(mask) |
| 81 | output = self.dcnv2(input, offset, mask) |
| 82 | return output |