def run():
    experiment_name = 'cifar'

    img_size = 32
    epoch_size = 250
    batch_size = 64
    train_feed, test_feed, _ = dataset.cifar.feeds(split='test',
                                                   batch_size=batch_size,
                                                   epoch_size=epoch_size)

    model, experiment_name = aegan.build_model(
        experiment_name,
        img_size,
        n_hidden=256,
        recon_depth=6,
        recon_vs_gan_weight=1e-5,
        real_vs_gen_weight=0.5,
        discriminate_ae_recon=False,
        discriminate_sample_z=True,
    )

    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)
    aegan.train(model,
                output_dir,
                train_feed,
                test_feed,
                n_epochs=200,
                lr_start=0.025)
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)
def run():
    experiment_name = 'cifar'

    img_size = 32
    epoch_size = 250
    batch_size = 64
    train_feed, test_feed, _ = dataset.cifar.feeds(
        split='test', batch_size=batch_size, epoch_size=epoch_size
    )

    model, experiment_name = aegan.build_model(
        experiment_name, img_size, n_hidden=256, recon_depth=6,
        recon_vs_gan_weight=1e-5, real_vs_gen_weight=0.5,
        discriminate_ae_recon=False, discriminate_sample_z=True,
    )

    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model, output_dir, train_feed, test_feed, n_epochs=200, lr_start=0.025
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)
Exemple #3
0
def run():
    experiment_name = 'lfw'

    img_size = 64
    epoch_size = 250
    batch_size = 64
    np.random.seed(1)
    train_feed, test_feed = dataset.lfw.feeds(
        alignment='landmarks', crop_size=150, rescale_size=img_size,
        batch_size=batch_size, epoch_size=epoch_size, n_augment=250000,
        split='test',
    )

    model, experiment_name = aegan.build_model(
        experiment_name, img_size, n_hidden=128, recon_depth=9,
        recon_vs_gan_weight=1e-6, real_vs_gen_weight=0.33,
        discriminate_ae_recon=False, discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)

    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model, output_dir, train_feed, test_feed,
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)
def run():
    experiment_name = 'stl'

    img_size = 64
    epoch_size = 250
    batch_size = 64
    train_feed = dataset.stl.unlabeled_feed(
        img_size, batch_size=batch_size, epoch_size=epoch_size,
        n_augment=int(5e5)
    )
    _, test_feed = dataset.stl.supervised_feed(img_size)

    model, experiment_name = aegan.build_model(
        experiment_name, img_size, n_hidden=128, recon_depth=9,
        recon_vs_gan_weight=1e-6, real_vs_gen_weight=0.5,
        discriminate_ae_recon=False, discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)

    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model, output_dir, train_feed, test_feed, n_epochs=200,
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)
Exemple #5
0
def run():

    experiment_name = 'celeba'

    img_size = 64
    epoch_size = 1
    batch_size = 2000

    n_hidden = 128
    _, experiment_name = aegan.build_model(
        experiment_name,
        img_size,
        n_hidden=n_hidden,
        recon_depth=9,
        recon_vs_gan_weight=1e-6,
        real_vs_gen_weight=0.5,
        discriminate_ae_recon=False,
        discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)

    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Loading model from disk')
    print(model_path)
    with open(model_path, 'rb') as f:
        model = pickle.load(f)

    print('Getting data feed')
    model.phase = 'test'
    train_feed, test_feed = dataset.celeba.feeds(img_size,
                                                 batch_size=batch_size,
                                                 epoch_size=epoch_size,
                                                 split='test',
                                                 n_augment=0)

    save_dir = os.path.join(output_dir, 'User-Testing')
    if not os.path.expanduser(save_dir):
        os.mkdir(save_dir)

    original_x = np.array(test_feed.batches().next()[0])
    samples_z = np.random.normal(size=(len(original_x), n_hidden))
    samples_z = (samples_z).astype(dp.float_)

    print('Saving samples')
    output.samples(model, samples_z, save_dir, img_inverse_transform)

    print('Saving reconstructions')
    output.reconstructions(model, original_x, save_dir, img_inverse_transform)
