Esempio n. 1
0
def eff_k_cond(G, D, trial=100, label=None):
    z1 = sample_continuous(128, trial, distribution=G.distribution, xp=xp)
    z2 = sample_continuous(128, trial, distribution=G.distribution, xp=xp)
    labels = label * xp.ones(trial).astype(xp.int32)
    with chainer.using_config('train', False):
        f1 = D(G(batchsize=trial, y=labels, z=z1))
        f2 = D(G(batchsize=trial, y=labels, z=z2))
    nu = distance(f2 - f1)
    de_l = distance(z2 - z1)
    return cuda.to_cpu(F.max(nu / de_l).data)
Esempio n. 2
0
 def __call__(self, batchsize=64, z=None, y=None, **kwargs):
     if z is None:
         z = sample_continuous(self.dim_z,
                               batchsize,
                               distribution=self.distribution,
                               xp=self.xp)
     if y is None:
         y = sample_categorical(
             self.n_classes, batchsize, distribution="uniform",
             xp=self.xp) if self.n_classes > 0 else None
     if (y is not None) and z.shape[0] != y.shape[0]:
         raise Exception(
             'z.shape[0] != y.shape[0], z.shape[0]={}, y.shape[0]={}'.
             format(z.shape[0], y.shape[0]))
     h = z
     h = self.l1(h)
     h = F.reshape(h,
                   (h.shape[0], -1, self.bottom_width, self.bottom_width))
     h = self.block2(h, y, **kwargs)
     h = self.block3(h, y, **kwargs)
     h = self.block4(h, y, **kwargs)
     h = self.block5(h, y, **kwargs)
     h = self.block6(h, y, **kwargs)
     h = self.block7(h, y, **kwargs)
     h = self.b8(h)
     h = self.activation(h)
     h = F.tanh(self.l8(h))
     return h
Esempio n. 3
0
    def __call__(self, batchsize=64, z=None, gamma=None, beta=None, **kwargs):
        if z is None:
            z = sample_continuous(self.dim_z,
                                  batchsize,
                                  distribution=self.distribution,
                                  xp=self.xp)
        if gamma is None:
            gamma = [None] * 12
            beta = [None] * 12
        # この辺は無駄なので,あとで直す
        y = self.xp.zeros((1, self.n_classes), dtype="float32")
        if self.n_classes > 0:
            y[0, 0] = 1
        h = z
        h = self.l1(h)
        h = F.reshape(h, (batchsize, -1, self.bottom_width, self.bottom_width))
        if self.normalize_stat:
            gamma = [self.normalize(g) * 0.2 + 1 for g in gamma]
            beta = [self.normalize(b) * 0.15 for b in beta]

        h = self.shift(h, gamma[0], beta[0])
        h = self.block2(h, y, None, gamma[1:3], beta[1:3], **kwargs)
        h = self.block3(h, y, None, gamma[3:5], beta[3:5], **kwargs)
        h = self.block4(h, y, None, gamma[5:7], beta[5:7], **kwargs)
        h = self.block5(h, y, None, gamma[7:9], beta[7:9], **kwargs)
        h = self.block6(h, y, None, gamma[9:11], beta[9:11], **kwargs)
        h = self.b7(h)
        h = self.activation(h)
        h = F.tanh(self.l7(h))
        return h
Esempio n. 4
0
def make_image(G,
               D,
               batchsize,
               N_update=100,
               ot=True,
               mode='latent',
               k=1,
               lr=0.05,
               optmode='sgd'):
    label = sample_categorical(1000, batchsize, distribution="uniform", xp=xp)
    labels = label * xp.ones(batchsize).astype(xp.int32)
    zs = sample_continuous(128, batchsize, distribution=G.distribution, xp=xp)
    if k != 1:
        k = k(labels).data

    with chainer.using_config('train', False):
        if ot:
            z_xp = zs
            if optmode == 'sgd':
                Opt = chainer.optimizers.SGD(lr)
            elif optmode == 'adam':
                Opt = chainer.optimizers.Adam(lr, beta1=0.0, beta2=0.9)
            T = Transporter_in_latent(G, D, k, Opt, z_xp, labels, mode=mode)
            discriminator_optimal_transport_from(z_xp, T, N_update)
            tz_y = T.get_z_va().data
            y = G(batchsize=batchsize, y=labels, z=tz_y)
        else:
            y = G(batchsize=batchsize, y=labels, z=zs)
    return cuda.to_cpu(y.data)
