Pix2pix.py 15.3 KB
Newer Older
Z
zhumanyu 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from network.Pix2pix_network import Pix2pix_model
from util import utility
import paddle.fluid as fluid
H
hysunflower 已提交
21
from paddle.fluid import profiler
Z
zhumanyu 已提交
22 23
import sys
import time
C
ceci3 已提交
24
import numpy as np
Z
zhumanyu 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60


class GTrainer():
    def __init__(self, input_A, input_B, cfg, step_per_epoch):
        self.program = fluid.default_main_program().clone()
        with fluid.program_guard(self.program):
            model = Pix2pix_model()
            self.fake_B = model.network_G(input_A, "generator", cfg=cfg)
            self.fake_B.persistable = True
            self.infer_program = self.program.clone()
            AB = fluid.layers.concat([input_A, self.fake_B], 1)
            self.pred = model.network_D(AB, "discriminator", cfg)
            if cfg.gan_mode == "lsgan":
                ones = fluid.layers.fill_constant_batch_size_like(
                    input=self.pred,
                    shape=self.pred.shape,
                    value=1,
                    dtype='float32')
                self.g_loss_gan = fluid.layers.reduce_mean(
                    fluid.layers.square(
                        fluid.layers.elementwise_sub(
                            x=self.pred, y=ones)))
            elif cfg.gan_mode == "vanilla":
                pred_shape = self.pred.shape
                self.pred = fluid.layers.reshape(
                    self.pred,
                    [-1, pred_shape[1] * pred_shape[2] * pred_shape[3]],
                    inplace=True)
                ones = fluid.layers.fill_constant_batch_size_like(
                    input=self.pred,
                    shape=self.pred.shape,
                    value=1,
                    dtype='float32')
                self.g_loss_gan = fluid.layers.mean(
                    fluid.layers.sigmoid_cross_entropy_with_logits(
                        x=self.pred, label=ones))
L
lvmengsi 已提交
61 62 63
            else:
                raise NotImplementedError("gan_mode {} is not support!".format(
                    cfg.gan_mode))
Z
zhumanyu 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77

            self.g_loss_L1 = fluid.layers.reduce_mean(
                fluid.layers.abs(
                    fluid.layers.elementwise_sub(
                        x=input_B, y=self.fake_B))) * cfg.lambda_L1
            self.g_loss = fluid.layers.elementwise_add(self.g_loss_L1,
                                                       self.g_loss_gan)
            lr = cfg.learning_rate
            vars = []
            for var in self.program.list_vars():
                if fluid.io.is_parameter(var) and var.name.startswith(
                        "generator"):
                    vars.append(var.name)
            self.param = vars
L
lvmengsi 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
            if cfg.epoch <= 100:
                optimizer = fluid.optimizer.Adam(
                    learning_rate=lr, beta1=0.5, beta2=0.999, name="net_G")
            else:
                optimizer = fluid.optimizer.Adam(
                    learning_rate=fluid.layers.piecewise_decay(
                        boundaries=[99 * step_per_epoch] + [
                            x * step_per_epoch
                            for x in range(100, cfg.epoch - 1)
                        ],
                        values=[lr] + [
                            lr * (1.0 - (x - 99.0) / 101.0)
                            for x in range(100, cfg.epoch)
                        ]),
                    beta1=0.5,
                    beta2=0.999,
                    name="net_G")
Z
zhumanyu 已提交
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
            optimizer.minimize(self.g_loss, parameter_list=vars)


