Python chainer.functions.average_pooling_2d() Examples

The following are 30 code examples of chainer.functions.average_pooling_2d(). 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 chainer.functions , or try the search function .
Example #1
Source File: wrn1bit_cifar.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 stride,
                 binarized=False):
        super(PreResUnit1bit, self).__init__()
        self.resize_identity = (stride != 1)

        with self.init_scope():
            self.body = PreResBlock1bit(
                in_channels=in_channels,
                out_channels=out_channels,
                stride=stride,
                binarized=binarized)
            if self.resize_identity:
                self.identity_pool = partial(
                    F.average_pooling_2d,
                    ksize=3,
                    stride=2,
                    pad=1) 
Example #2
Source File: resnet50.py    From chainer-compiler with MIT License 6 votes vote down vote up
def forward(self, x, t):
        h = self.bn1(self.conv1(x))
        h = F.max_pooling_2d(F.relu(h), 3, stride=2)
        h = self.res2(h)
        h = self.res3(h)
        h = self.res4(h)
        h = self.res5(h)
        h = F.average_pooling_2d(h, 7, stride=1)
        h = self.fc(h)

        #loss = F.softmax_cross_entropy(h, t)
        loss = self.softmax_cross_entropy(h, t)
        if self.compute_accuracy:
            chainer.report({'loss': loss, 'accuracy': F.accuracy(h, np.argmax(t, axis=1))}, self)
        else:
            chainer.report({'loss': loss}, self)
        return loss 
Example #3
Source File: sknet.py    From imgclsmob with MIT License 6 votes vote down vote up
def __call__(self, x):
        y = self.branches(x)

        u = F.sum(y, axis=1)
        s = F.average_pooling_2d(u, ksize=u.shape[2:])
        z = self.fc1(s)
        w = self.fc2(z)

        batch = w.shape[0]
        w = F.reshape(w, shape=(batch, self.num_branches, self.out_channels))
        w = self.softmax(w)
        w = F.expand_dims(F.expand_dims(w, axis=3), axis=4)

        y = y * w
        y = F.sum(y, axis=1)
        return y 
Example #4
Source File: resneta.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 stride,
                 dilate=1,
                 **kwargs):
        super(ResADownBlock, self).__init__(**kwargs)
        with self.init_scope():
            # self.pool = partial(
            #     F.average_pooling_2d,
            #     ksize=(stride if dilate == 1 else 1),
            #     stride=(stride if dilate == 1 else 1))
            self.pool = partial(
                F.average_pooling_nd,
                ksize=(stride if dilate == 1 else 1),
                stride=(stride if dilate == 1 else 1),
                pad_value=None)
            self.conv = conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels,
                activation=None) 
Example #5
Source File: shakeshakeresnet_cifar.py    From imgclsmob with MIT License 6 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 stride):
        super(ShakeShakeShortcut, self).__init__()
        assert (out_channels % 2 == 0)
        mid_channels = out_channels // 2

        with self.init_scope():
            self.pool = partial(
                F.average_pooling_2d,
                ksize=1,
                stride=stride)
            self.conv1 = conv1x1(
                in_channels=in_channels,
                out_channels=mid_channels)
            self.conv2 = conv1x1(
                in_channels=in_channels,
                out_channels=mid_channels)
            self.bn = L.BatchNormalization(
                size=out_channels,
                eps=1e-5) 
Example #6
Source File: representation.py    From chainer-gqn with MIT License 5 votes vote down vote up
def __call__(self, x, v):
        out = super().__call__(x, v)
        out = cf.average_pooling_2d(out, self.r_size)
        return out 
Example #7
Source File: AveragePool2d.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        y1 = F.average_pooling_2d(x, 3)
        return y1 
Example #8
Source File: block_1d.py    From Deep_VoiceChanger with MIT License 5 votes vote down vote up
def _downsample(x):
    return F.average_pooling_2d(x, 2) 
Example #9
Source File: block.py    From Deep_VoiceChanger with MIT License 5 votes vote down vote up
def _downsample(x):
    return F.average_pooling_2d(x, 2) 
Example #10
Source File: block_1d.py    From Deep_VoiceChanger with MIT License 5 votes vote down vote up
def _downsample_frq(x):
    return F.average_pooling_2d(x, (1,2)) 
