Beispiel #1
0
 def __init__(self, in_channels, num_classes, name=None):
     super(InceptionAux, self).__init__()
     self.num_classes = num_classes
     self.pool0 = nn.AvgPool2D(kernel_size=5, stride=3)
     self.conv0 = ConvBNLayer(in_channels, 128, 1, name=name + '.conv0')
     self.conv1 = ConvBNLayer(128, 768, 5, name=name + '.conv1')
     self.pool1 = nn.AdaptiveAvgPool2D(1)
Beispiel #2
0
    def __init__(self,
                 num_blocks,
                 width_multiplier=None,
                 override_groups_map=None,
                 class_dim=1000):
        super(RepVGG, self).__init__()

        assert len(width_multiplier) == 4
        self.override_groups_map = override_groups_map or dict()

        assert 0 not in self.override_groups_map

        self.in_planes = min(64, int(64 * width_multiplier[0]))

        self.stage0 = RepVGGBlock(in_channels=3,
                                  out_channels=self.in_planes,
                                  kernel_size=3,
                                  stride=2,
                                  padding=1)
        self.cur_layer_idx = 1
        self.stage1 = self._make_stage(int(64 * width_multiplier[0]),
                                       num_blocks[0],
                                       stride=2)
        self.stage2 = self._make_stage(int(128 * width_multiplier[1]),
                                       num_blocks[1],
                                       stride=2)
        self.stage3 = self._make_stage(int(256 * width_multiplier[2]),
                                       num_blocks[2],
                                       stride=2)
        self.stage4 = self._make_stage(int(512 * width_multiplier[3]),
                                       num_blocks[3],
                                       stride=2)
        self.gap = nn.AdaptiveAvgPool2D(output_size=1)
        self.linear = nn.Linear(int(512 * width_multiplier[3]), class_dim)
Beispiel #3
0
    def __init__(self, block=BottleneckBlock, depth=101, with_pool=True):
        super(ResNet, self).__init__()
        layer_cfg = {
            18: [2, 2, 2, 2],
            34: [3, 4, 6, 3],
            50: [3, 4, 6, 3],
            101: [3, 4, 23, 3],
            152: [3, 8, 36, 3]
        }
        layers = layer_cfg[depth]
        self.with_pool = with_pool
        self._norm_layer = nn.BatchNorm2D

        self.inplanes = 64
        self.dilation = 1

        self.conv1 = nn.Conv2D(3,
                               self.inplanes,
                               kernel_size=7,
                               stride=2,
                               padding=3,
                               bias_attr=False)
        self.bn1 = self._norm_layer(self.inplanes)
        self.relu = nn.ReLU()
        self.maxpool = nn.MaxPool2D(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2)
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2)
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2)
        if with_pool:
            self.avgpool = nn.AdaptiveAvgPool2D((1, 1))
    def __init__(self, features, with_pool=True):
        super(VGG, self).__init__()
        self.features = features
        self.with_pool = with_pool

        if with_pool:
            self.avgpool = nn.AdaptiveAvgPool2D((7, 7))
Beispiel #5
0
    def __init__(self,
                 num_channels: int,
                 num_filters: int,
                 reduction_ratio: int,
                 name: str = None):
        super(SELayer, self).__init__()

        self.pool2d_gap = nn.AdaptiveAvgPool2D(1)

        self._num_channels = num_channels

        med_ch = int(num_channels / reduction_ratio)
        stdv = 1.0 / math.sqrt(num_channels * 1.0)
        self.squeeze = nn.Linear(
            num_channels,
            med_ch,
            weight_attr=paddle.ParamAttr(
                initializer=nn.initializer.Uniform(-stdv, stdv)))

        stdv = 1.0 / math.sqrt(med_ch * 1.0)
        self.excitation = nn.Linear(
            med_ch,
            num_filters,
            weight_attr=paddle.ParamAttr(
                initializer=nn.initializer.Uniform(-stdv, stdv)))
