Python torch.nn.functional.avg_pool2d() Examples

The following are 30 code examples of torch.nn.functional.avg_pool2d(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module torch.nn.functional , or try the search function .
Example #1
Source File: inception.py    From fast-MPN-COV with MIT License 6 votes vote down vote up
def forward(self, x):
        branch1x1 = self.branch1x1(x)

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

        branch3x3dbl = self.branch3x3dbl_1(x)
        branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
        branch3x3dbl = [
            self.branch3x3dbl_3a(branch3x3dbl),
            self.branch3x3dbl_3b(branch3x3dbl),
        ]
        branch3x3dbl = torch.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 torch.cat(outputs, 1) 
Example #2
Source File: vision_encoders.py    From Character-Level-Language-Modeling-with-Deeper-Self-Attention-pytorch with MIT License 6 votes vote down vote up
def forward(self, x):
        with torch.set_grad_enabled(self.finetune):
            x = self.model.conv1(x)
            x = self.model.bn1(x)
            x = self.model.relu(x)
            x = self.model.maxpool(x)

            x = self.model.layer1(x)
            x = self.model.layer2(x)
            x = self.model.layer3(x)
            x = self.model.layer4(x)

        if not self.spatial_context:
            x = F.avg_pool2d(x, kernel_size=7).view(x.size(0), x.size(1))
        if hasattr(self, 'context_transform'):
            x = self.context_transform(x)
        if hasattr(self, 'context_nonlinearity'):
            x = self.context_nonlinearity(x)
        return x 
Example #3
Source File: models.py    From AdaptiveWingLoss with Apache License 2.0 6 votes vote down vote up
def _forward(self, level, inp):
        # Upper branch
        up1 = inp
        up1 = self._modules['b1_' + str(level)](up1)

        # Lower branch
        low1 = F.avg_pool2d(inp, 2, stride=2)
        low1 = self._modules['b2_' + str(level)](low1)

        if level > 1:
            low2 = self._forward(level - 1, low1)
        else:
            low2 = low1
            low2 = self._modules['b2_plus_' + str(level)](low2)

        low3 = low2
        low3 = self._modules['b3_' + str(level)](low3)

        up2 = F.upsample(low3, scale_factor=2, mode='nearest')

        return up1 + up2 
Example #4
Source File: WideResNet.py    From overhaul-distillation with MIT License 6 votes vote down vote up
def extract_feature(self, x, preReLU=False):
        out = self.conv1(x)
        feat1 = self.block1(out)
        feat2 = self.block2(feat1)
        feat3 = self.block3(feat2)
        out = self.relu(self.bn1(feat3))
        out = F.avg_pool2d(out, 8)
        out = out.view(-1, self.nChannels[3])
        out = self.fc(out)

        if preReLU:
            feat1 = self.block2.layer[0].bn1(feat1)
            feat2 = self.block3.layer[0].bn1(feat2)
            feat3 = self.bn1(feat3)

        return [feat1, feat2, feat3], out 
Example #5
Source File: pytorch_ops.py    From deep_architect with MIT License 6 votes vote down vote up
def avg_pool2d(h_kernel_size, h_stride=1):

    def compile_fn(di, dh):
        (_, _, height, width) = di['in'].size()
        padding = nn.ZeroPad2d(
            calculate_same_padding(height, width, dh['stride'],
                                   dh['kernel_size']))
        avg_pool = nn.AvgPool2d(dh['kernel_size'], stride=dh['stride'])

        def fn(di):
            x = padding(di['in'])
            return {'out': avg_pool(x)}

        return fn, [padding, avg_pool]

    return siso_pytorch_module('AvgPool2D', compile_fn, {
        'kernel_size': h_kernel_size,
        'stride': h_stride
    }) 
Example #6
Source File: fpn.py    From seamseg with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def forward(self, x):
        """ROI head module for FPN

        Parameters
        ----------
        x : torch.Tensor
            A tensor of input features with shape N x C x H x W

        Returns
        -------
        cls_logits : torch.Tensor
            A tensor of classification logits with shape S x (num_thing + 1)
        bbx_logits : torch.Tensor
            A tensor of class-specific bounding box regression logits with shape S x num_thing x 4
        """
        x = functional.avg_pool2d(x, 2)

        # Run head
        x = self.fc(x.view(x.size(0), -1))
        return self.roi_cls(x), self.roi_bbx(x).view(x.size(0), -1, 4) 
Example #7
Source File: inception.py    From fast-MPN-COV with MIT License 6 votes vote down vote up
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 torch.cat(outputs, 1) 
Example #8
Source File: models.py    From VTuber_Unity with MIT License 6 votes vote down vote up
def _forward(self, level, inp):
        # Upper branch
        up1 = inp
        up1 = self._modules['b1_' + str(level)](up1)

        # Lower branch
        low1 = F.avg_pool2d(inp, 2, stride=2)
        low1 = self._modules['b2_' + str(level)](low1)

        if level > 1:
            low2 = self._forward(level - 1, low1)
        else:
            low2 = low1
            low2 = self._modules['b2_plus_' + str(level)](low2)

        low3 = low2
        low3 = self._modules['b3_' + str(level)](low3)

        up2 = F.interpolate(low3, scale_factor=2, mode='nearest')

        return up1 + up2 
Example #9
Source File: models.py    From AdaptiveWingLoss with Apache License 2.0 5 votes vote down vote up
def forward(self, x):
        x, _ = self.conv1(x)
        x = F.relu(self.bn1(x), True)
        # x = F.relu(self.bn1(self.conv1(x)), True)
        x = F.avg_pool2d(self.conv2(x), 2, stride=2)
        x = self.conv3(x)
        x = self.conv4(x)

        previous = x

        outputs = []
        boundary_channels = []
        tmp_out = None
        for i in range(self.num_modules):
            hg, boundary_channel = self._modules['m' + str(i)](previous,
                                                               tmp_out)

            ll = hg
            ll = self._modules['top_m_' + str(i)](ll)

            ll = F.relu(self._modules['bn_end' + str(i)]
                        (self._modules['conv_last' + str(i)](ll)), True)

            # Predict heatmaps
            tmp_out = self._modules['l' + str(i)](ll)
            if self.end_relu:
                tmp_out = F.relu(tmp_out) # HACK: Added relu
            outputs.append(tmp_out)
            boundary_channels.append(boundary_channel)

            if i < self.num_modules - 1:
                ll = self._modules['bl' + str(i)](ll)
                tmp_out_ = self._modules['al' + str(i)](tmp_out)
                previous = previous + ll + tmp_out_

        return outputs, boundary_channels 
Example #10
Source File: downsampler.py    From BMSG-GAN with MIT License 5 votes vote down vote up
def main(args):
    """
    Main function for the script
    :param args: parsed command line arguments
    :return: None
    """

    img_file = os.path.join(args.image_path)
    if args.npz_file:
        img = np.load(img_file)
    else:
        img = np.array(Image.open(img_file))
        img = np.expand_dims(img, axis=0).transpose(0, 3, 1, 2)

    img = th.from_numpy(img).float()  # make a tensor out of image

    # progressively downsample the image and save it at the given location
    dim = img.shape[-1]  # gives the highest dimension
    ds_img = img  # start from the highest resolution image
    images = [ds_img]  # original image already in
    while dim > 4:
        ds_img = avg_pool2d(ds_img, kernel_size=2, stride=2)
        images.append(ds_img)
        dim //= 2

    images = progressive_upscaling(list(reversed(images)))

    # save the images:
    for count in range(len(images)):
        imsave(os.path.join(args.out_dir, str(count + 1) + ".png"),
               images[count].squeeze(0).permute(1, 2, 0)) 
Example #11
Source File: models.py    From VTuber_Unity with MIT License 5 votes vote down vote up
def forward(self, x):
        x = F.relu(self.bn1(self.conv1(x)), True)
        x = F.avg_pool2d(self.conv2(x), 2, stride=2)
        x = self.conv3(x)
        x = self.conv4(x)

        previous = x

        outputs = []
        for i in range(self.num_modules):
            hg = self._modules['m' + str(i)](previous)

            ll = hg
            ll = self._modules['top_m_' + str(i)](ll)

            ll = F.relu(self._modules['bn_end' + str(i)]
                        (self._modules['conv_last' + str(i)](ll)), True)

            # Predict heatmaps
            tmp_out = self._modules['l' + str(i)](ll)
            outputs.append(tmp_out)

            if i < self.num_modules - 1:
                ll = self._modules['bl' + str(i)](ll)
                tmp_out_ = self._modules['al' + str(i)](tmp_out)
                previous += ll + tmp_out_

        return outputs 
Example #12
Source File: densenet.py    From pneumothorax-segmentation with MIT License 5 votes vote down vote up
def forward(self, x):
        features = self.features(x)
        out = F.relu(features, inplace=True)
        out = F.avg_pool2d(out, kernel_size=7, stride=1).view(features.size(0), -1)
        out = self.classifier(out)
        return out 
Example #13
Source File: fpn.py    From seamseg with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def _global_pooling(self, x):
            pooling_size = (min(try_index(self.pooling_size, 0), x.shape[2]),
                            min(try_index(self.pooling_size, 1), x.shape[3]))
            padding = (
                (pooling_size[1] - 1) // 2,
                (pooling_size[1] - 1) // 2 if pooling_size[1] % 2 == 1 else (pooling_size[1] - 1) // 2 + 1,
                (pooling_size[0] - 1) // 2,
                (pooling_size[0] - 1) // 2 if pooling_size[0] % 2 == 1 else (pooling_size[0] - 1) // 2 + 1
            )

            pool = functional.avg_pool2d(x, pooling_size, stride=1)
            pool = functional.pad(pool, pad=padding, mode="replicate")
            return pool 
Example #14
Source File: fpn.py    From seamseg with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
def forward(self, x, do_cls_bbx=True, do_msk=True):
        """ROI head module for FPN

        Parameters
        ----------
        x : torch.Tensor
            A tensor of input features with shape N x C x H x W
        do_cls_bbx : bool
            Whether to compute or not the class and bounding box regression predictions
        do_msk : bool
            Whether to compute or not the mask predictions

        Returns
        -------
        cls_logits : torch.Tensor
            A tensor of classification logits with shape S x (num_thing + 1)
        bbx_logits : torch.Tensor
            A tensor of class-specific bounding box regression logits with shape S x num_thing x 4
        msk_logits : torch.Tensor
            A tensor of class-specific mask logits with shape S x num_thing x (H_roi * 2) x (W_roi * 2)
        """
        # Run fully-connected head
        if do_cls_bbx:
            x_fc = functional.avg_pool2d(x, 2)
            x_fc = self.fc(x_fc.view(x_fc.size(0), -1))

            cls_logits = self.roi_cls(x_fc)
            bbx_logits = self.roi_bbx(x_fc).view(x_fc.size(0), -1, 4)
        else:
            cls_logits = None
            bbx_logits = None

        # Run convolutional head
        if do_msk:
            x = self.conv(x)
            msk_logits = self.roi_msk(x)
        else:
            msk_logits = None

        return cls_logits, bbx_logits, msk_logits 
Example #15
Source File: pytorch_ops.py    From deep_architect with MIT License 5 votes vote down vote up
def global_pool2d():

    def compile_fn(di, dh):
        (_, _, height, width) = di['in'].size()

        def fn(di):
            x = F.avg_pool2d(di['in'], (height, width))
            x = torch.squeeze(x, 2)
            return {'out': torch.squeeze(x, 2)}

        return fn, []

    return siso_pytorch_module('GlobalAveragePool', compile_fn, {}) 
Example #16
Source File: HRFPN.py    From Parsing-R-CNN with MIT License 5 votes vote down vote up
def __init__(self, dim_in, spatial_scale):
        super().__init__()
        self.dim_in = sum(dim_in)
        self.spatial_scale = spatial_scale

        hrfpn_dim = cfg.FPN.HRFPN.DIM  # 256
        use_lite = cfg.FPN.HRFPN.USE_LITE
        use_bn = cfg.FPN.HRFPN.USE_BN
        use_gn = cfg.FPN.HRFPN.USE_GN
        if cfg.FPN.HRFPN.POOLING_TYPE == 'AVG':
            self.pooling = F.avg_pool2d
        else:
            self.pooling = F.max_pool2d
        self.num_extra_pooling = cfg.FPN.HRFPN.NUM_EXTRA_POOLING    # 1
        self.num_output = len(dim_in) + self.num_extra_pooling  # 5

        self.reduction_conv = make_conv(self.dim_in, hrfpn_dim, kernel=1, use_bn=use_bn, use_gn=use_gn)
        self.dim_in = hrfpn_dim

        self.fpn_conv = nn.ModuleList()
        for i in range(self.num_output):
            self.fpn_conv.append(
                make_conv(self.dim_in, hrfpn_dim, kernel=3, use_dwconv=use_lite, use_bn=use_bn, use_gn=use_gn,
                          suffix_1x1=use_lite)
            )
            self.dim_in = hrfpn_dim

        if self.num_extra_pooling:
            self.spatial_scale.append(self.spatial_scale[-1] * 0.5)
        self.dim_out = [self.dim_in for _ in range(self.num_output)]
        self._init_weights() 
Example #17
Source File: dpn.py    From pneumothorax-segmentation with MIT License 5 votes vote down vote up
def logits(self, features):
        if not self.training and self.test_time_pool:
            x = F.avg_pool2d(features, kernel_size=7, stride=1)
            out = self.classifier(x)
            # The extra test time pool should be pooling an img_size//32 - 6 size patch
            out = adaptive_avgmax_pool2d(out, pool_type='avgmax')
        else:
            x = adaptive_avgmax_pool2d(features, pool_type='avg')
            out = self.classifier(x)
        return out.view(out.size(0), -1) 
Example #18
Source File: wide_resnet.py    From dfw with MIT License 5 votes vote down vote up
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 = out.view(-1, self.nChannels)
        return self.fc(out) 
Example #19
Source File: densenet.py    From dfw with MIT License 5 votes vote down vote up
def forward(self, x):
        out = self.conv1(x)
        out = self.trans1(self.block1(out))
        out = self.trans2(self.block2(out))
        out = self.block3(out)
        out = self.relu(self.bn1(out))
        out = F.avg_pool2d(out, 8)
        out = out.view(-1, self.in_planes)
        return self.fc(out) 
Example #20
Source File: densenet.py    From dfw with MIT License 5 votes vote down vote up
def forward(self, x):
        out = self.conv1(self.relu(self.bn1(x)))
        if self.droprate > 0:
            out = F.dropout(out, p=self.droprate, inplace=False,
                            training=self.training)
        return F.avg_pool2d(out, 2) 
Example #21
Source File: rnn_aux_skip_attention.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        if self.scale_factor >= 2:
            x = F.avg_pool2d(x, self.scale_factor)
        x = self.conv(x)
        x = self.bn(x)
        x = self.relu(x)
        return x 
Example #22
Source File: train_l2t_ww.py    From L2T-ww with MIT License 5 votes vote down vote up
def forward(self, source_features):
        outputs = []
        for i, (idx, _) in enumerate(self.pairs):
            f = source_features[idx]
            f = F.avg_pool2d(f, f.size(2)).view(-1, f.size(1))
            outputs.append(F.softmax(self[i](f), 1))
        return outputs 
Example #23
Source File: WideResNet.py    From overhaul-distillation with MIT License 5 votes vote down vote up
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 = out.view(-1, self.nChannels[3])
        return self.fc(out) 
Example #24
Source File: simple_kaggle.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = F.avg_pool2d(x, 2)
        return x 
Example #25
Source File: simple_attention.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = F.avg_pool2d(x, 2)
        return x 
Example #26
Source File: skip_attention.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = F.avg_pool2d(x, 2)
        return x 
Example #27
Source File: wideresnet.py    From JEM with Apache License 2.0 5 votes vote down vote up
def forward(self, x, vx=None):
        out = self.conv1(x)
        out = self.layer1(out)
        out = self.layer2(out)
        out = self.layer3(out)
        out = self.lrelu(self.bn1(out))
        if self.sum_pool:
            out = out.view(out.size(0), out.size(1), -1).sum(2)
        else:
            out = F.avg_pool2d(out, 8)
        out = out.view(out.size(0), -1)
        return out 
Example #28
Source File: aux_skip_attention.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        if self.scale_factor >= 2:
            x = F.avg_pool2d(x, self.scale_factor)
        x = self.conv(x)
        x = self.bn(x)
        x = self.relu(x)
        return x 
Example #29
Source File: rnn_aux_skip_attention.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = F.avg_pool2d(x, 2)
        return x 
Example #30
Source File: aux_skip_attention.py    From argus-freesound with MIT License 5 votes vote down vote up
def forward(self, x):
        x = self.conv1(x)
        x = self.conv2(x)
        x = F.avg_pool2d(x, 2)
        return x