Example #11
Source File: functions.py    From Semantic-Segmentation-using-Adversarial-Networks with MIT License 5 votes vote down vote up
def global_average_pooling_2d(x, use_cudnn=True):
    """Spatial global average pooling function.

    Args:
        x (~chainer.Variable): Input variable.
        use_cudnn (bool): If ``True`` and cuDNN is enabled, then this function
            uses cuDNN as the core implementation.
    """

    return F.average_pooling_2d(x, ksize=(x.shape[2], x.shape[3]), use_cudnn=use_cudnn) 
Example #12
Source File: Resnet_with_loss.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x, t):
        h = self.bn1(self.conv1(x))
        h = F.max_pooling_2d(F.relu(h), 3, stride=2)
        h = self.res2(h)
        h = self.res3(h)
        h = self.res4(h)
        h = self.res5(h)
        h = F.average_pooling_2d(h, 7, stride=1)
        h = self.fc(h)

        loss = F.softmax_cross_entropy(h, t)
        # chainer.report({'loss': loss, 'accuracy': F.accuracy(h, t)}, self)
        return loss

# from https://github.com/chainer/chainer/blob/master/examples/imagenet/resnet50.py 
Example #13
Source File: AveragePool2d.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        y1 = F.average_pooling_2d(x, (1, 3), stride=(1, 4))
        return y1 
Example #14
Source File: AveragePool2d.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        y1 = F.average_pooling_2d(x, (3, 4), stride=1, pad=(2, 3))
        return y1 
Example #15
Source File: AveragePool2d.py    From chainer-compiler with MIT License 5 votes vote down vote up
def forward(self, x):
        y1 = F.average_pooling_2d(x, (3, 4))
        return y1


# ====================================== 
Example #16
Source File: auxiliary_links.py    From chainer-stylegan with MIT License 5 votes vote down vote up
def __call__(self, x):
        return F.average_pooling_2d(x, self.ksize, self.stride, self.pad) 
Example #17
Source File: block.py    From Deep_VoiceChanger with MIT License 5 votes vote down vote up
def _downsample_frq(x):
    return F.average_pooling_2d(x, (1,2)) 
Example #18
Source File: rescale.py    From chainer-stylegan with MIT License 5 votes vote down vote up
def downscale2x(h):
    return F.average_pooling_2d(h, 2, 2, 0) 
Example #19
Source File: sknet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 channels,
                 init_block_channels,
                 in_channels=3,
                 in_size=(224, 224),
                 classes=1000):
        super(SKNet, self).__init__()
        self.in_size = in_size
        self.classes = classes

        with self.init_scope():
            self.features = SimpleSequential()
            with self.features.init_scope():
                setattr(self.features, "init_block", ResInitBlock(
                    in_channels=in_channels,
                    out_channels=init_block_channels))
                in_channels = init_block_channels
                for i, channels_per_stage in enumerate(channels):
                    stage = SimpleSequential()
                    with stage.init_scope():
                        for j, out_channels in enumerate(channels_per_stage):
                            stride = 2 if (j == 0) and (i != 0) else 1
                            setattr(stage, "unit{}".format(j + 1), SKNetUnit(
                                in_channels=in_channels,
                                out_channels=out_channels,
                                stride=stride))
                            in_channels = out_channels
                    setattr(self.features, "stage{}".format(i + 1), stage)
                setattr(self.features, "final_pool", partial(
                    F.average_pooling_2d,
                    ksize=7,
                    stride=1))

            self.output = SimpleSequential()
            with self.output.init_scope():
                setattr(self.output, "flatten", partial(
                    F.reshape,
                    shape=(-1, in_channels)))
                setattr(self.output, "fc", L.Linear(
                    in_size=in_channels,
                    out_size=classes)) 
Example #20
Source File: sinet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        w = F.average_pooling_2d(x, ksize=x.shape[2:])
        w = self.fc1(w)
        if self.use_conv2:
            w = self.activ(w)
            w = self.fc2(w)
        w = self.sigmoid(w)
        w = F.broadcast_to(F.expand_dims(F.expand_dims(w, axis=2), axis=3), x.shape)
        x = x * w
        return x 
Example #21
Source File: condensenet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self):
        super(TransitionBlock, self).__init__()
        with self.init_scope():
            self.pool = partial(
                F.average_pooling_2d,
                ksize=2,
                stride=2,
                pad=0) 
