示例#1
0
 def __init__(self, dim_in, stage):
     super(Grid_output, self).__init__()
     self.stage = stage
     self.dim_in = dim_in[-1]
     self.grid_points = cfg.GRID_RCNN.GRID_POINTS if not cfg.GRID_RCNN.CASCADE_MAPPING_ON else \
     cfg.GRID_RCNN.CASCADE_MAPPING_OPTION.GRID_NUM[stage]
     self.point_feat_channels = cfg.GRID_RCNN.GRID_HEAD.POINT_FEAT_CHANNELS
     self.conv_out_channels = self.point_feat_channels * self.grid_points
     deconv_kernel_size = 4
     self.norm1 = nn.GroupNorm(self.grid_points, self.conv_out_channels)
     self.deconv_1 = nn.ConvTranspose2d(self.conv_out_channels,
                                        self.conv_out_channels,
                                        kernel_size=deconv_kernel_size,
                                        stride=2,
                                        padding=(deconv_kernel_size - 2) //
                                        2,
                                        groups=self.grid_points)
     self.deconv_2 = nn.ConvTranspose2d(self.conv_out_channels,
                                        self.grid_points,
                                        kernel_size=deconv_kernel_size,
                                        stride=2,
                                        padding=(deconv_kernel_size - 2) //
                                        2,
                                        groups=self.grid_points)
     if cfg.GRID_RCNN.IOU_HELPER and self.stage == cfg.GRID_RCNN.CASCADE_MAPPING_OPTION.STAGE_NUM - 1:
         resolution = cfg.GRID_RCNN.ROI_XFORM_RESOLUTION_CLS
         input_size = self.conv_out_channels * resolution[0] * resolution[1]
         self.iou_fc1 = make_fc(input_size, 1024)
         self.iou_fc2 = make_fc(1024, 1024)
         self.iou_pred = nn.Linear(1024, 2)
         init.normal_(self.iou_pred.weight, std=0.01)
         init.constant_(self.iou_pred.bias, 0)
     if cfg.GRID_RCNN.SE_ON:
         self.se_helper = SeConv2d(self.conv_out_channels,
                                   int(self.conv_out_channels * 0.0625))
示例#2
0
    def __init__(self, dim_in, spatial_scale):
        super().__init__()
        self.dim_in = dim_in[-1]

        method = cfg.GRID_RCNN.ROI_XFORM_METHOD
        resolution = cfg.GRID_RCNN.ROI_XFORM_RESOLUTION_CLS
        sampling_ratio = cfg.GRID_RCNN.ROI_XFORM_SAMPLING_RATIO
        pooler = Pooler(
            method=method,
            output_size=resolution,
            scales=spatial_scale,
            sampling_ratio=sampling_ratio,
        )
        input_size = self.dim_in * resolution[0] * resolution[1]
        mlp_dim = cfg.GRID_RCNN.MLP_HEAD.MLP_DIM
        use_bn = cfg.GRID_RCNN.MLP_HEAD.USE_BN
        use_gn = cfg.GRID_RCNN.MLP_HEAD.USE_GN
        self.pooler = pooler
        self.fc6 = make_fc(input_size, mlp_dim, use_bn, use_gn)
        self.fc7 = make_fc(mlp_dim, mlp_dim, use_bn, use_gn)
        self.dim_out = mlp_dim

        if cfg.GRID_RCNN.MLP_HEAD.USE_WS:
            self = convert_conv2convws_model(self)
