From 862d459dc62a838b4400173f88e8fccd7c5ecba6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B4=BE=E6=99=93?= Date: Fri, 10 Jan 2020 17:55:09 +0800 Subject: [PATCH] add PaddleGAN models (#4184) --- PaddleCV/PaddleGAN/trainer/AttGAN.py | 43 +++++++++++++++++++++--- PaddleCV/PaddleGAN/trainer/STGAN.py | 47 +++++++++++++++++++++++---- PaddleCV/PaddleGAN/trainer/StarGAN.py | 34 +++++++++++++++++-- 3 files changed, 111 insertions(+), 13 deletions(-) diff --git a/PaddleCV/PaddleGAN/trainer/AttGAN.py b/PaddleCV/PaddleGAN/trainer/AttGAN.py index f7899d69..02d840d6 100644 --- a/PaddleCV/PaddleGAN/trainer/AttGAN.py +++ b/PaddleCV/PaddleGAN/trainer/AttGAN.py @@ -156,8 +156,13 @@ class DTrainer(): def gradient_penalty(self, f, real, fake=None, cfg=None, name=None): def _interpolate(a, b=None): if b is None: - beta = fluid.layers.uniform_random_batch_size_like( - input=a, shape=a.shape, min=0.0, max=1.0) + if cfg.enable_ce: + beta = fluid.layers.uniform_random_batch_size_like( + input=a, shape=a.shape, min=0.0, max=1.0, seed=1) + else: + beta = fluid.layers.uniform_random_batch_size_like( + input=a, shape=a.shape, min=0.0, max=1.0) + mean = fluid.layers.reduce_mean( a, dim=list(range(len(a.shape))), keep_dim=True) input_sub_mean = fluid.layers.elementwise_sub(a, mean, axis=0) @@ -167,8 +172,13 @@ class DTrainer(): keep_dim=True) b = beta * fluid.layers.sqrt(var) * 0.5 + a shape = [a.shape[0]] - alpha = fluid.layers.uniform_random_batch_size_like( - input=a, shape=shape, min=0.0, max=1.0) + if cfg.enable_ce: + alpha = fluid.layers.uniform_random_batch_size_like( + input=a, shape=shape, min=0.0, max=1.0, seed=1) + else: + alpha = fluid.layers.uniform_random_batch_size_like( + input=a, shape=shape, min=0.0, max=1.0) + inner = fluid.layers.elementwise_mul((b-a), alpha, axis=0) + a return inner @@ -254,6 +264,10 @@ class AttGAN(object): default=None, help="the normalization in discriminator, choose in [None, instance_norm]" ) + parser.add_argument( + '--enable_ce', + action='store_true', + help="if set, run the tasks with continuous evaluation logs") return parser @@ -282,6 +296,9 @@ class AttGAN(object): name='label_org_', shape=[None, self.cfg.c_dim], dtype='float32') label_trg_ = fluid.data( name='label_trg_', shape=[None, self.cfg.c_dim], dtype='float32') + # used for continuous evaluation + if self.cfg.enable_ce: + fluid.default_startup_program().random_seed = 90 py_reader = fluid.io.PyReader( feed_list=[image_real, label_org, label_trg], @@ -325,7 +342,11 @@ class AttGAN(object): dis_trainer.program).with_data_parallel( loss_name=dis_trainer.d_loss.name, build_strategy=build_strategy) - + # used for continuous evaluation + if self.cfg.enable_ce: + gen_trainer_program.random_seed = 90 + dis_trainer_program.random_seed = 90 + t_time = 0 for epoch_id in range(self.cfg.epoch): @@ -367,6 +388,8 @@ class AttGAN(object): d_loss_gp[0], batch_time)) sys.stdout.flush() batch_id += 1 + if self.cfg.enable_ce and batch_id == 100: + break if self.cfg.run_test: image_name = fluid.data( @@ -393,3 +416,13 @@ class AttGAN(object): "net_G") utility.checkpoints(epoch_id, self.cfg, exe, dis_trainer, "net_D") + # used for continuous evaluation + if self.cfg.enable_ce: + device_num = fluid.core.get_cuda_device_count() if self.cfg.use_gpu else 1 + print("kpis\tattgan_g_loss_fake_card{}\t{}".format(device_num, g_loss_fake[0])) + print("kpis\tattgan_g_loss_rec_card{}\t{}".format(device_num, g_loss_rec[0])) + print("kpis\tattgan_g_loss_cls_card{}\t{}".format(device_num, g_loss_cls[0])) + print("kpis\tattgan_d_loss_real_card{}\t{}".format(device_num, d_loss_real[0])) + print("kpis\tattgan_d_loss_fake_card{}\t{}".format(device_num,d_loss_fake[0])) + print("kpis\tattgan_d_loss_gp_card{}\t{}".format(device_num,d_loss_gp[0])) + print("kpis\tattgan_Batch_time_cost_card{}\t{}".format(device_num,batch_time)) diff --git a/PaddleCV/PaddleGAN/trainer/STGAN.py b/PaddleCV/PaddleGAN/trainer/STGAN.py index d55497b2..7d4275c0 100644 --- a/PaddleCV/PaddleGAN/trainer/STGAN.py +++ b/PaddleCV/PaddleGAN/trainer/STGAN.py @@ -164,8 +164,13 @@ class DTrainer(): def gradient_penalty(self, f, real, fake=None, cfg=None, name=None): def _interpolate(a, b=None): if b is None: - beta = fluid.layers.uniform_random_batch_size_like( - input=a, shape=a.shape, min=0.0, max=1.0) + if cfg.enable_ce: + beta = fluid.layers.uniform_random_batch_size_like( + input=a, shape=a.shape, min=0.0, max=1.0, seed=1) + else: + beta = fluid.layers.uniform_random_batch_size_like( + input=a, shape=a.shape, min=0.0, max=1.0) + mean = fluid.layers.reduce_mean( a, dim=list(range(len(a.shape))), keep_dim=True) input_sub_mean = fluid.layers.elementwise_sub(a, mean, axis=0) @@ -175,8 +180,13 @@ class DTrainer(): keep_dim=True) b = beta * fluid.layers.sqrt(var) * 0.5 + a shape = [a.shape[0]] - alpha = fluid.layers.uniform_random_batch_size_like( - input=a, shape=shape, min=0.0, max=1.0) + if cfg.enable_ce: + alpha = fluid.layers.uniform_random_batch_size_like( + input=a, shape=shape, min=0.0, max=1.0, seed=1) + else: + alpha = fluid.layers.uniform_random_batch_size_like( + input=a, shape=shape, min=0.0, max=1.0) + inner = fluid.layers.elementwise_mul((b-a), alpha, axis=0) + a return inner @@ -269,7 +279,10 @@ class STGAN(object): default=None, help="the normalization in discriminator, choose in [None, instance_norm]" ) - + parser.add_argument( + '--enable_ce', + action='store_true', + help="if set, run the tasks with continuous evaluation logs") return parser def __init__(self, @@ -296,6 +309,9 @@ class STGAN(object): name='label_org_', shape=[None, self.cfg.c_dim], dtype='float32') label_trg_ = fluid.data( name='label_trg_', shape=[None, self.cfg.c_dim], dtype='float32') + # used for continuous evaluation + if self.cfg.enable_ce: + fluid.default_startup_program().random_seed = 90 test_gen_trainer = GTrainer(image_real, label_org, label_org_, label_trg, label_trg_, self.cfg, @@ -339,7 +355,11 @@ class STGAN(object): dis_trainer.program).with_data_parallel( loss_name=dis_trainer.d_loss.name, build_strategy=build_strategy) - + # used for continuous evaluation + if self.cfg.enable_ce: + gen_trainer_program.random_seed = 90 + dis_trainer_program.random_seed = 90 + t_time = 0 total_train_batch = 0 # used for benchmark @@ -382,6 +402,9 @@ class STGAN(object): d_loss_gp[0], batch_time)) sys.stdout.flush() batch_id += 1 + if self.cfg.enable_ce and batch_id == 100: + break + total_train_batch += 1 # used for benchmark # profiler tools if self.cfg.profile and epoch_id == 0 and batch_id == self.cfg.print_freq: @@ -413,3 +436,15 @@ class STGAN(object): "net_G") utility.checkpoints(epoch_id, self.cfg, exe, dis_trainer, "net_D") + # used for continuous evaluation + if self.cfg.enable_ce: + device_num = fluid.core.get_cuda_device_count() if self.cfg.use_gpu else 1 + print("kpis\tstgan_g_loss_fake_card{}\t{}".format(device_num, g_loss_fake[0])) + print("kpis\tstgan_g_loss_rec_card{}\t{}".format(device_num, g_loss_rec[0])) + print("kpis\tstgan_g_loss_cls_card{}\t{}".format(device_num, g_loss_cls[0])) + print("kpis\tstgan_d_loss_card{}\t{}".format(device_num, d_loss[0])) + print("kpis\tstgan_d_loss_real_card{}\t{}".format(device_num, d_loss_real[0])) + print("kpis\tstgan_d_loss_fake_card{}\t{}".format(device_num,d_loss_fake[0])) + print("kpis\tstgan_d_loss_cls_card{}\t{}".format(device_num, d_loss_cls[0])) + print("kpis\tstgan_d_loss_gp_card{}\t{}".format(device_num,d_loss_gp[0])) + print("kpis\tstgan_Batch_time_cost_card{}\t{}".format(device_num,batch_time)) diff --git a/PaddleCV/PaddleGAN/trainer/StarGAN.py b/PaddleCV/PaddleGAN/trainer/StarGAN.py index 9e2e8332..6fa72be7 100644 --- a/PaddleCV/PaddleGAN/trainer/StarGAN.py +++ b/PaddleCV/PaddleGAN/trainer/StarGAN.py @@ -159,8 +159,12 @@ class DTrainer(): def gradient_penalty(self, f, real, fake, cfg=None, name=None): def _interpolate(a, b): shape = [a.shape[0]] - alpha = fluid.layers.uniform_random_batch_size_like( - input=a, shape=shape, min=0.0, max=1.0) + if cfg.enable_ce: + alpha = fluid.layers.uniform_random_batch_size_like( + input=a, shape=shape, min=0.0, max=1.0, seed=1) + else: + alpha = fluid.layers.uniform_random_batch_size_like( + input=a, shape=shape, min=0.0, max=1.0) inner = fluid.layers.elementwise_mul(b, (1.0-alpha), axis=0) + fluid.layers.elementwise_mul(a, alpha, axis=0) return inner @@ -245,6 +249,10 @@ class StarGAN(object): help="the attributes we selected to change") parser.add_argument( '--n_samples', type=int, default=1, help="batch size when testing") + parser.add_argument( + '--enable_ce', + action='store_true', + help="if set, run the tasks with continuous evaluation logs") return parser @@ -268,6 +276,9 @@ class StarGAN(object): name='label_org', shape=[None, self.cfg.c_dim], dtype='float32') label_trg = fluid.data( name='label_trg', shape=[None, self.cfg.c_dim], dtype='float32') + # used for continuous evaluation + if self.cfg.enable_ce: + fluid.default_startup_program().random_seed = 90 py_reader = fluid.io.PyReader( feed_list=[image_real, label_org, label_trg], @@ -304,6 +315,10 @@ class StarGAN(object): dis_trainer.program).with_data_parallel( loss_name=dis_trainer.d_loss.name, build_strategy=build_strategy) + # used for continuous evaluation + if self.cfg.enable_ce: + gen_trainer_program.random_seed = 90 + dis_trainer_program.random_seed = 90 t_time = 0 total_train_batch = 0 # used for benchmark @@ -347,6 +362,10 @@ class StarGAN(object): sys.stdout.flush() batch_id += 1 + # used for ce + if self.cfg.enable_ce and batch_id == 100: + break + total_train_batch += 1 # used for benchmark # profiler tools if self.cfg.profile and epoch_id == 0 and batch_id == self.cfg.print_freq: @@ -378,3 +397,14 @@ class StarGAN(object): "net_G") utility.checkpoints(epoch_id, self.cfg, exe, dis_trainer, "net_D") + # used for continuous evaluation + if self.cfg.enable_ce: + device_num = fluid.core.get_cuda_device_count() if self.cfg.use_gpu else 1 + print("kpis\tstargan_g_loss_fake_card{}\t{}".format(device_num, g_loss_fake[0])) + print("kpis\tstargan_g_loss_rec_card{}\t{}".format(device_num, g_loss_rec[0])) + print("kpis\tstargan_g_loss_cls_card{}\t{}".format(device_num, g_loss_cls[0])) + print("kpis\tstargan_d_loss_real_card{}\t{}".format(device_num, d_loss_real[0])) + print("kpis\tstargan_d_loss_fake_card{}\t{}".format(device_num,d_loss_fake[0])) + print("kpis\tstargan_d_loss_cls_card{}\t{}".format(device_num, d_loss_cls[0])) + print("kpis\tstargan_d_loss_gp_card{}\t{}".format(device_num,d_loss_gp[0])) + print("kpis\tstargan_Batch_time_cost_card{}\t{}".format(device_num,batch_time)) -- GitLab