Example #22
Source File: bisenet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        in_size = self.in_size if self.in_size is not None else x.shape[2:]
        x = F.average_pooling_2d(x, ksize=x.shape[2:])
        x = self.conv(x)
        x = self.up(x, size=in_size)
        return x 
Example #23
Source File: bisenet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        x = self.conv1(x)
        w = F.average_pooling_2d(x, ksize=x.shape[2:])
        w = self.conv2(w)
        x = x * w
        return x 
Example #24
Source File: bisenet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x, y):
        x = F.concat((x, y), axis=1)
        x = self.conv_merge(x)
        w = F.average_pooling_2d(x, ksize=x.shape[2:])
        w = self.conv1(w)
        w = self.activ(w)
        w = self.conv2(w)
        w = self.sigmoid(w)
        x_att = x * w
        x = x + x_att
        return x 
Example #25
Source File: dpn.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        batch, channels, height, width = x.shape
        x_avg = F.average_pooling_2d(x, ksize=(height, width))
        x_max = F.max_pooling_2d(x, ksize=(height, width), cover_all=False)
        x = 0.5 * (x_avg + x_max)
        return x 
Example #26
Source File: ror_cifar.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 channels,
                 init_block_channels,
                 dropout_rate=0.0,
                 in_channels=3,
                 in_size=(32, 32),
                 classes=10):
        super(CIFARRoR, self).__init__()
        self.in_size = in_size
        self.classes = classes

        with self.init_scope():
            self.features = SimpleSequential()
            with self.features.init_scope():
                setattr(self.features, "init_block", conv3x3_block(
                    in_channels=in_channels,
                    out_channels=init_block_channels))
                in_channels = init_block_channels
                setattr(self.features, "body", RoRResBody(
                    in_channels=in_channels,
                    out_channels_lists=channels,
                    dropout_rate=dropout_rate))
                in_channels = channels[-1][-1]
                setattr(self.features, "final_pool", partial(
                    F.average_pooling_2d,
                    ksize=8,
                    stride=1))

            self.output = SimpleSequential()
            with self.output.init_scope():
                setattr(self.output, "flatten", partial(
                    F.reshape,
                    shape=(-1, in_channels)))
                setattr(self.output, "fc", L.Linear(
                    in_size=in_channels,
                    out_size=classes)) 
Example #27
Source File: fishnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        x = self.bn(x)
        x = F.relu(x)
        x = F.average_pooling_2d(x, ksize=x.shape[2:])
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.sigmoid(x)
        return x 
Example #28
Source File: bamresnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __call__(self, x):
        input_shape = x.shape
        x = F.average_pooling_2d(x, ksize=x.shape[2:])
        x = F.reshape(x, shape=(x.shape[0], -1))
        x = self.init_fc(x)
        x = self.main_fcs(x)
        x = self.final_fc(x)
        x = F.broadcast_to(F.expand_dims(F.expand_dims(x, axis=2), axis=3), input_shape)
        return x 
Example #29
Source File: densenet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels):
        super(TransitionBlock, self).__init__()
        with self.init_scope():
            self.conv = pre_conv1x1_block(
                in_channels=in_channels,
                out_channels=out_channels)
            self.pool = partial(
                F.average_pooling_2d,
                ksize=2,
                stride=2,
                pad=0) 
Example #30
Source File: shufflenet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 groups,
                 downsample,
                 ignore_group):
        super(ShuffleUnit, self).__init__()
        self.downsample = downsample
        mid_channels = out_channels // 4

        if downsample:
            out_channels -= in_channels

        with self.init_scope():
            self.compress_conv1 = conv1x1(
                in_channels=in_channels,
                out_channels=mid_channels,
                groups=(1 if ignore_group else groups))
            self.compress_bn1 = L.BatchNormalization(size=mid_channels)
            self.c_shuffle = ChannelShuffle(
                channels=mid_channels,
                groups=groups)
            self.dw_conv2 = depthwise_conv3x3(
                channels=mid_channels,
                stride=(2 if self.downsample else 1))
            self.dw_bn2 = L.BatchNormalization(size=mid_channels)
            self.expand_conv3 = conv1x1(
                in_channels=mid_channels,
                out_channels=out_channels,
                groups=groups)
            self.expand_bn3 = L.BatchNormalization(size=out_channels)
            if downsample:
                self.avgpool = partial(
                    F.average_pooling_2d,
                    ksize=3,
                    stride=2,
                    pad=1)
            self.activ = F.relu