def main(): xp = np using_gpu = args.gpu_device >= 0 if using_gpu: cuda.get_device(args.gpu_device).use() xp = cupy hyperparams = Hyperparameters(args.snapshot_path) hyperparams.print() num_bins_x = 2.0**hyperparams.num_bits_x image_size = (28, 28) _, test = chainer.datasets.mnist.get_mnist() images = [] labels = [] for entity in test: image, label = entity images.append(image) labels.append(label) labels = np.asarray(labels) images = 255.0 * np.asarray(images).reshape((-1, ) + image_size + (1, )) if hyperparams.num_image_channels != 1: images = np.broadcast_to(images, (images.shape[0], ) + image_size + (hyperparams.num_image_channels, )) images = preprocess(images, hyperparams.num_bits_x) # images = images[:200] # labels = labels[:200] sections = len(images) // 100 dataset_image = np.split(images, sections) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() fig = plt.figure(figsize=(8, 8)) t_sne_inputs = [] with chainer.no_backprop_mode(): for n, image_batch in enumerate(dataset_image): x = to_gpu(image_batch) x += xp.random.uniform(0, 1.0 / num_bins_x, size=x.shape) factorized_z_distribution, _ = encoder.forward_step(x) factorized_z = [] for (zi, mean, ln_var) in factorized_z_distribution: factorized_z.append(zi) z = encoder.merge_factorized_z(factorized_z, factor=hyperparams.squeeze_factor) z = z.reshape((-1, hyperparams.num_image_channels * 28 * 28)) z = to_cpu(z) t_sne_inputs.append(z) t_sne_inputs = np.asanyarray(t_sne_inputs).reshape( (-1, hyperparams.num_image_channels * 28 * 28)) print(t_sne_inputs.shape) z_reduced = TSNE(n_components=2, random_state=0).fit_transform(t_sne_inputs) print(z_reduced.shape) plt.scatter(z_reduced[:, 0], z_reduced[:, 1], c=labels, s=1, cmap="Spectral") plt.colorbar() plt.savefig("scatter.png")
def main(): xp = np using_gpu = args.gpu_device >= 0 if using_gpu: cuda.get_device(args.gpu_device).use() xp = cupy hyperparams = Hyperparameters(args.snapshot_path) hyperparams.print() num_bins_x = 2.0**hyperparams.num_bits_x image_size = (28, 28) images = chainer.datasets.mnist.get_mnist(withlabel=False)[0] images = 255.0 * np.asarray(images).reshape((-1, ) + image_size + (1, )) if hyperparams.num_image_channels != 1: images = np.broadcast_to(images, (images.shape[0], ) + image_size + (hyperparams.num_image_channels, )) images = preprocess(images, hyperparams.num_bits_x) dataset = glow.dataset.Dataset(images) iterator = glow.dataset.Iterator(dataset, batch_size=2) print(tabulate([["#image", len(dataset)]])) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() total = args.num_steps + 2 fig = plt.figure(figsize=(4 * total, 4)) subplots = [] for n in range(total): subplot = fig.add_subplot(1, total, n + 1) subplots.append(subplot) with chainer.no_backprop_mode() and encoder.reverse() as decoder: while True: for data_indices in iterator: x = to_gpu(dataset[data_indices]) x += xp.random.uniform(0, 1.0 / num_bins_x, size=x.shape) factorized_z_distribution, _ = encoder.forward_step(x) factorized_z = [] for (zi, mean, ln_var) in factorized_z_distribution: factorized_z.append(zi) z = encoder.merge_factorized_z(factorized_z) z_start = z[0] z_end = z[1] z_batch = [z_start] for n in range(args.num_steps): ratio = n / (args.num_steps - 1) z_interp = ratio * z_end + (1.0 - ratio) * z_start z_batch.append(args.temperature * z_interp) z_batch.append(z_end) z_batch = xp.stack(z_batch) rev_x_batch, _ = decoder.reverse_step(z_batch) for n in range(args.num_steps): rev_x_img = make_uint8(rev_x_batch.data[n + 1], num_bins_x) subplots[n + 1].imshow(rev_x_img, interpolation="none") x_start_img = make_uint8(x[0], num_bins_x) subplots[0].imshow(x_start_img, interpolation="none") x_end_img = make_uint8(x[-1], num_bins_x) subplots[-1].imshow(x_end_img, interpolation="none") plt.pause(.01)
def main(): xp = np using_gpu = args.gpu_device >= 0 if using_gpu: cuda.get_device(args.gpu_device).use() xp = cupy hyperparams = Hyperparameters(args.snapshot_path) hyperparams.print() num_bins_x = 2.0**hyperparams.num_bits_x assert args.dataset_format in ["png", "npy"] files = Path(args.dataset_path).glob("*.{}".format(args.dataset_format)) if args.dataset_format == "png": images = [] for filepath in files: image = np.array(Image.open(filepath)).astype("float32") image = preprocess(image, hyperparams.num_bits_x) images.append(image) assert len(images) > 0 images = np.asanyarray(images) elif args.dataset_format == "npy": images = [] for filepath in files: array = np.load(filepath).astype("float32") array = preprocess(array, hyperparams.num_bits_x) images.append(array) assert len(images) > 0 num_files = len(images) images = np.asanyarray(images) images = images.reshape((num_files * images.shape[1], ) + images.shape[2:]) else: raise NotImplementedError dataset = glow.dataset.Dataset(images) iterator = glow.dataset.Iterator(dataset, batch_size=2) print(tabulate([["#image", len(dataset)]])) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() total = args.num_steps + 2 fig = plt.figure(figsize=(4 * total, 4)) subplots = [] for n in range(total): subplot = fig.add_subplot(1, total, n + 1) subplots.append(subplot) with chainer.no_backprop_mode() and encoder.reverse() as decoder: while True: for data_indices in iterator: x = to_gpu(dataset[data_indices]) x += xp.random.uniform(0, 1.0 / num_bins_x, size=x.shape) factorized_z_distribution, _ = encoder.forward_step(x) factorized_z = [] for (zi, mean, ln_var) in factorized_z_distribution: factorized_z.append(zi) z = encoder.merge_factorized_z(factorized_z) z_start = z[0] z_end = z[1] z_batch = [args.temperature * z_start] for n in range(args.num_steps): ratio = n / (args.num_steps - 1) z_interp = ratio * z_end + (1.0 - ratio) * z_start z_batch.append(args.temperature * z_interp) z_batch.append(args.temperature * z_end) for z, subplot in zip(z_batch, subplots): z = z[None, ...] rev_x, _ = decoder.reverse_step(z) rev_x_img = make_uint8(rev_x.data[0], num_bins_x) subplot.imshow(rev_x_img, interpolation="none") plt.pause(.01)