def run():
    experiment_name = 'stl'

    img_size = 64
    epoch_size = 250
    batch_size = 64
    train_feed = dataset.stl.unlabeled_feed(img_size,
                                            batch_size=batch_size,
                                            epoch_size=epoch_size,
                                            n_augment=int(5e5))
    _, test_feed = dataset.stl.supervised_feed(img_size)

    model, experiment_name = aegan.build_model(
        experiment_name,
        img_size,
        n_hidden=128,
        recon_depth=9,
        recon_vs_gan_weight=1e-6,
        real_vs_gen_weight=0.5,
        discriminate_ae_recon=False,
        discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)

    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model,
        output_dir,
        train_feed,
        test_feed,
        n_epochs=200,
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)
def run():
    experiment_name = 'cifar'

    img_size = 32
    epoch_size = 250
    batch_size = 64
    train_input, test_input, _ = dataset.cifar.inputs(
        split='test', batch_size=batch_size, epoch_size=epoch_size
    )

    model, experiment_name = aegan.build_model(
        experiment_name, img_size, n_hidden=128, recon_depth=6,
        recon_vs_gan_weight=5e-6, real_vs_gen_weight=0.66,
    )
    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model, output_dir, train_input, test_input,
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)
def run():
    experiment_name = 'celeba'

    img_size = 64
    epoch_size = 250
    batch_size = 64
    n_augment = int(6e5)
    train_feed, test_feed = dataset.celeba.feeds(
        img_size,
        split='test',
        batch_size=batch_size,
        epoch_size=epoch_size,
        n_augment=n_augment,
    )
    n_hidden = 128
    model, experiment_name = aegan.build_model(
        experiment_name,
        img_size,
        n_hidden=n_hidden,
        recon_depth=9,
        recon_vs_gan_weight=1e-6,
        real_vs_gen_weight=0.5,
        discriminate_ae_recon=False,
        discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model,
        output_dir,
        train_feed,
        test_feed,
        n_epochs=250,
        lr_start=0.025,
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)

    print('Extracting visual attribute vectors')
    model.phase = 'test'
    train_feed, test_feed = dataset.celeba.feeds(
        img_size,
        batch_size=batch_size,
        epoch_size=epoch_size,
        with_attributes=True,
        split='test',
    )

    n_attr_imgs = 10000
    x = img_transform(train_feed.x[:n_attr_imgs], to_bc01=False)
    y = train_feed.y[:n_attr_imgs]
    z = model.encode(x)

    all_attributes = list(dp.dataset.CelebA().attribute_names)
    selected_attributes = [
        'Bald',
        'Bangs',
        'Black_Hair',
        'Blond_Hair',
        'Bushy_Eyebrows',
        'Eyeglasses',
        'Gray_Hair',
        'Heavy_Makeup',
        'High_Cheekbones',
        'Male',
        'Mustache',
        'Pale_Skin',
        'Rosy_Cheeks',
        'Smiling',
        'Straight_Hair',
        'Wavy_Hair',
        'Wearing_Lipstick',
        'Young',
    ]
    attr_idxs = [all_attributes.index(attr) for attr in selected_attributes]
    attr_vecs = []
    for attr_idx in attr_idxs:
        on_mask = y[:, attr_idx] == 1.0
        off_mask = np.logical_not(on_mask)
        vec = (np.mean(z[on_mask, :], axis=0, dtype=float) -
               np.mean(z[off_mask, :], axis=0, dtype=float))
        attr_vecs.append(vec)

    print('Outputting visual attribute vectors')
    original_x = test_feed.batches().next()[0]
    original_z = model.encode(original_x)
    attributes_dir = os.path.join(output_dir, 'attributes')
    if not os.path.exists(attributes_dir):
        os.mkdir(attributes_dir)
    for attr_idx, attr_vec in zip(attr_idxs, attr_vecs):
        attr_name = all_attributes[attr_idx].lower()
        attrs_z = original_z + attr_vec
        attrs_x = model.decode(attrs_z.astype(dp.float_))
        attrs_x = img_inverse_transform(attrs_x)
        for i, attr_x in enumerate(attrs_x):
            path = os.path.join(attributes_dir, '%.3d_%s.png' % (i, attr_name))
            sp.misc.imsave(path, attr_x)