Beispiel #6
0
    def _make_stage(self, in_channels: int, out_channels: int, size: int):
        """
        Create one pooling layer.

        In our implementation, we adopt the same dimension reduction as the original paper that might be
        slightly different with other implementations.

        After pooling, the channels are reduced to 1/len(bin_sizes) immediately, while some other implementations
        keep the channels to be same.

        Args:
            in_channels (int): The number of intput channels to pyramid pooling module.
            out_channels (int): The number of output channels to pyramid pooling module.
            size (int): The out size of the pooled layer.

        Returns:
            conv (Tensor): A tensor after Pyramid Pooling Module.
        """

        prior = nn.AdaptiveAvgPool2D(output_size=(size, size))
        conv = ConvBNReLU(in_channels=in_channels,
                          out_channels=out_channels,
                          kernel_size=1)

        return nn.Sequential(prior, conv)
Beispiel #7
0
 def __init__(self, planes, r=16):
     super(SEBlock, self).__init__()
     self.squeeze = nn.AdaptiveAvgPool2D(1)
     self.excitation = nn.Sequential(nn.Linear(planes, planes // r),
                                     nn.ReLU(),
                                     nn.Linear(planes // r, planes),
                                     nn.Sigmoid())
 def __init__(self, channel, lr_mult, conv_decay, reduction=4, name=""):
     super(SEModule, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     mid_channels = int(channel // reduction)
     self.conv1 = nn.Conv2D(
         in_channels=channel,
         out_channels=mid_channels,
         kernel_size=1,
         stride=1,
         padding=0,
         weight_attr=ParamAttr(
             learning_rate=lr_mult,
             regularizer=L2Decay(conv_decay),
             name=name + "_1_weights"),
         bias_attr=ParamAttr(
             learning_rate=lr_mult,
             regularizer=L2Decay(conv_decay),
             name=name + "_1_offset"))
     self.conv2 = nn.Conv2D(
         in_channels=mid_channels,
         out_channels=channel,
         kernel_size=1,
         stride=1,
         padding=0,
         weight_attr=ParamAttr(
             learning_rate=lr_mult,
             regularizer=L2Decay(conv_decay),
             name=name + "_2_weights"),
         bias_attr=ParamAttr(
             learning_rate=lr_mult,
             regularizer=L2Decay(conv_decay),
             name=name + "_2_offset"))
Beispiel #9
0
 def __init__(self, in_channels, channels, se_ratio=12):
     super(SE, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     self.fc = nn.Sequential(
         nn.Conv2D(in_channels, channels // se_ratio, kernel_size=1, padding=0),
         nn.BatchNorm2D(channels // se_ratio), nn.ReLU(),
         nn.Conv2D(channels // se_ratio, channels, kernel_size=1, padding=0), nn.Sigmoid())
 def __init__(self, model_name='ResNet50', last_stride=1):
     super(ResNetEmbedding, self).__init__()
     assert model_name in ['ResNet50', 'ResNet101'
                           ], "Unsupported ReID arch: {}".format(model_name)
     self.base = eval(model_name)(last_conv_stride=last_stride)
     self.gap = nn.AdaptiveAvgPool2D(output_size=1)
     self.flatten = nn.Flatten(start_axis=1, stop_axis=-1)
     self.bn = nn.BatchNorm1D(self.in_planes, bias_attr=False)
Beispiel #11
0
    def __init__(self, in_dim, out_dim):
        super(ContextEmbeddingBlock, self).__init__()

        self.gap = nn.AdaptiveAvgPool2D(1)
        self.bn = layers.SyncBatchNorm(in_dim)

        self.conv_1x1 = layers.ConvBNReLU(in_dim, out_dim, 1)
        self.conv_3x3 = nn.Conv2D(out_dim, out_dim, 3, 1, 1)
Beispiel #12
0
 def __init__(self, in_channels, out_channels, with_avg_pool=True):
     super(LinearNeck, self).__init__()
     self.with_avg_pool = with_avg_pool
     if with_avg_pool:
         self.avgpool = nn.AdaptiveAvgPool2D((1, 1))
     self.fc = nn.Linear(in_channels, out_channels)
     # init_backbone_weight(self.fc)
     self.init_parameters()
Beispiel #13
0
    def __init__(self, scale=1.0, num_classes=1000, with_pool=True):
        super(MobileNetV2, self).__init__()
        self.num_classes = num_classes
        self.with_pool = with_pool
        input_channel = 32
        last_channel = 1280

        block = InvertedResidual
        round_nearest = 8
        norm_layer = nn.BatchNorm2D
        inverted_residual_setting = [
            [1, 16, 1, 1],
            [6, 24, 2, 2],
            [6, 32, 3, 2],
            [6, 64, 4, 2],
            [6, 96, 3, 1],
            [6, 160, 3, 2],
            [6, 320, 1, 1],
        ]

        input_channel = _make_divisible(input_channel * scale, round_nearest)
        self.last_channel = _make_divisible(last_channel * max(1.0, scale),
                                            round_nearest)
        features = [
            ConvNormActivation(3,
                               input_channel,
                               stride=2,
                               norm_layer=norm_layer,
                               activation_layer=nn.ReLU6)
        ]

        for t, c, n, s in inverted_residual_setting:
            output_channel = _make_divisible(c * scale, round_nearest)
            for i in range(n):
                stride = s if i == 0 else 1
                features.append(
                    block(input_channel,
                          output_channel,
                          stride,
                          expand_ratio=t,
                          norm_layer=norm_layer))
                input_channel = output_channel

        features.append(
            ConvNormActivation(input_channel,
                               self.last_channel,
                               kernel_size=1,
                               norm_layer=norm_layer,
                               activation_layer=nn.ReLU6))

        self.features = nn.Sequential(*features)

        if with_pool:
            self.pool2d_avg = nn.AdaptiveAvgPool2D(1)

        if self.num_classes > 0:
            self.classifier = nn.Sequential(
                nn.Dropout(0.2), nn.Linear(self.last_channel, num_classes))
Beispiel #14
0
    def __init__(self, in_channels, num_fiducial, loc_lr, model_name):
        super(LocalizationNetwork, self).__init__()
        self.F = num_fiducial
        F = num_fiducial
        if model_name == "large":
            num_filters_list = [64, 128, 256, 512]
            fc_dim = 256
        else:
            num_filters_list = [16, 32, 64, 128]
            fc_dim = 64

        self.block_list = []
        for fno in range(0, len(num_filters_list)):
            num_filters = num_filters_list[fno]
            name = "loc_conv%d" % fno
            conv = self.add_sublayer(
                name,
                ConvBNLayer(in_channels=in_channels,
                            out_channels=num_filters,
                            kernel_size=3,
                            act='relu',
                            name=name))
            self.block_list.append(conv)
            if fno == len(num_filters_list) - 1:
                pool = nn.AdaptiveAvgPool2D(1)
            else:
                pool = nn.MaxPool2D(kernel_size=2, stride=2, padding=0)
            in_channels = num_filters
            self.block_list.append(pool)
        name = "loc_fc1"
        stdv = 1.0 / math.sqrt(num_filters_list[-1] * 1.0)
        self.fc1 = nn.Linear(in_channels,
                             fc_dim,
                             weight_attr=ParamAttr(
                                 learning_rate=loc_lr,
                                 name=name + "_w",
                                 initializer=nn.initializer.Uniform(
                                     -stdv, stdv)),
                             bias_attr=ParamAttr(name=name + '.b_0'),
                             name=name)

        # Init fc2 in LocalizationNetwork
        initial_bias = self.get_initial_fiducials()
        initial_bias = initial_bias.reshape(-1)
        name = "loc_fc2"
        param_attr = ParamAttr(learning_rate=loc_lr,
                               initializer=nn.initializer.Assign(
                                   np.zeros([fc_dim, F * 2])),
                               name=name + "_w")
        bias_attr = ParamAttr(learning_rate=loc_lr,
                              initializer=nn.initializer.Assign(initial_bias),
                              name=name + "_b")
        self.fc2 = nn.Linear(fc_dim,
                             F * 2,
                             weight_attr=param_attr,
                             bias_attr=bias_attr,
                             name=name)
        self.out_channels = F * 2
Beispiel #15
0
 def __init__(self, inplanes, reduction=16):
     super(SELayer, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     self.fc = nn.Sequential(
         nn.Linear(inplanes, inplanes // reduction, bias_attr=False),
         nn.ReLU(),
         nn.Linear(inplanes // reduction, inplanes, bias_attr=False),
         nn.Sigmoid(),
     )
Beispiel #16
0
    def __init__(self, label_list: list = None, load_checkpoint: str = None):
        super(RepVGG_B3G4, self).__init__()

        if label_list is not None:
            self.labels = label_list
            class_dim = len(self.labels)
        else:
            label_list = []
            label_file = os.path.join(self.directory, 'label_list.txt')
            files = open(label_file)
            for line in files.readlines():
                line = line.strip('\n')
                label_list.append(line)
            self.labels = label_list
            class_dim = len(self.labels)

        num_blocks = [4, 6, 16, 1]
        width_multiplier = [3, 3, 3, 5]
        optional_groupwise_layers = [
            2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26
        ]
        self.override_groups_map = {l: 4 for l in optional_groupwise_layers}

        assert 0 not in self.override_groups_map

        self.in_planes = min(64, int(64 * width_multiplier[0]))

        self.stage0 = RepVGGBlock(in_channels=3,
                                  out_channels=self.in_planes,
                                  kernel_size=3,
                                  stride=2,
                                  padding=1)
        self.cur_layer_idx = 1
        self.stage1 = self._make_stage(int(64 * width_multiplier[0]),
                                       num_blocks[0],
                                       stride=2)
        self.stage2 = self._make_stage(int(128 * width_multiplier[1]),
                                       num_blocks[1],
                                       stride=2)
        self.stage3 = self._make_stage(int(256 * width_multiplier[2]),
                                       num_blocks[2],
                                       stride=2)
        self.stage4 = self._make_stage(int(512 * width_multiplier[3]),
                                       num_blocks[3],
                                       stride=2)
        self.gap = nn.AdaptiveAvgPool2D(output_size=1)
        self.linear = nn.Linear(int(512 * width_multiplier[3]), class_dim)

        if load_checkpoint is not None:
            self.model_dict = paddle.load(load_checkpoint)
            self.set_dict(self.model_dict)
            print("load custom checkpoint success")
        else:
            checkpoint = os.path.join(self.directory, 'model.pdparams')
            self.model_dict = paddle.load(checkpoint)
            self.set_dict(self.model_dict)
            print("load pretrained checkpoint success")
Beispiel #17
0
 def __init__(self, channel, reduction=16):
     super(SEBlock, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     self.fc = nn.Sequential(
         nn.Linear(channel, channel // reduction),
         nn.PReLU(),
         nn.Linear(channel // reduction, channel),
         nn.Sigmoid()
     )
Beispiel #18
0
 def __init__(self, in_channels, ratio=8):
     super(CAM, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     self.max_pool = nn.AdaptiveMaxPool2D(1)
     self.mlp = nn.Sequential(
         nn.Conv2D(in_channels, in_channels//ratio, 1),
         nn.ReLU(),
         nn.Conv2D(in_channels//ratio, in_channels, 1)
     )
Beispiel #19
0
    def __init__(self,
                 stem,
                 stages,
                 num_classes=None,
                 out_features=None,
                 freeze_at=0):
        super(ResNet, self).__init__()
        self.stem = stem
        self.num_classes = num_classes

        current_stride = self.stem.stride
        self._out_feature_strides = {"stem": current_stride}
        self._out_feature_channels = {"stem": self.stem.out_channels}

        self.stage_names, self.stages = [], []

        if out_features is not None:
            num_stages = max([{
                "res2": 1,
                "res3": 2,
                "res4": 3,
                "res5": 4
            }.get(f, 0) for f in out_features])
            stages = stages[:num_stages]
        for i, blocks in enumerate(stages):
            assert len(blocks) > 0, len(blocks)
            for block in blocks:
                assert isinstance(block, CNNBlockBase), block

            name = "res" + str(i + 2)
            stage = nn.Sequential(*blocks)

            self.add_sublayer(name, stage)
            self.stage_names.append(name)
            self.stages.append(stage)

            self._out_feature_strides[name] = current_stride = int(
                current_stride * np.prod([k.stride for k in blocks]))
            self._out_feature_channels[name] = curr_channels = blocks[
                -1].out_channels
        self.stage_names = tuple(self.stage_names)

        if num_classes is not None:
            self.avgpool = nn.AdaptiveAvgPool2D(1)
            self.linear = nn.Linear(curr_channels, num_classes)
            name = "linear"

        if out_features is None:
            out_features = [name]
        self._out_features = out_features
        assert len(self._out_features)
        children = [x[0] for x in self.named_children()]
        for out_feature in self._out_features:
            assert out_feature in children, "Available children: {}".format(
                ", ".join(children))
        self.freeze(freeze_at)
Beispiel #20
0
    def __init__(self,
                 config,
                 last_channel,
                 scale=1.0,
                 num_classes=1000,
                 with_pool=True):
        super().__init__()

        self.config = config
        self.scale = scale
        self.last_channel = last_channel
        self.num_classes = num_classes
        self.with_pool = with_pool
        self.firstconv_in_channels = config[0].in_channels
        self.lastconv_in_channels = config[-1].in_channels
        self.lastconv_out_channels = self.lastconv_in_channels * 6
        norm_layer = partial(nn.BatchNorm2D, epsilon=0.001, momentum=0.99)

        self.conv = ConvNormActivation(in_channels=3,
                                       out_channels=self.firstconv_in_channels,
                                       kernel_size=3,
                                       stride=2,
                                       padding=1,
                                       groups=1,
                                       activation_layer=nn.Hardswish,
                                       norm_layer=norm_layer)

        self.blocks = nn.Sequential(*[
            InvertedResidual(in_channels=cfg.in_channels,
                             expanded_channels=cfg.expanded_channels,
                             out_channels=cfg.out_channels,
                             filter_size=cfg.kernel,
                             stride=cfg.stride,
                             use_se=cfg.use_se,
                             activation_layer=cfg.activation_layer,
                             norm_layer=norm_layer) for cfg in self.config
        ])

        self.lastconv = ConvNormActivation(
            in_channels=self.lastconv_in_channels,
            out_channels=self.lastconv_out_channels,
            kernel_size=1,
            stride=1,
            padding=0,
            groups=1,
            norm_layer=norm_layer,
            activation_layer=nn.Hardswish)

        if with_pool:
            self.avgpool = nn.AdaptiveAvgPool2D(1)

        if num_classes > 0:
            self.classifier = nn.Sequential(
                nn.Linear(self.lastconv_out_channels, self.last_channel),
                nn.Hardswish(), nn.Dropout(p=0.2),
                nn.Linear(self.last_channel, num_classes))
    def __init__(self, in_planes, ratio=16):
        super(ChannelAttention, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2D(1)
        self.max_pool = nn.AdaptiveMaxPool2D(1)

        self.fc1 = nn.Conv2D(in_planes, in_planes // 16, 1)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Conv2D(in_planes // 16, in_planes, 1)

        self.sigmoid = nn.Sigmoid()
Beispiel #22
0
 def __init__(self, in_channels, reduction=16):
     super(SELayer, self).__init__()
     assert reduction >= 16
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     self.fc = nn.Sequential(
         nn.Linear(in_channels, in_channels//reduction),
         nn.ReLU(),
         nn.Linear(in_channels // reduction, in_channels),
         nn.Sigmoid()
     )
Beispiel #23
0
    def __init__(self,
                 in_channels=2,
                 edge_importance_weighting=True,
                 data_bn=True,
                 layout='fsd10',
                 strategy='spatial',
                 **kwargs):
        super(STGCN, self).__init__()
        self.data_bn = data_bn
        # load graph
        self.graph = Graph(
            layout=layout,
            strategy=strategy,
        )
        A = paddle.to_tensor(self.graph.A, dtype='float32')
        self.register_buffer('A', A)

        # build networks
        spatial_kernel_size = A.shape[0]
        temporal_kernel_size = 9
        kernel_size = (temporal_kernel_size, spatial_kernel_size)
        self.data_bn = nn.BatchNorm1D(in_channels *
                                      A.shape[1]) if self.data_bn else iden
        kwargs0 = {k: v for k, v in kwargs.items() if k != 'dropout'}
        self.st_gcn_networks = nn.LayerList((
            st_gcn_block(in_channels,
                         64,
                         kernel_size,
                         1,
                         residual=False,
                         **kwargs0),
            st_gcn_block(64, 64, kernel_size, 1, **kwargs),
            st_gcn_block(64, 64, kernel_size, 1, **kwargs),
            st_gcn_block(64, 64, kernel_size, 1, **kwargs),
            st_gcn_block(64, 128, kernel_size, 2, **kwargs),
            st_gcn_block(128, 128, kernel_size, 1, **kwargs),
            st_gcn_block(128, 128, kernel_size, 1, **kwargs),
            st_gcn_block(128, 256, kernel_size, 2, **kwargs),
            st_gcn_block(256, 256, kernel_size, 1, **kwargs),
            st_gcn_block(256, 256, kernel_size, 1, **kwargs),
        ))

        # initialize parameters for edge importance weighting
        if edge_importance_weighting:
            self.edge_importance = nn.ParameterList([
                self.create_parameter(
                    shape=self.A.shape,
                    default_initializer=nn.initializer.Constant(1))
                for i in self.st_gcn_networks
            ])
        else:
            self.edge_importance = [1] * len(self.st_gcn_networks)

        self.pool = nn.AdaptiveAvgPool2D(output_size=(1, 1))
Beispiel #24
0
    def __init__(self, input_ch=16, final_ch=180, width_mult=1.0, depth_mult=1.0,
                 use_se=True, se_ratio=12, dropout_ratio=0.2, class_dim=1000):
        super(ReXNetV1, self).__init__()

        layers = [1, 2, 2, 3, 3, 5]
        strides = [1, 2, 2, 2, 1, 2]
        use_ses = [False, False, True, True, True, True]

        layers = [ceil(element * depth_mult) for element in layers]
        strides = sum([[element] + [1] * (layers[idx] - 1)
                       for idx, element in enumerate(strides)], [])
        if use_se:
            use_ses = sum([[element] * layers[idx]
                           for idx, element in enumerate(use_ses)], [])
        else:
            use_ses = [False] * sum(layers[:])
        ts = [1] * layers[0] + [6] * sum(layers[1:])

        self.depth = sum(layers[:]) * 3
        stem_channel = 32 / width_mult if width_mult < 1.0 else 32
        inplanes = input_ch / width_mult if width_mult < 1.0 else input_ch

        features = []
        in_channels_group = []
        channels_group = []

        # The following channel configuration is a simple instance to make each layer become an expand layer.
        for i in range(self.depth // 3):
            if i == 0:
                in_channels_group.append(int(round(stem_channel * width_mult)))
                channels_group.append(int(round(inplanes * width_mult)))
            else:
                in_channels_group.append(int(round(inplanes * width_mult)))
                inplanes += final_ch / (self.depth // 3 * 1.0)
                channels_group.append(int(round(inplanes * width_mult)))

        ConvBN(features, 3, int(round(stem_channel * width_mult)),
               kernel=3, stride=2, pad=1, act='swish')

        for block_idx, (in_c, c, t, s, se) in enumerate(zip(in_channels_group, channels_group, ts, strides, use_ses)):
            features.append(LinearBottleneck(in_channels=in_c,
                                             channels=c,
                                             t=t,
                                             stride=s,
                                             use_se=se, se_ratio=se_ratio))

        pen_channels = int(1280 * width_mult)
        ConvBN(features, c, pen_channels, act='swish')

        features.append(nn.AdaptiveAvgPool2D(1))
        self.features = nn.Sequential(*features)
        self.output = nn.Sequential(
            nn.Dropout(dropout_ratio),
            nn.Conv2D(pen_channels, class_dim, 1))
Beispiel #25
0
    def __init__(self,
                 cfg,
                 in_chans=3,
                 class_num=1000,
                 output_stride=32,
                 global_pool='avg',
                 drop_rate=0.,
                 act_layer=nn.LeakyReLU,
                 norm_layer=nn.BatchNorm2D,
                 zero_init_last_bn=True,
                 stage_fn=CrossStage,
                 block_fn=DarkBlock):
        super().__init__()
        self.class_num = class_num
        self.drop_rate = drop_rate
        assert output_stride in (8, 16, 32)
        layer_args = dict(act_layer=act_layer, norm_layer=norm_layer)

        # Construct the stem
        self.stem, stem_feat_info = create_stem(in_chans, **cfg['stem'],
                                                **layer_args)
        self.feature_info = [stem_feat_info]
        prev_chs = stem_feat_info['num_chs']
        curr_stride = stem_feat_info[
            'reduction']  # reduction does not include pool
        if cfg['stem']['pool']:
            curr_stride *= 2

        # Construct the stages
        per_stage_args = _cfg_to_stage_args(cfg['stage'],
                                            curr_stride=curr_stride,
                                            output_stride=output_stride)
        self.stages = nn.LayerList()
        for i, sa in enumerate(per_stage_args):
            self.stages.add_sublayer(
                str(i),
                stage_fn(prev_chs, **sa, **layer_args, block_fn=block_fn))
            prev_chs = sa['out_chs']
            curr_stride *= sa['stride']
            self.feature_info += [
                dict(num_chs=prev_chs,
                     reduction=curr_stride,
                     module=f'stages.{i}')
            ]

        # Construct the head
        self.num_features = prev_chs

        self.pool = nn.AdaptiveAvgPool2D(1)
        self.flatten = nn.Flatten(1)
        self.fc = nn.Linear(prev_chs,
                            class_num,
                            weight_attr=ParamAttr(),
                            bias_attr=ParamAttr())
Beispiel #26
0
    def __init__(self, dropout, dim, max_len=5000):
        super(PositionalEncoding_2d, self).__init__()
        self.dropout = nn.Dropout(p=dropout)

        pe = paddle.zeros([max_len, dim])
        position = paddle.arange(0, max_len, dtype=paddle.float32).unsqueeze(1)
        div_term = paddle.exp(
            paddle.arange(0, dim, 2).astype('float32') *
            (-math.log(10000.0) / dim))
        pe[:, 0::2] = paddle.sin(position * div_term)
        pe[:, 1::2] = paddle.cos(position * div_term)
        pe = pe.unsqueeze(0).transpose([1, 0, 2])
        self.register_buffer('pe', pe)

        self.avg_pool_1 = nn.AdaptiveAvgPool2D((1, 1))
        self.linear1 = nn.Linear(dim, dim)
        self.linear1.weight.data.fill_(1.)
        self.avg_pool_2 = nn.AdaptiveAvgPool2D((1, 1))
        self.linear2 = nn.Linear(dim, dim)
        self.linear2.weight.data.fill_(1.)
Beispiel #27
0
 def __init__(self,
              input_channels,
              squeeze_channels,
              activation=nn.ReLU,
              scale_activation=nn.Sigmoid):
     super().__init__()
     self.avgpool = nn.AdaptiveAvgPool2D(1)
     self.fc1 = nn.Conv2D(input_channels, squeeze_channels, 1)
     self.fc2 = nn.Conv2D(squeeze_channels, input_channels, 1)
     self.activation = activation()
     self.scale_activation = scale_activation()
Beispiel #28
0
 def __init__(self, in_channels, class_dim, **kwargs):
     super(ClsHead, self).__init__()
     self.pool = nn.AdaptiveAvgPool2D(1)
     stdv = 1.0 / math.sqrt(in_channels * 1.0)
     self.fc = nn.Linear(
         in_channels,
         class_dim,
         weight_attr=ParamAttr(name="fc_0.w_0",
                               initializer=nn.initializer.Uniform(
                                   -stdv, stdv)),
         bias_attr=ParamAttr(name="fc_0.b_0"),
     )
Beispiel #29
0
 def __init__(self, in_channel, channel, reduction=16):
     super(CSELayer, self).__init__()
     self.avg_pool = nn.AdaptiveAvgPool2D(1)
     self.fc = nn.Sequential(nn.Linear(channel, channel // reduction),
                             nn.ReLU(),
                             nn.Linear(channel // reduction, channel),
                             nn.Sigmoid())
     if in_channel != channel:
         self.att_fc = nn.Sequential(nn.Linear(in_channel, channel),
                                     nn.LayerNorm(channel), nn.ReLU())
     self.conv = nn.Sequential(nn.Conv2D(2, 1, kernel_size=1),
                               nn.LayerNorm(channel), nn.ReLU())
Beispiel #30
0
    def __init__(self, channel, reduction=16):
        super(SELayer, self).__init__()
        self.avgpool = nn.AdaptiveAvgPool2D(1)
        self.activation = nn.Sigmoid()

        self.reduction = reduction

        self.fc = nn.Sequential(
            nn.Linear(channel, channel // self.reduction),
            nn.ReLU(),
            nn.Linear(channel // self.reduction, channel),
        )