def __init__(self, in_channels, out_channels, norm='BN', fuse_type='fast_norm'): super().__init__() self.in_features = list(in_channels.keys())[::-1] td_channels = in_channels[self.in_features[0]] for in_feat in self.in_features[1:]: td_conv = Conv2d( td_channels, out_channels, kernel_size=1, padding=0, bias=True, norm=get_norm(norm, out_channels), activation=nn.Upsample(scale_factor=2) ) in_conv = Conv2d( in_channels[in_feat], out_channels, kernel_size=1, padding=0, bias=True, norm=get_norm(norm, out_channels), ) fuse = FuseBlock(out_channels, num_weights=2, norm=norm, fuse_type=fuse_type) self.add_module(f'{in_feat}_td', td_conv) self.add_module(f'{in_feat}_in', in_conv) self.add_module(f'{in_feat}_fuse', fuse) td_channels = out_channels
def __init__(self, in_features, out_features, in_channels, out_channels, norm='BN'): super().__init__() self.in_features = in_features self.out_features = out_features assert len(in_features) == len(in_channels) in_channel = in_channels[-1] self.out_channels = in_channels self.num_block = 0 for out_feat in out_features[len(in_features):]: conv1x1 = Conv2d( in_channel, out_channels, kernel_size=1, stride=1, padding=0, bias=True, norm=get_norm(norm, out_channels), activation=nn.MaxPool2d(kernel_size=2), ) self.add_module(out_feat, conv1x1) self.num_block += 1 in_channel = out_channels self.out_channels.append(out_channels) self.out_channels = dict(zip(self.out_features, self.out_channels))
def __init__(self, fpn, in_features, out_channels, use_bias=True, norm=""): super(PANetBase, self).__init__() assert isinstance(fpn, Backbone) self.fpn = fpn self.in_features = in_features fpn_shape = fpn.output_shape() in_channels = [fpn_shape[k].channels for k in self.in_features] in_strides = [fpn_shape[k].stride for k in self.in_features] self.lateral_convs = [] self.output_convs = [] for idx, in_channels in enumerate(in_channels[:-1], start=1): t = get_norm(norm, out_channels) lateral_conv = Conv2d( in_channels, out_channels, kernel_size=3, stride=2, padding=1, bias=use_bias, norm=get_norm(norm, out_channels) ) output_conv = Conv2d( out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=use_bias, norm=get_norm(norm, out_channels) ) weight_init.c2_xavier_fill(lateral_conv) weight_init.c2_xavier_fill(output_conv) stage = int(math.log2(in_strides[idx])) self.add_module(f"panet_lateral{stage}", lateral_conv) self.add_module(f"panet_output{stage}", output_conv) self.lateral_convs.append(lateral_conv) self.output_convs.append(output_conv) self._out_feature_strides = {f"n{int(math.log2(s))}": s for s in in_strides} self._out_features = list(self._out_feature_strides.keys()) self._out_feature_channels = {k: out_channels for k in self._out_features} self._size_divisibility = in_strides[-1]
def __init__(self, in_channels, out_channels, norm='BN', fuse_type='fast_norm'): super().__init__() self.in_features = list(in_channels.keys()) for in_feat in self.in_features[1:]: bu_conv = Conv2d( out_channels, out_channels, kernel_size=1, padding=0, bias=True, norm=get_norm(norm, out_channels), activation=nn.MaxPool2d(kernel_size=2) ) in_conv = Conv2d( in_channels[in_feat], out_channels, kernel_size=1, padding=0, bias=True, norm=get_norm(norm, out_channels), ) self.add_module(f'{in_feat}_bu', bu_conv) self.add_module(f'{in_feat}_in', in_conv) for in_feat in self.in_features[1:-1]: td_conv = Conv2d( out_channels, out_channels, kernel_size=1, padding=0, bias=True, norm=get_norm(norm, out_channels), ) fuse = FuseBlock(out_channels, num_weights=3, norm=norm, fuse_type=fuse_type) self.add_module(f'{in_feat}_td', td_conv) self.add_module(f'{in_feat}_fuse', fuse) fuse = FuseBlock(out_channels, num_weights=2, norm=norm, fuse_type=fuse_type) self.add_module(f'{self.in_features[-1]}_fuse', fuse)
def __init__(self, in_channels=3, out_channels=32, norm="BN"): super().__init__() self.conv1 = Conv2d( in_channels, out_channels, kernel_size=3, stride=2, padding=1, bias=False, norm=get_norm(norm, out_channels), )
def __init__(self, input_shape : ShapeSpec, norm='BN', use_bias=True): super().__init__() in_channel = input_shape.channels self.conv = Conv2d( in_channel, in_channel, kernel_size=3, stride=1, padding=1, bias=use_bias, norm=get_norm(norm, in_channel), ) weight_init.c2_xavier_fill(self.conv)
def __init__(self, in_channels=3, out_channels=64, norm="BN", stem_bias=True): super().__init__() self.conv1 = Conv2d( in_channels, out_channels, kernel_size=7, stride=2, padding=3, bias=stem_bias, norm=get_norm(norm, out_channels), ) weight_init.c2_msra_fill(self.conv1)
def __init__( self, in_channels, out_channels, norm="BN", memory_efficient=True, ): super().__init__() self.conv1 = Conv2d( in_channels, out_channels, kernel_size=1, bias=False, norm=get_norm(norm, out_channels), activation=MemoryEfficientSwish() if memory_efficient else Swish(), )
def __init__(self, bottom_up, in_features, out_channels, norm="", top_block=None, fuse_type="sum"): super(FPNBase, self).__init__() assert isinstance(bottom_up, Backbone) in_strides = [bottom_up.out_feature_strides[f] for f in in_features] in_channels = [bottom_up.out_feature_channels[f] for f in in_features] _assert_strides_are_log2_contiguous(in_strides) lateral_convs = [] output_convs = [] use_bias = norm == "" for idx, in_channels in enumerate(in_channels): lateral_norm = get_norm(norm, out_channels) output_norm = get_norm(norm, out_channels) lateral_conv = Conv2d(in_channels, out_channels, kernel_size=1, bias=use_bias, norm=lateral_norm) output_conv = Conv2d( out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=use_bias, norm=output_norm, ) weight_init.c2_xavier_fill(lateral_conv) weight_init.c2_xavier_fill(output_conv) stage = int(math.log2(in_strides[idx])) self.add_module(f"fpn_lateral{stage}", lateral_conv) self.add_module(f"fpn_output{stage}", output_conv) lateral_convs.append(lateral_conv) output_convs.append(output_conv) self.lateral_convs = lateral_convs[::-1] self.output_convs = output_convs[::-1] self.top_block = top_block self.in_features = in_features self.bottom_up = bottom_up self._out_feature_strides = { f"p{int(math.log2(s))}": s for s in in_strides } if self.top_block is not None: for s in range(stage, stage + self.top_block.num_levels): self._out_feature_strides[f"p{s+1}"] = 2**(s + 1) self._out_features = list(self._out_feature_strides.keys()) self._out_feature_channels = { k: out_channels for k in self._out_features } self._size_divisibility = in_strides[-1] assert fuse_type in {"avg", "sum"} self._fuse_type = fuse_type
def __init__( self, in_channels, out_channels, *, bottleneck_channels, stride=1, num_groups=1, norm="BN", stride_in_1x1=False, dilation=1, ): super().__init__(in_channels, out_channels, stride) if in_channels != out_channels: self.shortcut = Conv2d( in_channels, out_channels, kernel_size=1, stride=stride, bias=False, norm=get_norm(norm, out_channels), ) else: self.shortcut = None # The original MSRA ResNet models have stride in the first 1x1 conv # The subsequent fb.torch.resnet and Caffe2 ResNe[X]t implementations have # stride in the 3x3 conv stride_1x1, stride_3x3 = (stride, 1) if stride_in_1x1 else (1, stride) self.conv1 = Conv2d( in_channels, bottleneck_channels, kernel_size=1, stride=stride_1x1, bias=False, norm=get_norm(norm, bottleneck_channels), ) self.conv2 = Conv2d( bottleneck_channels, bottleneck_channels, kernel_size=3, stride=stride_3x3, padding=1 * dilation, bias=False, groups=num_groups, dilation=dilation, norm=get_norm(norm, bottleneck_channels), ) self.conv3 = Conv2d( bottleneck_channels, out_channels, kernel_size=1, bias=False, norm=get_norm(norm, out_channels), ) for layer in [self.conv1, self.conv2, self.conv3, self.shortcut]: if layer is not None: weight_init.c2_msra_fill(layer)
def __init__( self, in_channels, out_channels, kernel_size, *, stride=1, expand_dim=1, num_groups=1, norm="BN", dilation=1, se_ratio=0.25, drop_connect_prob=0.2, memory_efficient=True, is_skip=True, ): super().__init__(in_channels, out_channels, stride) self.is_skip = (is_skip and stride == 1 and in_channels != out_channels) self.drop_connect_prob = drop_connect_prob if expand_dim > 1: self.expand_conv = Conv2d( in_channels, in_channels * expand_dim, kernel_size=1, stride=1, bias=False, norm=get_norm(norm, in_channels * expand_dim), activation=MemoryEfficientSwish() if memory_efficient else Swish(), ) self.depthwise_conv = Conv2d( in_channels * expand_dim, in_channels * expand_dim, kernel_size=kernel_size, stride=stride, padding=int((kernel_size - 1) / 2) * dilation, bias=False, groups=in_channels * expand_dim, dilation=dilation, norm=get_norm(norm, in_channels * expand_dim), activation=MemoryEfficientSwish() if memory_efficient else Swish(), ) if se_ratio > 0: self.SEblock = SqueezeExcitation2d( in_channels * expand_dim, int(in_channels * se_ratio), in_channels * expand_dim, memory_efficient=memory_efficient, ) self.project_conv = Conv2d( in_channels * expand_dim, out_channels, kernel_size=1, stride=1, bias=False, norm=get_norm(norm, out_channels), activation=None, )