示例#3
0
    def __init__(self, dim_in, spatial_scale, stage=1):
        super().__init__()
        self.dim_in = dim_in[-1]

        method = cfg.FAST_RCNN.ROI_XFORM_METHOD
        resolution = cfg.FAST_RCNN.ROI_XFORM_RESOLUTION
        sampling_ratio = cfg.FAST_RCNN.ROI_XFORM_SAMPLING_RATIO
        pooler = Pooler(
            method=method,
            output_size=resolution,
            scales=spatial_scale,
            sampling_ratio=sampling_ratio,
        )
        self.pooler = pooler

        use_lite = cfg.FAST_RCNN.CONVFC_HEAD.USE_LITE
        use_bn = cfg.FAST_RCNN.CONVFC_HEAD.USE_BN
        use_gn = cfg.FAST_RCNN.CONVFC_HEAD.USE_GN
        conv_dim = cfg.FAST_RCNN.CONVFC_HEAD.CONV_DIM
        num_stacked_convs = cfg.FAST_RCNN.CONVFC_HEAD.NUM_STACKED_CONVS
        dilation = cfg.FAST_RCNN.CONVFC_HEAD.DILATION

        xconvs = []
        for ix in range(num_stacked_convs):
            xconvs.append(
                make_conv(self.dim_in,
                          conv_dim,
                          kernel=3,
                          stride=1,
                          dilation=dilation,
                          use_dwconv=use_lite,
                          use_bn=use_bn,
                          use_gn=use_gn,
                          suffix_1x1=use_lite,
                          use_relu=True))
            self.dim_in = conv_dim
        self.add_module("xconvs", nn.Sequential(*xconvs))

        input_size = self.dim_in * resolution[0] * resolution[1]
        mlp_dim = cfg.FAST_RCNN.CONVFC_HEAD.MLP_DIM
        self.fc6 = make_fc(input_size, mlp_dim, use_bn=False, use_gn=False)
        self.dim_out = mlp_dim
        self.stage = stage

        if cfg.FAST_RCNN.CONVFC_HEAD.USE_WS:
            self = convert_conv2convws_model(self)
示例#4
0
    def __init__(self, dim_in, spatial_scale, stage):
        super(roi_grid_head, self).__init__()
        self.grid_points = cfg.GRID_RCNN.GRID_POINTS if not cfg.GRID_RCNN.CASCADE_MAPPING_ON else \
            cfg.GRID_RCNN.CASCADE_MAPPING_OPTION.GRID_NUM[stage]
        self.roi_feat_size = cfg.GRID_RCNN.ROI_FEAT_SIZE

        self.num_convs = cfg.GRID_RCNN.GRID_HEAD.NUM_CONVS
        self.point_feat_channels = cfg.GRID_RCNN.GRID_HEAD.POINT_FEAT_CHANNELS

        self.conv_out_channels = self.point_feat_channels * self.grid_points
        self.class_agnostic = False
        self.dim_in = dim_in[-1]

        assert self.grid_points >= 4
        self.grid_size = int(np.sqrt(self.grid_points))
        if self.grid_size * self.grid_size != self.grid_points:
            raise ValueError('grid_points must be a square number')

        # the predicted heatmap is half of whole_map_size
        if not isinstance(self.roi_feat_size, int):
            raise ValueError('Only square RoIs are supporeted in Grid R-CNN')
        self.whole_map_size = self.roi_feat_size * 4

        self.convs = []
        conv_kernel_size = 3
        for i in range(self.num_convs):
            in_channels = (self.dim_in if i == 0 else self.conv_out_channels)
            stride = 2 if i == 0 else 1
            padding = (conv_kernel_size - 1) // 2
            self.convs.append(
                nn.Sequential(
                    nn.Conv2d(in_channels,
                              self.conv_out_channels,
                              kernel_size=conv_kernel_size,
                              stride=stride,
                              padding=padding),
                    nn.GroupNorm(4 * self.grid_points,
                                 self.conv_out_channels,
                                 eps=1e-5), nn.ReLU(inplace=True)))
        self.convs = nn.Sequential(*self.convs)

        # find the 4-neighbor of each grid point
        self.neighbor_points = self._get_neighbors()
        # total edges in the grid
        self.num_edges = sum([len(p) for p in self.neighbor_points])

        if cfg.GRID_RCNN.FUSED_ON:
            self.forder_trans = self._build_trans(
                nn.ModuleList())  # first-order feature transition
            self.sorder_trans = self._build_trans(
                nn.ModuleList())  # second-order feature transition

        method = cfg.GRID_RCNN.ROI_XFORM_METHOD
        resolution = cfg.GRID_RCNN.ROI_XFORM_RESOLUTION_GRID
        sampling_ratio = cfg.GRID_RCNN.ROI_XFORM_SAMPLING_RATIO
        spatial_scale = [spatial_scale[0]
                         ] if cfg.GRID_RCNN.FINEST_LEVEL_ROI else spatial_scale
        pooler = Pooler(
            method=method,
            output_size=resolution,
            scales=spatial_scale,
            sampling_ratio=sampling_ratio,
        )
        self.pooler = pooler
        self.dim_out = dim_in

        if cfg.GRID_RCNN.OFFSET_ON:
            self.offset_conv = make_conv(self.dim_in, 64, kernel=3, stride=2)
            self.offset_fc = make_fc(64 * 7 * 7, 4 * self.grid_points)