Exemplo n.º 1
0
def visualize_walkthrough():
	x_batch = sample_x_from_data_distribution(20)
	z_batch = gen(x_batch, test=True)
	if use_gpu:
		z_batch.to_cpu()

	fig = pylab.gcf()
	fig.set_size_inches(16.0, 16.0)
	pylab.clf()
	if config.img_channel == 1:
		pylab.gray()
	
	z_a = z_batch.data[:10,:]
	z_b = z_batch.data[10:,:]
	for col in range(10):
		_z_batch = z_a * (1 - col / 9.0) + z_b * col / 9.0
		_z_batch = Variable(_z_batch)
		if use_gpu:
			_z_batch.to_gpu()
		_x_batch = dec(_z_batch, test=True)
		if use_gpu:
			_x_batch.to_cpu()
		for row in range(10):
			pylab.subplot(10, 10, row * 10 + col + 1)
			if config.img_channel == 1:
				pylab.imshow(np.clip((_x_batch.data[row] + 1.0) / 2.0, 0.0, 1.0).reshape((config.img_width, config.img_width)), interpolation="none")
			elif config.img_channel == 3:
				pylab.imshow(np.clip((_x_batch.data[row] + 1.0) / 2.0, 0.0, 1.0).reshape((config.img_channel, config.img_width, config.img_width)), interpolation="none")
			pylab.axis("off")
				
	pylab.savefig("%s/walk_through.png" % args.visualization_dir)
Exemplo n.º 2
0
def visualize_walkthrough():
	x_batch = sample_x_from_data_distribution(20)
	z_batch = gen(x_batch, test=True)
	if use_gpu:
		z_batch.to_cpu()

	fig = pylab.gcf()
	fig.set_size_inches(16.0, 16.0)
	pylab.clf()
	if config.img_channel == 1:
		pylab.gray()
	
	z_a = z_batch.data[:10,:]
	z_b = z_batch.data[10:,:]
	for col in range(10):
		_z_batch = z_a * (1 - col / 9.0) + z_b * col / 9.0
		_z_batch = Variable(_z_batch)
		if use_gpu:
			_z_batch.to_gpu()
		_x_batch = dec(_z_batch, test=True)
		if use_gpu:
			_x_batch.to_cpu()
		for row in range(10):
			pylab.subplot(10, 10, row * 10 + col + 1)
			if config.img_channel == 1:
				pylab.imshow(np.clip((_x_batch.data[row] + 1.0) / 2.0, 0.0, 1.0).reshape((config.img_width, config.img_width)), interpolation="none")
			elif config.img_channel == 3:
				pylab.imshow(np.clip((_x_batch.data[row] + 1.0) / 2.0, 0.0, 1.0).reshape((config.img_channel, config.img_width, config.img_width)), interpolation="none")
			pylab.axis("off")
				
	pylab.savefig("%s/walk_through.png" % args.visualization_dir)
Exemplo n.º 3
0
def visualize_reconstruction():
    x_batch = sample_x_from_data_distribution(100)

    z_batch = gen(x_batch, test=True)
    _x_batch = dec(z_batch, test=True)
    if use_gpu:
        _x_batch.to_cpu()

    fig = pylab.gcf()
    fig.set_size_inches(16.0, 16.0)
    pylab.clf()
    if config.img_channel == 1:
        pylab.gray()
    for m in range(100):
        pylab.subplot(10, 10, m + 1)
        if config.img_channel == 1:
            pylab.imshow(np.clip((_x_batch.data[m] + 1.0) / 2.0, 0.0,
                                 1.0).reshape(
                                     (config.img_width, config.img_width)),
                         interpolation="none")
        elif config.img_channel == 3:
            pylab.imshow(np.clip(
                (_x_batch.data[m] + 1.0) / 2.0, 0.0, 1.0).reshape(
                    (config.img_channel, config.img_width, config.img_width)),
                         interpolation="none")
        pylab.axis("off")
    pylab.savefig("%s/reconstruction.png" % args.visualization_dir)
