def __init__(self, in_channels, out_channels, kernel_size=1, stride=1, dilation=1, groups=1, offset_groups=1): super().__init__() offset_channels = 2 * kernel_size * kernel_size self.conv2d_offset = nn.Conv2d( in_channels, offset_channels * offset_groups, kernel_size=3, stride=stride, padding=dilation, dilation=dilation, ) self.conv2d = DeformConv2d(in_channels, out_channels, kernel_size=kernel_size, stride=stride, padding=dilation, dilation=dilation, groups=groups, bias=False)
def __init__(self, inplanes, planes, stride=1, downsample=None, dcn=None): super(BasicBlock, self).__init__() self.with_dcn = dcn is not None self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = BatchNorm2d(planes) self.relu = nn.ReLU(inplace=True) self.with_modulated_dcn = False if not self.with_dcn: self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, padding=1, bias=False) else: from torchvision.ops import DeformConv2d deformable_groups = dcn.get('deformable_groups', 1) offset_channels = 18 self.conv2_offset = nn.Conv2d(planes, deformable_groups * offset_channels, kernel_size=3, padding=1) self.conv2 = DeformConv2d(planes, planes, kernel_size=3, padding=1, bias=False) self.bn2 = BatchNorm2d(planes) self.downsample = downsample self.stride = stride
def __init__(self, channels_conv=64, channels_deform=64, num_conv=1, use_act=False): super(DeformConvBlock, self).__init__() # The conv and defromConv should have the same stride, kernel size and dilation so # that the output of conv matches the size of output of deformConv and therefore can # serve as the offset self.kernel_size = 3 self.conv = nn.Sequential() for i in range(num_conv - 1): if use_act: self.conv.add_module("act %d" % i, nn.PReLU(channels_conv)) self.conv.add_module( "conv %d" % i, nn.Conv2d(channels_conv, channels_conv, self.kernel_size, 1, 1, 1)) if use_act: self.conv.add_module("act_final", nn.PReLU(channels_conv)) if __USE_WEIGHT__: self.conv.add_module( "conv_final", nn.Conv2d(channels_conv, 64 + 2 * 3 * 3, self.kernel_size, 1, 1, 1)) else: self.conv.add_module( "conv_final", nn.Conv2d(channels_conv, 2 * 3 * 3, self.kernel_size, 1, 1, 1)) self.deformConv = DeformConv2d(channels_deform, channels_deform, self.kernel_size, 1, 1, 1)
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=nn.BatchNorm2d, deform_num_groups=1): super(DeformBottleneckBlock, self).__init__() width = int(planes * (base_width / 64.)) * groups # Both self.conv2 and self.downsample layers downsample the input when stride != 1 self.conv1 = conv1x1(inplanes, width) self.bn1 = norm_layer(width) offset_channels = 18 self.conv2_offset = conv3x3(width, offset_channels * deform_num_groups, stride, dilation) self.conv2 = DeformConv2d(width, width, kernel_size=3, stride=stride, padding=dilation, groups=groups, dilation=dilation) self.bn2 = norm_layer(width) self.conv3 = conv1x1(width, planes * self.expansion) self.bn3 = norm_layer(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def __init__(self, in_channel, out_channel, norm_func): super(DeformConv, self).__init__() self.norm = norm_func(out_channel) self.relu = nn.ReLU(inplace=True) self.deform_conv = DeformConv2d(in_channels=in_channel, out_channels=out_channel, kernel_size=(3, 3), stride=1, padding=1, dilation=1)
def __init__(self, in_dim, out_dim, k=3, stride=1, bias=True): super(DConv2d, self).__init__() self.offset = nn.Conv2d(in_dim, 2 * k * k, 1, stride, 0) self.offset.weight.data *= .1 self.dconv = DeformConv2d(in_dim, out_dim, k, stride, k // 2, bias=False)
def __init__(self, chin, chout, kernel_size, stride=1, padding=None, bias=True, dcn_conv=False): super(CR, self).__init__() if padding is None: padding = (kernel_size - 1) // 2 self.conv=nn.Conv2d(chin,chout,kernel_size,stride,padding,bias=bias) if dcn_conv is False \ else DeformConv2d(chin,chout,kernel_size,stride,padding,bias=bias) self.act = nn.ReLU(inplace=True)
def __init__(self, inplanes, planes, stride=1, downsample=None, cbam=False, dcn=False): super(Bottleneck, self).__init__() self.with_dcn = dcn self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=1, bias=False) self.bn1 = nn.BatchNorm2d(planes) if not self.with_dcn: self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=stride, padding=1, bias=False) else: # deformable_groups = dcn.get('deformable_groups', 1) deformable_groups = 1 from torchvision.ops import DeformConv2d offset_channels = 18 self.conv2_offset = nn.Conv2d(planes, deformable_groups * offset_channels, stride=stride, kernel_size=3, padding=1) self.conv2 = DeformConv2d(planes, planes, kernel_size=3, padding=1, stride=stride, bias=False) self.bn2 = nn.BatchNorm2d(planes) self.conv3 = nn.Conv2d(planes, planes * 4, kernel_size=1, bias=False) self.bn3 = nn.BatchNorm2d(planes * 4) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride self.use_cbam = cbam if self.use_cbam: self.cbam = CBAM(n_channels_in=self.expansion * planes, reduction_ratio=1, kernel_size=3)
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None, with_dcn=False): """Init function.""" super(Bottleneck, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d self.with_dcn = with_dcn width = int(planes * (base_width / 64.)) * groups # Both self.conv2 and self.downsample layers downsample the input when stride != 1 self.conv1 = conv1x1(inplanes, width) self.bn1 = norm_layer(width) if self.with_dcn: offset_channels = 18 self.conv2_offset = nn.Conv2d(width, groups * offset_channels, kernel_size=3, stride=stride, padding=1) self.conv2_offset.weight.data.fill_(0) self.conv2_offset.bias.data.fill_(0) self.conv2 = DeformConv2d(width, width, kernel_size=3, stride=stride, padding=1, groups=groups, dilation=dilation, bias=False) else: self.conv2 = conv3x3(width, width, stride, groups, dilation) self.bn2 = norm_layer(width) self.conv3 = conv1x1(width, planes * self.expansion) self.bn3 = norm_layer(planes * self.expansion) self.relu = nn.ReLU(inplace=True) self.downsample = downsample self.stride = stride
def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1, base_width=64, dilation=1, norm_layer=None, with_dcn=False): """Init function.""" super(BasicBlock, self).__init__() if norm_layer is None: norm_layer = nn.BatchNorm2d if groups != 1 or base_width != 64: raise ValueError( 'BasicBlock only supports groups=1 and base_width=64') if dilation > 1: raise NotImplementedError( "Dilation > 1 not supported in BasicBlock") self.with_dcn = with_dcn # Both self.conv1 and self.downsample layers downsample the input when stride != 1 if self.with_dcn: offset_channels = 18 self.conv1_offset = nn.Conv2d(inplanes, groups * offset_channels, kernel_size=3, stride=stride, padding=1) self.conv1_offset.weight.data.fill_(0) self.conv1_offset.bias.data.fill_(0) self.conv1 = DeformConv2d(inplanes, planes, kernel_size=3, padding=1, stride=stride, bias=False) else: self.conv1 = conv3x3(inplanes, planes, stride) # self.conv1 = conv3x3(inplanes, planes, stride) self.bn1 = norm_layer(planes) self.relu = nn.ReLU(inplace=True) if self.with_dcn: offset_channels = 18 self.conv2_offset = nn.Conv2d(planes, groups * offset_channels, kernel_size=3, padding=1) self.conv2_offset.weight.data.fill_(0) self.conv2_offset.bias.data.fill_(0) self.conv2 = DeformConv2d(planes, planes, kernel_size=3, padding=1, groups=groups, bias=False) else: self.conv2 = conv3x3(planes, planes) self.bn2 = norm_layer(planes) self.downsample = downsample self.stride = stride