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 encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() with chainer.no_backprop_mode() and encoder.reverse() as decoder: while True: z = xp.random.normal(0, args.temperature, size=( 1, 3, ) + hyperparams.image_size).astype("float32") x, _ = decoder.reverse_step(z) x_img = make_uint8(x.data[0], num_bins_x) plt.imshow(x_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 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=1) print(tabulate([["#image", len(dataset)]])) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() fig = plt.figure(figsize=(8, 4)) left = fig.add_subplot(1, 2, 1) right = fig.add_subplot(1, 2, 2) 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) # for zi in factorized_z: # noise = xp.random.normal( # 0, 0.2, size=zi.shape).astype("float32") # zi.data += noise rev_x, _ = decoder.reverse_step(factorized_z) x_img = make_uint8(x[0], num_bins_x) rev_x_img = make_uint8(rev_x.data[0], num_bins_x) left.imshow(x_img, interpolation="none") right.imshow(rev_x_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=1) print(tabulate([["#image", len(dataset)]])) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() with chainer.no_backprop_mode() and encoder.reverse() as decoder: for data_indices in iterator: print("data:", data_indices) 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) for (_, mean, ln_var) in factorized_z_distribution: print(xp.mean(mean.data), xp.mean(xp.exp(ln_var.data)))
def forward_and_reverse_output_shape(self, in_channel, data, levels=3, depth=4): glow = Glow(in_channel, levels, depth) z, logdet, eps = glow(data) height, width = data.shape[2], data.shape[3] """ cifar example: Level = 3 initial shape -> [4, 3, 32, 32] iter 1 -> z: [4, 12, 16, 16] because of squeeze from outside the loop iter 2 -> z: [4, 24, 8, 8] because of squeeze + split iter 3 -> z: [4, 48, 4, 4] because of squeeze + split """ assert list(z.shape) == [4, in_channel * 4 * 2**(levels - 1), 4, 4] assert list(logdet.shape) == [4] # because batch_size = 4 assert len( eps ) == levels - 1 # because L = 3 and split is executed whenever < L, i.e 2 times in total factor = 1 for e in eps: factor *= 2 # example: first eps -> from iter 1 take z shape and divide channel by 2: [4, 12/2, 16, 16] assert list(e.shape) == [ 4, in_channel * factor, height / factor, width / factor ] """ In total depth * levels = 4 * 3 = 12, so we got 12 instances of actnorm, inconv and affinecoupling Actnorm = 2 trainable parameters Invconv = 3 trainable parameter Affinecoupling = 6 trainable parameters (got 3 conv layers, each layer has weight + bias, so for all layers combined we get 6 in total) Zeroconv = 4 (2 conv layers, each with weight + bias) 12 * (2+3+6) + 4= 136 """ assert len(list( glow.parameters())) == (levels * depth) * (2 + 3 + 6) + 4 for param in glow.parameters(): assert param.requires_grad # reverse # For cifar we expect z with level=3 to be of shape [4,48,4,4] z = glow.reverse(z, eps) assert list(z.shape) == [4, 3, 32, 32]
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 encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() temperatures = [0.0, 0.25, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0] total = len(temperatures) fig = plt.figure(figsize=(total * 4, 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: z_batch = [] for temperature in temperatures: z = np.random.normal(0, temperature, size=(3, ) + hyperparams.image_size).astype("float32") z_batch.append(z) z_batch = np.asanyarray(z_batch) if using_gpu: z_batch = cuda.to_gpu(z_batch) x, _ = decoder.reverse_step(z_batch) for n, (temperature, subplot) in enumerate(zip(temperatures, subplots)): x_img = make_uint8(x.data[n], num_bins_x) # x_img = np.broadcast_to(x_img, (28, 28, 3)) subplot.imshow(x_img, interpolation="none") subplot.set_title("temperature={}".format(temperature)) plt.pause(.01)
conv_lu=not args.no_lu) model = nn.DataParallel(model_single) # model = model_single model = model.to(device) model.load_state_dict( torch.load('checkpoint/model.pt', map_location=lambda storage, loc: storage)) optimizer = optim.Adam(model.parameters(), lr=args.lr) torch.cuda.set_device(0) # adding this line fixed it optimizer.load_state_dict( torch.load('checkpoint/optim.pt', map_location=lambda storage, loc: storage)) model.eval() for i in range(3): z_sample = [] z_shapes = calc_z_shapes(3, args.img_size, args.n_flow, args.n_block) for z in z_shapes: z_new = torch.randn(args.n_sample, *z) * args.temp z_sample.append(z_new.to(device)) with torch.no_grad(): utils.save_image( model_single.reverse(z_sample).cpu().data, f'sample/{str(i + 1).zfill(6)}.png', normalize=True, nrow=10, range=(-0.5, 0.5), ) # train(args, model, optimizer)
n_bins = 2.0**args.n_bits img1 = Image.open(path + '12.jpg') img2 = Image.open(path + '13.jpg') img1 = preprocess(img1, transform, n_bins) img2 = preprocess(img2, transform, n_bins) images = [img1, img2] z_list = [] for image in images: log_p, logdet, z = model(image + torch.rand_like(image) / n_bins) z_list.append(z) z1 = z_list[0] z2 = z_list[1] all_rate = [0.1, 0.3, 0.5, 0.7, 0.9] img_concat = img1 for rate in all_rate: interpolate_z = [] for i in range(len(z1)): interpolate_z.append(torch.lerp(z1[i], z2[i], rate)) with torch.no_grad(): reconstruct_img = model_single.reverse(interpolate_z).cpu().data img_concat = torch.cat((img_concat, reconstruct_img), dim=0) img_concat = torch.cat((img_concat, img2), dim=0) utils.save_image( img_concat, "interpolate.png", normalize=True, nrow=7, range=(-0.5, 0.5), )
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 encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() total = hyperparams.levels + 1 fig = plt.figure(figsize=(4 * total, 4)) subplots = [] for n in range(total): subplot = fig.add_subplot(1, total, n + 1) subplots.append(subplot) def reverse_step(z, sampling=True): if isinstance(z, list): factorized_z = z else: factorized_z = encoder.factor_z(z) assert len(factorized_z) == len(encoder.blocks) out = None sum_logdet = 0 for block, zi in zip(encoder.blocks[::-1], factorized_z[::-1]): out, logdet = block.reverse_step( out, gaussian_eps=zi, squeeze_factor=encoder.hyperparams.squeeze_factor, sampling=sampling) sum_logdet += logdet return out, sum_logdet with chainer.no_backprop_mode() and encoder.reverse() as decoder: while True: base_z = xp.random.normal(0, args.temperature, size=( 1, 3, ) + hyperparams.image_size, dtype="float32") factorized_z = encoder.factor_z(base_z) rev_x, _ = decoder.reverse_step(factorized_z) rev_x_img = make_uint8(rev_x.data[0], num_bins_x) subplots[0].imshow(rev_x_img, interpolation="none") z = xp.copy(base_z) factorized_z = encoder.factor_z(z) for n in range(hyperparams.levels - 1): factorized_z[n] = xp.random.normal(0, args.temperature, size=factorized_z[n].shape, dtype="float32") rev_x, _ = decoder.reverse_step(factorized_z) rev_x_img = make_uint8(rev_x.data[0], num_bins_x) subplots[1].imshow(rev_x_img, interpolation="none") # for n in range(hyperparams.levels): # z = xp.copy(base_z) # factorized_z = encoder.factor_z(z) # for m in range(n + 1): # factorized_z[m] = xp.random.normal( # 0, # args.temperature, # size=factorized_z[m].shape, # dtype="float32") # # factorized_z[m] = xp.zeros_like(factorized_z[m]) # out = None # for k, (block, zi) in enumerate( # zip(encoder.blocks[::-1], factorized_z[::-1])): # sampling = False # out, _ = block.reverse_step( # out, # gaussian_eps=zi, # squeeze_factor=encoder.hyperparams.squeeze_factor, # sampling=sampling) # rev_x = out # rev_x_img = make_uint8(rev_x.data[0], num_bins_x) # subplots[n + 1].imshow(rev_x_img, interpolation="none") for n in range(hyperparams.levels): z = xp.copy(base_z) factorized_z = encoder.factor_z(z) factorized_z[n] = xp.random.normal(0, args.temperature, size=factorized_z[n].shape, dtype="float32") factorized_z[n] = xp.zeros_like(factorized_z[n]) out = None for k, (block, zi) in enumerate( zip(encoder.blocks[::-1], factorized_z[::-1])): sampling = False if k == hyperparams.levels - n - 1 else True out, _ = block.reverse_step( out, gaussian_eps=zi, squeeze_factor=encoder.hyperparams.squeeze_factor, sampling=sampling) rev_x = out rev_x_img = make_uint8(rev_x.data[0], num_bins_x) subplots[n + 1].imshow(rev_x_img, interpolation="none") plt.pause(.01)
def main(): try: os.mkdir(args.ckpt) except: pass 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 num_pixels = 3 * hyperparams.image_size[0] * hyperparams.image_size[1] encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() # Load picture x = np.array(Image.open(args.img)).astype('float32') x = preprocess(x, hyperparams.num_bits_x) x = to_gpu(xp.expand_dims(x, axis=0)) x += xp.random.uniform(0, 1.0 / num_bins_x, size=x.shape) if True: # Print this image info: b = xp.zeros((1, 3, 128, 128)) z, fw_ldt = encoder.forward_step(x, b) fw_ldt -= math.log(num_bins_x) * num_pixels logpZ = 0 ez = [] factor_z = [] for (zi, mean, ln_var) in z: factor_z.append(zi.data) logpZ += cf.gaussian_nll(zi, mean, ln_var) ez.append(zi.data.reshape(-1, )) ez = np.concatenate(ez) logpZ2 = cf.gaussian_nll(ez, xp.zeros(ez.shape), xp.zeros(ez.shape)).data print(fw_ldt, logpZ, logpZ2) with encoder.reverse() as decoder: rx, _ = decoder.reverse_step(factor_z) rx_img = make_uint8(rx.data[0], num_bins_x) rx_img = Image.fromarray(rx_img) rx_img.save(args.t + 'ori_revx.png') np.save(args.t + 'ori_z.npy', ez.get()) # Construct epsilon class eps(chainer.Chain): def __init__(self, shape, glow_encoder): super().__init__() self.encoder = glow_encoder with self.init_scope(): self.b = chainer.Parameter(initializers.Zero(), (1, 3, 128, 128)) self.m = chainer.Parameter(initializers.One(), (3, 8, 8)) def forward(self, x): # b = cf.tanh(self.b) * 0.5 b = self.b # Not sure if implementation is wrong m = cf.softplus(self.m) # m = cf.repeat(m, 8, axis=2) # m = cf.repeat(m, 8, axis=1) # m = cf.repeat(m, 16, axis=2) # m = cf.repeat(m, 16, axis=1) # b = b * m # cur_x = cf.add(x, b) # cur_x = cf.clip(cur_x, -0.5,0.5) z = [] zs, logdet = self.encoder.forward_step(x, b) for (zi, mean, ln_var) in zs: z.append(zi) z = merge_factorized_z(z) # return z, zs, logdet, cf.batch_l2_norm_squared(b), xp.tanh(self.b.data*1), cur_x, m return z, zs, logdet, xp.sum(xp.abs(b.data)), self.b.data * 1, m, x def save(self, path): filename = 'loss_model.hdf5' self.save_parameter(path, filename, self) def save_parameter(self, path, filename, params): tmp_filename = str(uuid.uuid4()) tmp_filepath = os.path.join(path, tmp_filename) save_hdf5(tmp_filepath, params) os.rename(tmp_filepath, os.path.join(path, filename)) epsilon = eps(x.shape, encoder) if using_gpu: epsilon.to_gpu() # optimizer = Optimizer(epsilon) optimizer = optimizers.Adam().setup(epsilon) # optimizer = optimizers.SGD().setup(epsilon) epsilon.b.update_rule.hyperparam.lr = 0.01 epsilon.m.update_rule.hyperparam.lr = 0.1 print('init finish') training_step = 0 z_s = [] b_s = [] loss_s = [] logpZ_s = [] logDet_s = [] m_s = [] j = 0 for iteration in range(args.total_iteration): epsilon.cleargrads() z, zs, fw_ldt, b_norm, b, m, cur_x = epsilon.forward(x) fw_ldt -= math.log(num_bins_x) * num_pixels logpZ1 = 0 factor_z = [] for (zi, mean, ln_var) in zs: factor_z.append(zi.data) logpZ1 += cf.gaussian_nll(zi, mean, ln_var) logpZ2 = cf.gaussian_nll(z, xp.zeros(z.shape), xp.zeros(z.shape)).data # logpZ2 = cf.gaussian_nll(z, np.mean(z), np.log(np.var(z))).data logpZ = (logpZ2 + logpZ1) / 2 loss = b_norm + (logpZ - fw_ldt) loss.backward() optimizer.update() training_step += 1 z_s.append(z.get()) b_s.append(cupy.asnumpy(b)) m_s.append(cupy.asnumpy(m.data)) loss_s.append(_float(loss)) logpZ_s.append(_float(logpZ)) logDet_s.append(_float(fw_ldt)) printr( "Iteration {}: loss: {:.6f} - b_norm: {:.6f} - logpZ: {:.6f} - logpZ1: {:.6f} - logpZ2: {:.6f} - log_det: {:.6f} - logpX: {:.6f}\n" .format(iteration + 1, _float(loss), _float(b_norm), _float(logpZ), _float(logpZ1), _float(logpZ2), _float(fw_ldt), _float(logpZ) - _float(fw_ldt))) if iteration % 100 == 99: np.save(args.ckpt + '/' + str(j) + 'z.npy', z_s) np.save(args.ckpt + '/' + str(j) + 'b.npy', b_s) np.save(args.ckpt + '/' + str(j) + 'loss.npy', loss_s) np.save(args.ckpt + '/' + str(j) + 'logpZ.npy', logpZ_s) np.save(args.ckpt + '/' + str(j) + 'logDet.npy', logDet_s) # cur_x = make_uint8(cur_x[0].data, num_bins_x) # np.save(args.ckpt + '/'+str(j)+'image.npy', cur_x) np.save(args.ckpt + '/' + str(j) + 'm.npy', m_s) with encoder.reverse() as decoder: rx, _ = decoder.reverse_step(factor_z) rx_img = make_uint8(rx.data[0], num_bins_x) np.save(args.ckpt + '/' + str(j) + 'res.npy', rx_img) z_s = [] b_s = [] loss_s = [] logpZ_s = [] logDet_s = [] m_s = [] j += 1 epsilon.save(args.ckpt)
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 encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() total = hyperparams.levels 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: seed = int(time.time()) for level in range(1, hyperparams.levels): xp.random.seed(seed) z = xp.random.normal(0, args.temperature, size=( 1, 3, ) + hyperparams.image_size, dtype="float32") factorized_z = glow.nn.functions.factor_z( z, level + 1, squeeze_factor=hyperparams.squeeze_factor) out = glow.nn.functions.unsqueeze( factorized_z.pop(-1), factor=hyperparams.squeeze_factor, module=xp) for n, zi in enumerate(factorized_z[::-1]): block = encoder.blocks[level - n - 1] out, _ = block.reverse_step( out, gaussian_eps=zi, squeeze_factor=hyperparams.squeeze_factor) rev_x = out rev_x_img = make_uint8(rev_x.data[0], num_bins_x) subplot = subplots[level - 1] subplot.imshow(rev_x_img, interpolation="none") subplot.set_title("level = {}".format(level)) # original #levels xp.random.seed(seed) z = xp.random.normal(0, args.temperature, size=( 1, 3, ) + hyperparams.image_size, dtype="float32") factorized_z = encoder.factor_z(z) rev_x, _ = decoder.reverse_step(factorized_z) rev_x_img = make_uint8(rev_x.data[0], num_bins_x) subplot = subplots[-1] subplot.imshow(rev_x_img, interpolation="none") subplot.set_title("level = {}".format(hyperparams.levels)) plt.pause(.01)
tmp_z = torch.index_select(z[i], 0, wo_attr_idx) tmp_z = torch.sum(tmp_z, dim=0) wo_attr_z[i] += tmp_z print(iters) if iters == 30: break # calculate mean for i in range(len(with_attr_z)): with_attr_z[i] = with_attr_z[i] / with_attr_cnt wo_attr_z[i] = wo_attr_z[i] / wo_attr_cnt img_concat = torch.tensor([]) rates = [0, 0.5, 1, 1.5, 2] for rate in rates: latent = [] for i in range(len(with_attr_z)): val = with_attr_z[i] - rate * wo_attr_z[i] latent.append(val.unsqueeze(0).cuda()) rec_img = model_single.reverse(latent).cpu().data img_concat = torch.cat((img_concat, rec_img), dim=0) utils.save_image( img_concat, "attriube_manipulation.png", normalize=True, nrow=5, range=(-0.5, 0.5), )
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 num_pixels = 3 * hyperparams.image_size[0] * hyperparams.image_size[1] # Get Dataset: if True: 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=1) print(tabulate([["#image", len(dataset)]])) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() ori_x = [] enc_z = [] rev_x = [] fw_logdet = [] logpZ = [] logpZ2 = [] i = 0 with chainer.no_backprop_mode() and encoder.reverse() as decoder: for data_indices in iterator: i += 1 x = to_gpu(dataset[data_indices]) # 1x3x64x64 x += xp.random.uniform(0, 1.0 / num_bins_x, size=x.shape) x_img = make_uint8(x[0], num_bins_x) ori_x.append(x_img) factorized_z_distribution, fw_ldt = encoder.forward_step(x) fw_ldt -= math.log(num_bins_x) * num_pixels fw_logdet.append(cupy.asnumpy(fw_ldt.data)) factor_z = [] ez = [] nll = 0 for (zi, mean, ln_var) in factorized_z_distribution: nll += cf.gaussian_nll(zi, mean, ln_var) factor_z.append(zi.data) ez.append(zi.data.reshape(-1, )) ez = np.concatenate(ez) enc_z.append(ez.get()) logpZ.append(cupy.asnumpy(nll.data)) logpZ2.append( cupy.asnumpy( cf.gaussian_nll(ez, np.mean(ez), np.log(np.var(ez))).data)) rx, _ = decoder.reverse_step(factor_z) rx_img = make_uint8(rx.data[0], num_bins_x) rev_x.append(rx_img) if i % 100 == 0: np.save(str(i) + '/ori_x.npy', ori_x) fw_logdet = np.array(fw_logdet) np.save(str(i) + '/fw_logdet.npy', fw_logdet) np.save(str(i) + '/enc_z.npy', enc_z) logpZ = np.array(logpZ) np.save(str(i) + '/logpZ.npy', logpZ) logpZ2 = np.array(logpZ2) np.save(str(i) + '/logpZ2.npy', logpZ2) np.save(str(i) + '/rev_x.npy', rev_x) ori_x = [] enc_z = [] rev_x = [] fw_logdet = [] logpZ = [] logpZ2 = [] return
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 encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() with chainer.no_backprop_mode() and encoder.reverse() as decoder: # while True: # z = xp.random.normal( # 0, args.temperature, size=( # 1, # 3, # ) + hyperparams.image_size).astype("float32") # x, _ = decoder.reverse_step(z) # x_img = make_uint8(x.data[0], num_bins_x) # plt.imshow(x_img, interpolation="none") # plt.pause(.01) i = 0 j = 0 enc_z = [] rev_x = [] bk_logdet = [] logpZ2 = [] fw_logdet = [] sec_z = [] sec_pz = [] sec_pz2 = [] while j < 6: i += 1 z = xp.random.normal(0, args.temperature, size=( 1, 3, ) + hyperparams.image_size).astype("float32") enc_z.append(cupy.asnumpy(z)) lvar = xp.log(args.temperature) logpZ2.append(cupy.asnumpy(cf.gaussian_nll(z, 0, lvar).data)) x, blogd = decoder.reverse_step(z) x_img = make_uint8(x.data[0], num_bins_x) rev_x.append(x_img) bk_logdet.append(cupy.asnumpy(blogd.data)) factorized_z_distribution, fw_ldt = encoder.forward_step(x) fw_logdet.append(cupy.asnumpy(fw_ldt.data)) factor_z = [] ez = [] nll = 0 for (zi, mean, ln_var) in factorized_z_distribution: nll += cf.gaussian_nll(zi, mean, ln_var) factor_z.append(zi.data) ez.append(zi.data.reshape(-1, )) ez = np.concatenate(ez) sec_z.append(ez.get()) sec_pz.append(cupy.asnumpy(nll.data)) sec_pz2.append( cupy.asnumpy( cf.gaussian_nll(ez, np.mean(ez), np.log(np.var(ez))).data)) if i % 250 == 0: i = 0 j += 1 print('dataset: ', j) np.save('sample/' + str(j) + 'enc_z.npy', enc_z) np.save('sample/' + str(j) + 'rev_x.npy', rev_x) bk_logdet = cupy.asnumpy(bk_logdet) np.save('sample/' + str(j) + 'bk_logdet.npy', bk_logdet) logpZ2 = cupy.asnumpy(logpZ2) np.save('sample/' + str(j) + 'logpZ2.npy', logpZ2) fw_logdet = cupy.asnumpy(fw_logdet) np.save('sample/' + str(j) + 'fw_logdet.npy', fw_logdet) np.save('sample/' + str(j) + 'sec_z.npy', sec_z) sec_pz = cupy.asnumpy(sec_pz) np.save('sample/' + str(j) + 'sec_pz.npy', sec_pz) sec_pz2 = cupy.asnumpy(sec_pz2) np.save('sample/' + str(j) + 'sec_pz2.npy', sec_pz2) enc_z = [] rev_x = [] bk_logdet = [] logpZ2 = [] fw_logdet = [] sec_z = [] sec_pz = [] sec_pz2 = []
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=1) print(tabulate([["#image", len(dataset)]])) encoder = Glow(hyperparams, hdf5_path=args.snapshot_path) if using_gpu: encoder.to_gpu() fig = plt.figure(figsize=(8, 4)) left = fig.add_subplot(1, 2, 1) right = fig.add_subplot(1, 2, 2) 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) rev_x, _ = decoder.reverse_step(factorized_z) x_img = make_uint8(x[0], num_bins_x) rev_x_img = make_uint8(rev_x.data[0], num_bins_x) left.imshow(x_img, interpolation="none") right.imshow(rev_x_img, interpolation="none") plt.pause(.01)
image = image.to('cuda') log_p, logdet, out = model(image + torch.rand_like(image) / n_bins) loss, log_p, log_det = calc_loss(log_p, logdet, img_size, img_channels, n_bins) loss.backward() optimizer.step() writer.add_scalar('loss', loss.cpu().item(), i) i += 1 total_loss.append(loss.cpu().item()) break if plot: plt.scatter(i, loss.cpu().item(), color='blue') plt.pause(0.0001) with torch.no_grad(): img = model.reverse(z_sample) fig = get_fig(img) writer.add_figure('fixed sample Z', fig) with torch.no_grad(): log_p, logdet, total_out = model(image) img = model.reverse(total_out).cpu().data fig = get_fig(img[:4]) writer.add_figure('Inverted', fig) fig = get_fig(image[:4]) writer.add_figure('Original', fig) # Invertible ?
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)