Python mxnet.gluon.nn.Conv2DTranspose() Examples

The following are 30 code examples of mxnet.gluon.nn.Conv2DTranspose(). 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 mxnet.gluon.nn , or try the search function .
Example #1
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_deconv2d_16c():
    in_chn_list = [1024, 512, 256, 128, 64, 32, 16]
    out_chn_list = [512, 256, 128, 64, 32, 16, 3]
    kernel_list = [1, 3, 5, 7]
    in_shape = [4, 8, 16, 32, 64, 224]
    batch_size = 4
    class Net(gluon.HybridBlock):
        def __init__(self, chn_num, kernel, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.deconv0 = gluon.nn.Conv2DTranspose(chn_num, (kernel, kernel))

        def hybrid_forward(self, F, x):
            out = self.deconv0(x)
            return out
    for i in range(len(in_shape)):
        x = mx.nd.random.uniform(-1.0, 1.0, shape=(batch_size, in_chn_list[i], in_shape[i], in_shape[i]))
        for j in range(len(kernel_list)):
            net = Net(out_chn_list[i], kernel_list[j])
            check_layer_forward_withinput(net, x) 
Example #2
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_reshape_deconv():
    class Net(gluon.HybridBlock):
        def __init__(self, shape, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.reshape = shape
                self.conv0 = nn.Conv2DTranspose(64, (3, 3))

        def hybrid_forward(self, F, x):
            x_reshape = x.reshape(self.reshape)
            out = self.conv0(x_reshape)
            return out
    x = mx.nd.random.uniform(shape=(4, 16, 32, 32))
    shape = (4, 16, 64, -1)
    net = Net(shape)
    check_layer_forward_withinput(net, x) 
Example #3
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_slice_deconv():
    class Net(gluon.HybridBlock):
        def __init__(self, slice, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.slice = slice
                self.conv0 = nn.Conv2DTranspose(64, (3, 3))

        def hybrid_forward(self, F, x):
            x_slice = x.slice(begin=self.slice[0], end=self.slice[1])
            out = self.conv0(x_slice)
            return out
    x = mx.nd.random.uniform(shape=(8, 32, 64, 64))
    slice = [(0, 16, 0, 0), (4, 32, 32, 32)]
    net = Net(slice)
    check_layer_forward_withinput(net, x) 
Example #4
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_reshape_deconv_reshape_deconv():
    class Net(gluon.HybridBlock):
        def __init__(self, shape, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.reshape = shape
                self.conv0 = nn.Conv2DTranspose(32, (3, 3))
                self.conv1 = nn.Conv2DTranspose(64, (3, 3), strides=(2, 2))

        def hybrid_forward(self, F, x):
            x_reshape = x.reshape(self.reshape[0])
            y = self.conv0(x_reshape)
            "shape of y is (4, 32, 66, 18)"
            y_reshape = y.reshape(self.reshape[1])
            out = self.conv1(y_reshape)
            return out
    x = mx.nd.random.uniform(shape=(4, 16, 32, 32))
    shape = [(4, 16, 64, -1), (4, 32, 33, -1)]
    net = Net(shape)
    check_layer_forward_withinput(net, x) 
Example #5
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_reshape_deconv_slice_deconv():
    class Net(gluon.HybridBlock):
        def __init__(self, shape, slice, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.reshape = shape
                self.slice = slice
                self.conv0 = nn.Conv2DTranspose(32, (3, 3))
                self.conv1 = nn.Conv2DTranspose(64, (3, 3), strides=(2, 2))

        def hybrid_forward(self, F, x):
            x_reshape = x.reshape(self.reshape)
            y = self.conv0(x_reshape)
            "shape of y is (4, 32, 66, 18)"
            y_slice = y.slice(begin=self.slice[0], end=self.slice[1])
            out = self.conv1(y_slice)
            return out
    x = mx.nd.random.uniform(shape=(4, 16, 32, 32))
    shape = (4, 16, 64, -1)
    slice = [(0, 0, 0, 0), (2, 16, 16, 16)]
    net = Net(shape, slice)
    check_layer_forward_withinput(net, x) 
Example #6
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 6 votes vote down vote up
def test_slice_deconv_reshape_deconv():
    class Net(gluon.HybridBlock):
        def __init__(self, shape, slice, **kwargs):
            super(Net, self).__init__(**kwargs)
            with self.name_scope():
                self.reshape = shape
                self.slice = slice
                self.conv0 = nn.Conv2DTranspose(32, (3, 3))
                self.conv1 = nn.Conv2DTranspose(96, (3, 3), strides=(2, 2))

        def hybrid_forward(self, F, x):
            x_slice = x.slice(begin=self.slice[0], end=self.slice[1])
            y = self.conv0(x_slice)
            "shape of y is (4, 32, 34, 34)"
            y_reshape = y.reshape(self.reshape)
            out = self.conv1(y_reshape)
            return out
    x = mx.nd.random.uniform(shape=(8, 32, 64, 64))
    shape = (4, 64, 34, -1)
    slice = [(4, 0, 0, 0), (8, 16, 32, 32)]
    net = Net(shape, slice)
    check_layer_forward_withinput(net, x) 
Example #7
Source File: mask_rcnn.py    From panoptic-fpn-gluon with Apache License 2.0 6 votes vote down vote up
def __init__(self, batch_images, classes, mask_channels, deep_fcn=False, **kwargs):
        super(Mask, self).__init__(**kwargs)
        self._batch_images = batch_images
        self.classes = classes
        init = mx.init.Xavier(rnd_type='gaussian', factor_type='out', magnitude=2)
        with self.name_scope():
            if deep_fcn:
                self.deconv = nn.HybridSequential()
                for _ in range(4):
                    self.deconv.add(
                        nn.Conv2D(mask_channels, kernel_size=(3, 3), strides=(1, 1), padding=(1, 1),
                                  weight_initializer=init),
                        nn.Activation('relu'))
                self.deconv.add(
                    nn.Conv2DTranspose(mask_channels, kernel_size=(2, 2), strides=(2, 2),
                                       padding=(0, 0), weight_initializer=init))
            else:
                self.deconv = nn.Conv2DTranspose(mask_channels, kernel_size=(2, 2), strides=(2, 2),
                                                 padding=(0, 0), weight_initializer=init)
            self.mask = nn.Conv2D(len(classes), kernel_size=(1, 1), strides=(1, 1), padding=(0, 0),
                                  weight_initializer=init)

    # pylint: disable=arguments-differ 
Example #8
Source File: test_gluon.py    From SNIPER-mxnet with Apache License 2.0 5 votes vote down vote up
def test_deconv():
    # layers1d = [
    #     nn.Conv1DTranspose(16, 3, in_channels=4),
    #     nn.Conv1DTranspose(16, 3, groups=2, in_channels=4),
    #     nn.Conv1DTranspose(16, 3, strides=3, groups=2, in_channels=4),
    #     ]
    # for layer in layers1d:
    #     check_layer_forward(layer, (1, 4, 10))


    layers2d = [
        nn.Conv2DTranspose(16, (3, 4), in_channels=4),
        nn.Conv2DTranspose(16, (5, 4), in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), groups=2, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), strides=4, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), dilation=4, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), padding=4, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), strides=4, output_padding=3, in_channels=4),
        ]
    for layer in layers2d:
        check_layer_forward(layer, (1, 4, 20, 20))


    # layers3d = [
    #     nn.Conv3DTranspose(16, (1, 8, 4), in_channels=4),
    #     nn.Conv3DTranspose(16, (5, 4, 3), in_channels=4),
    #     nn.Conv3DTranspose(16, (3, 3, 3), groups=2, in_channels=4),
    #     nn.Conv3DTranspose(16, 4, strides=4, in_channels=4),
    #     nn.Conv3DTranspose(16, (3, 3, 3), padding=4, in_channels=4),
    #     ]
    # for layer in layers3d:
    #     check_layer_forward(layer, (1, 4, 10, 10, 10))
    #
    #
    # layer = nn.Conv2DTranspose(16, (3, 3), layout='NHWC', in_channels=4)
    # # check_layer_forward(layer, (1, 10, 10, 4))
    #
    # layer = nn.Conv3DTranspose(16, (3, 3, 3), layout='NDHWC', in_channels=4)
    # # check_layer_forward(layer, (1, 10, 10, 10, 4)) 
