예제 #1
0
    def __init__(self, ch=512, enable_blur=False, sn=False):
        super(Discriminator, self).__init__()
        self.max_stage = 17
        self.sn = sn

        with self.init_scope():
            # NOTE: called in reversed order.
            self.blocks = chainer.ChainList(
                DiscriminatorBlockBase(ch, sn=sn),
                DiscriminatorBlock(ch, ch, enable_blur=enable_blur, sn=sn),
                DiscriminatorBlock(ch, ch, enable_blur=enable_blur, sn=sn),
                DiscriminatorBlock(ch, ch, enable_blur=enable_blur, sn=sn),
                DiscriminatorBlock(ch // 2, ch, enable_blur=enable_blur,
                                   sn=sn),
            )
            if not sn:
                self.ins = chainer.ChainList(
                    EqualizedConv2d(3, ch // 2, 1, 1, 0), )
            else:
                w = chainer.initializers.GlorotUniform(math.sqrt(2))
                self.ins = chainer.ChainList(
                    L.Convolution2D(3, ch // 2, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()), )

            self.enable_blur = enable_blur
예제 #2
0
 def __init__(self, ch, out_dim=1, sn=False):
     super(DiscriminatorBlockBase, self).__init__()
     with self.init_scope():
         if not sn:
             self.c0 = EqualizedConv2d(ch, ch, 3, 1, 1)
             self.c1 = EqualizedConv2d(ch, ch, 4, 1, 0)
             self.l2 = EqualizedLinear(ch, out_dim, gain=1)
         else:
             w = chainer.initializers.Uniform(1)
             self.c0 = L.Convolution2D(ch, ch, 3, 1, 1,
                                       initialW=w).add_hook(
                                           SpectralNormalization())
             self.c1 = L.Convolution2D(ch, ch, 4, 1, 0,
                                       initialW=w).add_hook(
                                           SpectralNormalization())
             self.l2 = L.Linear(ch, out_dim, initialW=w).add_hook(
                 SpectralNormalization())
 def setUp(self):
     self.bs, self.in_size, self.out_size = 10, 20, 30
     self.x = numpy.random.normal(size=(self.bs,
                                        self.in_size)).astype(numpy.float32)
     self.layer = L.Linear(self.out_size)  # Lazy initialization
     in_size = None if self.lazy_init else self.in_size
     self.layer = L.Linear(in_size, self.out_size)
     self.hook = SpectralNormalization(use_gamma=self.use_gamma)
 def _init_layer(self):
     hook = SpectralNormalization(use_gamma=self.use_gamma)
     layer = self.layer
     layer.add_hook(hook)
     if self.lazy_init:
         # Initialize weight and bias.
         with chainer.using_config('train', False):
             layer(self.x)
     return layer, hook
 def setUp(self):
     self.in_channels, self.out_channels = 3, 10
     in_channels = None if self.lazy_init else self.in_channels
     conv_init_args = {'ksize': 3, 'stride': 1, 'pad': 1}
     self.layer = self.link(in_channels, self.out_channels,
                            **conv_init_args)
     self.x = numpy.random.normal(size=(5, self.in_channels, 4, 4,
                                        4)).astype(numpy.float32)
     self.hook = SpectralNormalization(use_gamma=self.use_gamma)
     self.out_size = self.out_channels  # For compatibility
예제 #6
0
 def __init__(self,
              in_ch,
              out_ch,
              enable_blur=False,
              sn=False,
              res=False,
              bn=False):
     super(DiscriminatorBlock, self).__init__()
     self.in_ch = in_ch
     self.out_ch = out_ch
     self.res = res
     with self.init_scope():
         if not sn:
             self.c0 = EqualizedConv2d(in_ch, out_ch, 3, 1, 1)
             self.c1 = EqualizedConv2d(out_ch, out_ch, 3, 1, 1)
             if res:
                 self.c_sc = EqualizedConv2d(in_ch, out_ch, 3, 1, 1)
         else:
             w = chainer.initializers.Uniform(1)
             self.c0 = L.Convolution2D(in_ch, out_ch, 3, 1, 1,
                                       initialW=w).add_hook(
                                           SpectralNormalization())
             self.c1 = L.Convolution2D(out_ch, out_ch, 3, 1, 1,
                                       initialW=w).add_hook(
                                           SpectralNormalization())
             if res:
                 self.c_sc = L.Convolution2D(in_ch,
                                             out_ch,
                                             3,
                                             1,
                                             1,
                                             initialW=w).add_hook(
                                                 SpectralNormalization())
         if bn:
             self.b0 = L.BatchNormalization(out_ch)
             self.b1 = L.BatchNormalization(out_ch)
         else:
             self.b0 = lambda x: x
             self.b1 = lambda x: x
     self.blur_k = None
     self.enable_blur = enable_blur
예제 #7
0
 def __init__(self, ch, out_ch, sn=False):
     super(MLP, self).__init__()
     w = chainer.initializers.Uniform(1)
     if sn:
         net = [
             L.Linear(ch, ch, initialW=w).add_hook(SpectralNormalization()),
             F.leaky_relu,
             L.Linear(ch, ch, initialW=w).add_hook(SpectralNormalization()),
             F.leaky_relu,
             L.Linear(ch, out_ch,
                      initialW=w).add_hook(SpectralNormalization()),
         ]
     else:
         net = [
             EqualizedLinear(ch, ch),
             F.leaky_relu,
             EqualizedLinear(ch, ch),
             F.leaky_relu,
             EqualizedLinear(ch, out_ch),
         ]
     with self.init_scope():
         self.net = chainer.Sequential(*net)
예제 #8
0
 def __init__(self, ch, dim_z, enable_blur=False, sn=False, res=False):
     super(BigBiGANDiscriminator, self).__init__()
     self.sn = sn
     w = chainer.initializers.Uniform(1)
     with self.init_scope():
         self.f = Discriminator(ch,
                                ch,
                                enable_blur=enable_blur,
                                sn=sn,
                                res=res)
         self.h_z = MLP(dim_z, ch, sn=sn)
         self.h_cp = MLP(9, ch, sn=sn)
         self.j = MLP(ch * 3, 1, sn=sn)
         if sn:
             self.s_x = L.Linear(ch, 1, initialW=w).add_hook(
                 SpectralNormalization())
             self.s_z = L.Linear(ch, 1, initialW=w).add_hook(
                 SpectralNormalization())
             self.s_cp = L.Linear(ch, 1, initialW=w).add_hook(
                 SpectralNormalization())
         else:
             self.s_x = EqualizedLinear(ch, 1)
             self.s_z = EqualizedLinear(ch, 1)
             self.s_cp = EqualizedLinear(ch, 1)
 def test_add_sn_hook(self):
     hook = SpectralNormalization(use_gamma=self.use_gamma)
     layer = self.layer
     layer.add_hook(hook)
     if self.lazy_init:
         assert not hasattr(layer, hook.vector_name)
         if self.use_gamma:
             assert not hasattr(layer, 'gamma')
         with chainer.using_config('train', False):
             layer(self.x)
     assert hasattr(layer, hook.vector_name)
     assert (self.in_size, ) == getattr(layer, hook.vector_name).shape
     if not self.use_gamma:
         assert not hasattr(layer, 'gamma')
     else:  # Use gamma parameter
         assert hasattr(layer, 'gamma')
         assert layer.gamma.ndim == 0 and layer.gamma.size == 1
예제 #10
0
    def __init__(self, ch0, ch1, ksize=5, stride=2, sn=False):
        super(DisConv, self).__init__()

        initializer = chainer.initializers.GlorotUniform(math.sqrt(2))
        pad = (ksize - stride + 1) // 2
        with self.init_scope():
            if sn:
                self.conv = L.Convolution2D(ch0,
                                            ch1,
                                            ksize,
                                            stride,
                                            pad,
                                            initialW=initializer).add_hook(
                                                SpectralNormalization())
            else:
                self.conv = L.Convolution2D(ch0,
                                            ch1,
                                            ksize,
                                            stride,
                                            pad,
                                            initialW=initializer)
예제 #11
0
    def __init__(self,
                 ch=512,
                 enable_blur=False,
                 sn=False,
                 res=False,
                 num_z=2):
        super(DisentangledDiscriminator, self).__init__()
        self.max_stage = 17
        self.sn = sn

        with self.init_scope():
            # NOTE: called in reversed order.
            self.shared_blocks = chainer.ChainList(
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 16
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 32
                DiscriminatorBlock(ch // 2,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 64
                DiscriminatorBlock(ch // 4,
                                   ch // 2,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 128
                DiscriminatorBlock(ch // 8,
                                   ch // 4,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 256
                DiscriminatorBlock(ch // 16,
                                   ch // 8,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 512
                DiscriminatorBlock(ch // 32,
                                   ch // 16,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),
            )  # 1024

            self.camera_parameter_blocks = chainer.Sequential(  # camera parameter variant feature
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 8
                DiscriminatorBlockBase(
                    ch, sn=sn,
                    out_dim=9),  # 4  # Euler angles, translation vector
            )

            self.z_regression_blocks = chainer.Sequential(  # camera parameter invariant feature
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 8
                DiscriminatorBlockBase(ch, sn=sn, out_dim=ch * num_z),  # 4
            )

            self.discriminator_blocks = chainer.Sequential(  # camera parameter variant feature
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 8
                DiscriminatorBlockBase(
                    ch, sn=sn),  # 4  # Euler angles*2, translation vector
            )

            if not sn:
                self.ins = chainer.ChainList(
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch // 2, 1, 1, 0),
                    EqualizedConv2d(3, ch // 4, 1, 1, 0),
                    EqualizedConv2d(3, ch // 8, 1, 1, 0),
                    EqualizedConv2d(3, ch // 16, 1, 1, 0),
                    EqualizedConv2d(3, ch // 32, 1, 1, 0),
                )
            else:
                w = chainer.initializers.Uniform(1)
                self.ins = chainer.ChainList(
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 2, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 4, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 8, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 16, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 32, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                )
            self.camera_param_discriminator = CameraParamDiscriminator()
            self.enable_blur = enable_blur
예제 #12
0
    def __init__(self,
                 ch=512,
                 out_dim=1,
                 enable_blur=False,
                 sn=False,
                 res=False):
        super(Discriminator, self).__init__()
        self.max_stage = 17
        self.sn = sn

        with self.init_scope():
            # NOTE: called in reversed order.
            self.blocks = chainer.ChainList(
                DiscriminatorBlockBase(ch, out_dim, sn=sn),  # 4
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 8
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 16
                DiscriminatorBlock(ch,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 32
                DiscriminatorBlock(ch // 2,
                                   ch,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 64
                DiscriminatorBlock(ch // 4,
                                   ch // 2,
                                   enable_blur=enable_blur,
                                   sn=sn,
                                   res=res),  # 128
            )

            if not sn:
                self.ins = chainer.ChainList(
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch, 1, 1, 0),
                    EqualizedConv2d(3, ch // 2, 1, 1, 0),
                    EqualizedConv2d(3, ch // 4, 1, 1, 0),
                )
            else:
                w = chainer.initializers.Uniform(1)
                self.ins = chainer.ChainList(
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 2, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                    L.Convolution2D(3, ch // 4, 1, 1, 0, initialW=w).add_hook(
                        SpectralNormalization()),
                )

            self.enable_blur = enable_blur
 def test_invalid_shaped_weight(self):
     with pytest.raises(ValueError):
         L.Linear(10, 0).add_hook(SpectralNormalization())
 def test_raises(self):
     with pytest.raises(NotImplementedError):
         with SpectralNormalization():
             self.layer(self.x)
 def setUp(self):
     self.lazy_init = False  # For convenience.
     self.bs, self.in_size, self.out_size = 5, 10, 20
     self.x = numpy.arange(self.in_size, dtype=numpy.int32)
     self.layer = L.EmbedID(self.in_size, self.out_size)
     self.hook = SpectralNormalization(use_gamma=self.use_gamma)
 def test_wrong_weight_name(self):
     wrong_Weight_name = 'w'
     hook = SpectralNormalization(weight_name=wrong_Weight_name)
     with pytest.raises(ValueError):
         self.layer.add_hook(hook)