def test_expression_transfer(self, sess, e, step): realbatch_array_test, realbatch_array_emtoions_lm = CelebA.landmark_transfer_test( ) print() # print('realbatch_array', realbatch_array_test.shape) batch_size = self.batch_size img_width = int(np.sqrt(batch_size)) # input img save_images( realbatch_array_test[0:batch_size], [img_width, img_width], '{}/train_{:02d}_{:04d}_in.png'.format(self.exp_transfer_path, e, step)) # 生成的图像 sample_images = sess.run(self.x_tilde, feed_dict={ self.images: realbatch_array_test, self.isTrain: False, self.emotion_images_lm: realbatch_array_emtoions_lm }) save_images( sample_images[0:batch_size], [img_width, img_width], '{}/train_{:02d}_{:04d}.png'.format(self.exp_transfer_path, e, step))
def test_basic(self, sess, test_list, e, step): ne_imgs, emo_imgs, ne_lm, emo_lm, emo_label, \ emo_lm_ref, emo_label_ref = CelebA.getTrainImages_lm_embed(test_list) # input image save_images( ne_imgs[0:64], [8, 8], '{}/train_{:02d}_{:04d}_in.png'.format(self.sample_path, e, step)) # ground truth save_images( emo_imgs[0:64], [8, 8], '{}/train_{:02d}_{:04d}_r.png'.format(self.sample_path, e, step)) # generate image sample_images = sess.run(self.x_tilde, feed_dict={ self.images: ne_imgs, self.images_lm: ne_lm, self.isTrain: False, self.emotion_images_lm: emo_lm }) save_images( sample_images[0:64], [8, 8], '{}/train_{:02d}_{:04d}.png'.format(self.sample_path, e, step))
def main(): root_log_dir = "./logs/" mkdir_p(root_log_dir) batch_size = FLAGS.batch_size max_iters = FLAGS.max_iters sample_size = 256 GAN_learn_rate = FLAGS.learn_rate OPER_FLAG = FLAGS.OPER_FLAG data_In = CelebA(FLAGS.path) #print ("the num of dataset", len(data_In.image_list)) if OPER_FLAG == 0: r_fl = 5 test_list = [sys.argv[1]] #'image_03902', 'image_06751', 'image_06069', #'image_05211', 'image_05757', 'image_05758', #'image_05105', 'image_03877', 'image_04325', #'image_05173', 'image_06667', 'image_03133', #'image_06625', 'image_06757', 'image_04065', #'image_03155' t = False f = open("captions_all.txt","a") f_c = open(sys.argv[2],"r") f.write(sys.argv[1]+'\n') for line in f_c: f.write(line) f.write("----\n") f.close() f_c.close() pggan_checkpoint_dir_write = "./model_flowers_test/" sample_path = "./PGGanFlowers/sample_test/" mkdir_p(pggan_checkpoint_dir_write) mkdir_p(sample_path) pggan_checkpoint_dir_read = "./model_flowers_{}/{}/".format(OPER_FLAG, r_fl) pggan = PGGAN(batch_size=batch_size, max_iters=max_iters, model_path=pggan_checkpoint_dir_write, read_model_path=pggan_checkpoint_dir_read, data=data_In, sample_size=sample_size, sample_path=sample_path, log_dir=root_log_dir, learn_rate=GAN_learn_rate, PG= r_fl, t=t) pggan.build_model_PGGan() pggan.test(test_list,int(sys.argv[3]))
def __init__(self, batch_size, max_epoch, model_path, data, network_type, sample_size, sample_path, log_dir, gen_learning_rate, dis_learning_rate, info_reg_coeff): self.batch_size = batch_size self.max_epoch = max_epoch self.infogan_model_path = model_path[0] self.ds_train = data self.type = network_type self.sample_size = sample_size self.sample_path = sample_path self.log_dir = log_dir self.learning_rate_gen = gen_learning_rate self.learning_rate_dis = dis_learning_rate self.info_reg_coeff = info_reg_coeff self.log_vars = [] ### Parameters ### self.fft_size = 2048 # window size for the FFT self.step_size = self.fft_size / 16 # distance to slide along the window (in time) self.spec_thresh = 4 # threshold for spectrograms (lower filters out more noise) self.lowcut = 500 # Hz # Low cut for our butter bandpass filter self.highcut = 15000 # Hz # High cut for our butter bandpass filter self.channel = 1 self.output_size = CelebA().image_size self.content_music = tf.placeholder(tf.float32, [self.batch_size, 6880, 1024, 1]) self.images = tf.placeholder(tf.float32, [self.batch_size, 6880, 1024, 1]) # self.style_music = tf.placeholder(tf.float32, [self.batch_size, 2, 352800, 1]) self.z_p = tf.placeholder(tf.float32, [self.batch_size, self.sample_size]) self.ep = tf.random_normal(shape=[self.batch_size, 2]) self.y = tf.placeholder(tf.float32, [self.batch_size, 2]) self.con_dataset = self.con_read( '/home2/yyan7582/Sample/SampleDataSet/01Happy_Birthday.wav')
def test_landmark_interpolation(self, sess, test_list, e, step): ne_imgs, emo_imgs, ne_lm, emo_lm, emo_label, \ emo_lm_ref, emo_label_ref = CelebA.getTrainImages_lm_embed(test_list) #可以先测试landmark空间的插值,然后再试试隐空间的插值,以每个batch的第一张图像为基准 # print('realbatch_array', realbatch_array_test.shape) batch_size = self.batch_size img_width = int(np.sqrt(batch_size)) test_img = ne_imgs[0] test_img_lm = ne_lm[0] test_img_emotion_lm = emo_lm[0] test_lm_interpolation = [] for i in range(64): factor = i / 63 test_lm_interpolation.append(factor * test_img_lm + (1 - factor) * test_img_emotion_lm) test_lm_interpolation = np.asarray(test_lm_interpolation, dtype=np.float32) test_imgs = np.repeat(np.expand_dims(test_img, axis=0), 64, axis=0) # input img save_images( test_imgs[0:batch_size], [img_width, img_width], '{}/train_{:02d}_{:04d}_in.png'.format(self.lm_interpolation_path, 0, step)) # generate img sample_images = sess.run(self.x_tilde, feed_dict={ self.images: test_imgs, self.isTrain: False, self.emotion_images_lm: test_lm_interpolation }) save_images( sample_images[0:batch_size], [img_width, img_width], '{}/train_{:02d}_{:04d}.png'.format(self.lm_interpolation_path, 0, step))
from utils import CelebA from SG_GAN import SG_GAN from config import Config import os config = Config() os.environ['CUDA_VISIBLE_DEVICES']=str(config.gpu_id) FLAGS = config.get_hyper_para() if __name__ == "__main__": m_ob = CelebA(config.data_dir, FLAGS.image_size, FLAGS.sp_type) sg_gan = SG_GAN(batch_size= FLAGS.batch_size, max_iters= FLAGS.max_iters, model_path= config.model_path, data_ob= m_ob, sample_path= config.sample_path , log_dir= config.exp_dir, learning_rate= FLAGS.learn_rate, is_load_= FLAGS.is_load, use_sp=FLAGS.use_sp, lam_recon= FLAGS.lam_recon, range_attri= FLAGS.range_attri, beta1= FLAGS.beta1, beta2= FLAGS.beta2) if config.OPER_FLAG == 0: sg_gan.build_model_GAN() sg_gan.train() if config.OPER_FLAG == 1: sg_gan.build_model_GAN() sg_gan.test()
def train(self): global_step = tf.Variable(0, trainable=False) add_global = global_step.assign_add(1) new_learning_rate = tf.train.exponential_decay(self.learn_rate_init, global_step=global_step, decay_steps=20000, decay_rate=0.98) #for D trainer_D = tf.train.AdamOptimizer(learning_rate=new_learning_rate, beta1=0.5) gradients_D = trainer_D.compute_gradients(self.D_loss, var_list=self.d_vars) # clipped_gradients_D = [(tf.clip_by_value(grad, -1.0, 1.0), var) for grad, var in gradients_D] opti_D = trainer_D.apply_gradients(gradients_D) #for G trainer_G = tf.train.AdamOptimizer(learning_rate=new_learning_rate, beta1=0.5) gradients_G = trainer_G.compute_gradients(self.G_loss, var_list=self.g_vars) # clipped_gradients_G = [(tf.clip_by_value(_[0], -1, 1.), _[1]) for _ in gradients_G] opti_G = trainer_G.apply_gradients(gradients_G) #for E trainer_E = tf.train.AdamOptimizer(learning_rate=new_learning_rate, beta1=0.5) gradients_E = trainer_E.compute_gradients(self.encode_loss, var_list=self.e_vars) # clipped_gradients_E = [(tf.clip_by_value(_[0], -1, 1.), _[1]) for _ in gradients_E] opti_E = trainer_E.apply_gradients(gradients_E) #for Embed trainer_Embed = tf.train.AdamOptimizer(learning_rate=new_learning_rate, beta1=0.5) gradients_Embed = trainer_Embed.compute_gradients( self.Embed_loss, var_list=self.embed_vars) # clipped_gradients_E = [(tf.clip_by_value(_[0], -1, 1.), _[1]) for _ in gradients_E] opti_Embed = trainer_Embed.apply_gradients(gradients_Embed) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: embed_vector = [] embed_label = [] sess.run(init) # 从断点处继续训练 self.saver.restore(sess, self.saved_model_path) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(self.log_dir, sess.graph) batch_num = 0 e = 0 step = 0 counter = 0 # 训练之前就应该shuffle一次 self.ds_train = self.shuffle_train(self.ds_train) while e <= self.max_epoch: max_iter = len(self.ds_train) / self.batch_size - 1 while batch_num < max_iter: step = step + 1 if batch_num >= max_iter - 1: self.ds_train = self.shuffle_train(self.ds_train) train_list = CelebA.getNextBatch(self.ds_train, batch_num, self.batch_size) ne_imgs, emo_imgs, ne_lm, emo_lm, emo_label, emo_lm_ref, emo_label_ref\ = CelebA.getTrainImages_lm_embed(train_list) sample_z = np.random.normal( size=[self.batch_size, self.latent_dim]) # WGAN-GP loops = 5 # # 先获取到当前G网络生成的图像 # fake_emo_imgs = sess.run(self.x_tilde, feed_dict={self.images: ne_imgs, # self.images_lm: ne_lm, # self.emotion_images_lm: emo_lm, # self.isTrain: True}) # # optimization D # local_x_batch, local_completion_batch = self.crop_local_imgs(emo_imgs, fake_emo_imgs) # T-SNE # embed_v = sess.run(self.lm_embed, feed_dict={self.emotion_images_lm: emo_lm, self.isTrain:False} ) # embed_vector.append(embed_v) # embed_label.append(emo_label) # # if step == 20: # with open('embed_vector.pickle', 'wb') as f: # pickle.dump(embed_vector, f, protocol=-1) # with open('embed_label.pickle', 'wb') as f: # pickle.dump(embed_label, f, protocol=-1) for _ in range(loops): sess.run( opti_D, feed_dict={ self.images: ne_imgs, self.z_p: sample_z, self.images_emotion: emo_imgs, # self.real_local_imgs: local_x_batch, self.fake_local_imgs: local_completion_batch,\ self.images_lm: ne_lm, self.emotion_label: emo_label, self.emotion_images_lm: emo_lm, self.isTrain: True }) # 后面再改 self.images_lm for _ in range(1): #optimization Embed sess.run(opti_Embed, feed_dict={ self.emotion_label: emo_label, self.emotion_images_lm: emo_lm, self.isTrain: True, self.emotion_label_reference: emo_label_ref, self.emotion_images_lm_reference: emo_lm_ref }) #optimization E sess.run( opti_E, feed_dict={ self.images: ne_imgs, self.images_emotion: emo_imgs, self.images_lm: ne_lm, self.isTrain: True, # self.real_local_imgs: local_x_batch, self.fake_local_imgs: local_completion_batch, self.emotion_label: emo_label, self.emotion_images_lm: emo_lm }) #optimizaiton G sess.run( opti_G, feed_dict={ self.images: ne_imgs, self.z_p: sample_z, self.images_emotion: emo_imgs, self.images_lm: ne_lm, self.isTrain: True, # self.real_local_imgs: local_x_batch, self.fake_local_imgs: local_completion_batch, self.emotion_label: emo_label, self.emotion_images_lm: emo_lm }) summary_str = sess.run( summary_op, feed_dict={ self.images: ne_imgs, self.z_p: sample_z, self.images_emotion: emo_imgs, self.images_lm: ne_lm, self.emotion_label: emo_label, self.emotion_images_lm: emo_lm, self.emotion_label_reference: emo_label_ref, self.isTrain: False, # self.real_local_imgs: local_x_batch, self.fake_local_imgs: local_completion_batch, self.emotion_images_lm_reference: emo_lm_ref }) summary_writer.add_summary(summary_str, step) batch_num += 1 new_learn_rate = sess.run(new_learning_rate) if new_learn_rate > 0.00005: sess.run(add_global) if step % 20 == 0: D_loss, fake_loss, encode_loss, LL_loss, kl_loss, recon_loss, positive_loss, negtive_loss, lm_recon_loss, Embed_loss, real_cls, fake_cls = sess.run( [ self.D_loss, self.G_loss, self.encode_loss, self.D_loss, self.LL_loss, self.recon_loss, self.positive_loss, self.negative_loss, self.lm_recon_loss, self.Embed_loss, self.real_emotion_cls_loss, self.fake_emotion_cls_loss ], feed_dict={ self.images: ne_imgs, self.z_p: sample_z, self.images_emotion: emo_imgs, self.images_lm: ne_lm, self.emotion_label: emo_label, self.emotion_images_lm: emo_lm, self.isTrain: False, # self.real_local_imgs: local_x_batch, self.fake_local_imgs: local_completion_batch, self.emotion_label_reference: emo_label_ref, self.emotion_images_lm_reference: emo_lm_ref }) print( "EPOCH %d step %d: D: loss = %.7f G: loss=%.7f Encode: loss=%.7f identity loss=%.7f KL=%.7f recon_loss=%.7f " "positive_loss=%.7f negtive_loss=%.7f lm_recon_loss=%.7f Embed_loss==%.7f real_cls=%.7f fake_cls=%.7f" % (e, step, D_loss, fake_loss, encode_loss, LL_loss, kl_loss, recon_loss, positive_loss, negtive_loss, lm_recon_loss, Embed_loss, real_cls, fake_cls)) # previous if np.mod(step, 20) == 1: self.ds_test = self.shuffle_train(self.ds_test) test_list = CelebA.getNextBatch( self.ds_test, 0, self.batch_size) self.test_basic(sess, test_list, 0, step) self.test_landmark_interpolation( sess, test_list, 0, step) self.test_expression_transfer(sess, 0, step) self.saver.save(sess, self.saved_model_path) # for tsne interpolation # if step > 0: # print('step', step) # self.ds_test = self.shuffle_train(self.ds_test) # test_list = CelebA.getNextBatch(self.ds_test, 0, self.batch_size) # # self.test_basic(sess, test_list, 0, step) # embed_inter_v, embed_img_n = self.test_landmark_interpolation(sess, test_list, 0, step) # embed_inter_vector.append(embed_inter_v) # embed_img_names.append(embed_img_n) # # if step == 20: # with open('embed_inter_vector.pickle', 'wb') as f: # pickle.dump(embed_inter_vector, f, protocol=-1) # with open('embed_img_names.pickle', 'wb') as f: # pickle.dump(embed_img_names, f, protocol=-1) # # # self.test_one_eye_close(sess, test_list, 0, step) # # self.test_expression_transfer(sess, test_list, 0, step) # self.saver.save(sess, self.saved_model_path) e += 1 batch_num = 0 save_path = self.saver.save(sess, self.saved_model_path) print("Model saved in file: %s" % save_path)
def __init__(self, batch_size, max_epoch, model_path, train_data, test_data, latent_dim, sample_path, exp_transfer_path, lm_interpolation_path, log_dir, learnrate_init): self.batch_size = batch_size self.max_epoch = max_epoch self.saved_model_path = model_path self.ds_train = train_data self.ds_test = test_data self.latent_dim = latent_dim self.sample_path = sample_path self.exp_transfer_path = exp_transfer_path self.lm_interpolation_path = lm_interpolation_path self.log_dir = log_dir self.learn_rate_init = learnrate_init self.log_vars = [] self.test_lm_transfer_path = 'test_lm_transfer' self.channel = 3 self.output_size = CelebA().image_size self.local_size = int(self.output_size / 2) #总觉得在这里用分类不太好 self.lambda_cls = 0.0001 self.lambda_embed = 0.0001 self.lambda_gen = 0.001 self.landmark_size = 68 * 2 self.landmark_embed_size = 32 # self.tv_penalty = 0.0001 self.lambda_kl = 0.001 #输入的NE图像 self.images = tf.placeholder(tf.float32, [ self.batch_size, self.output_size, self.output_size, self.channel ]) #和landmark对应的表情图像, 输入有landmark点, 输出没有 self.images_emotion = tf.placeholder(tf.float32, [ self.batch_size, self.output_size, self.output_size, self.channel ]) self.real_local_imgs = tf.placeholder( tf.float32, [self.batch_size, self.local_size, self.local_size, self.channel]) self.fake_local_imgs = tf.placeholder( tf.float32, [self.batch_size, self.local_size, self.local_size, self.channel]) #输入图像的landmark通道 self.images_lm = tf.placeholder(tf.float32, [self.batch_size, self.landmark_size]) self.emotion_images_lm = tf.placeholder( tf.float32, [self.batch_size, self.landmark_size]) self.emotion_images_lm_reference = tf.placeholder( tf.float32, [self.batch_size, self.landmark_size]) self.emotion_label = tf.placeholder(tf.int32, [self.batch_size]) self.emotion_label_reference = tf.placeholder(tf.int32, [self.batch_size]) self.lm_embed_input = tf.placeholder( tf.float32, [self.batch_size, self.landmark_embed_size]) self.isTrain = tf.placeholder(tf.bool) #z self.z_p = tf.placeholder(tf.float32, [self.batch_size, self.latent_dim]) # the noise is fixed, not changed, 可能是存在一定问题的 self.ep = tf.random_normal(shape=[self.batch_size, self.latent_dim], seed=1000)
if not os.path.isdir(vaegan_checkpoint_dir): mkdir_p(vaegan_checkpoint_dir) os.system('fuser 6006/tcp -k') # model_path = None if FLAGS.load == 'none' else vaegan_checkpoint_dir +'/' + FLAGS.load + '.ckpt' tf.set_random_seed(FLAGS.seed) batch_size = FLAGS.batch_size max_iters = FLAGS.max_iters if not FLAGS.test else 3 latent_dim = FLAGS.latent_dim data_repeat = FLAGS.repeat print_every = FLAGS.print_every if not FLAGS.test else 1 save_every = FLAGS.save_every learn_rate_init = FLAGS.lr cb_ob = CelebA(FLAGS.path) if FLAGS.dataset == 'celebA' else None # import pdb; pdb.set_trace() vaeGan = vaegan(batch_size=batch_size, max_iters=max_iters, repeat=data_repeat, load_type=FLAGS.load, latent_dim=latent_dim, log_dir=root_log_dir, learnrate_init=learn_rate_init, mdevice=mdevice, _lambda=[FLAGS.alpha, FLAGS.beta, FLAGS.gamma], data_ob=cb_ob, print_every=print_every, save_every=save_every, ckp_dir=vaegan_checkpoint_dir, flags=FLAGS)
ngpu = 1 lr = 0.0001 beta1 = 0.5 img_size = 64 batch_size = 128 num_epochs = 100 # make output folder os.makedirs(args.save_model, exist_ok=True) os.makedirs(args.save_img, exist_ok=True) # load the trainset trainset = CelebA(image_dir='hw3_data/face/train', csv_dir='hw3_data/face/train.csv', transform=transforms.Compose([ transforms.Resize(img_size), transforms.CenterCrop(img_size), transforms.ToTensor(), transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5)), ])) print('# images in trainset:', len(trainset)) # # Use the torch dataloader to iterate through the dataset dataloader = DataLoader(trainset, batch_size=batch_size, shuffle=True, num_workers=4) images, labels = next(iter(dataloader)) print('Image tensor in each batch:', images.shape, images.dtype) print('Label tensor in each batch:', labels.shape, labels.dtype)
def train(self): step_pl = tf.placeholder(tf.float32, shape=None) alpha_tra_assign = self.alpha_tra.assign(step_pl / self.max_iters) opti_D = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.0, beta2=0.99).minimize( self.D_loss, var_list=self.d_vars) opti_G = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.0, beta2=0.99).minimize( self.G_loss, var_list=self.g_vars) init = tf.global_variables_initializer() config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: sess.run(init) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(self.log_dir, sess.graph) if self.pg != 1 and self.pg != 7: if self.trans: self.r_saver.restore(sess, self.read_model_path) self.rgb_saver.restore(sess, self.read_model_path) else: self.saver.restore(sess, self.read_model_path) step = 0 batch_num = 0 while step <= self.max_iters: # optimization D n_critic = 1 if self.pg == 5 and self.trans: n_critic = 1 for i in range(n_critic): sample_z = np.random.normal( size=[self.batch_size, self.sample_size]) train_list = self.data_In.getNextBatch( batch_num, self.batch_size) realbatch_array = CelebA.getShapeForData( train_list, resize_w=self.output_size) if self.trans and self.pg != 0: alpha = np.float(step) / self.max_iters low_realbatch_array = scipy.ndimage.zoom( realbatch_array, zoom=[1, 0.5, 0.5, 1]) low_realbatch_array = scipy.ndimage.zoom( low_realbatch_array, zoom=[1, 2, 2, 1]) realbatch_array = alpha * realbatch_array + ( 1 - alpha) * low_realbatch_array sess.run(opti_D, feed_dict={ self.images: realbatch_array, self.z: sample_z }) batch_num += 1 # optimization G sess.run(opti_G, feed_dict={self.z: sample_z}) summary_str = sess.run(summary_op, feed_dict={ self.images: realbatch_array, self.z: sample_z }) summary_writer.add_summary(summary_str, step) # the alpha of fake_in process sess.run(alpha_tra_assign, feed_dict={step_pl: step}) if step % 1000 == 0: D_loss, G_loss, D_origin_loss, alpha_tra = sess.run( [ self.D_loss, self.G_loss, self.D_origin_loss, self.alpha_tra ], feed_dict={ self.images: realbatch_array, self.z: sample_z }) print( "PG %d, step %d: D loss=%.7f G loss=%.7f, D_or loss=%.7f, opt_alpha_tra=%.7f" % (self.pg, step, D_loss, G_loss, D_origin_loss, alpha_tra)) realbatch_array = np.clip(realbatch_array, -1, 1) save_images( realbatch_array[0:self.batch_size], [2, self.batch_size / 2], '{}/{:02d}_real.png'.format(self.sample_path, step)) if self.trans and self.pg != 0: low_realbatch_array = np.clip(low_realbatch_array, -1, 1) save_images( low_realbatch_array[0:self.batch_size], [2, self.batch_size / 2], '{}/{:02d}_real_lower.png'.format( self.sample_path, step)) fake_image = sess.run(self.fake_images, feed_dict={ self.images: realbatch_array, self.z: sample_z }) fake_image = np.clip(fake_image, -1, 1) save_images( fake_image[0:self.batch_size], [2, self.batch_size / 2], '{}/{:02d}_train.png'.format(self.sample_path, step)) if np.mod(step, 4000) == 0 and step != 0: self.saver.save(sess, self.gan_model_path) step += 1 save_path = self.saver.save(sess, self.gan_model_path) print("Model saved in file: %s" % save_path) tf.reset_default_graph()
vaegan_checkpoint_dir = "./model_vaegan/model.ckpt" sample_path = "./vaeganSample/sample" mkdir_p(root_log_dir) mkdir_p('./model_vaegan/') mkdir_p(sample_path) model_path = vaegan_checkpoint_dir batch_size = FLAGS.batch_size max_iters = FLAGS.max_iters latent_dim = FLAGS.latent_dim data_repeat = FLAGS.repeat learn_rate_init = FLAGS.learn_rate_init cb_ob = CelebA(FLAGS.path) vaeGan = vaegan(batch_size=batch_size, max_iters=max_iters, repeat=data_repeat, model_path=model_path, data_ob=cb_ob, latent_dim=latent_dim, sample_path=sample_path, log_dir=root_log_dir, learnrate_init=learn_rate_init) if FLAGS.op == 0: vaeGan.build_model_vaegan() vaeGan.train()
#1 : 128sx128 if __name__ == "__main__": root_log_dir = "./output/logs/logs{}".format(FLAGS.OPER_FLAG) gan_checkpoint_dir = "./output/model_gan/{}_model.ckpt".format( FLAGS.OPER_FLAG) sample_path = "./output/sample{}/sample_{}".format(FLAGS.OPER_NAME, FLAGS.OPER_FLAG) mkdir_p(root_log_dir) mkdir_p(gan_checkpoint_dir) mkdir_p(sample_path) model_path = gan_checkpoint_dir m_ob = CelebA(FLAGS.IMAGE_PATH, FLAGS.IMG_SIZE) print "dom1_train_data_list", len(m_ob.dom_1_train_data_list) print "dom2_train_data_list", len(m_ob.dom_2_train_data_list) print "the number of train data", len(m_ob.dom_1_train_data_list + m_ob.dom_2_train_data_list) reGAN = reImageGAN(batch_size=FLAGS.BATCH_SIZE, max_iters=FLAGS.MAX_ITERS, model_path=model_path, data_ob=m_ob, sample_path=sample_path, log_dir=root_log_dir, learning_rate=FLAGS.LEARN_RATE, l1_w=FLAGS.L1_W, per_w=FLAGS.PER_W)
flags.DEFINE_boolean("use_wscale", True, "Using the scale of weight") flags.DEFINE_boolean("celeba", True, "Whether using celeba or using CelebA-HQ") FLAGS = flags.FLAGS if __name__ == "__main__": root_log_dir = "./output/{}/logs/".format(FLAGS.OPER_NAME) mkdir_p(root_log_dir) fl = [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6] r_fl = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6] for i in range(FLAGS.pg): if FLAGS.dataset == 'celeba': data_In = CelebA(FLAGS.data_path) elif FLAGS.dataset == 'celeba_hq': data_In = CelebA_HQ(FLAGS.data_path) elif FLAGS.dataset == 'channel': data_In = Channel(FLAGS.data_path, FLAGS.epoch, FLAGS.batch_size) t = False if (i % 2 == 0) else True pggan_checkpoint_dir_write = "./output/{}/model_pggan_{}/{}/".format( FLAGS.OPER_NAME, FLAGS.dataset, fl[i]) sample_path = "./output/{}/sample_{}_{}".format( FLAGS.OPER_NAME, FLAGS.dataset, fl[i], t) mkdir_p(pggan_checkpoint_dir_write) mkdir_p(sample_path) pggan_checkpoint_dir_read = "./output/{}/model_pggan_{}/{}/".format( FLAGS.OPER_NAME, FLAGS.dataset, r_fl[i])
def train(self): # iteration이 지날때 마다 linearly alpha를 증가시킴 step_pl = tf.placeholder(tf.float32, shape=None) alpha_tra_assign = self.alpha_tra.assign(step_pl / self.max_iters) # optimizer를 이용하여 loss를 줄이는 방향으로 variable을 업데이트해나간다. opti_D = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.0, beta2=0.99).minimize( self.D_loss, var_list=self.d_vars) opti_G = tf.train.AdamOptimizer(learning_rate=self.learning_rate, beta1=0.0, beta2=0.99).minimize( self.G_loss, var_list=self.g_vars) # 변수들을 초기화하는 그래프 init = tf.global_variables_initializer() # GPU 메모리를 필요할때 마다 조금씩 늘리도록 한다. config = tf.ConfigProto() config.gpu_options.allow_growth = True with tf.Session(config=config) as sess: # 변수들을 초기화하는 그래프를 돌림 sess.run(init) summary_op = tf.summary.merge_all() summary_writer = tf.summary.FileWriter(self.log_dir, sess.graph) # progressive 값이 1, 7을 제외한 값이라면 if self.pg != 1 and self.pg != 7: # transition이 일어나는 때라면 if self.trans: # d_vars_n_read와 g_vars_n_read를 담당하는 r_saver가 이전 progressive에서 # training 하여 저장했던 weight들을 불러온다 self.r_saver.restore(sess, self.read_model_path) # 이전에 학습했던 d_vars_n_2_rgb와 g_vars_n_2_rgb를 불러온다 self.rgb_saver.restore(sess, self.read_model_path) # trainsition이 일어나는 단계가 아니라면 else: # 전체 이전 단계의 전체 weight를 불러온다. self.saver.restore(sess, self.read_model_path) step = 0 batch_num = 0 # 16 x 32000 = 512,000개의 실제 이미지를 discriminator에게 보여줄 때까지 돌림 while step <= self.max_iters: # optimization D n_critic = 1 if self.pg == 5 and self.trans: n_critic = 1 for i in range(n_critic): # 512 짜리 latent vector를 만듬 sample_z = np.random.normal( size=[self.batch_size, self.sample_size]) # 실제 이미지의 path를 batch단위씩 얻음 (16개씩) train_list = self.data_In.getNextBatch( batch_num, self.batch_size) # 이 path를 현재 단계의 아웃풋 사이즈만큼 리사이즈 시킴 # 예를 들어 1단계에서 output_size가 4이므로 4x4로 resize한다. realbatch_array = CelebA.getShapeForData( train_list, resize_w=self.output_size) # 만약 transition(fade)가 일어나는 차례라면 if self.trans and self.pg != 0: alpha = np.float(step) / self.max_iters # 이미지 해상도 변경 # https://stackoverflow.com/questions/37119071/scipy-rotate-and-zoom-an-image-without-changing-its-dimensions low_realbatch_array = scipy.ndimage.zoom( realbatch_array, zoom=[1, 0.5, 0.5, 1]) low_realbatch_array = scipy.ndimage.zoom( low_realbatch_array, zoom=[1, 2, 2, 1]) # resolution transition중에 실제이미지의 resolution 사이의 보간을 한다. realbatch_array = alpha * realbatch_array + ( 1 - alpha) * low_realbatch_array sess.run(opti_D, feed_dict={ self.images: realbatch_array, self.z: sample_z }) # 다음 번 배치 batch_num += 1 # optimization G sess.run(opti_G, feed_dict={self.z: sample_z}) summary_str = sess.run(summary_op, feed_dict={ self.images: realbatch_array, self.z: sample_z }) summary_writer.add_summary(summary_str, step) # the alpha of fake_in process # 1씩 증가하는 step을 step_placeholder에 넣고 alpha_transition값을 계산 sess.run(alpha_tra_assign, feed_dict={step_pl: step}) # 400번째 step마다 loss 출력하고 이미지를 저장 if step % 400 == 0: D_loss, G_loss, D_origin_loss, alpha_tra = sess.run( [ self.D_loss, self.G_loss, self.D_origin_loss, self.alpha_tra ], feed_dict={ self.images: realbatch_array, self.z: sample_z }) print( "PG %d, step %d: D loss=%.7f G loss=%.7f, D_or loss=%.7f, opt_alpha_tra=%.7f" % (self.pg, step, D_loss, G_loss, D_origin_loss, alpha_tra)) realbatch_array = np.clip(realbatch_array, -1, 1) save_images( realbatch_array[0:self.batch_size], [2, self.batch_size / 2], '{}/{:02d}_real.png'.format(self.sample_path, step)) # 만약 transition 단계인 경우에 if self.trans and self.pg != 0: low_realbatch_array = np.clip(low_realbatch_array, -1, 1) save_images( low_realbatch_array[0:self.batch_size], [2, self.batch_size / 2], '{}/{:02d}_real_lower.png'.format( self.sample_path, step)) fake_image = sess.run(self.fake_images, feed_dict={ self.images: realbatch_array, self.z: sample_z }) fake_image = np.clip(fake_image, -1, 1) save_images( fake_image[0:self.batch_size], [2, self.batch_size / 2], '{}/{:02d}_train.png'.format(self.sample_path, step)) # 4000번째 마다 g_vars와 d_vars (network의 weight)를 중간저장함 if np.mod(step, 4000) == 0 and step != 0: self.saver.save(sess, self.gan_model_path) step += 1 # max_iter 끝나고 최종 모델을 저장함 save_path = self.saver.save(sess, self.gan_model_path) print("Model saved in file: %s" % save_path) tf.reset_default_graph()
vaegan_checkpoint_dir = "./model_vaegan/model.ckpt" sample_path = "./vaeganSample/sample" mkdir_p(root_log_dir) mkdir_p(vaegan_checkpoint_dir) mkdir_p(sample_path) model_path = vaegan_checkpoint_dir batch_size = FLAGS.batch_size max_epoch = FLAGS.max_epoch latent_dim = FLAGS.latent_dim learn_rate_init = FLAGS.learn_rate_init data_list = CelebA().load_celebA(image_path=FLAGS.path) print "the num of dataset", len(data_list) vaeGan = vaegan(batch_size=batch_size, max_epoch=max_epoch, model_path=model_path, data=data_list, latent_dim=latent_dim, sample_path=sample_path, log_dir=root_log_dir, learnrate_init=learn_rate_init) if FLAGS.operation == 0: vaeGan.build_model_vaegan() vaeGan.train()
flags.DEFINE_float("learn_rate", 0.001, "Learning rate for G and D networks") flags.DEFINE_integer("lam_gp", 10, "Weight of gradient penalty term") flags.DEFINE_integer("lam_eps", 0.001, "Weight for the epsilon term") flags.DEFINE_float("flag", 11, "FLAG of gan training process") flags.DEFINE_boolean("use_wscale", True, "Using the scale of weight") FLAGS = flags.FLAGS if __name__ == "__main__": root_log_dir = "./PGGanCeleba/logs/celeba_test2" mkdir_p(root_log_dir) OPER_NAME = FLAGS.OPER_NAME OPER_FLAG = FLAGS.OPER_FLAG data_In = CelebA(FLAGS.path) print("the num of dataset", len(data_In.image_list)) if OPER_FLAG == 0: fl = [1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6] r_fl = [1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6] for i in range(FLAGS.flag): t = False if (i % 2 == 0) else True pggan_checkpoint_dir_write = "./PGGanCeleba{}/model_pggan_{}/{}/".format( OPER_NAME, OPER_FLAG, fl[i]) sample_path = "./PGGanCeleba{}/{}/sample_{}_{}".format( OPER_NAME, FLAGS.OPER_FLAG, fl[i], t)
FLAGS = flags.FLAGS if __name__ == "__main__": root_log_dir = "./output/logs/logs{}".format(FLAGS.OPER_FLAG) gan_checkpoint_dir = "./output/model_gan/{}_model.ckpt".format( FLAGS.OPER_FLAG) sample_path = "./output/sample{}/sample_{}".format(FLAGS.OPER_NAME, FLAGS.OPER_FLAG) mkdir_p(root_log_dir) mkdir_p(gan_checkpoint_dir) mkdir_p(sample_path) model_path = gan_checkpoint_dir m_ob = CelebA(FLAGS.IMAGE_PATH, FLAGS.IMG_SIZE, FLAGS.attri_id) print "dom1_train_data_list", len(m_ob.dom_1_train_data_list) print "dom2_train_data_list", len(m_ob.dom_2_train_data_list) print "the number of train data", len(m_ob.dom_1_train_data_list + m_ob.dom_2_train_data_list) reGAN = reImageGAN(batch_size=FLAGS.BATCH_SIZE, max_iters=FLAGS.MAX_ITERS, model_path=model_path, data_ob=m_ob, sample_path=sample_path, log_dir=root_log_dir, learning_rate=FLAGS.LEARN_RATE, l1_w=FLAGS.L1_W, per_w=FLAGS.PER_W,