Exemplo n.º 4
0
def visualize_reconstruction():
	x_batch = sample_x_from_data_distribution(100)
	x_batch = dec(gen(x_batch, test=True), test=True)
	if use_gpu:
		x_batch.to_cpu()

	fig = pylab.gcf()
	fig.set_size_inches(16.0, 16.0)
	pylab.clf()
	if config.img_channel == 1:
		pylab.gray()
	for m in range(100):
		pylab.subplot(10, 10, m + 1)
		if config.img_channel == 1:
			pylab.imshow(np.clip((x_batch.data[m] + 1.0) / 2.0, 0.0, 1.0).reshape((config.img_width, config.img_width)), interpolation="none")
		elif config.img_channel == 3:
			pylab.imshow(np.clip((x_batch.data[m] + 1.0) / 2.0, 0.0, 1.0).reshape((config.img_channel, config.img_width, config.img_width)), interpolation="none")
		pylab.axis("off")
	pylab.savefig("%s/reconstruction.png" % args.visualization_dir)
Exemplo n.º 5
0
def train(dataset, labels):
	if config.n_z % 2 != 0:
		raise Exception("The dimension of the latent code z must be a multiple of 2.")
	batchsize = 100
	n_epoch = 10000
	n_train_each_epoch = 2000
	total_time = 0

	xp = cuda.cupy if config.use_gpu else np

	# Discriminatorの学習回数
	## 詳細は[Generative Adversarial Networks](http://arxiv.org/abs/1406.2661)
	n_steps_to_optimize_dis = 1

	# Use Adam
	optimizer_dec = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_dis = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_gen.setup(gen)
	optimizer_dec.setup(dec)
	optimizer_dis.setup(dis)
	optimizer_dec.add_hook(optimizer.WeightDecay(0.0001))
	optimizer_gen.add_hook(optimizer.WeightDecay(0.0001))
	optimizer_dis.add_hook(optimizer.WeightDecay(0.0001))

	start_epoch = 1 if args.load_epoch == 0 else args.load_epoch + 1

	for epoch in xrange(start_epoch, n_epoch):
		# Adversarial Networksの誤差
		sum_loss_regularization = 0
		# 復号誤差
		sum_loss_reconstruction = 0

		start_time = time.time()

		for i in xrange(0, n_train_each_epoch):

			# Sample minibatch of examples
			x_batch, label_index_batch, label_one_hot = sample_x_and_label_from_data_distribution(batchsize)

			# Reconstruction phase
			z_fake_batch = gen(x_batch)
			## 12d -> 2d
			_x_batch = dec(z_fake_batch)

			## 復号誤差を最小化する
			loss_reconstruction = F.mean_squared_error(x_batch, _x_batch)
			sum_loss_reconstruction += loss_reconstruction.data

			optimizer_dec.zero_grads()
			optimizer_gen.zero_grads()
			loss_reconstruction.backward()
			optimizer_dec.update()
			optimizer_gen.update()

			# Adversarial phase
			for k in xrange(n_steps_to_optimize_dis):
				if k > 0:
					x_batch, label_index_batch, label_one_hot = sample_x_and_label_from_data_distribution(batchsize)

				z_real_batch = util.sample_z_from_n_2d_gaussian_mixture(batchsize, config.n_z, label_index_batch, 10, config.use_gpu)
				z_real_labeled_batch = util.add_label(z_real_batch, label_one_hot)

				## Discriminator loss
				p_real_batch = dis(z_real_labeled_batch)
				## p_real_batch[0] -> 本物である度合い
				## p_real_batch[1] -> 偽物である度合い
				loss_dis_real = F.softmax_cross_entropy(p_real_batch, Variable(xp.zeros(batchsize, dtype=np.int32)))

				## 上で一度z_fake_batchは計算しているため省く
				if k > 0:
					z_fake_batch = gen(x_batch)
				z_fake_batch_labeled = util.add_label(z_fake_batch, label_one_hot)

				p_fake_batch = dis(z_fake_batch_labeled)
				## p_fake_batch[0] -> 本物である度合い
				## p_fake_batch[1] -> 偽物である度合い
				loss_dis_fake = F.softmax_cross_entropy(p_fake_batch, Variable(xp.ones(batchsize, dtype=np.int32)))

				loss_dis = loss_dis_fake + loss_dis_real
				sum_loss_regularization += loss_dis.data / float(k + 1)
				
				optimizer_dis.zero_grads()
				loss_dis.backward()
				optimizer_dis.update()

			## p_fake_batch[0] -> 本物である度合い
			## p_fake_batch[1] -> 偽物である度合い
			## generatorの学習では偽のデータを本物であると思い込ませる
			loss_gen = F.softmax_cross_entropy(p_fake_batch, Variable(xp.zeros(batchsize, dtype=np.int32)))
			sum_loss_regularization += loss_gen.data

			optimizer_gen.zero_grads()
			loss_gen.backward()
			optimizer_gen.update()

		# Saving the models
		print "epoch", epoch
		print "	reconstruction_loss", (sum_loss_reconstruction / n_train_each_epoch)
		print "	regularization_loss", (sum_loss_regularization / n_train_each_epoch)
		p_real_batch.to_cpu()
		p_real_batch = p_real_batch.data.transpose(1, 0)
		p_real_batch = np.exp(p_real_batch)
		sum_p_real_batch = p_real_batch[0] + p_real_batch[1]
		win_real = p_real_batch[0] / sum_p_real_batch
		print "	D(real_z)", win_real.mean()
		p_fake_batch.to_cpu()
		p_fake_batch = p_fake_batch.data.transpose(1, 0)
		p_fake_batch = np.exp(p_fake_batch)
		sum_p_fake_batch = p_fake_batch[0] + p_fake_batch[1]
		win_fake = p_fake_batch[0] / sum_p_fake_batch
		print "	D(gen_z) ", win_fake.mean()
		serializers.save_hdf5("%s/gen_epoch_%d.model" % (args.model_dir, epoch), gen)
		serializers.save_hdf5("%s/dis_epoch_%d.model" % (args.model_dir, epoch), dis)
		serializers.save_hdf5("%s/dec_epoch_%d.model" % (args.model_dir, epoch), dec)
		elapsed_time = time.time() - start_time
		print "	time", elapsed_time
		total_time += elapsed_time
		print "	total_time", total_time