class DTrainer():
    def __init__(self, input_A, input_B, fake_B, cfg, step_per_epoch):
        self.program = fluid.default_main_program().clone()
        lr = cfg.learning_rate
        with fluid.program_guard(self.program):
            model = Pix2pix_model()
            self.real_AB = fluid.layers.concat([input_A, input_B], 1)
            self.fake_AB = fluid.layers.concat([input_A, fake_B], 1)
            self.pred_real = model.network_D(
                self.real_AB, "discriminator", cfg=cfg)
            self.pred_fake = model.network_D(
                self.fake_AB, "discriminator", cfg=cfg)
            if cfg.gan_mode == "lsgan":
                ones = fluid.layers.fill_constant_batch_size_like(
                    input=self.pred_real,
                    shape=self.pred_real.shape,
                    value=1,
                    dtype='float32')
                self.d_loss_real = fluid.layers.reduce_mean(
                    fluid.layers.square(
                        fluid.layers.elementwise_sub(
                            x=self.pred_real, y=ones)))
                self.d_loss_fake = fluid.layers.reduce_mean(
                    fluid.layers.square(x=self.pred_fake))
            elif cfg.gan_mode == "vanilla":
                pred_shape = self.pred_real.shape
                self.pred_real = fluid.layers.reshape(
                    self.pred_real,
                    [-1, pred_shape[1] * pred_shape[2] * pred_shape[3]],
                    inplace=True)
                self.pred_fake = fluid.layers.reshape(
                    self.pred_fake,
                    [-1, pred_shape[1] * pred_shape[2] * pred_shape[3]],
                    inplace=True)
                zeros = fluid.layers.fill_constant_batch_size_like(
                    input=self.pred_fake,
                    shape=self.pred_fake.shape,
                    value=0,
                    dtype='float32')
                ones = fluid.layers.fill_constant_batch_size_like(
                    input=self.pred_real,
                    shape=self.pred_real.shape,
                    value=1,
                    dtype='float32')
                self.d_loss_real = fluid.layers.mean(
                    fluid.layers.sigmoid_cross_entropy_with_logits(
                        x=self.pred_real, label=ones))
                self.d_loss_fake = fluid.layers.mean(
                    fluid.layers.sigmoid_cross_entropy_with_logits(
                        x=self.pred_fake, label=zeros))
L
lvmengsi 已提交
148 149 150 151
            else:
                raise NotImplementedError("gan_mode {} is not support!".format(
                    cfg.gan_mode))

Z
zhumanyu 已提交
152 153 154 155 156 157 158 159
            self.d_loss = 0.5 * (self.d_loss_real + self.d_loss_fake)
            vars = []
            for var in self.program.list_vars():
                if fluid.io.is_parameter(var) and var.name.startswith(
                        "discriminator"):
                    vars.append(var.name)

            self.param = vars
L
lvmengsi 已提交
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
            if cfg.epoch <= 100:
                optimizer = fluid.optimizer.Adam(
                    learning_rate=lr, beta1=0.5, beta2=0.999, name="net_D")
            else:
                optimizer = fluid.optimizer.Adam(
                    learning_rate=fluid.layers.piecewise_decay(
                        boundaries=[99 * step_per_epoch] + [
                            x * step_per_epoch
                            for x in range(100, cfg.epoch - 1)
                        ],
                        values=[lr] + [
                            lr * (1.0 - (x - 99.0) / 101.0)
                            for x in range(100, cfg.epoch)
                        ]),
                    beta1=0.5,
                    beta2=0.999,
                    name="net_D")
Z
zhumanyu 已提交
177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199

            optimizer.minimize(self.d_loss, parameter_list=vars)


class Pix2pix(object):
    def add_special_args(self, parser):
        parser.add_argument(
            '--net_G',
            type=str,
            default="unet_256",
            help="Choose the Pix2pix generator's network, choose in [resnet_9block|resnet_6block|unet_128|unet_256]"
        )
        parser.add_argument(
            '--net_D',
            type=str,
            default="basic",
            help="Choose the Pix2pix discriminator's network, choose in [basic|nlayers|pixel]"
        )
        parser.add_argument(
            '--d_nlayers',
            type=int,
            default=3,
            help="only used when Pix2pix discriminator is nlayers")
u010070587's avatar
u010070587 已提交
200 201 202 203
        parser.add_argument(
            '--enable_ce',
            action='store_true',
            help="if set, run the tasks with continuous evaluation logs")
Z
zhumanyu 已提交
204 205 206 207 208 209
        return parser

    def __init__(self,
                 cfg=None,
                 train_reader=None,
                 test_reader=None,
L
lvmengsi 已提交
210 211
                 batch_num=1,
                 id2name=None):
