def __init__(self, in_channels, out_channels, *, stride=1, norm="BN"): """ Args: in_channels (int): Number of input channels. out_channels (int): Number of output channels. stride (int): Stride for the first conv. norm (str or callable): normalization for all conv layers. See :func:`layers.get_norm` for supported format. """ 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 self.conv1 = Conv2d( in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False, norm=get_norm(norm, out_channels), ) self.conv2 = Conv2d( out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm=get_norm(norm, out_channels), ) for layer in [self.conv1, self.conv2, self.shortcut]: if layer is not None: # shortcut can be None weight_init.c2_msra_fill(layer)
def __init__( self, _RConv, in_channels, out_channels, is_first, rot_1x1_in, rot_1x1_out, noise_var=0, stride=1, padding=1, dilation=1, norm=None, activation=None, ): super(PRConvBlock, self).__init__() self.conv = _RConv( in_channels=in_channels, out_channels=out_channels, is_first=is_first, rot_1x1_in=rot_1x1_in, stride=stride, padding=padding, dilation=dilation, norm=None, #norm, activation=F.relu) self.is_first = is_first self.rot_1x1_out = rot_1x1_out self.noise_var = noise_var self.kernel_rot = self.conv.kernel_rot self.activation = activation nn.init.kaiming_normal_(self.conv.weight, mode="fan_out", nonlinearity="relu") if self.noise_var > 0: self.positional_noise = DefemLayer() if self.rot_1x1_out: self.conv_rot_1x1 = GConv1x1( rot_1x1=True, in_channels=out_channels, out_channels=out_channels, kernel_size=1, kernel_rot=self.kernel_rot, stride=1, padding=0, dilation=1, # norm=get_norm(norm, out_channels) ) weight_init.c2_msra_fill(self.conv_rot_1x1) self.norm = None if norm != None: self.norm = get_norm(norm, out_channels)
def __init__(self, input_shape: ShapeSpec, *, num_classes, conv_dims, conv_norm="", **kwargs): """ NOTE: this interface is experimental. Args: input_shape (ShapeSpec): shape of the input feature num_classes (int): the number of classes. 1 if using class agnostic prediction. conv_dims (list[int]): a list of N>0 integers representing the output dimensions of N-1 conv layers and the last upsample layer. conv_norm (str or callable): normalization for the conv layers. See :func:`detectron2.layers.get_norm` for supported types. """ super().__init__(**kwargs) assert len(conv_dims) >= 1, "conv_dims have to be non-empty!" self.conv_norm_relus = [] cur_channels = input_shape.channels for k, conv_dim in enumerate(conv_dims[:-1]): conv = Conv2d( cur_channels, conv_dim, kernel_size=3, stride=1, padding=1, bias=not conv_norm, norm=get_norm(conv_norm, conv_dim), activation=F.relu, ) self.add_module("mask_fcn{}".format(k + 1), conv) self.conv_norm_relus.append(conv) cur_channels = conv_dim self.deconv = ConvTranspose2d(cur_channels, conv_dims[-1], kernel_size=2, stride=2, padding=0) cur_channels = conv_dims[-1] self.predictor = Conv2d(cur_channels, num_classes, kernel_size=1, stride=1, padding=0) for layer in self.conv_norm_relus + [self.deconv]: weight_init.c2_msra_fill(layer) # use normal distribution initialization for mask prediction layer nn.init.normal_(self.predictor.weight, std=0.001) if self.predictor.bias is not None: nn.init.constant_(self.predictor.bias, 0)
def __init__(self, cfg, input_shape: List[ShapeSpec]): super().__init__() # Standard RPN is shared across levels: in_channels = [s.channels for s in input_shape] assert len( set(in_channels)) == 1, "Each level must have the same channel!" in_channels = in_channels[0] dwexpand_factor = cfg.MODEL.RPN.DWEXPAND_FACTOR norm = cfg.MODEL.RPN.NORM # RPNHead should take the same input as anchor generator # NOTE: it assumes that creating an anchor generator does not have unwanted side effect. anchor_generator = build_anchor_generator(cfg, input_shape) num_cell_anchors = anchor_generator.num_cell_anchors box_dim = anchor_generator.box_dim assert (len(set(num_cell_anchors)) == 1 ), "Each level must have the same number of cell anchors" num_cell_anchors = num_cell_anchors[0] # 3x3 conv for the hidden representation expand_channels = dwexpand_factor * in_channels conv = [] conv.append(Conv2d(in_channels, expand_channels, kernel_size=1, bias=not norm,\ norm=get_norm(norm, expand_channels), activation=F.relu)) conv.append(Conv2d(expand_channels, expand_channels, kernel_size=5, padding=2, groups=expand_channels,\ bias=not norm, norm=get_norm(norm, expand_channels), activation=F.relu)) self.add_module('conv', nn.Sequential(*conv)) # in_channels = expand_channels # self.conv = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1) # 1x1 conv for predicting objectness logits self.objectness_logits = nn.Conv2d(in_channels, num_cell_anchors, kernel_size=1, stride=1) # 1x1 conv for predicting box2box transform deltas self.anchor_deltas = nn.Conv2d(in_channels, num_cell_anchors * box_dim, kernel_size=1, stride=1) for l in [*self.conv, self.objectness_logits, self.anchor_deltas]: nn.init.normal_(l.weight, std=0.01) if l.bias is not None: nn.init.constant_(l.bias, 0)
def __init__(self, dim, norm="BN", **kwargs): super().__init__() num_groups = kwargs.pop("num_group", None) self.block = nn.Sequential( nn.ReLU(True), nn.Conv2d(dim, dim, 3, 1, 1), get_norm(norm, dim) if not num_groups else get_norm(norm, dim, num_groups=num_groups), nn.ReLU(True), nn.Conv2d(dim, dim, 1), get_norm(norm, dim) if not num_groups else get_norm(norm, dim, num_groups=num_groups), ) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu') elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)): nn.init.constant_(m.weight, 1) nn.init.constant_(m.bias, 0)
def __init__(self, cfg, input_shape: ShapeSpec): """ The following attributes are parsed from config: num_conv: the number of conv layers conv_dim: the dimension of the conv layers norm: normalization for the conv layers """ super().__init__(cfg, input_shape) # fmt: off num_classes = cfg.MODEL.ROI_HEADS.NUM_CLASSES conv_dims = cfg.MODEL.ROI_VISIBLE_MASK_HEAD.CONV_DIM self.norm = cfg.MODEL.ROI_VISIBLE_MASK_HEAD.NORM num_conv = cfg.MODEL.ROI_VISIBLE_MASK_HEAD.NUM_CONV input_channels = input_shape.channels cls_agnostic_mask = cfg.MODEL.ROI_VISIBLE_MASK_HEAD.CLS_AGNOSTIC_MASK # fmt: on self.conv_norm_relus = [] for k in range(num_conv): conv = Conv2d( input_channels if k == 0 else conv_dims, conv_dims, kernel_size=3, stride=1, padding=1, bias=not self.norm, norm=get_norm(self.norm, conv_dims), activation=F.relu, ) self.add_module("visible_mask_fcn{}".format(k + 1), conv) # this mask_fcn means visible_mask_fcn self.conv_norm_relus.append(conv) self.deconv = ConvTranspose2d( conv_dims if num_conv > 0 else input_channels, conv_dims, kernel_size=2, stride=2, padding=0, ) num_mask_classes = 1 if cls_agnostic_mask else num_classes self.predictor = Conv2d(conv_dims, num_mask_classes, kernel_size=1, stride=1, padding=0) for layer in self.conv_norm_relus + [self.deconv]: weight_init.c2_msra_fill(layer) # use normal distribution initialization for mask prediction layer nn.init.normal_(self.predictor.weight, std=0.001) if self.predictor.bias is not None: nn.init.constant_(self.predictor.bias, 0)
def __init__(self, w_in, w_out, stride, norm, activation_class, params): super().__init__(w_in, w_out, stride) self.proj, self.bn = None, None if (w_in != w_out) or (stride != 1): self.proj = conv2d(w_in, w_out, 1, stride=stride) self.bn = get_norm(norm, w_out) self.f = BottleneckTransform(w_in, w_out, stride, norm, activation_class, params) self.af = activation_class()
def __init__(self, inp, oup, stride, expand_ratio, norm='BN', ReL=True): super(InvertedResidual, self).__init__() self.stride = stride assert stride in [1, 2] hidden_dim = int(round(inp * expand_ratio)) self.use_res_connect = self.stride == 1 and inp == oup if expand_ratio == 1: self.conv = nn.Sequential( # dw Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False), get_norm(norm, hidden_dim), nn.ReLU6(inplace=True) if ReL else Mish(), # pw-linear Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), get_norm(norm, oup), ) else: self.conv = nn.Sequential( # pw Conv2d(inp, hidden_dim, 1, 1, 0, bias=False), get_norm(norm, hidden_dim), nn.ReLU6(inplace=True) if ReL else Mish(), # dw Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False), get_norm(norm, hidden_dim), nn.ReLU6(inplace=True) if ReL else Mish(), # pw-linear Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), get_norm(norm, oup), )
def __init__(self, input_shape: ShapeSpec, *, conv_dims: List[int], fc_dims: List[int], conv_norm=""): """ NOTE: this interface is experimental. Args: input_shape (ShapeSpec): shape of the input feature. conv_dims (list[int]): the output dimensions of the conv layers fc_dims (list[int]): the output dimensions of the fc layers conv_norm (str or callable): normalization for the conv layers. See :func:`detectron2.layers.get_norm` for supported types. """ logger = logging.getLogger(__name__) logger.info("FastRCNNConvFCHead input_shape: {}".format(input_shape)) logger.info("FastRCNNConvFCHead conv_dims: {}".format(conv_dims)) logger.info("FastRCNNConvFCHead fc_dims: {}".format(fc_dims)) logger.info("FastRCNNConvFCHead conv_norm: {}".format(conv_norm)) super().__init__() assert len(conv_dims) + len(fc_dims) > 0 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) self.conv_norm_relus = [] for k, conv_dim in enumerate(conv_dims): conv = Conv2d( self._output_size[0], conv_dim, kernel_size=3, padding=1, bias=not conv_norm, norm=get_norm(conv_norm, conv_dim), activation=F.relu, ) self.add_module("conv{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self._output_size = (conv_dim, self._output_size[1], self._output_size[2]) self.fcs = [] for k, fc_dim in enumerate(fc_dims): fc = Linear(np.prod(self._output_size), fc_dim) self.add_module("fc{}".format(k + 1), fc) self.fcs.append(fc) self._output_size = fc_dim for layer in self.conv_norm_relus: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer)
def __init__( self, input_shape: ShapeSpec, *, conv_dims: List[int], fc_dims: List[int], conv_norm="", box_head_depthwise_convs=False, box_head_depthwise_double_activation=False, ): """ NOTE: this interface is experimental. Args: input_shape (ShapeSpec): shape of the input feature. conv_dims (list[int]): the output dimensions of the conv layers fc_dims (list[int]): the output dimensions of the fc layers conv_norm (str or callable): normalization for the conv layers. See :func:`detectron2.layers.get_norm` for supported types. """ super().__init__() assert len(conv_dims) + len(fc_dims) > 0 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) self.conv_norm_relus = [] for k, conv_dim in enumerate(conv_dims): if box_head_depthwise_convs: conv = DepthwiseSeparableConv2d(self._output_size[0], conv_dim, kernel_size=3, padding=1, norm1=conv_norm if box_head_depthwise_double_activation else None, activation1=nn.ReLU() if box_head_depthwise_double_activation else None, norm2=conv_norm, activation2=nn.ReLU()) else: conv = Conv2d( self._output_size[0], conv_dim, kernel_size=3, padding=1, bias=not conv_norm, norm=get_norm(conv_norm, conv_dim), activation=nn.ReLU(), ) self.add_module("conv{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self._output_size = (conv_dim, self._output_size[1], self._output_size[2]) self.fcs = [] for k, fc_dim in enumerate(fc_dims): if k == 0: self.add_module("flatten", nn.Flatten()) fc = nn.Linear(int(np.prod(self._output_size)), fc_dim) self.add_module("fc{}".format(k + 1), fc) self.add_module("fc_relu{}".format(k + 1), nn.ReLU()) self.fcs.append(fc) self._output_size = fc_dim for layer in self.conv_norm_relus: if not box_head_depthwise_convs: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer)
def __init__(self, input_shape: ShapeSpec, *, conv_dims: List[int], fc_dims: List[int], conv_norm=""): """ NOTE: this interface is experimental. Args: input_shape (ShapeSpec): shape of the input feature. conv_dims (list[int]): the output dimensions of the conv layers fc_dims (list[int]): the output dimensions of the fc layers conv_norm (str or callable): normalization for the conv layers. See :func:`detectron2.layers.get_norm` for supported types. """ super().__init__() assert len(conv_dims) + len(fc_dims) > 0 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) self.conv_norm_relus = [] for k, conv_dim in enumerate(conv_dims): conv = Conv2d( self._output_size[0], conv_dim, kernel_size=3, padding=1, bias=not conv_norm, norm=get_norm(conv_norm, conv_dim), activation=nn.ReLU(inplace=True), ) self.add_module("conv{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self._output_size = (conv_dim, self._output_size[1], self._output_size[2]) self.fcs = [] for k, fc_dim in enumerate(fc_dims): if k == 0: self.add_module("flatten", nn.Flatten()) fc = Linear(int(np.prod(self._output_size)), fc_dim) self.add_module("fc{}".format(k + 1), fc) self.add_module("fc_relu{}".format(k + 1), nn.ReLU(inplace=True)) self.add_module("fc_dropout{}".format(k + 1), nn.Dropout(p=0.5, inplace=False)) self.fcs.append(fc) self._output_size = fc_dim for layer in self.conv_norm_relus: weight_init.c2_msra_fill(layer) for layer in self.fcs: # weight_init.c2_xavier_fill(layer) torch.nn.init.normal_(layer.weight, std=0.005) torch.nn.init.constant_(layer.bias, 0.1)
def __init__(self, input_shape: ShapeSpec, num_classes, num_conv, conv_dim, conv_norm="", vis_period=0): """ Args: input_shape (ShapeSpec): shape of the input feature num_classes (int): the number of classes. 1 if using class agnostic prediction. num_conv (int): the number of conv layers conv_dim (int): the dimension of the conv layers conv_norm (str or callable): normalization for the conv layers. See :func:`detectron2.layers.get_norm` for supported types. vis_period (int): visualization period. 0 to disable visualization. """ super().__init__(vis_period) input_channels = input_shape.channels self.conv_norm_relus = [] for k in range(num_conv): conv = Conv2d( input_channels if k == 0 else conv_dim, conv_dim, kernel_size=3, stride=1, padding=1, bias=not conv_norm, norm=get_norm(conv_norm, conv_dim), activation=F.relu, ) self.add_module("mask_fcn{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self.deconv = ConvTranspose2d( conv_dim if num_conv > 0 else input_channels, conv_dim, kernel_size=2, stride=2, padding=0, ) self.predictor = Conv2d(conv_dim, num_classes, kernel_size=1, stride=1, padding=0) for layer in self.conv_norm_relus + [self.deconv]: weight_init.c2_msra_fill(layer) # use normal distribution initialization for mask prediction layer nn.init.normal_(self.predictor.weight, std=0.001) if self.predictor.bias is not None: nn.init.constant_(self.predictor.bias, 0)
def __init__(self, feature_extractor, out_channels, norm="", fuse_type="sum"): """ Args: feature_extractor (Backbone): module representing the bottom up subnetwork. Must be a subclass of :class:`Backbone`. The multi-scale feature maps generated by the bottom up network, and listed in `in_features`, are used to generate PANET levels. out_channels (int): number of channels in the output feature maps. norm (str): the normalization to use. fuse_type (str): types for fusing the top down features and the lateral ones. It can be "sum" (default), which sums up element-wise; or "avg", which takes the element-wise mean of the two. TODO: concatenation """ super(PAN, self).__init__() assert isinstance(feature_extractor, Backbone) self.feature_extractor = feature_extractor self.panet_bottomup_conv1_modules = nn.ModuleList() self.panet_bottomup_conv2_modules = nn.ModuleList() use_bias = norm == "" for i in range(len(feature_extractor._out_features) - 1): bottomup_conv1 = Conv2d(out_channels, out_channels, kernel_size=3, stride=2, padding=1, bias=use_bias, norm=get_norm(norm, out_channels)) bottomup_conv2 = 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(bottomup_conv1) weight_init.c2_xavier_fill(bottomup_conv2) self.panet_bottomup_conv1_modules.append(bottomup_conv1) self.panet_bottomup_conv2_modules.append(bottomup_conv2)
def convert_norm_to_detectron2_format(module, norm): module_output = module if isinstance(module, torch.nn.BatchNorm2d): module_output = get_norm(norm, out_channels=module.num_features) module_output.load_state_dict(module.state_dict()) for name, child in module.named_children(): new_child = convert_norm_to_detectron2_format_and_init_default(child, norm) if new_child is not child: module_output.add_module(name, new_child) return module_output
def __init__(self, in_channels=3, out_channels=64, norm="BN"): """ Args: norm (str or callable): norm after the first conv layer. See :func:`layers.get_norm` for supported format. """ super().__init__(in_channels, out_channels, 4) self.in_channels = in_channels self.conv1 = nn.Sequential( Conv2d( in_channels, 32, kernel_size=3, stride=2, padding=1, bias=False, ), get_norm(norm, 32), nn.ReLU(inplace=True), Conv2d( 32, 32, kernel_size=3, stride=1, padding=1, bias=False, ), get_norm(norm, 32), nn.ReLU(inplace=True), Conv2d( 32, out_channels, kernel_size=3, stride=1, padding=1, bias=False, ), ) self.bn1 = get_norm(norm, out_channels) for layer in self.conv1: if isinstance(layer, Conv2d): weight_init.c2_msra_fill(layer)
def __init__(self, inp, oup, stride, expand_ratio, bn): super(InvertedResidual, self).__init__() assert stride in [1, 2] hidden_dim = round(inp * expand_ratio) self.identity = stride == 1 and inp == oup if expand_ratio == 1: self.conv = nn.Sequential( # dw nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False), get_norm(bn, hidden_dim), nn.ReLU6(inplace=True), # pw-linear nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), get_norm(bn, oup), ) else: self.conv = nn.Sequential( # pw nn.Conv2d(inp, hidden_dim, 1, 1, 0, bias=False), get_norm(bn, hidden_dim), nn.ReLU6(inplace=True), # dw nn.Conv2d(hidden_dim, hidden_dim, 3, stride, 1, groups=hidden_dim, bias=False), get_norm(bn, hidden_dim), nn.ReLU6(inplace=True), # pw-linear nn.Conv2d(hidden_dim, oup, 1, 1, 0, bias=False), get_norm(bn, oup), )
def conv_bn_no_relu(in_channel, out_channel, stride, norm="BN"): return Conv2d( in_channel, out_channel, kernel_size=3, stride=stride, padding=1, bias=False, norm=get_norm(norm, out_channel), )
def __init__(self, in_channels, out_channels, module_name, postfix, dilation=1, groups=1, with_modulated_dcn=None, deformable_groups=1): super(DFConv3x3, self).__init__() self.module_names = [] self.with_modulated_dcn = with_modulated_dcn if self.with_modulated_dcn: deform_conv_op = ModulatedDeformConv # offset channels are 2 or 3 (if with modulated) * kernel_size * kernel_size offset_channels = 27 else: deform_conv_op = DeformConv offset_channels = 18 unit_name = f"{module_name}_{postfix}/conv_offset" self.module_names.append(unit_name) self.add_module( unit_name, Conv2d( in_channels, offset_channels * deformable_groups, kernel_size=3, stride=1, padding=1 * dilation, dilation=dilation, )) for m in self.modules(): if isinstance(m, nn.Conv2d): nn.init.constant_(m.weight, 0) nn.init.constant_(m.bias, 0) unit_name = f"{module_name}_{postfix}/conv" self.module_names.append(unit_name) self.add_module( f"{module_name}_{postfix}/conv", deform_conv_op( in_channels, out_channels, kernel_size=3, stride=1, padding=1 * dilation, bias=False, groups=groups, dilation=1, deformable_groups=deformable_groups, )) unit_name = f"{module_name}_{postfix}/norm" self.module_names.append(unit_name) self.add_module(unit_name, get_norm(_NORM, out_channels))
def __init__(self, cfg, input_shape: ShapeSpec): """ The following attributes are parsed from config: num_conv, num_fc: the number of conv/fc layers conv_dim/fc_dim: the dimension of the conv/fc layers norm: normalization for the conv layers """ super().__init__() # fmt: off num_classes = cfg.MODEL.ROI_HEADS.NUM_CLASSES num_conv = cfg.MODEL.ROI_MASK_HEAD.RECLS_NET.NUM_CONV conv_dim = cfg.MODEL.ROI_MASK_HEAD.RECLS_NET.CONV_DIM num_fc = cfg.MODEL.ROI_MASK_HEAD.RECLS_NET.NUM_FC fc_dim = cfg.MODEL.ROI_MASK_HEAD.RECLS_NET.FC_DIM norm = cfg.MODEL.ROI_MASK_HEAD.RECLS_NET.NORM self.rescoring = cfg.MODEL.ROI_MASK_HEAD.RECLS_NET.RESCORING self.attention_mode = cfg.MODEL.ROI_MASK_HEAD.ATTENTION_MODE # fmt: on assert num_conv + num_fc > 0 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) self.conv_norm_relus = [] for k in range(num_conv): conv = Conv2d( self._output_size[0], conv_dim, kernel_size=3, padding=1, bias=not norm, norm=get_norm(norm, conv_dim), activation=F.relu, ) self.add_module("conv{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self._output_size = (conv_dim, self._output_size[1], self._output_size[2]) self.fcs = [] for k in range(num_fc): fc = nn.Linear(np.prod(self._output_size), fc_dim) self.add_module("fc{}".format(k + 1), fc) self.fcs.append(fc) self._output_size = fc_dim cls = nn.Linear(fc_dim, num_classes) self.add_module("recls", cls) self._output_size = num_classes for layer in self.conv_norm_relus: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer) nn.init.normal_(self.recls.weight, std=0.01) nn.init.constant_(self.recls.bias, 0)
def __init__(self, in_channels=3, out_channels=64, norm="BN"): """ Args: norm (str or callable): norm after the first conv layer. See :func:`layers.get_norm` for supported format. """ super().__init__(in_channels, out_channels, 4) self.in_channels = in_channels self.conv1 = Conv2d( in_channels, out_channels, kernel_size=3, stride=2, padding=1, bias=False, norm=get_norm(norm, out_channels), ) weight_init.c2_msra_fill(self.conv1) self.conv2 = Conv2d( out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm=get_norm(norm, out_channels), ) weight_init.c2_msra_fill(self.conv2) self.conv3 = Conv2d( out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False, norm=get_norm(norm, out_channels), ) weight_init.c2_msra_fill(self.conv3) self.pool = nn.MaxPool2d(kernel_size=2, stride=2, padding=0)
def build_convs_stack(norm_type, channels, conv_func, init_type): stack_list = [] for i in range(len(channels) - 1): stack_list.extend([ get_conv_func(norm_type, conv_func, channels[i], channels[i + 1], init_type), get_norm(norm_type, channels[i + 1]), nn.ReLU(inplace=True), nn.Upsample(scale_factor=2, mode="nearest"), ]) return nn.Sequential(*stack_list)
def conv_dw(inp, oup, stride, width_mult=1.0): inp = 3 if inp == 3 else scale_chan(inp, width_mult) oup = scale_chan(oup, width_mult) conv1 = Conv2d(inp, inp, 3, stride, 1, groups=inp, bias=False, norm=get_norm("BN", inp)) conv2 = Conv2d(inp, oup, 1, 1, 0, bias=False, norm=get_norm("BN", oup)) weight_init.c2_msra_fill(conv1) weight_init.c2_msra_fill(conv2) return nn.Sequential( conv1, nn.ReLU(inplace=True), conv2, nn.ReLU(inplace=True), )
def __init__(self, inplanes, planes, stride=1, dilation=1, norm='BN'): super(BasicBlock, self).__init__() self.conv1 = nn.Conv2d(inplanes, planes, kernel_size=3, stride=stride, padding=dilation, bias=False, dilation=dilation) self.bn1 = get_norm(norm, planes) self.relu = nn.ReLU(inplace=True) self.conv2 = nn.Conv2d(planes, planes, kernel_size=3, stride=1, padding=dilation, bias=False, dilation=dilation) self.bn2 = get_norm(norm, planes) self.stride = stride
def convert_norm_to_detectron2_format_and_init_default(module, norm): module_output = module if isinstance(module, torch.nn.BatchNorm2d): module_output = get_norm(norm, out_channels=module.num_features) module_output.weight.data.fill_(1.0) module_output.bias.data.zero_() for name, child in module.named_children(): new_child = convert_norm_to_detectron2_format_and_init_default(child, norm) if new_child is not child: module_output.add_module(name, new_child) return module_output
def __init__(self, cfg, input_shape: ShapeSpec): """ The following attributes are parsed from config: num_conv, num_fc: the number of conv/fc layers conv_dim/fc_dim: the dimension of the conv/fc layers norm: normalization for the conv layers """ super().__init__() # fmt: off num_conv = cfg.MODEL.ROI_BOX_HEAD.NUM_CONV conv_dim = cfg.MODEL.ROI_BOX_HEAD.CONV_DIM num_fc = cfg.MODEL.ROI_BOX_HEAD.NUM_FC fc_dim = cfg.MODEL.ROI_BOX_HEAD.FC_DIM norm = cfg.MODEL.ROI_BOX_HEAD.NORM # fmt: on assert num_conv + num_fc > 0 self._output_size = ( input_shape.channels, input_shape.height, input_shape.width, ) self.conv_norm_relus = [] for k in range(num_conv): conv = Conv2d( self._output_size[0], conv_dim, kernel_size=3, padding=1, bias=not norm, norm=get_norm(norm, conv_dim), activation=F.relu, ) self.add_module("conv{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self._output_size = ( conv_dim, self._output_size[1], self._output_size[2], ) self.fcs = [] for k in range(num_fc): fc = nn.Linear(np.prod(self._output_size), fc_dim) self.add_module("fc{}".format(k + 1), fc) self.fcs.append(fc) self._output_size = fc_dim for layer in self.conv_norm_relus: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer)
def __init__(self, cfg, input_shape: ShapeSpec): """ The following attributes are parsed from config: num_fc: the number of fc layers fc_dim: the dimension of the fc layers """ super().__init__() # fmt: off num_conv = cfg.MODEL.ROI_EMBEDDING_HEAD.NUM_CONV conv_dim = cfg.MODEL.ROI_EMBEDDING_HEAD.CONV_DIM num_fc = cfg.MODEL.ROI_PLANE_HEAD.NUM_FC fc_dim = cfg.MODEL.ROI_PLANE_HEAD.FC_DIM param_dim = cfg.MODEL.ROI_PLANE_HEAD.PARAM_DIM norm = cfg.MODEL.ROI_PLANE_HEAD.NORM # fmt: on self._plane_normal_only = cfg.MODEL.ROI_PLANE_HEAD.NORMAL_ONLY self._output_size = ( input_shape.channels, input_shape.height, input_shape.width, ) self.conv_norm_relus = [] for k in range(num_conv): conv = Conv2d( self._output_size[0], conv_dim, kernel_size=3, padding=1, bias=not norm, norm=get_norm(norm, conv_dim), activation=F.relu, ) self.add_module("plane_conv{}".format(k + 1), conv) self.conv_norm_relus.append(conv) self._output_size = (conv_dim, self._output_size[1], self._output_size[2]) self.fcs = [] for k in range(num_fc): fc = nn.Linear(np.prod(self._output_size), fc_dim) self.add_module("plane_fc{}".format(k + 1), fc) self.fcs.append(fc) self._output_size = fc_dim self.param_pred = nn.Linear(fc_dim, param_dim) for layer in self.conv_norm_relus: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer) self._loss_weight = cfg.MODEL.ROI_PLANE_HEAD.LOSS_WEIGHT
def __init__(self, in_channels, out_channels, norm): super().__init__() self.conv = nn.Conv2d(in_channels, out_channels, kernel_size=1, stride=1, bias=norm == '') self.norm = get_norm(norm, out_channels) if norm != '' else nn.Sequential() weight_init.c2_xavier_fill(self.conv)
def __init__(self, num_features_list, cfg=None): super(SwitchableBatchNorm2d, self).__init__() self.num_features_list = num_features_list self.num_features = max(num_features_list) bns = [] norm = cfg.MODEL.SLRESNETS.NORM for i in num_features_list: bns.append(get_norm(norm, i)) self.bn = nn.ModuleList(bns) self.ignore_model_profiling = True self.width_mult_list = cfg.MODEL.SLRESNETS.WIDTH_MULT_LIST self.width_mult = cfg.MODEL.SLRESNETS.WIDTH_MULT
def __init__(self, in_channel, out_channel, norm="BN"): super(DeformConv, self).__init__() self.actf = nn.Sequential(get_norm(norm, out_channel), nn.ReLU(inplace=True)) self.conv = DCN( in_channel, out_channel, kernel_size=(3, 3), stride=1, padding=1, dilation=1, deformable_groups=1, )
def __init__(self, in_channels, out_channels): super().__init__() self.num_levels = 2 self.in_feature = "p5" norm = "GN" conv_fcn = [] conv_fcn.append(Conv2d(in_channels, in_channels, kernel_size=3, stride=2, padding=1, bias=not norm,\ groups=in_channels, norm=get_norm(norm, in_channels), activation=F.relu)) conv_fcn.append(Conv2d(in_channels, out_channels, kernel_size=1, bias=not norm,\ norm=get_norm(norm, out_channels), activation=F.relu)) self.add_module('p6', nn.Sequential(*conv_fcn)) conv_fcn = [] conv_fcn.append(Conv2d(out_channels, out_channels, kernel_size=3, stride=2, padding=1, bias=not norm,\ groups=out_channels, norm=get_norm(norm, out_channels), activation=F.relu)) conv_fcn.append(Conv2d(out_channels, out_channels, kernel_size=1, bias=not norm,\ norm=get_norm(norm, out_channels), activation=F.relu)) self.add_module('p7', nn.Sequential(*conv_fcn)) for layer in [*self.p6, *self.p7]: weight_init.c2_msra_fill(layer)