def run():
    experiment_name = 'celeba'

    img_size = 64
    epoch_size = 25
    batch_size = 18 # This batch size divides the test data without remainder

    n_hidden = 128
    _, experiment_name = aegan.build_model(
        experiment_name, img_size, n_hidden=n_hidden, recon_depth=9,
        recon_vs_gan_weight=1e-6, real_vs_gen_weight=0.5,
        discriminate_ae_recon=False, discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)

    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Loading model from disk')
    print(model_path)
    with open(model_path, 'rb') as f:
        model = pickle.load(f)

    print('Getting data feed')
    model.phase = 'test'
    train_feed, test_feed = dataset.celeba.feeds(
        img_size, batch_size=batch_size, epoch_size=epoch_size,
        split='test', n_augment=0
    )

    save_dir = os.path.join(output_dir, 'MSE-Results')
    if not os.path.expanduser(save_dir):
        os.mkdir(save_dir)

    t = time.time()

    # Data in [-1,1] range
    mse_train = 0.0
    j = 0
    # This loop only iterates once
    for batch in train_feed.batches():
        original_x = np.array(batch[0])

        recon_x = model.decode(model.encode(original_x))

        mse_train += np.sum(np.mean((original_x - recon_x) ** 2, axis=(1, 2, 3)))
        j += 1
    mse_train /= batch_size * j

    # NLL code from model/ae.py NLLNormal class
    c = -0.5 * np.log(2 * np.pi)
    sigma = 1.0
    multiplier = 1.0 / (2.0 * sigma ** 2)

    nll_test = 0.0
    mse_test = 0.0
    mse_array = np.zeros(test_feed.n_samples*epoch_size)
    mse_array_255 = np.zeros(test_feed.n_samples*epoch_size)

    # Sanity checks on different ways to compute the mse
    # The 0_255 version will be a bit off because the network output is float and the img_inverse_transform function
    # will clamp the values to uint8, loosing some information in the process
    compute_extra_mse = False
    mse_test_mean_ite = 0.0
    mse_test_0_1 = 0.0
    mse_test_255 = 0.0

    j = 0
    t = time.time()
    for i in xrange(epoch_size):
        print(i,epoch_size,time.time() - t)
        t = time.time()
        for batch in test_feed.batches():
            original_x = np.array(batch[0])

            recon_x = model.decode(model.encode(original_x))

            mse_array[j*batch_size:(j+1)*batch_size] = np.mean((original_x - recon_x) ** 2, axis=(1, 2, 3))
            mse_test += np.sum(mse_array[j*batch_size:(j+1)*batch_size])


            if compute_extra_mse:
                mse_test_mean_ite += np.mean(np.mean((original_x - recon_x) ** 2, axis=(1, 2, 3)))
                mse_test_0_1 += np.sum(np.mean(((original_x - recon_x)*0.5) ** 2, axis=(1, 2, 3)))

            original_x_255 = img_inverse_transform(original_x).astype(original_x.dtype)
            recon_x_255 = img_inverse_transform(recon_x).astype(recon_x.dtype)
            mse_array_255[j*batch_size:(j+1)*batch_size] = np.mean((original_x_255 - recon_x_255) ** 2, axis=(1, 2, 3))
            mse_test_255 += np.sum(mse_array_255[j*batch_size:(j+1)*batch_size])

            # c - multiplier*(pred - target)**2
            tmp = original_x - recon_x
            tmp **= 2.0
            tmp *= -multiplier
            tmp += c

            # axis = tuple(range(1, len(original_x.shape)))
            # nll_test += np.sum(tmp, axis=axis)
            nll_test += np.sum(tmp)

            j += 1
    mse_test /= batch_size * j
    mse_test_std = np.std(mse_array)
    mse_test_mean_ite /= j
    mse_test_0_1 /= batch_size * j
    mse_test_std_255 = mse_array_255.std()
    mse_test_255 = mse_array_255.mean()
    nll_test /= batch_size * j

    total_time = time.time() - t

    def print_write(file, line):
        print(line)
        file.write(line + "\n")

    with open(os.path.join(save_dir, 'mse{}.txt'.format(epoch_size)), "w") as file:
        line = "Num batches: {}, num samples: {}, time: {} minutes".format(j, test_feed.n_samples, total_time / 60.0)
        print_write(file, line)

        line = "MSE train: {}, MSE test: +-{}, with mean per ite {} ".format(mse_train, mse_test, mse_test_std, mse_test_mean_ite)
        print_write(file, line)

        line = "NLL test: {}".format(nll_test)
        print_write(file, line)

        # ((x + 1)/2 - (y + 1)/2)^2 = (x/2 - y/2)^2 = ((x - y)*1/2)^2 = [(x - y)^2]*[(1/2)^2] = (1/4)*(x-y)^2
        line = "MSE for [0,1]: {}, hardcoded {}".format(mse_test / 4.0, mse_test_0_1)
        print_write(file, line)

        # (((x + 1)*0.5*255 - (y +1) 0.5* 255)^2 = ((0.5*255)^2) * (x-y)^2
        line = "MSE for [0,255]: {} +-{}, hardcoded {}".format(mse_test * ((0.5*255) ** 2), mse_test_std_255, mse_test_255)
        print_write(file, line)


    np.savez(os.path.join(save_dir, 'mse{}.npz'.format(epoch_size)), mse_train=mse_train, mse_test=mse_test, nll_test=nll_test,
             total_time=total_time, mse_test_std=mse_test_std, mse_array=mse_array, mse_array_255=mse_array_255)