Example #9
Source File: test_gluon.py    From dynamic-training-with-apache-mxnet-on-aws with Apache License 2.0 5 votes vote down vote up
def test_deconv():
    # layers1d = [
    #     nn.Conv1DTranspose(16, 3, in_channels=4),
    #     nn.Conv1DTranspose(16, 3, groups=2, in_channels=4),
    #     nn.Conv1DTranspose(16, 3, strides=3, groups=2, in_channels=4),
    #     ]
    # for layer in layers1d:
    #     check_layer_forward(layer, (1, 4, 10))


    layers2d = [
        nn.Conv2DTranspose(16, (3, 4), in_channels=4),
        nn.Conv2DTranspose(16, (5, 4), in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), groups=2, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), strides=4, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), dilation=4, in_channels=4),
    #   nn.Conv2DTranspose(16, (3, 4), padding=4, in_channels=4),
        nn.Conv2DTranspose(16, (3, 4), strides=4, output_padding=3, in_channels=4),
        ]
    for layer in layers2d:
        check_layer_forward(layer, (1, 4, 20, 20))


    # layers3d = [
    #     nn.Conv3DTranspose(16, (1, 8, 4), in_channels=4),
    #     nn.Conv3DTranspose(16, (5, 4, 3), in_channels=4),
    #     nn.Conv3DTranspose(16, (3, 3, 3), groups=2, in_channels=4),
    #     nn.Conv3DTranspose(16, 4, strides=4, in_channels=4),
    #     nn.Conv3DTranspose(16, (3, 3, 3), padding=4, in_channels=4),
    #     ]
    # for layer in layers3d:
    #     check_layer_forward(layer, (1, 4, 10, 10, 10))
    #
    #
    # layer = nn.Conv2DTranspose(16, (3, 3), layout='NHWC', in_channels=4)
    # # check_layer_forward(layer, (1, 10, 10, 4))
    #
    # layer = nn.Conv3DTranspose(16, (3, 3, 3), layout='NDHWC', in_channels=4)
    # # check_layer_forward(layer, (1, 10, 10, 10, 4)) 
Example #10
Source File: simple_pose_resnet.py    From panoptic-fpn-gluon with Apache License 2.0 5 votes vote down vote up
def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
        assert num_layers == len(num_filters), \
            'ERROR: num_deconv_layers is different from len(num_deconv_filters)'
        assert num_layers == len(num_kernels), \
            'ERROR: num_deconv_layers is different from len(num_deconv_filters)'

        layer = nn.HybridSequential(prefix='')
        with layer.name_scope():
            for i in range(num_layers):
                kernel, padding, output_padding = \
                    self._get_deconv_cfg(num_kernels[i])

                planes = num_filters[i]
                layer.add(
                    nn.Conv2DTranspose(
                        channels=planes,
                        kernel_size=kernel,
                        strides=2,
                        padding=padding,
                        output_padding=output_padding,
                        use_bias=self.deconv_with_bias,
                        weight_initializer=initializer.Normal(0.001),
                        bias_initializer=initializer.Zero()))
                layer.add(gcv.nn.BatchNormCudnnOff(gamma_initializer=initializer.One(),
                                                   beta_initializer=initializer.Zero()))
                layer.add(nn.Activation('relu'))
                self.inplanes = planes

        return layer 
