(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(Two_RPNNoHeadBase_PSA, 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)

        ### Refine:
        self.bottle_conv = nn.Conv2d(sum(num_upsample_filters), sum(num_upsample_filters)//3, 1)

        self.block1_dec2x = nn.MaxPool2d(kernel_size=2)   ### C=64
        self.block1_dec4x = nn.MaxPool2d(kernel_size=4)   ### C=64

        self.block2_dec2x = nn.MaxPool2d(kernel_size=2)   ### C=128

        self.block2_inc2x = ConvTranspose2d(num_filters[1], num_filters[0]//2, 2, stride=2)
        self.block3_inc2x = ConvTranspose2d(num_filters[2], num_filters[1]//2, 2, stride=2)


        self.block3_inc4x = ConvTranspose2d(num_filters[2], num_filters[0]//2, 4,stride=4)  #### C=32

        if upsample_strides[0] < 1:
            stride0 = np.round(1.0 / upsample_strides[0]).astype(np.int64)
            self.refine_up1 = Sequential(
                Conv2d(num_filters[0], num_upsample_filters[0], stride0,stride=stride0),
                BatchNorm2d(num_upsample_filters[0]),
                nn.ReLU(),
            )
        else:
            stride0 = np.round(upsample_strides[0]).astype(np.int64)
            self.refine_up1 = Sequential(
                ConvTranspose2d(num_filters[0], num_upsample_filters[0], stride0,
                                stride=stride0),
                BatchNorm2d(num_upsample_filters[0]),
                nn.ReLU(),
            )

        if upsample_strides[1]<1:
            stride1 = np.round(1.0 / upsample_strides[1]).astype(np.int64)
            self.refine_up2 = Sequential(
                Conv2d(num_filters[1], num_upsample_filters[1], stride1,stride=stride1),
                BatchNorm2d(num_upsample_filters[1]),
                nn.ReLU(),
            )

        else:
            stride1 =np.round(upsample_strides[1]).astype(np.int64)
            self.refine_up2 = Sequential(
                ConvTranspose2d(num_filters[1], num_upsample_filters[1], stride1,stride=stride1),
                BatchNorm2d(num_upsample_filters[1]),
                nn.ReLU(),
            )

        if upsample_strides[2] < 1:
            stride2 = np.round(1.0 / upsample_strides[2]).astype(np.int64)
            self.refine_up3 = Sequential(
                ConvTranspose2d(num_filters[2], num_upsample_filters[2], stride2,
                                stride=stride2),
                BatchNorm2d(num_upsample_filters[2]),
                nn.ReLU(),
            )

        else:
            stride2 = np.round(upsample_strides[2]).astype(np.int64)
            self.refine_up3 = Sequential(
                ConvTranspose2d(num_filters[2], num_upsample_filters[2], stride2,
                                stride=stride2),
                BatchNorm2d(num_upsample_filters[2]),
                nn.ReLU(),
            )

        self.fusion_block1 = nn.Conv2d(num_filters[0]+num_filters[0]//2+num_filters[0]//2, num_filters[0], 1)
        self.fusion_block2 = nn.Conv2d(num_filters[0]+num_filters[1]+num_filters[1]//2, num_filters[1], 1)
        self.fusion_block3 = nn.Conv2d(num_filters[0]+num_filters[1]+num_filters[2], num_filters[2], 1)


        #######
        C_Bottle = cfg.PSA.C_Bottle
        C = cfg.PSA.C_Reudce

        self.RF1 = Sequential(  # 3*3
            Conv2d(C_Bottle*2, C, kernel_size=1, stride=1),
            BatchNorm2d(C),
            nn.ReLU(inplace=True),
            Conv2d(C, C_Bottle*2, kernel_size=3, stride=1, padding=1, dilation=1),
            BatchNorm2d(C_Bottle*2),
            nn.ReLU(inplace=True),
        )

        self.RF2 = Sequential(  # 5*5
            Conv2d(C_Bottle, C, kernel_size=3, stride=1, padding=1),
            BatchNorm2d(C),
            nn.ReLU(inplace=True),
            Conv2d(C, C_Bottle, kernel_size=3, stride=1, padding=1, dilation=1),
            BatchNorm2d(C_Bottle),
            nn.ReLU(inplace=True),
        )

        self.RF3 = Sequential(  # 7*7
            Conv2d(C_Bottle//2, C, kernel_size=3, stride=1, padding=1),
            BatchNorm2d(C),
            nn.ReLU(inplace=True),
            Conv2d(C, C, kernel_size=3, stride=1, padding=1),
            BatchNorm2d(C),
            nn.ReLU(inplace=True),
            Conv2d(C, C_Bottle//2, kernel_size=3, stride=1, padding=1),
            BatchNorm2d(C_Bottle//2),
            nn.ReLU(inplace=True),
        )

        self.concat_conv1 = nn.Conv2d(num_filters[1], num_filters[1], kernel_size=3, padding=1)  ## kernel_size=3
        self.concat_conv2 = nn.Conv2d(num_filters[1], num_filters[1], kernel_size=3, padding=1)
        self.concat_conv3 = nn.Conv2d(num_filters[1], num_filters[1], kernel_size=3, padding=1)
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 ( Two_RPNNoHeadBase_PSA , 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 ) ### Refine: self . bottle_conv = nn . Conv2d ( sum ( num_upsample_filters ) , sum ( num_upsample_filters ) // 3 , 1 ) self . block1_dec2x = nn . MaxPool2d ( kernel_size = 2 ) ### C=64 self . block1_dec4x = nn . MaxPool2d ( kernel_size = 4 ) ### C=64 self . block2_dec2x = nn . MaxPool2d ( kernel_size = 2 ) ### C=128 self . block2_inc2x = ConvTranspose2d ( num_filters [ 1 ] , num_filters [ 0 ] // 2 , 2 , stride = 2 ) self . block3_inc2x = ConvTranspose2d ( num_filters [ 2 ] , num_filters [ 1 ] // 2 , 2 , stride = 2 ) self . block3_inc4x = ConvTranspose2d ( num_filters [ 2 ] , num_filters [ 0 ] // 2 , 4 , stride = 4 ) #### C=32 if upsample_strides [ 0 ] < 1 : stride0 = np . round ( 1.0 / upsample_strides [ 0 ] ) . astype ( np . int64 ) self . refine_up1 = Sequential ( Conv2d ( num_filters [ 0 ] , num_upsample_filters [ 0 ] , stride0 , stride = stride0 ) , BatchNorm2d ( num_upsample_filters [ 0 ] ) , nn . ReLU ( ) , ) else : stride0 = np . round ( upsample_strides [ 0 ] ) . astype ( np . int64 ) self . refine_up1 = Sequential ( ConvTranspose2d ( num_filters [ 0 ] , num_upsample_filters [ 0 ] , stride0 , stride = stride0 ) , BatchNorm2d ( num_upsample_filters [ 0 ] ) , nn . ReLU ( ) , ) if upsample_strides [ 1 ] < 1 : stride1 = np . round ( 1.0 / upsample_strides [ 1 ] ) . astype ( np . int64 ) self . refine_up2 = Sequential ( Conv2d ( num_filters [ 1 ] , num_upsample_filters [ 1 ] , stride1 , stride = stride1 ) , BatchNorm2d ( num_upsample_filters [ 1 ] ) , nn . ReLU ( ) , ) else : stride1 = np . round ( upsample_strides [ 1 ] ) . astype ( np . int64 ) self . refine_up2 = Sequential ( ConvTranspose2d ( num_filters [ 1 ] , num_upsample_filters [ 1 ] , stride1 , stride = stride1 ) , BatchNorm2d ( num_upsample_filters [ 1 ] ) , nn . ReLU ( ) , ) if upsample_strides [ 2 ] < 1 : stride2 = np . round ( 1.0 / upsample_strides [ 2 ] ) . astype ( np . int64 ) self . refine_up3 = Sequential ( ConvTranspose2d ( num_filters [ 2 ] , num_upsample_filters [ 2 ] , stride2 , stride = stride2 ) , BatchNorm2d ( num_upsample_filters [ 2 ] ) , nn . ReLU ( ) , ) else : stride2 = np . round ( upsample_strides [ 2 ] ) . astype ( np . int64 ) self . refine_up3 = Sequential ( ConvTranspose2d ( num_filters [ 2 ] , num_upsample_filters [ 2 ] , stride2 , stride = stride2 ) , BatchNorm2d ( num_upsample_filters [ 2 ] ) , nn . ReLU ( ) , ) self . fusion_block1 = nn . Conv2d ( num_filters [ 0 ] + num_filters [ 0 ] // 2 + num_filters [ 0 ] // 2 , num_filters [ 0 ] , 1 ) self . fusion_block2 = nn . Conv2d ( num_filters [ 0 ] + num_filters [ 1 ] + num_filters [ 1 ] // 2 , num_filters [ 1 ] , 1 ) self . fusion_block3 = nn . Conv2d ( num_filters [ 0 ] + num_filters [ 1 ] + num_filters [ 2 ] , num_filters [ 2 ] , 1 ) ####### C_Bottle = cfg . PSA . C_Bottle C = cfg . PSA . C_Reudce self . RF1 = Sequential ( # 3*3 Conv2d ( C_Bottle * 2 , C , kernel_size = 1 , stride = 1 ) , BatchNorm2d ( C ) , nn . ReLU ( inplace = True ) , Conv2d ( C , C_Bottle * 2 , kernel_size = 3 , stride = 1 , padding = 1 , dilation = 1 ) , BatchNorm2d ( C_Bottle * 2 ) , nn . ReLU ( inplace = True ) , ) self . RF2 = Sequential ( # 5*5 Conv2d ( C_Bottle , C , kernel_size = 3 , stride = 1 , padding = 1 ) , BatchNorm2d ( C ) , nn . ReLU ( inplace = True ) , Conv2d ( C , C_Bottle , kernel_size = 3 , stride = 1 , padding = 1 , dilation = 1 ) , BatchNorm2d ( C_Bottle ) , nn . ReLU ( inplace = True ) , ) self . RF3 = Sequential ( # 7*7 Conv2d ( C_Bottle // 2 , C , kernel_size = 3 , stride = 1 , padding = 1 ) , BatchNorm2d ( C ) , nn . ReLU ( inplace = True ) , Conv2d ( C , C , kernel_size = 3 , stride = 1 , padding = 1 ) , BatchNorm2d ( C ) , nn . ReLU ( inplace = True ) , Conv2d ( C , C_Bottle // 2 , kernel_size = 3 , stride = 1 , padding = 1 ) , BatchNorm2d ( C_Bottle // 2 ) , nn . ReLU ( inplace = True ) , ) self . concat_conv1 = nn . Conv2d ( num_filters [ 1 ] , num_filters [ 1 ] , kernel_size = 3 , padding = 1 ) ## kernel_size=3 self . concat_conv2 = nn . Conv2d ( num_filters [ 1 ] , num_filters [ 1 ] , kernel_size = 3 , padding = 1 ) self . concat_conv3 = nn . Conv2d ( num_filters [ 1 ] , num_filters [ 1 ] , kernel_size = 3 , padding = 1 )