Esempio n. 1
0
def test_CVAE_latent_space():
    x = train_set.Ys_index_label
    idxs_labels = []
    for i in range(10):
        idxs_label = np.where(x == i)
        idxs_labels += [(full_Xs[idxs_label], full_Ys[idxs_label])]

    plot = PlotTools()

    model = CVAE
    params = {
        'loss_type':          'VAE',
        'learning_rate':      0.01,
        'latent_code_size':   2,
        'encoder_net_shapes': (512, 256, 128, 64, 32),
        'encoder_kwargs':     {
            'tail_bn':                 False,
            'tail_activation':         'none',
            'linear_stack_bn':         False,
            'linear_stack_activation': 'lrelu',
        },
        'decoder_net_shapes': (32, 64, 128, 256, 512),
        'decoder_kwargs':     {
            'tail_bn':                 True,
            'tail_activation':         'sigmoid',
            'linear_stack_bn':         True,
            'linear_stack_activation': 'relu'
        },
        'batch_size':         256,
        # 'KL_D_rate': 0.01
    }

    ae = model(**params)
    n_iter = 50
    for i in range(n_iter):
        ae.train(full_Xs, full_Ys, epoch=1)
        metric = ae.metric(full_Xs, full_Ys)
        # ae.save(save_path)
        if np.isnan(metric):
            print(f'metric is {metric}')
            break
        print(metric)

        codes = []
        for x, y in idxs_labels:
            code = ae.code(x, y)
            codes += [code]

        plot.scatter_2d(*codes, title=f'CVAE_latent_space_epoch_{i}.png')
        for label, code in enumerate(codes):
            plot.scatter_2d(code, title=f'vae_latent_space_epoch_{i}_label+{label}.png')

    del ae
Esempio n. 2
0
def test_VAE_latent_space(n_iter=100):
    dataset_path = """C:\\Users\\demetoir_desktop\\PycharmProjects\\MLtools\\data\\MNIST"""
    dataset_pack = MNIST().load(dataset_path)
    dataset_pack.shuffle()
    train_set = dataset_pack['train']
    full_Xs, full_Ys = train_set.full_batch()

    x = train_set.Ys_index_label
    idxs_labels = []
    for i in range(10):
        idxs_label = np.where(x == i)
        idxs_labels += [(full_Xs[idxs_label], full_Ys[idxs_label])]

    plot = PlotTools()

    model = VAE
    params = {
        'loss_type': 'VAE',
        'learning_rate': 0.01,
        'latent_code_size': 2,
        'encoder_net_shapes': (512, 256, 128, 64, 32),
        'encoder_kwargs': {
            'tail_bn': False,
            'tail_activation': 'lrelu',
            'linear_stack_bn': False,
            'linear_stack_activation': 'lrelu',
        },
        'decoder_net_shapes': (32, 64, 128, 256, 512),
        'decoder_kwargs': {
            'tail_bn': True,
            'tail_activation': 'sigmoid',
            'linear_stack_bn': True,
            'linear_stack_activation': 'relu'
        },
        'batch_size': 256,
        # 'KL_D_rate': 0.01
    }
    ae = model(**params)
    for i in range(n_iter):
        ae.train(full_Xs, epoch=1)
        # ae.save(save_path)
        metric = ae.metric(full_Xs)
        if np.isnan(metric):
            print(f'metric is {metric}')
            break
        print(metric)

        codes = [ae.code(x) for x, y in idxs_labels]
        plot.scatter_2d(*codes, title=f'vae_latent_space_epoch_{i}.png')

    del ae
Esempio n. 3
0
def test_VAE_latent_space_grid_search(n_iter=6):
    dataset_path = """C:\\Users\\demetoir_desktop\\PycharmProjects\\MLtools\\data\\MNIST"""
    dataset_pack = MNIST().load(dataset_path)
    dataset_pack.shuffle()
    train_set = dataset_pack['train']
    full_Xs, full_Ys = train_set.full_batch()

    x = train_set.Ys_index_label
    idxs_labels = []
    for i in range(10):
        idxs_label = np.where(x == i)
        idxs_labels += [(full_Xs[idxs_label], full_Ys[idxs_label])]

    plot = PlotTools()

    model = VAE
    save_path = './CVAE.pkl'

    BOOLs = [True, False]
    linear_stack_activation = ['sigmoid', 'tanh', 'relu', 'lrelu', 'elu']
    param_grid = params_to_dict(
        latent_code_size=[2],
        learning_rate=[0.005],
        encoder_net_shapes=[(512, 256, 128, 64, 32)],
        decoder_net_shapes=[(32, 64, 128, 256, 512)],
        batch_size=[256],
        # KL_D_rate=[0.01],
        loss_type=['VAE'],
        encoder_kwargs=params_to_dict(
            linear_stack_bn=[False],
            linear_stack_activation=['relu', 'lrelu', 'elu'],
            tail_bn=[False],
            tail_activation=['lrelu', 'none'],
        ),
        decoder_kwargs=params_to_dict(
            linear_stack_bn=[True],
            linear_stack_activation=['relu'],
            tail_bn=[False],
            tail_activation=['sigmoid'],
        ))
    param_grid = param_grid_full(param_grid)

    df = DF({'params': list(param_grid)})
    df.to_csv('./params.csv')

    for param_idx, params in enumerate(param_grid):
        pprint(param_idx, params)

        ae = model(**params)
        for i in range(50):
            ae.train(full_Xs, epoch=1)
            # ae.save(save_path)
            metric = ae.metric(full_Xs)
            if np.isnan(metric):
                print(f'param_idx:{param_idx}, metric is {metric}')
                break
            print(metric)

            codes = []
            for x, y in idxs_labels:
                code = ae.code(x)
                codes += [code]

            plot.scatter_2d(
                *codes,
                title=f'param_idx_{param_idx}_vae_latent_space_epoch_{i}.png')

        del ae