Z
zhumanyu 已提交
212 213 214 215
        self.cfg = cfg
        self.train_reader = train_reader
        self.test_reader = test_reader
        self.batch_num = batch_num
L
lvmengsi 已提交
216
        self.id2name = id2name
Z
zhumanyu 已提交
217 218

    def build_model(self):
L
lvmengsi 已提交
219
        data_shape = [None, 3, self.cfg.crop_size, self.cfg.crop_size]
Z
zhumanyu 已提交
220

L
lvmengsi 已提交
221 222 223
        input_A = fluid.data(name='input_A', shape=data_shape, dtype='float32')
        input_B = fluid.data(name='input_B', shape=data_shape, dtype='float32')
        input_fake = fluid.data(
Z
zhumanyu 已提交
224
            name='input_fake', shape=data_shape, dtype='float32')
u010070587's avatar
u010070587 已提交
225 226 227
        # used for continuous evaluation        
        if self.cfg.enable_ce:
            fluid.default_startup_program().random_seed = 90
Z
zhumanyu 已提交
228

L
lvmengsi 已提交
229
        loader = fluid.io.DataLoader.from_generator(
L
lvmengsi 已提交
230
            feed_list=[input_A, input_B],
L
lvmengsi 已提交
231
            capacity=4,
L
lvmengsi 已提交
232 233 234
            iterable=True,
            use_double_buffer=True)

Z
zhumanyu 已提交
235 236 237 238 239 240
        gen_trainer = GTrainer(input_A, input_B, self.cfg, self.batch_num)
        dis_trainer = DTrainer(input_A, input_B, input_fake, self.cfg,
                               self.batch_num)

        # prepare environment
        place = fluid.CUDAPlace(0) if self.cfg.use_gpu else fluid.CPUPlace()
L
lvmengsi 已提交
241
        loader.set_batch_generator(
L
lvmengsi 已提交
242 243 244
            self.train_reader,
            places=fluid.cuda_places()
            if self.cfg.use_gpu else fluid.cpu_places())
Z
zhumanyu 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())

        if self.cfg.init_model:
            utility.init_checkpoints(self.cfg, exe, gen_trainer, "net_G")
            utility.init_checkpoints(self.cfg, exe, dis_trainer, "net_D")

        ### memory optim
        build_strategy = fluid.BuildStrategy()

        gen_trainer_program = fluid.CompiledProgram(
            gen_trainer.program).with_data_parallel(
                loss_name=gen_trainer.g_loss.name,
                build_strategy=build_strategy)
        dis_trainer_program = fluid.CompiledProgram(
            dis_trainer.program).with_data_parallel(
                loss_name=dis_trainer.d_loss.name,
                build_strategy=build_strategy)

        t_time = 0

H
hysunflower 已提交
266 267
        total_train_batch = 0  # used for benchmark

Z
zhumanyu 已提交
268 269
        for epoch_id in range(self.cfg.epoch):
            batch_id = 0
L
lvmengsi 已提交
270
            for tensor in loader():
u010070587's avatar
u010070587 已提交
271
                if self.cfg.max_iter and total_train_batch == self.cfg.max_iter:  # used for benchmark
H
hysunflower 已提交
272
                    return
Z
zhumanyu 已提交
273
                s_time = time.time()
L
lvmengsi 已提交
274

Z
zhumanyu 已提交
275 276 277 278 279 280 281
                # optimize the generator network
                g_loss_gan, g_loss_l1, fake_B_tmp = exe.run(
                    gen_trainer_program,
                    fetch_list=[
                        gen_trainer.g_loss_gan, gen_trainer.g_loss_L1,
                        gen_trainer.fake_B
                    ],
L
lvmengsi 已提交
282
                    feed=tensor)
Z
zhumanyu 已提交
283

C
ceci3 已提交
284 285 286 287 288
                devices_num = utility.get_device_num(self.cfg)
                fake_per_device = int(len(fake_B_tmp) / devices_num)
                for dev in range(devices_num):
                    tensor[dev]['input_fake'] = fake_B_tmp[dev * fake_per_device : (dev+1) * fake_per_device]

