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_fc = cfg.MODEL.HOI_BOX_HEAD.NUM_FC fc_dim = cfg.MODEL.HOI_BOX_HEAD.FC_DIM # fmt: on assert num_fc > 0 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) self.fcs = [] for k in range(num_fc): 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.fcs: weight_init.c2_xavier_fill(layer)
def __init__(self, cfg, input_shape: ShapeSpec): """ The following attributes are parsed from config: conv_dim: the output dimension of the conv layers fc_dim: the feature dimenstion of the FC layers num_fc: the number of FC layers output_side_resolution: side resolution of the output square mask prediction """ super(CoarseMaskHead, self).__init__() # fmt: off self.num_classes = cfg.MODEL.ROI_HEADS.NUM_CLASSES conv_dim = cfg.MODEL.ROI_MASK_HEAD.CONV_DIM self.fc_dim = cfg.MODEL.ROI_MASK_HEAD.FC_DIM num_fc = cfg.MODEL.ROI_MASK_HEAD.NUM_FC self.output_side_resolution = cfg.MODEL.ROI_MASK_HEAD.OUTPUT_SIDE_RESOLUTION self.input_channels = input_shape.channels self.input_h = input_shape.height self.input_w = input_shape.width # fmt: on self.conv_layers = [] if self.input_channels > conv_dim: self.reduce_channel_dim_conv = Conv2d( self.input_channels, conv_dim, kernel_size=1, stride=1, padding=0, bias=True, activation=F.relu, ) self.conv_layers.append(self.reduce_channel_dim_conv) self.reduce_spatial_dim_conv = Conv2d( conv_dim, conv_dim, kernel_size=2, stride=2, padding=0, bias=True, activation=F.relu ) self.conv_layers.append(self.reduce_spatial_dim_conv) input_dim = conv_dim * self.input_h * self.input_w input_dim //= 4 self.fcs = [] for k in range(num_fc): fc = nn.Linear(input_dim, self.fc_dim) self.add_module("coarse_mask_fc{}".format(k + 1), fc) self.fcs.append(fc) input_dim = self.fc_dim output_dim = self.num_classes * self.output_side_resolution * self.output_side_resolution self.prediction = nn.Linear(self.fc_dim, output_dim) # use normal distribution initialization for mask prediction layer nn.init.normal_(self.prediction.weight, std=0.001) nn.init.constant_(self.prediction.bias, 0) for layer in self.conv_layers: 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_fc = cfg.MODEL.ROI_Z_HEAD.NUM_FC fc_dim = cfg.MODEL.ROI_Z_HEAD.FC_DIM cls_agnostic = cfg.MODEL.ROI_Z_HEAD.CLS_AGNOSTIC_Z_REG num_classes = cfg.MODEL.ROI_HEADS.NUM_CLASSES # fmt: on self._output_size = (input_shape.channels, input_shape.height, input_shape.width) self.fcs = [] for k in range(num_fc): fc = nn.Linear(np.prod(self._output_size), fc_dim) self.add_module("z_fc{}".format(k + 1), fc) self.fcs.append(fc) self._output_size = fc_dim num_z_reg_classes = 1 if cls_agnostic else num_classes self.z_pred = nn.Linear(fc_dim, num_z_reg_classes) for layer in self.fcs: weight_init.c2_xavier_fill(layer) nn.init.normal_(self.z_pred.weight, std=0.001) nn.init.constant_(self.z_pred.bias, 0)
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 fc_dim = cfg.MODEL.ROI_BOX_HEAD.FC_DIM sub_fc_dim = cfg.MODEL.ROI_BOX_HEAD.SUB_FC_DIM norm = cfg.MODEL.ROI_BOX_HEAD.NORM # fmt: on box_feat_shape = (input_shape.channels, input_shape.height, input_shape.width) self.fc_main = nn.Linear(np.prod(box_feat_shape), fc_dim) self.fc_reg = nn.Linear(fc_dim, sub_fc_dim) self.fc_cls = nn.Linear(fc_dim, sub_fc_dim) self._output_size = sub_fc_dim for layer in [self.fc_main, self.fc_reg, self.fc_cls]: weight_init.c2_xavier_fill(layer)
def __init__(self, in_channels, out_channels, kernel_size, norm): super().__init__() self.conv = Conv2d(in_channels, out_channels, kernel_size=1, stride=1, padding_mode="static_same") self.norm = get_norm(norm, out_channels) if norm != '' else lambda x: x self.resample = MaxPool2d(kernel_size=3, stride=2, padding_mode="static_same") weight_init.c2_xavier_fill(self.conv)
def __init__(self, x_channels, x_stride, y_channels, y_stride, norm=""): super(LateralFuser, self).__init__() self.x_stride = x_stride self.y_stride = y_stride self.interpolate = self.x_stride == self.y_stride self.lateral = not x_channels == y_channels use_bias = norm == "" f_norm = get_norm(norm, x_channels) if self.lateral: lateral_norm = get_norm(norm, x_channels) self.lateral_conv = Conv2d(y_channels, x_channels, kernel_size=1, bias=use_bias, norm=lateral_norm) weight_init.c2_xavier_fill(self.lateral_conv) self.fuse_out = Conv2d(x_channels, x_channels, kernel_size=3, padding=1, bias=use_bias, norm=f_norm) weight_init.c2_xavier_fill(self.fuse_out)
def __init__(self, in_channels, out_channels, in_features="res5"): super().__init__() self.num_levels = 1 self.in_feature = in_features self.p6 = nn.Conv2d(in_channels, out_channels, 3, 2, 1) for module in [self.p6]: weight_init.c2_xavier_fill(module)
def __init__(self, input_shape: ShapeSpec, *, conv_dim: int, fc_dims: List[int], output_shape: Tuple[int]): """ Args: conv_dim: the output dimension of the conv layers fc_dims: a list of N>0 integers representing the output dimensions of N FC layers output_shape: shape of the output mask prediction """ super().__init__() # fmt: off input_channels = input_shape.channels input_h = input_shape.height input_w = input_shape.width self.output_shape = output_shape # fmt: on self.conv_layers = [] if input_channels > conv_dim: self.reduce_channel_dim_conv = Conv2d( input_channels, conv_dim, kernel_size=1, stride=1, padding=0, bias=True, activation=F.relu, ) self.conv_layers.append(self.reduce_channel_dim_conv) self.reduce_spatial_dim_conv = Conv2d(conv_dim, conv_dim, kernel_size=2, stride=2, padding=0, bias=True, activation=F.relu) self.conv_layers.append(self.reduce_spatial_dim_conv) input_dim = conv_dim * input_h * input_w input_dim //= 4 self.fcs = [] for k, fc_dim in enumerate(fc_dims): fc = nn.Linear(input_dim, fc_dim) self.add_module("fc{}".format(k + 1), fc) self.fcs.append(fc) input_dim = fc_dim output_dim = int(np.prod(self.output_shape)) self.prediction = nn.Linear(fc_dims[-1], output_dim) # use normal distribution initialization for mask prediction layer nn.init.normal_(self.prediction.weight, std=0.001) nn.init.constant_(self.prediction.bias, 0) for layer in self.conv_layers: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer)
def __init__(self, in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=True, norm=""): super().__init__() dilation = 2 self.conv1 = Conv2d( in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=bias, norm=get_norm(norm, out_channels), ) self.conv2 = Conv2d( in_channels, out_channels, kernel_size=3, stride=1, padding=1 * dilation, bias=bias, groups=1, dilation=dilation, norm=get_norm(norm, out_channels), ) for conv in [self.conv1, self.conv2]: weight_init.c2_xavier_fill(conv)
def test_linear_weight_init(self): # Test weight initialization for linear layer. channel_in_dims = [128, 256, 512, 1024] channel_out_dims = [256, 512, 1024, 2048] for layer in [nn.Linear]: for c_in_dim, c_out_dim in itertools.product( channel_in_dims, channel_out_dims): p = {"in_features": c_in_dim, "out_features": c_out_dim} model = layer(**p) # Calculate fan_in and fan_out. fan_in = c_in_dim fan_out = c_out_dim # Msra weight init check. c2_msra_fill(model) self.assertTrue( TestWeightInit.weight_and_bias_dist_match( model.weight, model.bias, TestWeightInit.msra_fill_std(fan_out), )) # Xavier weight init check. c2_xavier_fill(model) self.assertTrue( TestWeightInit.weight_and_bias_dist_match( model.weight, model.bias, TestWeightInit.xavier_fill_std(fan_in), ))
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_cls = [] self.fcs_reg = [] for k in range(num_fc): fc = nn.Linear(np.prod(self._output_size), fc_dim) self.add_module("fc_cls{}".format(k + 1), fc) self.fcs_cls.append(fc) self._output_size = fc_dim self._output_size = (input_shape.channels, input_shape.height, input_shape.width) for k in range(num_fc): fc = nn.Linear(np.prod(self._output_size), fc_dim) self.add_module("fc_reg{}".format(k + 1), fc) self.fcs_reg.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_cls: weight_init.c2_xavier_fill(layer) for layer in self.fcs_reg: weight_init.c2_xavier_fill(layer)
def __init__(self, in_channels, out_channels): super().__init__() self.num_levels = 2 self.in_feature = "p5" self.p6 = nn.Conv2d(in_channels, out_channels, 3, 2, 1) self.p7 = nn.Conv2d(out_channels, out_channels, 3, 2, 1) for module in [self.p6, self.p7]: weight_init.c2_xavier_fill(module)
def __init__(self, in_channels, mask_classification, num_classes, hidden_dim, num_queries, nheads, dropout, dim_feedforward, enc_layers, dec_layers, pre_norm, deep_supervision, mask_dim, enforce_input_project, norm_cfg=None, act_cfg=None): super(Predictor, self).__init__() self.num_queries = num_queries self.in_channels = in_channels self.aux_loss = deep_supervision self.mask_classification = mask_classification # positional encoding self.pe_layer = PositionEmbeddingSine(hidden_dim // 2, apply_normalize=True) # transformer self.transformer = Transformer( d_model=hidden_dim, nhead=nheads, num_encoder_layers=enc_layers, num_decoder_layers=dec_layers, dim_feedforward=dim_feedforward, dropout=dropout, norm_before=pre_norm, return_intermediate_dec=deep_supervision, act_cfg=act_cfg, norm_cfg=norm_cfg, ) hidden_dim = self.transformer.d_model # query embed self.query_embed = nn.Embedding(num_queries, hidden_dim) # input project if in_channels != hidden_dim or enforce_input_project: import fvcore.nn.weight_init as weight_init self.input_proj = nn.Conv2d(in_channels, hidden_dim, kernel_size=1, stride=1, padding=0) weight_init.c2_xavier_fill(self.input_proj) else: self.input_proj = nn.Sequential() # output FFNs if self.mask_classification: self.class_embed = nn.Linear(hidden_dim, num_classes + 1) # mask embed self.mask_embed = MLP(hidden_dim, hidden_dim, mask_dim, 3)
def __init__( self, cfg, input_shape, ): super(GraphConnection, self).__init__() self.cfg = cfg.clone() self.graph_channel = cfg.GRAPH.CHANNEL self.ignore_value = cfg.MODEL.SEM_SEG_HEAD.IGNORE_VALUE self.heads = cfg.GRAPH.HEADS self.stuff_out_channel = cfg.GRAPH.STUFF_OUT_CHANNEL self.loss_weight_stuff = cfg.MODEL.SEM_SEG_HEAD.LOSS_WEIGHT self.num_classes = cfg.MODEL.ROI_HEADS.NUM_CLASSES self.region_in_proj = nn.Linear(cfg.MODEL.ROI_BOX_HEAD.FC_DIM, self.graph_channel) self.stuff_in_proj = nn.Linear(cfg.MODEL.SEM_SEG_HEAD.CONVS_DIM * 4, self.graph_channel) weight_init.c2_xavier_fill(self.region_in_proj) weight_init.c2_xavier_fill(self.stuff_in_proj) self.graph = GAT(nfeat=self.graph_channel, nhid=self.graph_channel // self.heads, nclass=self.graph_channel, dropout=0.1, alpha=0.4, nheads=self.heads) '''New box head''' self.region_out_proj = nn.Linear(self.graph_channel, self.graph_channel) weight_init.c2_xavier_fill(self.region_out_proj) # in_features = cfg.MODEL.ROI_HEADS.IN_FEATURES # pooler_resolution = cfg.MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION # box_head = build_box_head( # cfg, ShapeSpec(channels=256, height=pooler_resolution, width=pooler_resolution) # ) # TODO: hard code in the channels # print(box_head.output_shape) box_output_shape = ShapeSpec(channels=cfg.MODEL.ROI_BOX_HEAD.FC_DIM + self.graph_channel) self.new_box_predictor = FastRCNNOutputLayers(cfg, box_output_shape) '''New mask head''' ret_dict = self._init_mask_head(cfg, input_shape) self.mask_in_features = ret_dict["mask_in_features"] self.new_mask_pooler = ret_dict["mask_pooler"] self.new_mask_head = ret_dict["mask_head"] # weight_init.c2_xavier_fill(self.new_mask_head) '''New segment head''' self.stuff_out_proj = nn.Linear(self.graph_channel, self.stuff_out_channel) self.seg_score = nn.Conv2d( cfg.MODEL.SEM_SEG_HEAD.CONVS_DIM * 4 + self.stuff_out_channel, cfg.MODEL.SEM_SEG_HEAD.NUM_CLASSES, 1) self.upsample_rate = 4 weight_init.c2_xavier_fill(self.stuff_out_proj) weight_init.c2_xavier_fill(self.seg_score)
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, dim_in, feat_dim): super().__init__() self.head = nn.Sequential( nn.Linear(dim_in, dim_in), nn.ReLU(inplace=True), nn.Linear(dim_in, feat_dim), ) for layer in self.head: if isinstance(layer, nn.Linear): weight_init.c2_xavier_fill(layer)
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.convs = [] for k in range(num_conv): if k == 0: # import pdb; pdb.set_trace() conv = BasicBlock(input_shape.channels, conv_dim, norm=norm) # for name, param in conv.named_parameters(): # print(name, param.requires_grad) # bottleneck_channels = conv_dim // 4 # conv = BottleneckBlock(input_shape.channels, conv_dim, # bottleneck_channels=bottleneck_channels, norm=norm) # import pdb; pdb.set_trace() # for name, param in conv.named_parameters(): # print(name, param) else: bottleneck_channels = conv_dim // 4 conv = BottleneckBlock(conv_dim, conv_dim, bottleneck_channels=bottleneck_channels, norm=norm) self.add_module("conv{}".format(k + 1), conv) self.convs.append(conv) # this is a @property, see line 153, will be used as input_size for box_predictor # here when this function return, self._output_size = fc_dim (=1024) self._output_size = input_shape.channels * input_shape.height * input_shape.width self.fcs = [] for k in range(num_fc): fc = nn.Linear(self._output_size, fc_dim) self.add_module("fc{}".format(k + 1), fc) self.fcs.append(fc) self._output_size = fc_dim # init has already been done in BasicBlock and BottleneckBlock # 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, in_channels, out_channels, kernel_size=3, stride=1, padding=1, deform_modulated=True, bias=True, norm="", ): super().__init__() dilation = 2 self.deform_modulated = deform_modulated self.conv1 = Conv2d( in_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=bias, norm=get_norm(norm, out_channels), ) if deform_modulated: deform_conv_op = ModulatedDeformConv offset_channels = 27 else: deform_conv_op = DeformConv offset_channels = 18 self.conv2_offset = Conv2d( in_channels, offset_channels * 1, kernel_size=3, stride=1, padding=1 * dilation, dilation=dilation, ) self.conv2 = deform_conv_op( in_channels, out_channels, kernel_size=3, stride=1, padding=1 * dilation, bias=False, groups=1, dilation=dilation, deformable_groups=1, norm=get_norm(norm, out_channels), ) nn.init.constant_(self.conv2_offset.weight, 0) nn.init.constant_(self.conv2_offset.bias, 0) for conv in [self.conv1, self.conv2]: weight_init.c2_xavier_fill(conv)
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, n_inputs: int, n_outputs: int, norm: Optional[Union[str, torch.nn.Module]] = "", activation: Optional[Union[str, torch.nn.Module]] = "relu", shortcut: bool = False, ): super(BlockLayerBase, self).__init__() if shortcut == True and n_inputs != n_outputs: raise ValueError(f"can't use shortcut when {n_inputs=} != {n_outputs=}.") self.shortcut: bool = shortcut norm_module: torch.nn.Module if isinstance(norm, torch.nn.Module): norm_module = norm elif isinstance(norm, str): norm_module = {"": torch.nn.Identity, "bn": torch.nn.BatchNorm1d,}[norm]( n_inputs ) elif norm is None: norm_module = torch.nn.Identity() else: raise ValueError(f"unknown value for norm {norm}.") act_module: torch.nn.Module if isinstance(activation, torch.nn.Module): act_module = activation elif isinstance(activation, str): act_module = { "": torch.nn.Identity, "identity": torch.nn.Identity, "none": torch.nn.Identity, "relu": torch.nn.ReLU, "sigmoid": torch.nn.Sigmoid, "tanh": torch.nn.Tanh, }[activation]() elif activation is None: act_module = torch.nn.Identity() else: raise ValueError(f"unknown value for activation {activation}.") assert n_inputs > 0 and n_outputs > 0 linear_module: torch.nn.Module = torch.nn.Linear( n_inputs, n_outputs, bias=isinstance(norm_module, torch.nn.Identity) ) if isinstance(act_module, (torch.nn.ReLU, torch.nn.LeakyReLU)): weight_init.c2_msra_fill(linear_module) else: weight_init.c2_xavier_fill(linear_module) self.linear = linear_module self.norm = norm_module self.activation = act_module
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, 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, in_feature="res5"): super().__init__() self.num_levels = 2 self.in_feature = in_feature self.p6 = nn.Conv2d(in_channels, out_channels, 3, 2, 1) self.p7 = nn.Conv2d(out_channels, out_channels, 3, 2, 1) for module in [self.p6, self.p7]: weight_init.c2_xavier_fill(module) self._out_features = ['p6', 'p7'] self._out_feature_channels = [out_channels, out_channels] self._out_feature_strides = [64, 128]
def __init__(self, in_channel=512, output_channel=2, reduction=16): super(SELayer, self).__init__() self.avg_pool = nn.AdaptiveAvgPool2d(1) self.fc = nn.Sequential( nn.Linear(in_channel, in_channel // reduction, bias=False), nn.ReLU(inplace=True), nn.Linear(in_channel // reduction, output_channel, bias=False), nn.Softmax()) for module in self.fc: if isinstance(module, nn.Linear): weight_init.c2_xavier_fill(module)
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__() num_conv = cfg.MODEL.ROI_FIBERLENGTH_HEAD.NUM_CONV conv_dim = cfg.MODEL.ROI_FIBERLENGTH_HEAD.CONV_DIM num_fc = cfg.MODEL.ROI_FIBERLENGTH_HEAD.NUM_FC fc_dim = cfg.MODEL.ROI_FIBERLENGTH_HEAD.FC_DIM norm = cfg.MODEL.ROI_FIBERLENGTH_HEAD.NORM 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 # Append final FC layer with a single neuron, to predict the fiberlength. fc = nn.Linear(fc_dim, 1) self.add_module("fc{}".format(num_fc + 1), fc) self.fcs.append(fc) self._output_size = 1 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_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 dim_expand_factor = cfg.MODEL.ROI_BOX_HEAD.DWEXPAND_FACTOR expand_channels = dim_expand_factor * conv_dim assert num_conv + num_fc > 0 self._output_size = (input_shape.channels, input_shape.height, input_shape.width) in_channels = input_shape.channels if num_conv > 0: conv = [] for k in range(num_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=3, padding=1, bias=not norm,\ groups=expand_channels, norm=get_norm(norm, expand_channels), activation=F.relu)) in_channels = expand_channels conv.append(Conv2d(expand_channels, conv_dim, kernel_size=1, bias=not norm,\ norm=get_norm(norm, conv_dim), activation=F.relu)) self.add_module('conv', nn.Sequential(*conv)) self._output_size = (conv_dim, self._output_size[1], self._output_size[2]) else: self.conv = None 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 if self.conv is not None: for layer in self.conv: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer)
def init_weights(self, ): # use normal distribution initialization for mask prediction layer nn.init.normal_(self.prediction.weight, std=0.001) nn.init.constant_(self.prediction.bias, 0) for layer in self.conv_layers: if isinstance(layer, nn.Sequential): assert len(layer) == 2 weight_init.c2_msra_fill(layer[0]) else: weight_init.c2_msra_fill(layer) for layer in self.fcs: weight_init.c2_xavier_fill(layer)
def __init__(self, in_channels, out_channels, in_feature="res5", activation="ReLU"): super().__init__() self.num_levels = 2 self.in_feature = in_feature self.p6 = nn.Conv2d(in_channels, out_channels, 3, 2, 1) self.p7 = nn.Conv2d(out_channels, out_channels, 3, 2, 1) for module in [self.p6, self.p7]: weight_init.c2_xavier_fill(module) self.activation = get_activation(activation)
def __init__( self, input_shape: ShapeSpec, num_conv: int, conv_dim: int, num_fc: int, fc_dim: int, conv_norm="", ): """ Args: input_shape (ShapeSpec): shape of the input feature. num_conv, num_fc: the number of conv/fc layers conv_dim/fc_dim: the output dimension of the conv/fc layers conv_norm (str or callable): normalization for the conv layers. See :func:`detectron2.layers.get_norm` for supported types. """ super().__init__() 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 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 in range(num_fc): 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)