Example #11
Source File: train_cgan.py    From panoptic-fpn-gluon with Apache License 2.0 5 votes vote down vote up
def __init__(self, output_nc, ngf=64, use_dropout=False, n_blocks=6, padding_type='reflect'):
        assert(n_blocks >= 0)
        super(ResnetGenerator, self).__init__()
        self.output_nc = output_nc
        self.ngf = ngf
        self.model = nn.HybridSequential()
        with self.name_scope():
            self.model.add(
                nn.ReflectionPad2D(3),
                nn.Conv2D(ngf, kernel_size=7, padding=0),
                nn.InstanceNorm(),
                nn.Activation('relu')
            )

            n_downsampling = 2
            for i in range(n_downsampling):
                mult = 2**i
                self.model.add(
                    nn.Conv2D(ngf * mult * 2, kernel_size=3,strides=2, padding=1),
                    nn.InstanceNorm(),
                    nn.Activation('relu')
                )

            mult = 2**n_downsampling
            for i in range(n_blocks):
                self.model.add(
                    ResnetBlock(ngf * mult, padding_type=padding_type, use_dropout=use_dropout)
                )

            for i in range(n_downsampling):
                mult = 2**(n_downsampling - i)
                self.model.add(
                    nn.Conv2DTranspose(int(ngf * mult / 2),kernel_size=3,strides=2,padding=1,output_padding=1),
                    nn.InstanceNorm(),
                    nn.Activation('relu')
                )
            self.model.add(
                nn.ReflectionPad2D(3),
                nn.Conv2D(output_nc,kernel_size=7,padding=0),
                nn.Activation('tanh')
            ) 