Exemplo n.º 6
0
def train(dataset, labels):
	if config.n_z != 12:
		raise Exception("The dimension of the latent code z must be 12 (2d for z, 10d for number label).")
	batchsize = 100
	n_epoch = 10000
	n_train_each_epoch = 2000
	total_time = 0

	xp = cuda.cupy if config.use_gpu else np

	# Discriminatorの学習回数
	## 詳細は[Generative Adversarial Networks](http://arxiv.org/abs/1406.2661)
	n_steps_to_optimize_dis = 1

	# Use Adam
	optimizer_dec = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_dis = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_gen.setup(gen)
	optimizer_dec.setup(dec)
	optimizer_dis.setup(dis)

	start_epoch = 1 if args.load_epoch == 0 else args.load_epoch + 1

	for epoch in xrange(start_epoch, n_epoch):
		# Adversarial Networksの誤差
		sum_loss_regularization = 0
		# 復号誤差
		sum_loss_reconstruction = 0

		start_time = time.time()

		z_mask_batch = xp.zeros((batchsize, 12), dtype=xp.float32)
		z_mask_batch[:,:2] = xp.ones((batchsize, 2), dtype=xp.float32)
		z_mask_batch = Variable(z_mask_batch)
		
		for i in xrange(0, n_train_each_epoch):

			# Sample minibatch of examples
			x_batch, label_index_batch, label_1ofK_batch_extended = sample_x_and_label_from_data_distribution(batchsize)

			# Reconstruction phase
			z_fake_batch = gen(x_batch)
			## 12d -> 2d
			z_fake_batch = z_fake_batch * z_mask_batch
			_x_batch = dec(z_fake_batch)

			## 復号誤差を最小化する
			loss_reconstruction = F.mean_squared_error(x_batch, _x_batch)
			sum_loss_reconstruction += loss_reconstruction.data

			optimizer_dec.zero_grads()
			optimizer_gen.zero_grads()
			loss_reconstruction.backward()
			optimizer_dec.update()
			optimizer_gen.update()

			# Adversarial phase
			for k in xrange(n_steps_to_optimize_dis):
				if k > 0:
					x_batch, label_index_batch, label_1ofK_batch_extended = sample_x_and_label_from_data_distribution(batchsize)

				z_real_batch = sample_z_from_swiss_roll_distribution(batchsize, label_index_batch, 10, config.use_gpu)
				z_real_batch_labeled = xp.zeros((batchsize, 12), dtype=xp.float32)
				z_real_batch_labeled[:,:2] = z_real_batch.data[:,:]
				z_real_batch_labeled = z_real_batch_labeled + label_1ofK_batch_extended.data
				z_real_batch_labeled = Variable(z_real_batch_labeled)

				## Discriminator loss
				p_real_batch = dis(z_real_batch_labeled)
				## p_real_batch[0] -> 本物である度合い
				## p_real_batch[1] -> 偽物である度合い
				loss_dis_real = F.softmax_cross_entropy(p_real_batch, Variable(xp.zeros(batchsize, dtype=np.int32)))

				## 上で一度z_fake_batchは計算しているため省く
				if k > 0:
					z_fake_batch = gen(x_batch)
				z_fake_batch_labeled = z_fake_batch * z_mask_batch + label_1ofK_batch_extended

				p_fake_batch = dis(z_fake_batch_labeled)
				## p_fake_batch[0] -> 本物である度合い
				## p_fake_batch[1] -> 偽物である度合い
				loss_dis_fake = F.softmax_cross_entropy(p_fake_batch, Variable(xp.ones(batchsize, dtype=np.int32)))

				loss_dis = loss_dis_fake + loss_dis_real
				sum_loss_regularization += loss_dis.data / float(k + 1)
				
				optimizer_dis.zero_grads()
				loss_dis.backward()
				optimizer_dis.update()


			## p_fake_batch[0] -> 本物である度合い
			## p_fake_batch[1] -> 偽物である度合い
			## generatorの学習では偽のデータを本物であると思い込ませる
			loss_gen = F.softmax_cross_entropy(p_fake_batch, Variable(xp.zeros(batchsize, dtype=np.int32)))
			sum_loss_regularization += loss_gen.data

			optimizer_gen.zero_grads()
			loss_gen.backward()
			optimizer_gen.update()

		# Saving the models
		print "epoch", epoch
		print "	reconstruction_loss", (sum_loss_reconstruction / n_train_each_epoch)
		print "	regularization_loss", (sum_loss_regularization / n_train_each_epoch)
		p_real_batch.to_cpu()
		p_real_batch = p_real_batch.data.transpose(1, 0)
		p_real_batch = np.exp(p_real_batch)
		sum_p_real_batch = p_real_batch[0] + p_real_batch[1]
		win_real = p_real_batch[0] / sum_p_real_batch
		print "	D(real_z)", win_real.mean()
		p_fake_batch.to_cpu()
		p_fake_batch = p_fake_batch.data.transpose(1, 0)
		p_fake_batch = np.exp(p_fake_batch)
		sum_p_fake_batch = p_fake_batch[0] + p_fake_batch[1]
		win_fake = p_fake_batch[0] / sum_p_fake_batch
		print "	D(gen_z) ", win_fake.mean()
		serializers.save_hdf5("%s/gen_epoch_%d.model" % (args.model_dir, epoch), gen)
		serializers.save_hdf5("%s/dis_epoch_%d.model" % (args.model_dir, epoch), dis)
		serializers.save_hdf5("%s/dec_epoch_%d.model" % (args.model_dir, epoch), dec)
		elapsed_time = time.time() - start_time
		print "	time", elapsed_time
		total_time += elapsed_time
		print "	total_time", total_time
