def test_param_grid_random(): BOOLs = [True, False] encoder_kwargs = params_to_dict( linear_stack_bn=BOOLs, linear_stack_activation=activation_names, tail_bn=BOOLs, tail_activation=activation_names, ) decoder_kwargs = params_to_dict( linear_stack_bn=BOOLs, linear_stack_activation=activation_names, tail_bn=BOOLs, tail_activation=activation_names, ) param_grid = params_to_dict( latent_code_size=[2], learning_rate=[0.01], 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.loss_names, loss_type=['VAE'], encoder_kwargs=encoder_kwargs, # decoder_kwargs=decoder_kwargs ) param_grid = param_grid_random(param_grid, 50) for grid in param_grid: pprint(grid) pprint(len(param_grid))
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
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
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