(self,
                 use_norm=True,
                 num_class=2,
                 layer_nums=(3, 5, 5),
                 layer_strides=(2, 2, 2),
                 num_filters=(128, 128, 256),
                 upsample_strides=(1, 2, 4),
                 num_upsample_filters=(256, 256, 256),
                 num_input_features=128,
                 num_anchor_per_loc=2,
                 encode_background_as_zeros=True,
                 use_direction_classifier=True,
                 use_groupnorm=False,
                 num_groups=32,
                 box_code_size=7,
                 num_direction_bins=2,
                 name='rpn')
upsample_strides support float: [0.25, 0.5, 1]
        if upsample_strides < 1, conv2d will be used instead of convtranspose2d.
upsample_strides support float: [0.25, 0.5, 1]
        if upsample_strides < 1, conv2d will be used instead of convtranspose2d.
upsample_strides support float : [ 0 . 25 0 . 5 1 ] if upsample_strides < 1 conv2d will be used instead of convtranspose2d .
def __init__(self,
                 use_norm=True,
                 num_class=2,
                 layer_nums=(3, 5, 5),
                 layer_strides=(2, 2, 2),
                 num_filters=(128, 128, 256),
                 upsample_strides=(1, 2, 4),
                 num_upsample_filters=(256, 256, 256),
                 num_input_features=128,
                 num_anchor_per_loc=2,
                 encode_background_as_zeros=True,
                 use_direction_classifier=True,
                 use_groupnorm=False,
                 num_groups=32,
                 box_code_size=7,
                 num_direction_bins=2,
                 name='rpn'):
        """upsample_strides support float: [0.25, 0.5, 1]
        if upsample_strides < 1, conv2d will be used instead of convtranspose2d.
        """
        super(RPNNoHeadBase, self).__init__()
        self._layer_strides = layer_strides
        self._num_filters = num_filters
        self._layer_nums = layer_nums
        self._upsample_strides = upsample_strides
        self._num_upsample_filters = num_upsample_filters
        self._num_input_features = num_input_features
        self._use_norm = use_norm
        self._use_groupnorm = use_groupnorm
        self._num_groups = num_groups
        assert len(layer_strides) == len(layer_nums)
        assert len(num_filters) == len(layer_nums)
        assert len(num_upsample_filters) == len(upsample_strides)
        self._upsample_start_idx = len(layer_nums) - len(upsample_strides)
        must_equal_list = []
        for i in range(len(upsample_strides)):
            must_equal_list.append(upsample_strides[i] / np.prod(
                layer_strides[:i + self._upsample_start_idx + 1]))
        for val in must_equal_list:
            assert val == must_equal_list[0]

        if use_norm:
            if use_groupnorm:
                BatchNorm2d = change_default_args(
                    num_groups=num_groups, eps=1e-3)(GroupNorm)
            else:
                BatchNorm2d = change_default_args(
                    eps=1e-3, momentum=0.01)(nn.BatchNorm2d)
            Conv2d = change_default_args(bias=False)(nn.Conv2d)
            ConvTranspose2d = change_default_args(bias=False)(
                nn.ConvTranspose2d)
        else:
            BatchNorm2d = Empty
            Conv2d = change_default_args(bias=True)(nn.Conv2d)
            ConvTranspose2d = change_default_args(bias=True)(
                nn.ConvTranspose2d)

        in_filters = [num_input_features, *num_filters[:-1]]
        blocks = []
        deblocks = []

        for i, layer_num in enumerate(layer_nums):
            block, num_out_filters = self._make_layer(
                in_filters[i],
                num_filters[i],
                layer_num,
                stride=layer_strides[i])
            blocks.append(block)
            if i - self._upsample_start_idx >= 0:
                stride = upsample_strides[i - self._upsample_start_idx]
                if stride >= 1:
                    stride = np.round(stride).astype(np.int64)
                    deblock = nn.Sequential(
                        ConvTranspose2d(
                            num_out_filters,
                            num_upsample_filters[i - self._upsample_start_idx],
                            stride,
                            stride=stride),
                        BatchNorm2d(
                            num_upsample_filters[i -
                                                 self._upsample_start_idx]),
                        nn.ReLU(),
                    )
                else:
                    stride = np.round(1 / stride).astype(np.int64)
                    deblock = nn.Sequential(
                        Conv2d(
                            num_out_filters,
                            num_upsample_filters[i - self._upsample_start_idx],
                            stride,
                            stride=stride),
                        BatchNorm2d(
                            num_upsample_filters[i -
                                                 self._upsample_start_idx]),
                        nn.ReLU(),
                    )
                deblocks.append(deblock)
        self._num_out_filters = num_out_filters
        self.blocks = nn.ModuleList(blocks)
        self.deblocks = nn.ModuleList(deblocks)