Exemplo n.º 7
0
def train(dataset, labels):
	batchsize = 100
	n_epoch = 10000
	n_train_each_epoch = 500
	visualization_interval = 5

	xp = cuda.cupy if config.use_gpu else np

	# Discriminatorの学習回数
	## 詳細は[Generative Adversarial Networks](http://arxiv.org/abs/1406.2661)
	n_steps_to_optimize_dis = 3

	# Use Adam
	optimizer_dec = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_gen = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_dis = optimizers.Adam(alpha=0.0002, beta1=0.5)
	optimizer_gen.setup(gen)
	optimizer_dec.setup(dec)
	optimizer_dis.setup(dis)

	start_epoch = 1 if args.load_epoch == 0 else args.load_epoch + 1

	for epoch in xrange(start_epoch, n_epoch):
		# Adversarial Networksの誤差
		sum_loss_regularization = 0
		# 復号誤差
		sum_loss_reconstruction = 0

		start_time = time.time()
		
		for i in xrange(0, n_train_each_epoch):

			# Sample minibatch of examples
			x_batch, label_batch = sample_x_and_label_from_data_distribution(batchsize)

			# Reconstruction phase
			z_fake_batch = gen(x_batch)
			_x_batch = dec(z_fake_batch)

			## 復号誤差を最小化する
			loss_reconstruction = F.mean_squared_error(x_batch, _x_batch)
			sum_loss_reconstruction += loss_reconstruction.data

			optimizer_dec.zero_grads()
			optimizer_gen.zero_grads()
			loss_reconstruction.backward()
			optimizer_dec.update()
			optimizer_gen.update()

			# Adversarial phase
			for k in xrange(n_steps_to_optimize_dis):
				if k > 0:
					x_batch, label_batch = sample_x_and_label_from_data_distribution(batchsize)

				z_real_batch = sample_z_from_noise_prior(batchsize, config.n_z, config.use_gpu)

				## Discriminator loss
				p_real_batch = dis(z_real_batch)
				## p_real_batch[0] -> 本物である度合い
				## p_real_batch[1] -> 偽物である度合い
				loss_dis_real = F.softmax_cross_entropy(p_real_batch, Variable(xp.zeros(batchsize, dtype=np.int32)))

				## 上で一度z_fake_batchは計算しているため省く
				if k > 0:
					z_fake_batch = gen(x_batch)

				p_fake_batch = dis(z_fake_batch)
				## p_fake_batch[0] -> 本物である度合い
				## p_fake_batch[1] -> 偽物である度合い
				loss_dis_fake = F.softmax_cross_entropy(p_fake_batch, Variable(xp.ones(batchsize, dtype=np.int32)))

				loss_dis = loss_dis_fake + loss_dis_real
				sum_loss_regularization += loss_dis.data / float(k + 1)
				
				optimizer_dis.zero_grads()
				loss_dis.backward()
				optimizer_dis.update()


			## p_fake_batch[0] -> 本物である度合い
			## p_fake_batch[1] -> 偽物である度合い
			## generatorの学習では偽のデータを本物であると思い込ませる
			loss_gen = F.softmax_cross_entropy(p_fake_batch, Variable(xp.zeros(batchsize, dtype=np.int32)))
			sum_loss_regularization += loss_gen.data

			optimizer_gen.zero_grads()
			loss_gen.backward()
			optimizer_gen.update()

		# Saving the models
		print "epoch", epoch
		print "	reconstruction loss", (sum_loss_reconstruction / n_train_each_epoch)
		print "	regularization loss", (sum_loss_regularization / n_train_each_epoch)
		p_real_batch.to_cpu()
		p_real_batch = p_real_batch.data.transpose(1, 0)
		p_real_batch = np.exp(p_real_batch)
		sum_p_real_batch = p_real_batch[0] + p_real_batch[1]
		win_real = p_real_batch[0] / sum_p_real_batch
		loose_real = p_real_batch[1] / sum_p_real_batch
		print "	p_real_batch"
		print "		win  : ave", win_real.mean(), "std", win_real.std()
		print "		loose: ave", loose_real.mean(), "std", loose_real.std()
		p_fake_batch.to_cpu()
		p_fake_batch = p_fake_batch.data.transpose(1, 0)
		p_fake_batch = np.exp(p_fake_batch)
		sum_p_fake_batch = p_fake_batch[0] + p_fake_batch[1]
		win_fake = p_fake_batch[0] / sum_p_fake_batch
		loose_fake = p_fake_batch[1] / sum_p_fake_batch
		print "	p_fake_batch"
		print "		win  : ave", win_fake.mean(), "std", win_fake.std()
		print "		loose: ave", loose_fake.mean(), "std", loose_fake.std()
		# print "	x_p_real average", np.sum(x_p_real.data) / batchsize
		# print "	x_p_fake average", np.sum(x_p_fake.data) / batchsize
		serializers.save_hdf5("%s/gen_epoch_%d.model" % (args.model_dir, epoch), gen)
		serializers.save_hdf5("%s/dis_epoch_%d.model" % (args.model_dir, epoch), dis)
		serializers.save_hdf5("%s/dec_epoch_%d.model" % (args.model_dir, epoch), dec)
		print "	time", time.time() - start_time