Example #1
0
    def forward(self, x, pool_size=(2, 2), pool_type='avg'):
        x = self.conv1(x)
        x = self.bn1(x)
        x = F.relu(x)

        if pool_type == 'max':
            x = F.max_pool2d(x, kernel_size=pool_size)
        elif pool_type == 'avg':
            x = F.avg_pool2d(x, kernel_size=pool_size)
        elif pool_type == 'avg+max':
            x = F.avg_pool2d(x, kernel_size=pool_size) + F.max_pool2d(x, kernel_size=pool_size)
        else:
            raise Exception(
                f'Pooling type of {pool_type} is not supported. It must be one of "max", "avg" and "avg+max".')
        return x
Example #2
0
    def forward(self, inputs):

        out = self.branch2a(inputs)
        feature_split = paddle.split(out, self.scales, 1)
        out_split = []
        for i in range(self.scales - 1):
            if i == 0 or self.stride == 2:
                out_split.append(self.branch2b[i](feature_split[i]))
            else:
                out_split.append(self.branch2b[i](paddle.add(
                    feature_split[i], out_split[-1])))
        if self.stride == 1:
            out_split.append(feature_split[-1])
        else:
            out_split.append(F.avg_pool2d(feature_split[-1], 3, self.stride,
                                          1))
        out = self.branch2c(paddle.concat(out_split, 1))

        if self.shortcut:
            short = inputs
        else:
            short = self.branch1(inputs)

        out = paddle.add(out, short)
        out = F.relu(out)

        return out
Example #3
0
    def forward(self, x):
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        if self.network_type == "ImageNet":
            x = self.maxpool(x)

        x = self.layer1(x)
        if not self.bam1 is None:
            x = self.bam1(x)

        x = self.layer2(x)
        if not self.bam2 is None:
            x = self.bam2(x)

        x = self.layer3(x)
        if not self.bam3 is None:
            x = self.bam3(x)

        x = self.layer4(x)

        if self.network_type == "ImageNet":
            x = self.avgpool(x)
        else:
            x = F.avg_pool2d(x, 4)
        x = paddle.reshape(x, [x.shape[0], -1])
        x = self.fc(x)
        return x
Example #4
0
    def forward(self, body_feats):
        num_backbone_stages = len(body_feats)

        outs = []
        outs.append(body_feats[0])

        # resize
        for i in range(1, num_backbone_stages):
            resized = F.interpolate(
                body_feats[i], scale_factor=2**i, mode='bilinear')
            outs.append(resized)

        # concat
        out = paddle.concat(outs, axis=1)
        assert out.shape[
            1] == self.in_channel, 'in_channel should be {}, be received {}'.format(
                out.shape[1], self.in_channel)

        # reduction
        out = self.reduction(out)

        # conv
        outs = [out]
        for i in range(1, self.num_out):
            outs.append(F.avg_pool2d(out, kernel_size=2**i, stride=2**i))
        outputs = []

        for i in range(self.num_out):
            conv_func = self.fpn_conv if self.share_conv else self.fpn_conv[i]
            conv = conv_func(outs[i])
            outputs.append(conv)

        fpn_feats = [outputs[k] for k in range(self.num_out)]
        return fpn_feats
Example #5
0
    def forward(self, x):
        channel_att_sum = None
        for pool_type in self.pool_types:
            if pool_type == 'avg':
                avg_pool = F.avg_pool2d(x, (x.shape[2], x.shape[3]),
                                        stride=(x.shape[2], x.shape[3]))
                channel_att_raw = self.mlp(avg_pool)
            elif pool_type == 'max':
                max_pool = F.max_pool2d(x, (x.shape[2], x.shape[3]),
                                        stride=(x.shape[2], x.shape[3]))
                channel_att_raw = self.mlp(max_pool)
            elif pool_type == 'lp':
                lp_pool = F.lp_pool2d(x,
                                      2, (x.shape[2], x.shape[3]),
                                      stride=(x.shape[2], x.shape[3]))
                channel_att_raw = self.mlp(lp_pool)
            elif pool_type == 'lse':
                # LSE pool only
                lse_pool = logsumexp_2d(x)
                channel_att_raw = self.mlp(lse_pool)

            if channel_att_sum is None:
                channel_att_sum = channel_att_raw
            else:
                channel_att_sum = channel_att_sum + channel_att_raw

        scale = F.sigmoid(channel_att_sum)
        scale = paddle.unsqueeze(scale, 2)
        scale = paddle.unsqueeze(scale, 3)
        scale = paddle.expand_as(scale, x)
        return x * scale