def __init__ ( self , use_norm = True , num_class = 2 , layer_nums = ( 3 , 5 , 5 ) , layer_strides = ( 2 , 2 , 2 ) , num_filters = ( 128 , 128 , 256 ) , upsample_strides = ( 1 , 2 , 4 ) , num_upsample_filters = ( 256 , 256 , 256 ) , num_input_features = 128 , num_anchor_per_loc = 2 , encode_background_as_zeros = True , use_direction_classifier = True , use_groupnorm = False , num_groups = 32 , box_code_size = 7 , num_direction_bins = 2 , name = 'rpn' ) : super ( RPNNoHeadBase , self ) . __init__ ( ) self . _layer_strides = layer_strides self . _num_filters = num_filters self . _layer_nums = layer_nums self . _upsample_strides = upsample_strides self . _num_upsample_filters = num_upsample_filters self . _num_input_features = num_input_features self . _use_norm = use_norm self . _use_groupnorm = use_groupnorm self . _num_groups = num_groups assert len ( layer_strides ) == len ( layer_nums ) assert len ( num_filters ) == len ( layer_nums ) assert len ( num_upsample_filters ) == len ( upsample_strides ) self . _upsample_start_idx = len ( layer_nums ) - len ( upsample_strides ) must_equal_list = [ ] for i in range ( len ( upsample_strides ) ) : must_equal_list . append ( upsample_strides [ i ] / np . prod ( layer_strides [ : i + self . _upsample_start_idx + 1 ] ) ) for val in must_equal_list : assert val == must_equal_list [ 0 ] if use_norm : if use_groupnorm : BatchNorm2d = change_default_args ( num_groups = num_groups , eps = 1e-3 ) ( GroupNorm ) else : BatchNorm2d = change_default_args ( eps = 1e-3 , momentum = 0.01 ) ( nn . BatchNorm2d ) Conv2d = change_default_args ( bias = False ) ( nn . Conv2d ) ConvTranspose2d = change_default_args ( bias = False ) ( nn . ConvTranspose2d ) else : BatchNorm2d = Empty Conv2d = change_default_args ( bias = True ) ( nn . Conv2d ) ConvTranspose2d = change_default_args ( bias = True ) ( nn . ConvTranspose2d ) in_filters = [ num_input_features , * num_filters [ : - 1 ] ] blocks = [ ] deblocks = [ ] for i , layer_num in enumerate ( layer_nums ) : block , num_out_filters = self . _make_layer ( in_filters [ i ] , num_filters [ i ] , layer_num , stride = layer_strides [ i ] ) blocks . append ( block ) if i - self . _upsample_start_idx >= 0 : stride = upsample_strides [ i - self . _upsample_start_idx ] if stride >= 1 : stride = np . round ( stride ) . astype ( np . int64 ) deblock = nn . Sequential ( ConvTranspose2d ( num_out_filters , num_upsample_filters [ i - self . _upsample_start_idx ] , stride , stride = stride ) , BatchNorm2d ( num_upsample_filters [ i - self . _upsample_start_idx ] ) , nn . ReLU ( ) , ) else : stride = np . round ( 1 / stride ) . astype ( np . int64 ) deblock = nn . Sequential ( Conv2d ( num_out_filters , num_upsample_filters [ i - self . _upsample_start_idx ] , stride , stride = stride ) , BatchNorm2d ( num_upsample_filters [ i - self . _upsample_start_idx ] ) , nn . ReLU ( ) , ) deblocks . append ( deblock ) self . _num_out_filters = num_out_filters self . blocks = nn . ModuleList ( blocks ) self . deblocks = nn . ModuleList ( deblocks )