Ejemplo n.º 1
0
def run():
    mode = 'vaegan'
    vae_grad_scale = 0.025
    experiment_name = mode + 'scale_%.5f' % vae_grad_scale
    filename = 'savestates/mnist_' + experiment_name + '.pickle'
    in_filename = filename
    in_filename = None
    print('experiment_name', experiment_name)
    print('in_filename', in_filename)
    print('filename', filename)

    # Fetch dataset
    dataset = dp.dataset.MNIST()
    x_train, y_train, x_test, y_test = dataset.arrays(dp_dtypes=True)
    n_classes = dataset.n_classes
    img_shape = x_train.shape[1:]

    # Normalize pixel intensities
    scaler = dp.UniformScaler()
    x_train = scaler.fit_transform(x_train)
    x_test = scaler.transform(x_test)
    y_train = one_hot(y_train, n_classes).astype(dp.float_)
    y_test = one_hot(y_test, n_classes).astype(dp.float_)
    x_train = np.reshape(x_train, (x_train.shape[0], -1))
    x_test = np.reshape(x_test, (x_test.shape[0], -1))


    # Setup network
    if in_filename is None:
        print('Creating new model')
        expressions = model_expressions(img_shape)
    else:
        print('Starting from %s' % in_filename)
        with open(in_filename, 'rb') as f:
            expressions = pickle.load(f)

    encoder, sampler, generator, discriminator = expressions
    model = cond_vaegan.ConditionalVAEGAN(
        encoder=encoder,
        sampler=sampler,
        generator=generator,
        discriminator=discriminator,
        mode=mode,
        reconstruct_error=expr.nnet.BinaryCrossEntropy(),
        vae_grad_scale=vae_grad_scale,
    )

    # Prepare network inputs
    batch_size = 128
    train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size,
                                     epoch_size=250)

    # Plotting
    n_examples = 100
    examples = x_test[:n_examples]
    examples_y = y_test[:n_examples]
    samples_z = np.random.normal(size=(n_examples, model.sampler.n_hidden))
    samples_z = samples_z.astype(dp.float_)
    samples_y = ((np.arange(n_examples) // 10) % n_classes)
    samples_y = one_hot(samples_y, n_classes).astype(dp.float_)

    recon_video = Video('plots/mnist_' + experiment_name +
                        '_reconstruction.mp4')
    sample_video = Video('plots/mnist_' + experiment_name + '_samples.mp4')
    sp.misc.imsave('plots/mnist_examples.png',
                   img_tile(to_b01c(examples, img_shape)))

    def plot():
        model.phase = 'test'
        model.sampler.batch_size=100
        examples_z = model.embed(examples, examples_y)
        examples_recon = model.reconstruct(examples_z, examples_y)
        recon_video.append(img_tile(to_b01c(examples_recon, img_shape)))
        samples = model.reconstruct(samples_z, samples_y)
        sample_video.append(img_tile(to_b01c(samples, img_shape)))
        model.setup(**train_input.shapes)
        model.phase = 'train'


    # Train network
    runs = [
        (75, dp.RMSProp(learn_rate=0.075)),
        (25, dp.RMSProp(learn_rate=0.05)),
        (5, dp.RMSProp(learn_rate=0.01)),
        (5, dp.RMSProp(learn_rate=0.005)),
    ]
    try:
        for n_epochs, learn_rule in runs:
            if mode == 'vae':
                vaegan.train(model, train_input, learn_rule, n_epochs,
                             epoch_callback=plot)
            else:
                vaegan.margin_train(model, train_input, learn_rule, n_epochs,
                                    epoch_callback=plot)
    except KeyboardInterrupt:
        pass

    raw_input('\n\nsave model to %s?\n' % filename)
    with open(filename, 'wb') as f:
        expressions = encoder, sampler, generator, discriminator
        pickle.dump(expressions, f)

    model.phase = 'test'
    batch_size = 128
    model.sampler.batch_size=128
    z = []
    i = 0
    z = model.embed(x_train, y_train)
    print(z.shape)
    z_mean = np.mean(z, axis=0)
    z_std = np.std(z, axis=0)
    z_cov = np.cov(z.T)
    print(np.mean(z_mean), np.std(z_mean))
    print(np.mean(z_std), np.std(z_std))
    print(z_mean.shape, z_std.shape, z_cov.shape)


    raw_input('\n\ngenerate latent space video?\n')
    print('Generating latent space video')
    walk_video = Video('plots/mnist_' + experiment_name + '_walk.mp4')
    for z in random_walk(samples_z, 500, n_dir_steps=10, mean=z_mean, std=z_cov):
        samples = model.reconstruct(z, samples_y)
        walk_video.append(img_tile(to_b01c(samples, img_shape)))



    print('Generating AdversarialMNIST dataset')
    _, y_train, _, y_test = dataset.arrays(dp_dtypes=True)
    n = 0
    batch_size = 512
    advmnist_size = 1e6
    x_advmnist = np.empty((advmnist_size, 28*28))
    y_advmnist = np.empty((advmnist_size,))
    while n < advmnist_size:
        samples_z = np.random.multivariate_normal(mean=z_mean, cov=z_cov,
                                                  size=batch_size)
        samples_z = samples_z.astype(dp.float_)
        start_idx = n % len(y_train)
        stop_idx = (n + batch_size) % len(y_train)
        if start_idx > stop_idx:
            samples_y = np.concatenate([y_train[start_idx:], y_train[:stop_idx]])
        else:
            samples_y = y_train[start_idx:stop_idx]
        y_advmnist[n:n+batch_size] = samples_y[:advmnist_size-n]
        samples_y = one_hot(samples_y, n_classes).astype(dp.float_)
        samples = model.reconstruct(samples_z, samples_y)
        x_advmnist[n:n+batch_size] = samples[:advmnist_size-n]
        n += batch_size


    x_train = x_advmnist
    y_train = y_advmnist
    import sklearn.neighbors
    clf = sklearn.neighbors.KNeighborsClassifier(n_neighbors=1, algorithm='brute', n_jobs=-1)
    clf.fit(x_train, y_train)
    print('KNN predict')
    step = 2500
    errors = []
    i = 0
    while i < len(x_test):
        print(i)
        errors.append(clf.predict(x_test[i:i+step]) != y_test[i:i+step])
        i += step
    error = np.mean(errors)
    print('Test error rate: %.4f' % error)

    print('DONE ' + experiment_name)
Ejemplo n.º 2
0
def run():
    mode = 'vaegan'
    vae_grad_scale = 0.0001
    kld_weight = 1.0
    z_gan_prop = False

    experiment_name = mode
    experiment_name += '_scale%.1e' % vae_grad_scale
    experiment_name += '_kld%.2f' % kld_weight
    if z_gan_prop:
        experiment_name += '_zprop'

    filename = 'savestates/lfw_' + experiment_name + '.pickle'
    in_filename = None

    print('experiment_name', experiment_name)
    print('in_filename', in_filename)
    print('filename', filename)

    # Fetch dataset
    x_train = lfw.lfw_imgs(alignment='deepfunneled', size=64, crop=50,
                           shuffle=True)
    img_shape = x_train.shape[1:]

    # Normalize pixel intensities
    scaler = dp.UniformScaler(low=-1, high=1)
    x_train = scaler.fit_transform(x_train)

    # Setup network
    if in_filename is None:
        print('Creating new model')
        expressions = model_expressions(img_shape)
    else:
        print('Starting from %s' % in_filename)
        with open(in_filename, 'rb') as f:
            expressions = pickle.load(f)

    encoder, sampler, generator, discriminator = expressions
    model = vaegan.VAEGAN(
        encoder=encoder,
        sampler=sampler,
        generator=generator,
        discriminator=discriminator,
        mode=mode,
        vae_grad_scale=vae_grad_scale,
        kld_weight=kld_weight,
    )

    # Prepare network inputs
    batch_size = 64
    train_input = dp.Input(x_train, batch_size=batch_size, epoch_size=250)

    # Plotting
    n_examples = 100
    examples = x_train[:n_examples]
    samples_z = np.random.normal(size=(n_examples, model.sampler.n_hidden))
    samples_z = samples_z.astype(dp.float_)


    recon_video = Video('plots/lfw_' + experiment_name + '_reconstruction.mp4')
    sample_video = Video('plots/lfw_' + experiment_name + '_samples.mp4')
    sp.misc.imsave('lfw_examples.png', img_tile(dp.misc.to_b01c(examples)))


    def plot():
        model.phase = 'test'
        examples_z = model.embed(examples)
        reconstructed = clip_range(model.reconstruct(examples_z))
        recon_video.append(img_tile(dp.misc.to_b01c(reconstructed)))
        z = model.embed(x_train)
        z_mean = np.mean(z, axis=0)
        z_std = np.std(z, axis=0)
        model.hidden_std = z_std
        z_std = np.diagflat(z_std)
        samples_z = np.random.multivariate_normal(mean=z_mean, cov=z_std,
                                                  size=(n_examples,))
        samples_z = samples_z.astype(dp.float_)
        samples = clip_range(model.reconstruct(samples_z))
        sample_video.append(img_tile(dp.misc.to_b01c(samples)))

        model.phase = 'train'
        model.setup(**train_input.shapes)

    # Train network
    runs = [
        (150, dp.RMSProp(learn_rate=0.05)),
        (250, dp.RMSProp(learn_rate=0.03)),
        (100, dp.RMSProp(learn_rate=0.01)),
        (15, dp.RMSProp(learn_rate=0.005)),
    ]
    try:
        import timeit
        for n_epochs, learn_rule in runs:
            if mode == 'vae':
                vaegan.train(model, train_input, learn_rule, n_epochs,
                             epoch_callback=plot)
            else:
                vaegan.margin_train(model, train_input, learn_rule, n_epochs,
                                    epoch_callback=plot)
    except KeyboardInterrupt:
        pass

    raw_input('\n\nsave model to %s?\n' % filename)
    with open(filename, 'wb') as f:
        expressions = encoder, sampler, generator, discriminator
        pickle.dump(expressions, f)


    model.phase = 'test'
    batch_size = 128
    model.sampler.batch_size=128
    z = model.embed(x_train)
    z_mean = np.mean(z, axis=0)
    z_std = np.std(z, axis=0)
    z_cov = np.cov(z.T)
    print(np.mean(z_mean), np.std(z_mean))
    print(np.mean(z_std), np.std(z_std))
    print(z_mean.shape, z_std.shape, z_cov.shape)

    model.sampler.batch_size=100
    samples_z = model.embed(examples)

    print('Generating latent space video')
    walk_video = Video('plots/lfw_' + experiment_name + '_walk.mp4')
    for z in random_walk(samples_z, 500, n_dir_steps=10, mean=z_mean, std=z_cov):
        samples = clip_range(model.reconstruct(z))
        walk_video.append(img_tile(dp.misc.to_b01c(samples)))
Ejemplo n.º 3
0
def run():
    mode = 'gan'
    experiment_name = mode + '_stride_local_discrimination'
    filename = 'savestates/cifar_cond_' + experiment_name + '.pickle'
    in_filename = filename
    in_filename = None
    print('experiment_name', experiment_name)
    print('in_filename', in_filename)
    print('filename', filename)

    # Fetch dataset
    dataset = dp.dataset.CIFAR10()
    x_train, y_train, x_test, y_test = dataset.arrays(dp_dtypes=True)
    n_classes = dataset.n_classes

    # Normalize pixel intensities
    scaler = dp.StandardScaler()
    x_train = scaler.fit_transform(x_train)
    x_test = scaler.transform(x_test)
    y_train = one_hot(y_train, n_classes).astype(dp.float_)
    y_test = one_hot(y_test, n_classes).astype(dp.float_)

    # Setup network
    if in_filename is None:
        print('Creating new model')
        img_shape = x_train.shape[1:]
        expressions = model_expressions(img_shape)
    else:
        print('Starting from %s' % in_filename)
        with open(in_filename, 'rb') as f:
            expressions = pickle.load(f)

    encoder, sampler, generator, discriminator = expressions
    model = cond_vaegan.ConditionalVAEGAN(
        encoder=encoder,
        sampler=sampler,
        generator=generator,
        discriminator=discriminator,
        mode=mode,
    )

    # Prepare network inputs
    batch_size = 64
    train_input = dp.SupervisedInput(x_train, y_train, batch_size=batch_size,
                                     epoch_size=150)

    # Plotting
    n_examples = 100
    examples = x_test[:n_examples]
    examples_y = y_test[:n_examples]
    samples_z = np.random.normal(size=(n_examples, model.sampler.n_hidden))
    samples_z = samples_z.astype(dp.float_)
    samples_y = ((np.arange(n_examples) // 10) % n_classes)
    samples_y = one_hot(samples_y, n_classes).astype(dp.float_)

    recon_video = Video('plots/cifar_' + experiment_name +
                        '_reconstruction.mp4')
    sample_video = Video('plots/cifar_' + experiment_name + '_samples.mp4')
    sp.misc.imsave('cifar_examples.png', img_tile(dp.misc.to_b01c(examples)))

    def plot():
        examples_z = model.embed(examples, examples_y)
        examples_recon = model.reconstruct(examples_z, examples_y)
        examples_recon = clip_range(examples_recon)
        recon_video.append(img_tile(dp.misc.to_b01c(examples_recon)))
        samples = clip_range(model.reconstruct(samples_z, samples_y))
        sample_video.append(img_tile(dp.misc.to_b01c(samples)))
        model.setup(**train_input.shapes)

    # Train network
    runs = [
#        (10, dp.RMSProp(learn_rate=0.08)),
#        (25, dp.RMSProp(learn_rate=0.12)),
#        (100, dp.RMSProp(learn_rate=0.1)),
        (150, dp.RMSProp(learn_rate=0.075)),
        (150, dp.RMSProp(learn_rate=0.06)),
        (150, dp.RMSProp(learn_rate=0.05)),
        (150, dp.RMSProp(learn_rate=0.04)),
        (25, dp.RMSProp(learn_rate=0.01)),
    ]
    try:
        for n_epochs, learn_rule in runs:
            if mode == 'vae':
                vaegan.train(model, train_input, learn_rule, n_epochs,
                             epoch_callback=plot)
            else:
                vaegan.margin_train(model, train_input, learn_rule, n_epochs,
                                    epoch_callback=plot)
    except KeyboardInterrupt:
        pass

    raw_input('\n\nsave model to %s?\n' % filename)
    with open(filename, 'wb') as f:
        expressions = encoder, sampler, generator, discriminator
        pickle.dump(expressions, f)

    print('Generating latent space video')
    walk_video = Video('plots/cifar_' + experiment_name + '_walk.mp4')
    for z in random_walk(samples_z, 500, step_std=0.15):
        samples = clip_range(model.reconstruct(z, samples_y))
        walk_video.append(img_tile(dp.misc.to_b01c(samples)))