Example #6
0
 def forward(self, x):
     feat = self.conv(x)
     atten = F.avg_pool2d(feat, feat.shape[2:])
     atten = self.conv_atten(atten)
     atten = self.bn_atten(atten)
     atten = self.sigmoid_atten(atten)
     out = feat * atten
     return out
Example #7
0
 def forward(self, in_tensor):
     avg_pool = F.avg_pool2d(in_tensor,
                             in_tensor.shape[2],
                             stride=in_tensor.shape[2])
     tmp = paddle.unsqueeze(self.gate_c(avg_pool), 2)
     tmp = paddle.unsqueeze(tmp, 3)
     result = paddle.expand_as(tmp, in_tensor)
     return result
Example #8
0
 def forward(self, x):
     out = self.conv1(x)
     out = self.block1(out)
     out = self.block2(out)
     out = self.block3(out)
     out = self.relu(self.bn1(out))
     out = F.avg_pool2d(out, 8)
     out = paddle.reshape(out, shape=(-1, self.nChannels))
     # out = out.view(-1, self.nChannels)
     return self.fc(out)
Example #9
0
 def forward(self, fsp, fcp):
     fcat = paddle.concat([fsp, fcp], axis=1)
     feat = self.convblk(fcat)
     atten = F.avg_pool2d(feat, feat.shape[2:])
     atten = self.conv1(atten)
     atten = self.relu(atten)
     atten = self.conv2(atten)
     atten = self.sigmoid(atten)
     feat_atten = feat * atten
     feat_out = feat_atten + feat
     return feat_out
Example #10
0
 def run5():
     with fluid.dygraph.guard():
         input_np = np.random.uniform(-1, 1,
                                      [2, 3, 32, 32]).astype(np.float32)
         input_pd = fluid.dygraph.to_variable(input_np)
         padding = "padding"
         res_pd = avg_pool2d(input_pd,
                             kernel_size=2,
                             stride=2,
                             padding=padding,
                             data_format='NHWC')
Example #11
0
 def forward(self, x):
     out = x
     if self.sn is not None:
         self.conv.weight.set_value(self.sn(self.conv.weight))
     out = self.conv(out)
     if self.norm is not None:
         out = self.norm(out)
     out = F.leaky_relu(out, 0.2)
     if self.pool:
         out = F.avg_pool2d(out, kernel_size=2, stride=2, ceil_mode=False)
     return out
Example #12
0
    def forward(self, x):
        y = paddle.rand(shape=[1, 3, 4, 4])

        w1 = paddle.shape(y)[0]
        w2 = paddle.shape(x)[0]

        while w2 != w1:
            x = F.avg_pool2d(x, kernel_size=3, padding=1, stride=2)
            w2 = paddle.shape(x)[0]

        return x + y
Example #13
0
 def forward(self, feats):
     h, w = paddle.shape(feats)[2:4]
     out = [feats]
     for stage in self.stages:
         feats = F.avg_pool2d(feats, kernel_size=3, stride=2, padding='same')
         upsampled = F.interpolate(stage(feats),
                                   size=[h, w],
                                   mode='bilinear',
                                   align_corners=True)
         out.append(upsampled)
     return self.project(paddle.concat(out, axis=1))
Example #14
0
 def run_stride_out_of_range():
     with fluid.dygraph.guard():
         input_np = np.random.uniform(-1, 1,
                                      [2, 3, 32, 32]).astype(np.float32)
         input_pd = fluid.dygraph.to_variable(input_np)
         res_pd = avg_pool2d(
             input_pd,
             kernel_size=3,
             stride=[0, 2],
             padding=0,
             ceil_mode=False,
             data_format='NHWC')