def run():
    experiment_name = 'celeba'

    img_size = 64
    epoch_size = 250
    batch_size = 64
    n_augment = int(6e5)
    train_feed, test_feed = dataset.celeba.feeds(
        img_size, split='test', batch_size=batch_size, epoch_size=epoch_size,
        n_augment=n_augment,
    )
    n_hidden = 128
    model, experiment_name = aegan.build_model(
        experiment_name, img_size, n_hidden=n_hidden, recon_depth=9,
        recon_vs_gan_weight=1e-6, real_vs_gen_weight=0.5,
        discriminate_ae_recon=False, discriminate_sample_z=True,
    )
    print('experiment_name: %s' % experiment_name)
    output_dir = os.path.join('out', experiment_name)
    aegan.train(
        model, output_dir, train_feed, test_feed, n_epochs=250,
        lr_start=0.025,
    )
    model_path = os.path.join(output_dir, 'arch.pickle')
    print('Saving model to disk')
    print(model_path)
    with open(model_path, 'wb') as f:
        pickle.dump(model, f)

    print('Extracting visual attribute vectors')
    model.phase = 'test'
    train_feed, test_feed = dataset.celeba.feeds(
        img_size, batch_size=batch_size, epoch_size=epoch_size,
        with_attributes=True, split='test',
    )

    n_attr_imgs = 10000
    x = img_transform(train_feed.x[:n_attr_imgs], to_bc01=False)
    y = train_feed.y[:n_attr_imgs]
    z = model.encode(x)

    all_attributes = list(dp.dataset.CelebA().attribute_names)
    selected_attributes = [
        'Bald', 'Bangs', 'Black_Hair', 'Blond_Hair', 'Bushy_Eyebrows',
        'Eyeglasses', 'Gray_Hair', 'Heavy_Makeup', 'High_Cheekbones', 'Male',
        'Mustache', 'Pale_Skin', 'Rosy_Cheeks', 'Smiling', 'Straight_Hair',
        'Wavy_Hair', 'Wearing_Lipstick', 'Young',
    ]
    attr_idxs = [all_attributes.index(attr) for attr in selected_attributes]
    attr_vecs = []
    for attr_idx in attr_idxs:
        on_mask = y[:, attr_idx] == 1.0
        off_mask = np.logical_not(on_mask)
        vec = (np.mean(z[on_mask, :], axis=0, dtype=float) -
               np.mean(z[off_mask, :], axis=0, dtype=float))
        attr_vecs.append(vec)

    print('Outputting visual attribute vectors')
    original_x = test_feed.batches().next()[0]
    original_z = model.encode(original_x)
    attributes_dir = os.path.join(output_dir, 'attributes')
    if not os.path.exists(attributes_dir):
        os.mkdir(attributes_dir)
    for attr_idx, attr_vec in zip(attr_idxs, attr_vecs):
        attr_name = all_attributes[attr_idx].lower()
        attrs_z = original_z + attr_vec
        attrs_x = model.decode(attrs_z.astype(dp.float_))
        attrs_x = img_inverse_transform(attrs_x)
        for i, attr_x in enumerate(attrs_x):
            path = os.path.join(attributes_dir, '%.3d_%s.png' % (i, attr_name))
            sp.misc.imsave(path, attr_x)