Example #12
Source File: train_wgan.py    From panoptic-fpn-gluon with Apache License 2.0 5 votes vote down vote up
def __init__(self, isize, nz, nc, ngf, ngpu, n_extra_layers=0):
        super(DCGAN_G_nobn, self).__init__()
        self.ngpu = ngpu
        assert isize % 16 == 0, "isize has to be a multiple of 16"

        cngf, tisize = ngf // 2, 4
        while tisize != isize:
            cngf = cngf * 2
            tisize = tisize * 2
        with self.name_scope():
            main = nn.Sequential()
            main.add(
                nn.Conv2DTranspose(in_channels=nz, channels=cngf, kernel_size=4, strides=1, padding=0, use_bias=False,
                                   activation='relu', prefix='initial.{0}-{1}.convt'.format(nz, cngf)))

            csize, cndf = 4, cngf
            while csize < isize // 2:
                main.add(nn.Conv2DTranspose(in_channels=cngf, channels=cngf // 2, kernel_size=4, strides=2, padding=1,
                                            use_bias=False, activation='relu',
                                            prefix='pyramid.{0}-{1}.convt'.format(cngf, cngf // 2)))

                cngf = cngf // 2
                csize = csize * 2

            # Extra layers
            for t in range(n_extra_layers):
                main.add(nn.Conv2D(in_channels=cngf, channels=cngf, kernel_size=3, strides=1, padding=1, use_bias=False,
                                   activation='relu', prefix='extra-layers-{0}.{1}.conv'.format(t, cngf)))

            main.add(
                nn.Conv2DTranspose(in_channels=cngf, channels=nc, kernel_size=4, strides=2, padding=1, use_bias=False,
                                   activation='tanh', prefix='final.{0}-{1}.convt'.format(cngf, nc)))

        self.main = main 
Example #13
Source File: train_wgan.py    From panoptic-fpn-gluon with Apache License 2.0 5 votes vote down vote up
def __init__(self, isize, nz, nc, ngf, ngpu, n_extra_layers=0):
        super(DCGAN_G, self).__init__()
        self.ngpu = ngpu
        assert isize % 16 == 0, "isize has to be a multiple of 16"

        cngf, tisize = ngf // 2, 4
        while tisize != isize:
            cngf = cngf * 2
            tisize = tisize * 2
        with self.name_scope():
            main = nn.Sequential()
            # input is Z, going into a convolution
            main.add(
                nn.Conv2DTranspose(in_channels=nz, channels=cngf, kernel_size=4, strides=1, padding=0, use_bias=False,
                                   prefix='initial.{0}-{1}.convt'.format(nz, cngf)))
            main.add(nn.BatchNorm(in_channels=cngf, prefix='initial.{0}.batchnorm'.format(cngf)))
            main.add(nn.LeakyReLU(0, prefix='initial.{0}.relu'.format(cngf)))

            csize, cndf = 4, cngf
            while csize < isize // 2:
                main.add(nn.Conv2DTranspose(in_channels=cngf, channels=cngf // 2, kernel_size=4, strides=2, padding=1,
                                            use_bias=False, prefix='pyramid.{0}-{1}.convt'.format(cngf, cngf // 2)))
                main.add(nn.BatchNorm(in_channels=cngf // 2, prefix='pyramid.{0}.batchnorm'.format(cngf // 2)))
                main.add(nn.LeakyReLU(0, prefix='pyramid.{0}.relu'.format(cngf // 2)))
                cngf = cngf // 2
                csize = csize * 2

            # Extra layers
            for t in range(n_extra_layers):
                main.add(nn.Conv2D(in_channels=cngf, channels=cngf, kernel_size=3, strides=1, padding=1, use_bias=False,
                                   prefix='extra-layers-{0}.{1}.conv'.format(t, cngf)))
                main.add(nn.BatchNorm(in_channels=cngf, prefix='extra-layers-{0}.{1}.batchnorm'.format(t, cngf)))
                main.add(nn.LeakyReLU(0, prefix='extra-layers-{0}.{1}.relu'.format(t, cngf)))

            main.add(
                nn.Conv2DTranspose(in_channels=cngf, channels=nc, kernel_size=4, strides=2, padding=1, use_bias=False,
                                   activation='tanh', prefix='final.{0}-{1}.convt'.format(cngf, nc)))

        self.main = main 
Example #14
Source File: dcgan.py    From training_results_v0.6 with Apache License 2.0 5 votes vote down vote up
def get_netG():
    # build the generator
    netG = nn.Sequential()
    with netG.name_scope():
        # input is Z, going into a convolution
        netG.add(nn.Conv2DTranspose(ngf * 8, 4, 1, 0, use_bias=False))
        netG.add(nn.BatchNorm())
        netG.add(nn.Activation('relu'))
        # state size. (ngf*8) x 4 x 4
        netG.add(nn.Conv2DTranspose(ngf * 4, 4, 2, 1, use_bias=False))
        netG.add(nn.BatchNorm())
        netG.add(nn.Activation('relu'))
        # state size. (ngf*4) x 8 x 8
        netG.add(nn.Conv2DTranspose(ngf * 2, 4, 2, 1, use_bias=False))
        netG.add(nn.BatchNorm())
        netG.add(nn.Activation('relu'))
        # state size. (ngf*2) x 16 x 16
        netG.add(nn.Conv2DTranspose(ngf, 4, 2, 1, use_bias=False))
        netG.add(nn.BatchNorm())
        netG.add(nn.Activation('relu'))
        # state size. (ngf) x 32 x 32
        netG.add(nn.Conv2DTranspose(nc, 4, 2, 1, use_bias=False))
        netG.add(nn.Activation('tanh'))
        # state size. (nc) x 64 x 64

    return netG 
Example #15
Source File: common.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 in_channels,
                 out_channels,
                 kernel_size,
                 strides,
                 padding,
                 out_padding=0,
                 dilation=1,
                 groups=1,
                 use_bias=False,
                 use_bn=True,
                 bn_epsilon=1e-5,
                 bn_use_global_stats=False,
                 bn_cudnn_off=False,
                 activation=(lambda: nn.Activation("relu")),
                 **kwargs):
        super(DeconvBlock, self).__init__(**kwargs)
        self.activate = (activation is not None)
        self.use_bn = use_bn

        with self.name_scope():
            self.conv = nn.Conv2DTranspose(
                channels=out_channels,
                kernel_size=kernel_size,
                strides=strides,
                padding=padding,
                output_padding=out_padding,
                dilation=dilation,
                groups=groups,
                use_bias=use_bias,
                in_channels=in_channels)
            if self.use_bn:
                self.bn = BatchNormExtra(
                    in_channels=out_channels,
                    epsilon=bn_epsilon,
                    use_global_stats=bn_use_global_stats,
                    cudnn_off=bn_cudnn_off)
            if self.activate:
                self.activ = get_activation_layer(activation) 
Example #16
Source File: oth_centernet2.py    From imgclsmob with MIT License 5 votes vote down vote up
def _make_deconv_layer(self, num_filters, num_kernels):
        """Make deconv layers using the configs"""
        assert len(num_kernels) == len(num_filters), \
            'Deconv filters and kernels number mismatch: {} vs. {}'.format(
                len(num_filters), len(num_kernels))

        layers = nn.HybridSequential('deconv_')
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            self.base_network.initialize()
        in_planes = self.base_network(mx.nd.zeros((1, 3, 256, 256))).shape[1]
        for planes, k in zip(num_filters, num_kernels):
            kernel, padding, output_padding = self._get_deconv_cfg(k)
            layers.add(nn.Conv2D(channels=planes,
                                 kernel_size=3,
                                 strides=1,
                                 padding=1,
                                 in_channels=in_planes))
            layers.add(nn.BatchNorm())
            layers.add(nn.Activation('relu'))
            layers.add(nn.Conv2DTranspose(channels=planes,
                                          kernel_size=kernel,
                                          strides=2,
                                          padding=padding,
                                          output_padding=output_padding,
                                          use_bias=False,
                                          in_channels=planes,
                                          weight_initializer=BilinearUpSampleInitializer()))
            layers.add(nn.BatchNorm())
            layers.add(nn.Activation('relu'))
            in_planes = planes

        return layers 
Example #17
Source File: oth_simple_pose_resnet.py    From imgclsmob with MIT License 5 votes vote down vote up
def _make_deconv_layer(self,
                           num_layers,
                           num_filters,
                           num_kernels):
        assert num_layers == len(num_filters), \
            'ERROR: num_deconv_layers is different from len(num_deconv_filters)'
        assert num_layers == len(num_kernels), \
            'ERROR: num_deconv_layers is different from len(num_deconv_filters)'

        layer = nn.HybridSequential(prefix='')
        with layer.name_scope():
            for i in range(num_layers):
                kernel, padding, output_padding = \
                    self._get_deconv_cfg(num_kernels[i])

                planes = num_filters[i]
                layer.add(
                    nn.Conv2DTranspose(
                        channels=planes,
                        kernel_size=kernel,
                        strides=2,
                        padding=padding,
                        output_padding=output_padding,
                        use_bias=self.deconv_with_bias,
                        weight_initializer=initializer.Normal(0.001),
                        bias_initializer=initializer.Zero()))
                layer.add(gcv.nn.BatchNormCudnnOff(gamma_initializer=initializer.One(),
                                                   beta_initializer=initializer.Zero()))
                layer.add(nn.Activation('relu'))
                self.inplanes = planes

        return layer 
Example #18
Source File: sinet.py    From imgclsmob with MIT License 5 votes vote down vote up
def __init__(self,
                 dim2,
                 classes,
                 out_size,
                 bn_epsilon,
                 **kwargs):
        super(SBDecoder, self).__init__(**kwargs)
        with self.name_scope():
            self.decode1 = SBDecodeBlock(
                channels=classes,
                out_size=((out_size[0] // 8, out_size[1] // 8) if out_size else None),
                bn_epsilon=bn_epsilon)
            self.decode2 = SBDecodeBlock(
                channels=classes,
                out_size=((out_size[0] // 4, out_size[1] // 4) if out_size else None),
                bn_epsilon=bn_epsilon)
            self.conv3c = conv1x1_block(
                in_channels=dim2,
                out_channels=classes,
                bn_epsilon=bn_epsilon,
                activation=(lambda: PReLU2(classes)))
            self.output = nn.Conv2DTranspose(
                channels=classes,
                kernel_size=2,
                strides=2,
                padding=0,
                output_padding=0,
                in_channels=classes,
                use_bias=False)
            self.up = InterpolationBlock(
                scale_factor=2,
                out_size=out_size) 
Example #19
Source File: mask_rcnn.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def __init__(self, batch_images, classes, mask_channels, num_fcn_convs=0, norm_layer=None,
                 norm_kwargs=None, **kwargs):
        super(Mask, self).__init__(**kwargs)
        self._batch_images = batch_images
        self.classes = classes
        init = mx.init.Xavier(rnd_type='gaussian', factor_type='out', magnitude=2)
        with self.name_scope():
            if num_fcn_convs > 0:
                self.deconv = nn.HybridSequential()
                for _ in range(num_fcn_convs):
                    self.deconv.add(
                        nn.Conv2D(mask_channels, kernel_size=(3, 3), strides=(1, 1),
                                  padding=(1, 1), weight_initializer=init))
                    if norm_layer is not None and norm_layer is SyncBatchNorm:
                        self.deconv.add(norm_layer(**norm_kwargs))
                    self.deconv.add(nn.Activation('relu'))
                self.deconv.add(
                    nn.Conv2DTranspose(mask_channels, kernel_size=(2, 2), strides=(2, 2),
                                       padding=(0, 0), weight_initializer=init))
                if norm_layer is not None and norm_layer is SyncBatchNorm:
                    self.deconv.add(norm_layer(**norm_kwargs))
            else:
                # this is for compatibility of older models.
                self.deconv = nn.Conv2DTranspose(mask_channels, kernel_size=(2, 2), strides=(2, 2),
                                                 padding=(0, 0), weight_initializer=init)
            self.mask = nn.Conv2D(len(classes), kernel_size=(1, 1), strides=(1, 1), padding=(0, 0),
                                  weight_initializer=init)

    # pylint: disable=arguments-differ 
Example #20
Source File: simple_pose_resnet.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
        assert num_layers == len(num_filters), \
            'ERROR: num_deconv_layers is different from len(num_deconv_filters)'
        assert num_layers == len(num_kernels), \
            'ERROR: num_deconv_layers is different from len(num_deconv_filters)'

        layer = nn.HybridSequential(prefix='')
        with layer.name_scope():
            for i in range(num_layers):
                kernel, padding, output_padding = \
                    self._get_deconv_cfg(num_kernels[i])

                planes = num_filters[i]
                layer.add(
                    nn.Conv2DTranspose(
                        channels=planes,
                        kernel_size=kernel,
                        strides=2,
                        padding=padding,
                        output_padding=output_padding,
                        use_bias=self.deconv_with_bias,
                        weight_initializer=initializer.Normal(0.001),
                        bias_initializer=initializer.Zero()))
                layer.add(gcv.nn.BatchNormCudnnOff(gamma_initializer=initializer.One(),
                                                   beta_initializer=initializer.Zero()))
                layer.add(nn.Activation('relu'))
                self.inplanes = planes

        return layer 
Example #21
Source File: deconv_dla.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def __init__(self, out_channels, in_channels, up_f,
                 use_dcnv2=False, norm_layer=nn.BatchNorm, norm_kwargs=None, **kwargs):
        super(IDAUp, self).__init__(**kwargs)
        self.startp = 0
        self.endp = 1
        with self.name_scope():
            self.projs = nn.HybridSequential('ida_proj')
            self.ups = nn.HybridSequential('ida_ups')
            self.nodes = nn.HybridSequential('ida_nodes')
            for i in range(1, len(in_channels)):
                c = in_channels[i]
                f = int(up_f[i])
                proj = CustomConv(c, out_channels, use_dcnv2=use_dcnv2,
                                  norm_layer=norm_layer, norm_kwargs=norm_kwargs)
                node = CustomConv(out_channels, out_channels, use_dcnv2=use_dcnv2,
                                  norm_layer=norm_layer, norm_kwargs=norm_kwargs)

                up = nn.Conv2DTranspose(in_channels=out_channels, channels=out_channels,
                                        kernel_size=f * 2, strides=f,
                                        padding=f // 2, output_padding=0,
                                        groups=out_channels, use_bias=False,
                                        weight_initializer=BilinearUpSample())

                self.projs.add(proj)
                self.ups.add(up)
                self.nodes.add(node) 
Example #22
Source File: train_cgan.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def __init__(self, output_nc, ngf=64, use_dropout=False, n_blocks=6, padding_type='reflect'):
        assert(n_blocks >= 0)
        super(ResnetGenerator, self).__init__()
        self.output_nc = output_nc
        self.ngf = ngf
        self.model = nn.HybridSequential()
        with self.name_scope():
            self.model.add(
                nn.ReflectionPad2D(3),
                nn.Conv2D(ngf, kernel_size=7, padding=0),
                nn.InstanceNorm(),
                nn.Activation('relu')
            )

            n_downsampling = 2
            for i in range(n_downsampling):
                mult = 2**i
                self.model.add(
                    nn.Conv2D(ngf * mult * 2, kernel_size=3,strides=2, padding=1),
                    nn.InstanceNorm(),
                    nn.Activation('relu')
                )

            mult = 2**n_downsampling
            for i in range(n_blocks):
                self.model.add(
                    ResnetBlock(ngf * mult, padding_type=padding_type, use_dropout=use_dropout)
                )

            for i in range(n_downsampling):
                mult = 2**(n_downsampling - i)
                self.model.add(
                    nn.Conv2DTranspose(int(ngf * mult / 2),kernel_size=3,strides=2,padding=1,output_padding=1),
                    nn.InstanceNorm(),
                    nn.Activation('relu')
                )
            self.model.add(
                nn.ReflectionPad2D(3),
                nn.Conv2D(output_nc,kernel_size=7,padding=0),
                nn.Activation('tanh')
            ) 
Example #23
Source File: train_wgan.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def __init__(self, isize, nz, nc, ngf, ngpu, n_extra_layers=0):
        super(DCGAN_G_nobn, self).__init__()
        self.ngpu = ngpu
        assert isize % 16 == 0, "isize has to be a multiple of 16"

        cngf, tisize = ngf // 2, 4
        while tisize != isize:
            cngf = cngf * 2
            tisize = tisize * 2
        with self.name_scope():
            main = nn.Sequential()
            main.add(
                nn.Conv2DTranspose(in_channels=nz, channels=cngf, kernel_size=4, strides=1, padding=0, use_bias=False,
                                   activation='relu', prefix='initial.{0}-{1}.convt'.format(nz, cngf)))

            csize, cndf = 4, cngf
            while csize < isize // 2:
                main.add(nn.Conv2DTranspose(in_channels=cngf, channels=cngf // 2, kernel_size=4, strides=2, padding=1,
                                            use_bias=False, activation='relu',
                                            prefix='pyramid.{0}-{1}.convt'.format(cngf, cngf // 2)))

                cngf = cngf // 2
                csize = csize * 2

            # Extra layers
            for t in range(n_extra_layers):
                main.add(nn.Conv2D(in_channels=cngf, channels=cngf, kernel_size=3, strides=1, padding=1, use_bias=False,
                                   activation='relu', prefix='extra-layers-{0}.{1}.conv'.format(t, cngf)))

            main.add(
                nn.Conv2DTranspose(in_channels=cngf, channels=nc, kernel_size=4, strides=2, padding=1, use_bias=False,
                                   activation='tanh', prefix='final.{0}-{1}.convt'.format(cngf, nc)))

        self.main = main 
Example #24
Source File: train_wgan.py    From gluon-cv with Apache License 2.0 5 votes vote down vote up
def __init__(self, isize, nz, nc, ngf, ngpu, n_extra_layers=0):
        super(DCGAN_G, self).__init__()
        self.ngpu = ngpu
        assert isize % 16 == 0, "isize has to be a multiple of 16"

        cngf, tisize = ngf // 2, 4
        while tisize != isize:
            cngf = cngf * 2
            tisize = tisize * 2
        with self.name_scope():
            main = nn.Sequential()
            # input is Z, going into a convolution
            main.add(
                nn.Conv2DTranspose(in_channels=nz, channels=cngf, kernel_size=4, strides=1, padding=0, use_bias=False,
                                   prefix='initial.{0}-{1}.convt'.format(nz, cngf)))
            main.add(nn.BatchNorm(in_channels=cngf, prefix='initial.{0}.batchnorm'.format(cngf)))
            main.add(nn.LeakyReLU(0, prefix='initial.{0}.relu'.format(cngf)))

            csize, cndf = 4, cngf
            while csize < isize // 2:
                main.add(nn.Conv2DTranspose(in_channels=cngf, channels=cngf // 2, kernel_size=4, strides=2, padding=1,
                                            use_bias=False, prefix='pyramid.{0}-{1}.convt'.format(cngf, cngf // 2)))
                main.add(nn.BatchNorm(in_channels=cngf // 2, prefix='pyramid.{0}.batchnorm'.format(cngf // 2)))
                main.add(nn.LeakyReLU(0, prefix='pyramid.{0}.relu'.format(cngf // 2)))
                cngf = cngf // 2
                csize = csize * 2

            # Extra layers
            for t in range(n_extra_layers):
                main.add(nn.Conv2D(in_channels=cngf, channels=cngf, kernel_size=3, strides=1, padding=1, use_bias=False,
                                   prefix='extra-layers-{0}.{1}.conv'.format(t, cngf)))
                main.add(nn.BatchNorm(in_channels=cngf, prefix='extra-layers-{0}.{1}.batchnorm'.format(t, cngf)))
                main.add(nn.LeakyReLU(0, prefix='extra-layers-{0}.{1}.relu'.format(t, cngf)))

            main.add(
                nn.Conv2DTranspose(in_channels=cngf, channels=nc, kernel_size=4, strides=2, padding=1, use_bias=False,
                                   activation='tanh', prefix='final.{0}-{1}.convt'.format(cngf, nc)))

        self.main = main 
Example #25
Source File: oth_centernet.py    From imgclsmob with MIT License 4 votes vote down vote up
def _make_deconv_layer(self, num_filters, num_kernels):
        # pylint: disable=unused-variable
        """Make deconv layers using the configs"""
        assert len(num_kernels) == len(num_filters), \
            'Deconv filters and kernels number mismatch: {} vs. {}'.format(
                len(num_filters), len(num_kernels))

        layers = nn.HybridSequential('deconv_')
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            self.base_network.initialize()
        in_planes = self.base_network(mx.nd.zeros((1, 3, 256, 256))).shape[1]
        for planes, k in zip(num_filters, num_kernels):
            kernel, padding, output_padding = self._get_deconv_cfg(k)
            if self._use_dcnv2:
                assert hasattr(contrib.cnn, 'ModulatedDeformableConvolution'), \
                    "No ModulatedDeformableConvolution found in mxnet, consider upgrade..."
                layers.add(contrib.cnn.ModulatedDeformableConvolution(planes,
                                                                      kernel_size=3,
                                                                      strides=1,
                                                                      padding=1,
                                                                      dilation=1,
                                                                      num_deformable_group=1,
                                                                      in_channels=in_planes))
            else:
                layers.add(nn.Conv2D(channels=planes,
                                     kernel_size=3,
                                     strides=1,
                                     padding=1,
                                     in_channels=in_planes))
            layers.add(self._norm_layer(momentum=0.9, **self._norm_kwargs))
            layers.add(nn.Activation('relu'))
            layers.add(nn.Conv2DTranspose(channels=planes,
                                          kernel_size=kernel,
                                          strides=2,
                                          padding=padding,
                                          output_padding=output_padding,
                                          use_bias=False,
                                          in_channels=planes,
                                          weight_initializer=BilinearUpSample()))
            layers.add(self._norm_layer(momentum=0.9, **self._norm_kwargs))
            layers.add(nn.Activation('relu'))
            in_planes = planes

        return layers 
Example #26
Source File: train_cgan.py    From gluon-cv with Apache License 2.0 4 votes vote down vote up
def __init__(self, outer_nc, inner_nc,submodule=None, outermost=False, innermost=False, use_dropout=False):
        super(UnetSkipConnectionBlock, self).__init__()
        self.outermost = outermost
        downconv = nn.Conv2D(inner_nc, kernel_size=4,strides=2, padding=1)
        downrelu = nn.LeakyReLU(0.2)
        downnorm = nn.InstanceNorm()
        uprelu = nn.Activation('relu')
        upnorm = nn.InstanceNorm()
        self.model = nn.HybridSequential()
        with self.model.name_scope():
            if outermost:
                self.model.add(
                    downconv
                )
                if submodule is not None:
                    self.model.add(
                        submodule
                    )
                self.model.add(
                    uprelu,
                    nn.Conv2DTranspose(outer_nc, kernel_size=4, strides=2, padding=1),
                    nn.Activation('tanh')
                )
            elif innermost:
                self.model.add(
                    downrelu,
                    downconv,
                    uprelu,
                    nn.Conv2DTranspose(outer_nc, kernel_size=4, strides=2, padding=1),
                    upnorm
                )
            else:
                self.model.add(
                    downrelu,
                    downconv,
                    downnorm,
                )
                if submodule is not None:
                    self.model.add(
                        submodule
                    )
                self.model.add(
                    uprelu,
                    nn.Conv2DTranspose(outer_nc, kernel_size=4, strides=2, padding=1),
                    upnorm,
                )
                if use_dropout:
                    self.model.add(nn.Dropout(0.5)) 
Example #27
Source File: train_cgan.py    From panoptic-fpn-gluon with Apache License 2.0 4 votes vote down vote up
def __init__(self, outer_nc, inner_nc,submodule=None, outermost=False, innermost=False, use_dropout=False):
        super(UnetSkipConnectionBlock, self).__init__()
        self.outermost = outermost
        downconv = nn.Conv2D(inner_nc, kernel_size=4,strides=2, padding=1)
        downrelu = nn.LeakyReLU(0.2)
        downnorm = nn.InstanceNorm()
        uprelu = nn.Activation('relu')
        upnorm = nn.InstanceNorm()
        self.model = nn.HybridSequential()
        with self.model.name_scope():
            if outermost:
                self.model.add(
                    downconv
                )
                if submodule is not None:
                    self.model.add(
                        submodule
                    )
                self.model.add(
                    uprelu,
                    nn.Conv2DTranspose(outer_nc, kernel_size=4, strides=2, padding=1),
                    nn.Activation('tanh')
                )
            elif innermost:
                self.model.add(
                    downrelu,
                    downconv,
                    uprelu,
                    nn.Conv2DTranspose(outer_nc, kernel_size=4, strides=2, padding=1),
                    upnorm
                )
            else:
                self.model.add(
                    downrelu,
                    downconv,
                    downnorm,
                )
                if submodule is not None:
                    self.model.add(
                        submodule
                    )
                self.model.add(
                    uprelu,
                    nn.Conv2DTranspose(outer_nc, kernel_size=4, strides=2, padding=1),
                    upnorm,
                )
                if use_dropout:
                    self.model.add(nn.Dropout(0.5)) 
Example #28
Source File: resnet_symbol.py    From mxnet-centernet with MIT License 4 votes vote down vote up
def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
        assert num_layers == len(num_filters), \
            'ERROR: num_deconv_layers is different len(num_deconv_filters)'
        assert num_layers == len(num_kernels), \
            'ERROR: num_deconv_layers is different len(num_deconv_filters)'

        layers = []
        for i in range(num_layers):
            kernel, padding, output_padding = \
                self._get_deconv_cfg(num_kernels[i], i)

            planes = num_filters[i]

            print("Deconv {}, fc, in_channels: {}, out_channels: {}".format(i, self.inplanes, planes))

            fc = DeformableConvolution(planes,
                     kernel_size=3, strides=1, num_deformable_group=1,
                     in_channels = self.inplanes,
                     padding=1, dilation=1, use_bias=False) # http://34.201.8.176/versions/1.5.0/_modules/mxnet/gluon/contrib/cnn/conv_layers.html
            '''
            # optional: do not use deformable convolution
            fc = nn.Conv2D(planes,
                     kernel_size=3, strides=1,
                     in_channels = self.inplanes,
                     padding=1, dilation=1, use_bias=False)
            '''

            print("Deconv {}, up, in_channels: {}, out_channels: {}".format(i, planes, planes))

            up = nn.Conv2DTranspose(
                    channels=planes,
                    kernel_size=kernel,
                    in_channels=planes,
                    strides=2,
                    padding=padding,
                    output_padding=output_padding,
                    use_bias=self.deconv_with_bias)
            layers.append(fc)
            layers.append(nn.BatchNorm(momentum=BN_MOMENTUM))
            layers.append(nn.LeakyReLU(0))
            layers.append(up)
            layers.append(nn.BatchNorm(momentum=BN_MOMENTUM))
            layers.append(nn.LeakyReLU(0))
            self.inplanes = planes

        deconv_layers = nn.HybridSequential()
        deconv_layers.add(*layers)
        return deconv_layers 
Example #29
Source File: resnet_symbol_plus_decoder.py    From mxnet-centernet with MIT License 4 votes vote down vote up
def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
        assert num_layers == len(num_filters), \
            'ERROR: num_deconv_layers is different len(num_deconv_filters)'
        assert num_layers == len(num_kernels), \
            'ERROR: num_deconv_layers is different len(num_deconv_filters)'

        layers = []
        for i in range(num_layers):
            kernel, padding, output_padding = \
                self._get_deconv_cfg(num_kernels[i], i)

            planes = num_filters[i]

            print("Deconv {}, fc, in_channels: {}, out_channels: {}".format(i, self.inplanes, planes))

            fc = DeformableConvolution(planes,
                     kernel_size=3, strides=1, num_deformable_group=1,
                     in_channels = self.inplanes,
                     padding=1, dilation=1, use_bias=False) # http://34.201.8.176/versions/1.5.0/_modules/mxnet/gluon/contrib/cnn/conv_layers.html
            '''
            # optional: do not use deformable convolution
            fc = nn.Conv2D(planes,
                     kernel_size=3, strides=1,
                     in_channels = self.inplanes,
                     padding=1, dilation=1, use_bias=False)
            '''

            print("Deconv {}, up, in_channels: {}, out_channels: {}".format(i, planes, planes))

            up = nn.Conv2DTranspose(
                    channels=planes,
                    kernel_size=kernel,
                    in_channels=planes,
                    strides=2,
                    padding=padding,
                    output_padding=output_padding,
                    use_bias=self.deconv_with_bias)
            layers.append(fc)
            layers.append(nn.BatchNorm(momentum=BN_MOMENTUM))
            layers.append(nn.LeakyReLU(0))
            layers.append(up)
            layers.append(nn.BatchNorm(momentum=BN_MOMENTUM))
            layers.append(nn.LeakyReLU(0))
            self.inplanes = planes

        deconv_layers = nn.HybridSequential()
        deconv_layers.add(*layers)
        return deconv_layers 
Example #30
Source File: resnet.py    From mxnet-centernet with MIT License 4 votes vote down vote up
def _make_deconv_layer(self, num_layers, num_filters, num_kernels):
        assert num_layers == len(num_filters), \
            'ERROR: num_deconv_layers is different len(num_deconv_filters)'
        assert num_layers == len(num_kernels), \
            'ERROR: num_deconv_layers is different len(num_deconv_filters)'

        layers = []
        for i in range(num_layers):
            kernel, padding, output_padding = \
                self._get_deconv_cfg(num_kernels[i], i)

            planes = num_filters[i]

            print("Deconv {}, fc, in_channels: {}, out_channels: {}".format(i, self.inplanes, planes))

            fc = DeformableConvolution(planes,
                     kernel_size=3, strides=1, num_deformable_group=1,
                     in_channels = self.inplanes,
                     padding=1, dilation=1, use_bias=False) # http://34.201.8.176/versions/1.5.0/_modules/mxnet/gluon/contrib/cnn/conv_layers.html
            '''
            # optional: do not use deformable convolution
            fc = nn.Conv2D(planes,
                     kernel_size=3, strides=1,
                     in_channels = self.inplanes,
                     padding=1, dilation=1, use_bias=False)
            '''

            print("Deconv {}, up, in_channels: {}, out_channels: {}".format(i, planes, planes))

            up = nn.Conv2DTranspose(
                    channels=planes,
                    kernel_size=kernel,
                    in_channels=planes,
                    strides=2,
                    padding=padding,
                    output_padding=output_padding,
                    use_bias=self.deconv_with_bias)
            layers.append(fc)
            layers.append(nn.BatchNorm(momentum=BN_MOMENTUM))
            layers.append(nn.LeakyReLU(0))
            layers.append(up)
            layers.append(nn.BatchNorm(momentum=BN_MOMENTUM))
            layers.append(nn.LeakyReLU(0))
            self.inplanes = planes

        deconv_layers = nn.HybridSequential()
        deconv_layers.add(*layers)
        return deconv_layers