Example #15
0
def local_pairwise_distances2(x, y, max_distance=9):
    """Computes pairwise squared l2 distances using a local search window.
    Naive implementation using map_fn.
    Used as a slow fallback for when correlation_cost is not available.
    Args:
    x: Float32 tensor of shape [height, width, feature_dim].
    y: Float32 tensor of shape [height, width, feature_dim].
    max_distance: Integer, the maximum distance in pixel coordinates
      per dimension which is considered to be in the search window.
    Returns:
    Float32 distances tensor of shape
      [height, width, (2 * max_distance + 1) ** 2].
    """
    ori_h, ori_w, _ = x.shape
    x = paddle.transpose(x, [2, 0, 1]).unsqueeze(0)
    x = F.avg_pool2d(x, (2, 2), (2, 2))
    y = paddle.transpose(y, [2, 0, 1]).unsqueeze(0)
    y = F.avg_pool2d(y, (2, 2), (2, 2))

    _, channels, height, width = x.shape
    padding_val = 1e20
    padded_y = F.pad(y,
                     (max_distance, max_distance, max_distance, max_distance),
                     mode='constant',
                     value=padding_val)
    offset_y = F.unfold(padded_y, kernel_sizes=[height, width]).reshape(
        [1, channels, height, width, -1])
    x = x.reshape([1, channels, height, width, 1])
    minus = x - offset_y
    dists = paddle.sum(paddle.multiply(minus, minus),
                       axis=1).reshape([1, height, width,
                                        -1]).transpose([0, 3, 1, 2])
    dists = (paddle.nn.functional.sigmoid(dists) - 0.5) * 2
    dists = F.interpolate(dists,
                          size=[ori_h, ori_w],
                          mode='bilinear',
                          align_corners=True)
    dists = dists.squeeze(0).transpose([1, 2, 0])
    return dists
Example #16
0
    def _forward(self, x):
        branch1x1 = self.branch1x1(x)

        branch5x5 = self.branch5x5_1(x)
        branch5x5 = self.branch5x5_2(branch5x5)

        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)

        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        outputs = [branch1x1, branch5x5, branch3x3dbl, branch_pool]
        return outputs
Example #17
0
    def forward(self, x):
        x = self.conv1(x)
        # x = self.maxpool(x)

        x = self.stage2(x)
        x = self.stage3(x)
        x = self.stage4(x)

        # global average pooling layer
        x = F.avg_pool2d(x, x.shape[-2:])
        # flatten for input to fully-connected layer
        x = x.flatten(1)
        x = self.fc(x)

        return F.log_softmax(x, axis=1)
Example #18
0
    def forward(self, x):
        skips = []
        for i in range(self.n_skip):
            skips.append(getattr(self, 'ConvBlockskip' + str(i + 1))(x))
            x = F.avg_pool2d(x, 2)
            x = getattr(self, 'ConvBlock' + str(i + 1))(x)

        x = self.ConvBlock5(x)

        for i in range(self.n_skip):
            x = getattr(self, 'ConvBlock' + str(i + 6))(x)
            x = F.upsample(x, scale_factor=2)
            x = skips[self.n_skip - i - 1] + x

        return x
Example #19
0
    def _forward(self, level, inp):
        up1 = inp
        up1 = self._sub_layers['b1_' + str(level)](up1)
        low1 = F.avg_pool2d(inp, 2, stride=2)
        low1 = self._sub_layers['b2_' + str(level)](low1)

        if level > 1:
            low2 = self._forward(level - 1, low1)
        else:
            low2 = low1
            low2 = self._sub_layers['b2_plus_' + str(level)](low2)
        low3 = low2
        low3 = self._sub_layers['b3_' + str(level)](low3)
        up2 = F.interpolate(low3, scale_factor=2, mode='nearest')

        return up1 + up2