Z
zhumanyu 已提交
289 290 291 292 293 294
                # optimize the discriminator network
                d_loss_real, d_loss_fake = exe.run(dis_trainer_program,
                                                   fetch_list=[
                                                       dis_trainer.d_loss_real,
                                                       dis_trainer.d_loss_fake
                                                   ],
C
ceci3 已提交
295
                                                   feed=tensor)
Z
zhumanyu 已提交
296 297 298 299 300 301 302

                batch_time = time.time() - s_time
                t_time += batch_time
                if batch_id % self.cfg.print_freq == 0:
                    print("epoch{}: batch{}: \n\
                         g_loss_gan: {}; g_loss_l1: {}; \n\
                         d_loss_real: {}; d_loss_fake: {}; \n\
303
                         Batch_time_cost: {}"
Z
zhumanyu 已提交
304 305 306 307 308
                          .format(epoch_id, batch_id, g_loss_gan[0], g_loss_l1[
                              0], d_loss_real[0], d_loss_fake[0], batch_time))

                sys.stdout.flush()
                batch_id += 1
H
hysunflower 已提交
309 310 311 312 313 314
                total_train_batch += 1  # used for benchmark
                # profiler tools
                if self.cfg.profile and epoch_id == 0 and batch_id == self.cfg.print_freq:
                    profiler.reset_profiler()
                elif self.cfg.profile and epoch_id == 0 and batch_id == self.cfg.print_freq + 5:
                    return
Z
zhumanyu 已提交
315 316

            if self.cfg.run_test:
L
lvmengsi 已提交
317
                image_name = fluid.data(
L
lvmengsi 已提交
318
                    name='image_name',
L
lvmengsi 已提交
319
                    shape=[None, self.cfg.batch_size],
L
lvmengsi 已提交
320
                    dtype="int32")
L
lvmengsi 已提交
321
                test_loader = fluid.io.DataLoader.from_generator(
L
lvmengsi 已提交
322
                    feed_list=[input_A, input_B, image_name],
L
lvmengsi 已提交
323
                    capacity=4,
L
lvmengsi 已提交
324 325
                    iterable=True,
                    use_double_buffer=True)
L
lvmengsi 已提交
326
                test_loader.set_batch_generator(
L
lvmengsi 已提交
327 328 329
                    self.test_reader,
                    places=fluid.cuda_places()
                    if self.cfg.use_gpu else fluid.cpu_places())
Z
zhumanyu 已提交
330
                test_program = gen_trainer.infer_program
L
lvmengsi 已提交
331 332 333 334 335 336 337
                utility.save_test_image(
                    epoch_id,
                    self.cfg,
                    exe,
                    place,
                    test_program,
                    gen_trainer,
L
lvmengsi 已提交
338
                    test_loader,
L
lvmengsi 已提交
339
                    A_id2name=self.id2name)
Z
zhumanyu 已提交
340 341 342 343 344 345

            if self.cfg.save_checkpoints:
                utility.checkpoints(epoch_id, self.cfg, exe, gen_trainer,
                                    "net_G")
                utility.checkpoints(epoch_id, self.cfg, exe, dis_trainer,
                                    "net_D")
u010070587's avatar
u010070587 已提交
346 347 348 349 350 351 352 353 354 355 356 357 358
        if self.cfg.enable_ce:
            device_num = fluid.core.get_cuda_device_count(
            ) if self.cfg.use_gpu else 1
            print("kpis\tpix2pix_g_loss_gan_card{}\t{}".format(device_num,
                                                               g_loss_gan[0]))
            print("kpis\tpix2pix_g_loss_l1_card{}\t{}".format(device_num,
                                                              g_loss_l1[0]))
            print("kpis\tpix2pix_d_loss_real_card{}\t{}".format(device_num,
                                                                d_loss_real[0]))
            print("kpis\tpix2pix_d_loss_fake_card{}\t{}".format(device_num,
                                                                d_loss_fake[0]))
            print("kpis\tpix2pix_Batch_time_cost_card{}\t{}".format(device_num,
                                                                    batch_time))