Esempio n. 4
0
def test_AAE_latent_space():
    x = train_set.Ys_index_label
    idxs_labels = []
    for i in range(10):
        idxs_label = np.where(x == i)
        idxs_labels += [(full_Xs[idxs_label], full_Ys[idxs_label])]

    plot = PlotTools()

    model = AAE
    save_path = './CVAE.pkl'

    BOOLs = [True, False]
    linear_stack_activation = ['sigmoid', 'tanh', 'relu', 'lrelu', 'elu']
    param_grid = params_to_dict(
        latent_code_size=[2],
        learning_rate=[0.01],
        encoder_net_shapes=[(512, 256)],
        decoder_net_shapes=[(256, 512)],
        batch_size=[100],
        # KL_D_rate=[0.01],
        encoder_kwargs=params_to_dict(
            linear_stack_bn=[False],
            linear_stack_activation=['relu'],
            tail_bn=[False],
            tail_activation=['none'],
        ),
        decoder_kwargs=params_to_dict(
            linear_stack_bn=[False],
            linear_stack_activation=['relu'],
            tail_bn=[False],
            tail_activation=['sigmoid'],
        ))
    param_grid = param_grid_full(param_grid)

    df = DF({'params': list(param_grid)})
    df.to_csv('./params.csv')

    for param_idx, params in enumerate(param_grid):
        pprint(param_idx, params)

        ae = model(**params)
        for i in range(100):
            ae.train(full_Xs, full_Ys, epoch=1)
            # ae.save(save_path)
            metric = ae.metric(full_Xs, full_Ys)
            if any([np.isnan(val) for val in metric.values()]):
                print(f'param_idx:{param_idx}, metric is {metric}')
                break
            print(metric)

            codes = []
            for x, y in idxs_labels:
                code = ae.code(x)
                codes += [code]

            plot.scatter_2d(
                *codes,
                title=f'param_idx_{param_idx}/aae_latent_space_epoch_{i}.png')
            # for label, code in enumerate(codes):
            #     plot.scatter_2d(code, title=f'param_idx_{param_idx}/aae_latent_space_epoch_{i}_label+{label}.png')

            recon = ae.recon(sample_Xs, sample_Ys)
            gen = ae.generate(full_Ys[:30])
            code_walk = np.concatenate(
                [ae.augmentation(sample_Xs, sample_Ys) for _ in range(5)],
                axis=0)
            recon_sharpen = ae.recon_sharpen(sample_Xs, sample_Ys)
            np_img = np.concatenate(
                [sample_Xs, recon, recon_sharpen, gen, code_walk])
            np_img = np_img_float32_to_uint8(np_img)

            file_name = f'./matplot/param_idx_{param_idx}/aae_img_epoch_{i}.png'
            plot.plot_image_tile(np_img, file_name, column=5)
            # sample_imgs = Xs_gen

        del ae
Esempio n. 5
0
def test_CVAE_latent_space_grid_search():
    x = train_set.Ys_index_label
    idxs_labels = []
    for i in range(10):
        idxs_label = np.where(x == i)
        idxs_labels += [(full_Xs[idxs_label], full_Ys[idxs_label])]

    plot = PlotTools()

    model = CVAE
    save_path = './CVAE.pkl'

    BOOLs = [True, False]
    linear_stack_activation = ['sigmoid', 'tanh', 'relu', 'lrelu', 'elu']
    param_grid = params_to_dict(

        latent_code_size=[2],
        learning_rate=[0.005],
        encoder_net_shapes=[(512, 256, 128, 64, 32)],
        decoder_net_shapes=[(32, 64, 128, 256, 512)],
        batch_size=[256],
        # KL_D_rate=[0.01],
        loss_type=['VAE'],
        encoder_kwargs=params_to_dict(
            linear_stack_bn=[False],
            linear_stack_activation=['relu', 'lrelu', 'elu'],
            tail_bn=[False],
            tail_activation=['lrelu', 'none'],
        ),
        decoder_kwargs=params_to_dict(
            linear_stack_bn=[True],
            linear_stack_activation=['relu'],
            tail_bn=[False],
            tail_activation=['sigmoid'],
        )
    )
    param_grid = param_grid_full(param_grid)

    df = DF({
        'params': list(param_grid)
    })
    df.to_csv('./params.csv')

    for param_idx, params in enumerate(param_grid):
        pprint(param_idx, params)

        ae = model(**params)
        for i in range(10):
            ae.train(full_Xs, full_Ys, epoch=1)
            # ae.save(save_path)
            metric = ae.metric(full_Xs, full_Ys)
            if np.isnan(metric):
                print(f'param_idx:{param_idx}, metric is {metric}')
                break
            print(metric)

            codes = []
            for x, y in idxs_labels:
                code = ae.code(x, y)
                codes += [code]

            plot.scatter_2d(*codes, title=f'param_idx_{param_idx}/vae_latent_space_epoch_{i}.png')
            # for label, code in enumerate(codes):
            #     plot.scatter_2d(code, title=f'param_idx_{param_idx}/vae_latent_space_epoch_{i}_label+{label}.png')

            recon = ae.recon(sample_Xs, sample_Ys)
            gen = ae.generate(sample_Ys)
            np_img = np.concatenate([sample_Xs, recon, gen])
            np_img = np_img_float32_to_uint8(np_img)

            # sample_imgs = Xs_gen
            file_name = f'./matplot/param_idx_{param_idx}/vae_img_epoch_{i}.png'
            tile = np_img_to_tile(np_img, column_size=5)
            np_image_save(tile, file_name)

        del ae