Esempio n. 5
0
 def __call__(self, x, z=None, save_res=False, add_noise=False, **kwargs):
     if z is None and add_noise:
         z = sample_continuous(self.dim_z,
                               len(x),
                               distribution=self.distribution,
                               xp=self.xp)
     if add_noise:
         # # untested format of noise!!
         h = z if len(z.shape) == 4 else F.reshape(z, (z.shape[0],
                                                       z.shape[1], 1, 1))
     else:
         h = x
     # # save the outputs of each layer if requested.
     outs = []
     # # loop over the layers and apply convolutions along with the
     # # normalizations per layer.
     for l in range(1, self.n_layers):
         h = getattr(self, 'conv{}'.format(l))(h)
         #print('Debugging, encoder step {}: '.format(l), h.shape)
         if self.norms[l - 1] is not None:
             h = getattr(self, 'norm{}'.format(l))(h)
         h = self.activs[l - 1](h)
         if save_res:
             outs.append(h)
     # # last layer:
     h = getattr(self, 'conv{}'.format(self.n_layers))(h)
     #print('Debugging, encoder step {}: '.format(self.n_layers), h.shape)
     if save_res:
         outs.append(h)
     h = self.activs[-1](h)
     if not save_res:
         return h
     else:
         return h, outs
def langevin(batchsize, gen, dis, y_fake, eval=False, given_z=None):
    if eval:
        Step_lr = args.eval_step_lr
        num_steps = args.eval_num_steps
        Noise_scale = args.eval_noise_scale
    else:
        Step_lr = args.step_lr
        num_steps = args.num_steps
        Noise_scale = args.noise_scale
    if given_z is None:
        z = sample_continuous(gen.dim_z,
                              batchsize,
                              distribution=gen.distribution,
                              xp=gen.xp)
        z = chainer.Variable(z)
    else:
        z = given_z
    x_fake = gen(batchsize, z=z, y=y_fake)
    for step in range(num_steps):
        energy = dis(x_fake, y=y_fake) * args.temperature
        z_grad = chainer.grad(outputs=[energy], inputs=[z])[0]
        # pdb.set_trace()
        if args.anealing:
            step_lr = Step_lr * 0.1**(step // (num_steps / 5))
            noise_scale = Noise_scale * 0.1**(step // (num_steps / 5))
        else:
            step_lr = Step_lr
            noise_scale = Noise_scale
        z_grad_noise = step_lr/2*z_grad + \
            (step_lr**0.5)*cp.random.normal(size=z.shape, loc=0.0, scale=noise_scale)
        z = z + z_grad_noise
        z.unchain_backward()
        x_fake = gen(batchsize, z=z, y=y_fake)
    return x_fake, z
 def __call__(self, batchsize=64, z=None, y=None):
     if z is None:
         z = sample_continuous(self.dim_z,
                               batchsize,
                               distribution=self.distribution,
                               xp=self.xp)
     if y is None:
         y = sample_categorical(
             self.n_classes, batchsize, distribution="uniform",
             xp=self.xp) if self.n_classes > 0 else None
     if (y is not None) and z.shape[0] != y.shape[0]:
         raise ValueError('z.shape[0] != y.shape[0]')
     h = z
     h = self.l1(h)
     h = F.reshape(h,
                   (h.shape[0], -1, self.bottom_width, self.bottom_width))
     h = self.block2(h, y)
     out1 = h
     h = self.block3(h, y)
     out2 = h
     h = self.block4(h, y)
     out3 = h
     h = self.b5(h)
     h = self.activation(h)
     h = F.tanh(self.c5(h))
     return h, out1, out2, out3
Esempio n. 8
0
    def __call__(self,
                 x,
                 skips=None,
                 z=None,
                 save_res=False,
                 add_noise=False,
                 **kwargs):
        if z is None and add_noise:
            z = sample_continuous(self.dim_z,
                                  len(x),
                                  distribution=self.distribution,
                                  xp=self.xp)
        if add_noise:
            # # untested format of noise!!
            h = z if len(z.shape) == 4 else F.reshape(z, (z.shape[0],
                                                          z.shape[1], 1, 1))
        else:
            h = x

        # # save the outputs of each layer if requested.
        outs = []
        # # loop over the layers and apply convolutions along with the
        # # normalizations per layer.
        for l in range(1, self.n_layers):
            if self.skip_enc is not None and self.skip_enc[l] > 0:
                # # in this case, we concatenate the skip with our representation.
                # # The -1 below, since skips includes a zero-based list of all
                # # encoder layer outputs, i.e. skips[0] -> output of 1st
                # # encoder layer. To get l^th encoder layer's output -> skips[l-1].
                if self.noise_skip:
                    noise = self.xp.random.normal(loc=0,
                                                  scale=self.noise_cov,
                                                  size=h.shape).astype("f")
                    h = F.concat([skips[self.skip_enc[l] - 1] + noise, h])
                else:
                    h = F.concat([skips[self.skip_enc[l] - 1], h])
            h = getattr(self, 'tconv{}'.format(l))(h)
            if self.norms[l - 1] is not None:
                h = getattr(self, 'norm{}'.format(l))(h)
            h = self.activs[l - 1](h)
            if save_res:
                outs.append(h)
        # # last layer:
        l = self.n_layers
        if self.skip_enc is not None and self.skip_enc[l] > 0:
            h = F.concat([skips[self.skip_enc[l] - 1], h])
        h = getattr(self, 'tconv{}'.format(l))(h)
        if save_res:
            # # even though the outputs of the previous layers are appened
            # # (into outs) after the activation, this is before.
            outs.append(h)
        h = self.activs[-1](h)
        if not save_res:
            return h
        else:
            return h, outs
def gen_eval_images(gen, n=50000, batchsize=100, seeds=1234, langevin_steps=5):
    '''
    langevin_steps: column
    '''
    ims = []
    xp = gen.xp
    xp.random.seed(seeds)
    for i in range(0, n, batchsize):
        print(i)
        config = yaml_utils.Config(yaml.load(open(args.config_path)))
        is_conditional = config.updater['args']['conditional']
        if is_conditional:
            y = sample_categorical(gen.n_classes, batchsize, xp=gen.xp)
        else:
            y = None
        if args.sampling_space == 'pixel':
            with chainer.using_config('train', False), chainer.using_config(
                    'enable_backprop', False):
                x = gen(batchsize, y=y)
            for j in range(langevin_steps):
                x = sampler.langevin(x, y, dis)
                nx = chainer.cuda.to_cpu(x.data)
                nx = np.asarray(np.clip(nx * 127.5 + 127.5, 0.0, 255.0),
                                dtype=np.uint8)
                ims.append(nx)
        elif args.sampling_space == 'latent':
            z = Variable(
                sample_continuous(gen.dim_z,
                                  batchsize,
                                  distribution=gen.distribution,
                                  xp=gen.xp))
            x = gen(batchsize, y=y, z=z)
            nx = chainer.cuda.to_cpu(x.data)
            nx = np.asarray(np.clip(nx * 127.5 + 127.5, 0.0, 255.0),
                            dtype=np.uint8)
            ims.append(nx)
            for j in range(langevin_steps):
                x, z = latent_sampler.langevin(batchsize,
                                               gen,
                                               dis,
                                               y_fake=y,
                                               eval=True,
                                               given_z=z)
                nx = chainer.cuda.to_cpu(x.data)
                nx = np.asarray(np.clip(nx * 127.5 + 127.5, 0.0, 255.0),
                                dtype=np.uint8)
                ims.append(nx)
    ims = list(map(list, zip(*ims)))
    ims = np.asarray(ims)
    _, _, _, h, w = ims.shape
    if args.sampling_space == 'latent':
        langevin_steps += 1
    ims = ims.reshape((n * langevin_steps, 3, h, w))
    return ims
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('--config_path', type=str, default='configs/base.yml')
    parser.add_argument('--gpu', '-g', type=int, default=0)
    parser.add_argument('--results_dir', type=str, default='./results/gans')
    parser.add_argument('--snapshot', type=str, default='')
    parser.add_argument('--rows', type=int, default=5)
    parser.add_argument('--columns', type=int, default=5)
    parser.add_argument('--classes', type=int, nargs="*", default=None)
    parser.add_argument('--dump_z', action='store_true', default=False)
    args = parser.parse_args()

    np.random.seed(1234)

    chainer.cuda.get_device_from_id(args.gpu).use()
    config = yaml_utils.Config(yaml.load(open(args.config_path)))

    gen = load_models(config)
    gen.to_gpu(args.gpu)
    chainer.serializers.load_npz(args.snapshot, gen)

    out = args.results_dir

    classes = tuple(args.classes) if args.classes is not None else np.arange(
        0, gen.n_classes, dtype=np.int32)

    for c in classes:
        z = sample_continuous(128, args.rows * args.columns, xp=gen.xp)
        with chainer.using_config('train', False), chainer.using_config(
                'enable_backprop', False):
            x = gen_images_with_condition(gen,
                                          z=z,
                                          c=c,
                                          n=args.rows * args.columns,
                                          batchsize=args.rows * args.columns)
        _, _, h, w = x.shape
        x = x.reshape((args.rows, args.columns, 3, h, w))
        x = x.transpose(0, 3, 1, 4, 2)
        x = x.reshape((args.rows * h, args.columns * w, 3))

        save_path = os.path.join(out, '{}.png'.format(str(c)))
        if not os.path.exists(out):
            os.makedirs(out)
        Image.fromarray(x).save(save_path)

        # dump latent variables
        if args.dump_z:
            for i in range(args.rows * args.columns):
                np.save(os.path.join(out, 'z_{}-{}.npy'.format(c, i)),
                        z.get()[i])
    def __call__(self, batchsize=64, z=None, y=None, gt=None, **kwargs):
        outs = []
        fast_losses = []

        if z is None:
            z = sample_continuous(self.dim_z,
                                  batchsize,
                                  distribution=self.distribution,
                                  xp=self.xp)
        if y is None:
            y = sample_categorical(
                self.n_classes, batchsize, distribution="uniform",
                xp=self.xp) if self.n_classes > 0 else None
        if (y is not None) and z.shape[0] != y.shape[0]:
            raise Exception(
                'z.shape[0] != y.shape[0], z.shape[0]={}, y.shape[0]={}'.
                format(z.shape[0], y.shape[0]))

        # forward calculation without auxiliary network
        out_noab = self.forward(z=z, y=y, noAB=True, **kwargs)

        out, z, zeta, z_recon = self.forward(z=z,
                                             y=y,
                                             return_zs=True,
                                             **kwargs)
        outs.append(out)

        # beta1=0, beta2=0.9 <-> initial_t = 100
        optimizer = MyAdaGrad(zeta, self.xp, lr=self.fast_alpha())

        for _ in range(self.T):
            loss = F.sum(self.fast_loss(out, gt))
            fast_losses.append(loss)

            grads = chainer.grad([loss], [zeta],
                                 enable_double_backprop=True)[0]
            # use learned learning rate
            # z2 += - F.broadcast_to(self.lr(), grads[0].shape) * grads[0]
            zeta += optimizer.calc_update(grads)

            # forward run with z2 supply
            out = self.forward(z=z, y=y, zeta=zeta)
            outs.append(out)

        return outs, fast_losses, out_noab, zeta, z_recon
Esempio n. 12
0
 def __call__(self, batchsize=64, z=None, **kwargs):
     if z is None:
         z = sample_continuous(self.dim_z,
                               batchsize,
                               distribution=self.distribution,
                               xp=self.xp)
     h = z
     h = self.l1(h)
     h = F.reshape(h,
                   (h.shape[0], -1, self.bottom_width, self.bottom_width))
     h = self.block2(h, **kwargs)
     h = self.block3(h, **kwargs)
     h = self.block4(h, **kwargs)
     h = self.block5(h, **kwargs)
     h = self.block6(h, **kwargs)
     h = self.b7(h)
     h = self.activation(h)
     h = F.tanh(self.l7(h))
     return h
Esempio n. 13
0
    def __call__(self, batchsize=64, z=None, y=None):
        if z is None:
            z = sample_continuous(self.dim_z, batchsize, distribution=self.distribution, xp=self.xp)
        if y is None:
            y = sample_categorical(self.n_classes, batchsize, distribution="uniform",
                                   xp=self.xp) if self.n_classes > 0 else None
        if (y is not None) and z.shape[0] != y.shape[0]:
            raise ValueError('z.shape[0] != y.shape[0]')
        print("B0", np.sum(z.data))
        print("C2B0", np.sum(self.block2.c2.b.data))
        
        h = z
        h = self.l1(h)
        h = F.reshape(h, (h.shape[0], -1, self.bottom_width, self.bottom_width))
        print("B1", np.sum(h.data))
        print("C2B1", np.sum(self.block2.c2.b.data))

        h = self.block2(h, y)
        print("B2", np.sum(h.data))
        print("C2B2", np.sum(self.block2.c2.b.data))

        h = self.block3(h, y)
        print("B3", np.sum(h.data))
        print("C2B3", np.sum(self.block2.c2.b.data))

        h = self.block4(h, y)
        print("B4", np.sum(h.data))
        print("C2B4", np.sum(self.block2.c2.b.data))

        h = self.b5(h)
        print("B5", np.sum(h.data))
        print("C2B5", np.sum(self.block2.c2.b.data))

        h = self.activation(h)
        print("B6", np.sum(h.data))
        print("C2B6", np.sum(self.block2.c2.b.data))

        h = F.tanh(self.c5(h))
        print("B7", np.sum(h.data))
        print("C2B7", np.sum(self.block2.c2.b.data))

        return h
 def sample_z(self, batchsize=64):
     return sample_continuous(self.dim_z,
                              batchsize,
                              distribution=self.distribution,
                              xp=self.xp)
    def __call__(self,
                 batchsize=None,
                 y=None,
                 z=None,
                 mult_until_exec=None,
                 **kwargs):
        if z is None:
            z = sample_continuous(self.dim_z,
                                  batchsize,
                                  distribution=self.distribution,
                                  xp=self.xp)
        if y is None:
            y = sample_categorical(
                self.n_classes, batchsize, distribution="uniform",
                xp=self.xp) if self.n_classes > 0 else None
        activ = self.activation

        # # mult_until_exec: If set, we perform the multiplications until that layer.
        # # In the product of polynomials, it applies the same rule for *every*
        # # polynomial. E.g. if mult_until_exec == 2 it will perform the hadamard
        # # products until second order terms in every polynomial.
        if mult_until_exec is None:
            mult_until_exec = 10000
        z = self.prod_poly_FC(z, mult_until_exec, batchsize=batchsize, y=y)

        h = z + 0
        if self.bottom_width > 1:
            h = getattr(self, 'lin0')(h)
        h = F.reshape(h,
                      (h.shape[0], -1, self.bottom_width, self.bottom_width))

        # # loop over the layers and get the layers along with the
        # # normalizations per layer.
        for l in range(1, self.n_l + 1):
            if self.skip_rep:
                h_hold = h + 0
            if self.use_bn and y is None:
                h = getattr(self, 'bn{}'.format(l))(h)
            elif self.use_bn:
                h = getattr(self, 'bn{}'.format(l))(h, y)
            h = activ(getattr(self, 'l{}'.format(l))(h))
            h = self.return_injected(h, z, l, mult_until_exec=mult_until_exec)
            if self.skip_rep:
                # # transform the channels of h_hold if required.
                h_hold = getattr(self, 'skipch{}'.format(l))(h_hold)
                # # upsample if required.
                if h_hold.shape[-1] != h.shape[-1]:
                    h_hold = _upsample(h_hold)
                h += h_hold
        if self.order_out_poly is not None:
            z0 = h + 0
            for i in range(1, self.order_out_poly):
                h1 = getattr(self, 'oro{}'.format(i + 1))(h)
                if self.skip_rep:
                    # # model3 polynomial.
                    h += z0 * h1
                else:
                    h = z0 * h1
        # # last layer (no activation).
        output = getattr(self, 'l{}'.format(self.n_l + 1))(h)
        out = self.out_act(output)
        return out