Example #20
0
 def forward(self, x):
     # N x 768 x 17 x 17
     x = F.avg_pool2d(x, kernel_size=5, stride=3)
     # N x 768 x 5 x 5
     x = self.conv0(x)
     # N x 128 x 5 x 5
     x = self.conv1(x)
     # N x 768 x 1 x 1
     # Adaptive average pooling
     x = F.adaptive_avg_pool2d(x, (1, 1))
     # N x 768 x 1 x 1
     x = paddle.flatten(x, 1)
     # N x 768
     x = self.fc(x)
     # N x 1000
     return x
Example #21
0
    def forward(self, x):
        # save for combining later with output
        residual = x

        if self.combine == 'concat':
            residual = F.avg_pool2d(residual,
                                    kernel_size=3,
                                    stride=2,
                                    padding=1)

        out = self.g_conv_1x1_compress(x)
        out = channel_shuffle(out, self.groups)
        out = self.depthwise_conv3x3(out)
        out = self.bn_after_depthwise(out)
        out = self.g_conv_1x1_expand(out)

        out = self._combine_func(residual, out)
        return F.relu(out)
Example #22
0
    def _forward(self, x):
        branch1x1 = self.branch1x1(x)

        branch7x7 = self.branch7x7_1(x)
        branch7x7 = self.branch7x7_2(branch7x7)
        branch7x7 = self.branch7x7_3(branch7x7)

        branch7x7dbl = self.branch7x7dbl_1(x)
        branch7x7dbl = self.branch7x7dbl_2(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_3(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_4(branch7x7dbl)
        branch7x7dbl = self.branch7x7dbl_5(branch7x7dbl)

        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        outputs = [branch1x1, branch7x7, branch7x7dbl, branch_pool]
        return outputs
Example #23
0
    def check_avg_dygraph_results(self, place):
        with fluid.dygraph.guard(place):
            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            input = fluid.dygraph.to_variable(input_np)
            result = avg_pool2d(input, kernel_size=2, stride=2, padding=0)

            result_np = pool2D_forward_naive(
                input_np,
                ksize=[2, 2],
                strides=[2, 2],
                paddings=[0, 0],
                pool_type='avg')
            self.assertTrue(np.allclose(result.numpy(), result_np))

            avg_pool2d_dg = paddle.nn.layer.AvgPool2D(
                kernel_size=2, stride=2, padding=0)
            result = avg_pool2d_dg(input)
            self.assertTrue(np.allclose(result.numpy(), result_np))
Example #24
0
    def forward(self, x, inputs=None):
        avg_out = self.avg(x)
        eesp_out = self.eesp(x)
        output = paddle.concat([avg_out, eesp_out], axis=1)

        if inputs is not None:
            w1 = paddle.shape(avg_out)[2]
            w2 = paddle.shape(inputs)[2]
            
            while w2 != w1:
                inputs = F.avg_pool2d(inputs,
                                      kernel_size=3,
                                      padding=1,
                                      stride=2)
                w2 = paddle.shape(inputs)[2]
            # import pdb
            # pdb.set_trace()
            output = output + self.shortcut_layer(inputs)
        return self._act(output)
Example #25
0
    def check_avg_static_results(self, place):
        with fluid.program_guard(fluid.Program(), fluid.Program()):
            input = fluid.data(
                name="input", shape=[2, 3, 32, 32], dtype="float32")
            result = avg_pool2d(input, kernel_size=2, stride=2, padding=0)

            input_np = np.random.random([2, 3, 32, 32]).astype("float32")
            result_np = pool2D_forward_naive(
                input_np,
                ksize=[2, 2],
                strides=[2, 2],
                paddings=[0, 0],
                pool_type='avg')

            exe = fluid.Executor(place)
            fetches = exe.run(fluid.default_main_program(),
                              feed={"input": input_np},
                              fetch_list=[result])
            self.assertTrue(np.allclose(fetches[0], result_np))
Example #26
0
    def forward(self, x):
        H0, W0 = x.shape[2:]
        feat8, feat16, feat32 = self.backbone_forward(x)
        H8, W8 = feat8.shape[2:]
        H16, W16 = feat16.shape[2:]
        H32, W32 = feat32.shape[2:]

        avg = F.avg_pool2d(feat32, feat32.shape[2:])
        avg = self.conv_avg(avg)
        avg_up = F.interpolate(avg, size=(H32, W32), mode='nearest')

        feat32_arm = self.arm32(feat32)
        feat32_sum = feat32_arm + avg_up
        feat32_up = F.interpolate(feat32_sum, size=(H16, W16), mode='nearest')
        feat32_up = self.conv_head32(feat32_up)

        feat16_arm = self.arm16(feat16)
        feat16_sum = feat16_arm + feat32_up
        feat16_up = F.interpolate(feat16_sum, size=(H8, W8), mode='nearest')
        feat16_up = self.conv_head16(feat16_up)

        return feat8, feat16_up, feat32_up  # x8, x8, x16
    def forward(self, x):
        x, _ = self.conv1(x)
        x = F.relu(self.bn1(x), True)
        x = F.avg_pool2d(self.conv2(x), 2, stride=2)
        x = self.conv3(x)
        x = self.conv4(x)

        outputs = []
        boundary_channels = []
        tmp_out = None
        ll, boundary_channel = self._sub_layers['m0'](x, tmp_out)
        ll = self._sub_layers['top_m_0'](ll)
        ll = F.relu(self._sub_layers['bn_end0']
                    (self._sub_layers['conv_last0'](ll)), True)

        # Predict heatmaps
        tmp_out = self._sub_layers['l0'](ll)
        if self.end_relu:
            tmp_out = F.relu(tmp_out)  # HACK: Added relu
        outputs.append(tmp_out)
        boundary_channels.append(boundary_channel)
        return outputs, boundary_channels
    def pyramid_forward(self, feat):
        each_stripe_size = int(feat.shape[2] / self.num_stripes)

        feat_list, logits_list = [], []
        idx_levels = 0
        used_branches = 0
        for idx_branches in range(self.num_branches):
            if idx_branches >= sum(self.num_in_each_level[0:idx_levels + 1]):
                idx_levels += 1
            if self.used_levels[idx_levels] == 0:
                continue
            idx_in_each_level = idx_branches - sum(
                self.num_in_each_level[0:idx_levels])
            stripe_size_in_each_level = each_stripe_size * (idx_levels + 1)
            start = idx_in_each_level * each_stripe_size
            end = start + stripe_size_in_each_level

            k = feat.shape[-1]
            local_feat_avgpool = F.avg_pool2d(
                feat[:, :, start:end, :],
                kernel_size=(stripe_size_in_each_level, k))
            local_feat_maxpool = F.max_pool2d(
                feat[:, :, start:end, :],
                kernel_size=(stripe_size_in_each_level, k))
            local_feat = local_feat_avgpool + local_feat_maxpool

            local_feat = self.pyramid_conv_list0[used_branches](local_feat)
            local_feat = paddle.reshape(local_feat,
                                        shape=[local_feat.shape[0], -1])
            feat_list.append(local_feat)

            local_logits = self.pyramid_fc_list0[used_branches](
                self.dropout_layer(local_feat))
            logits_list.append(local_logits)

            used_branches += 1

        return feat_list, logits_list
Example #29
0
    def _forward(self, x):
        branch1x1 = self.branch1x1(x)

        branch3x3 = self.branch3x3_1(x)
        branch3x3 = [
            self.branch3x3_2a(branch3x3),
            self.branch3x3_2b(branch3x3),
        ]
        branch3x3 = paddle.cat(branch3x3, 1)

        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = [
            self.branch3x3dbl_3a(branch3x3dbl),
            self.branch3x3dbl_3b(branch3x3dbl),
        ]
        branch3x3dbl = paddle.cat(branch3x3dbl, 1)

        branch_pool = F.avg_pool2d(x, kernel_size=3, stride=1, padding=1)
        branch_pool = self.branch_pool(branch_pool)

        outputs = [branch1x1, branch3x3, branch3x3dbl, branch_pool]
        return outputs
 def forward(self, x):
     x = self.model(x)
     x = F.avg_pool2d(x, 7)
     x = x.view(-1, 1024)
     x = self.fc(x)
     return x