SPADE.py 17.0 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 21
#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.SPADE_network import SPADE_model
from util import utility
import paddle.fluid as fluid
import sys
L
lvmengsi 已提交
22
import os
Z
zhumanyu 已提交
23 24 25 26
import time
import network.vgg as vgg
import pickle as pkl
import numpy as np
L
lvmengsi 已提交
27 28


Z
zhumanyu 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class GTrainer():
    def __init__(self, input_label, input_img, input_ins, cfg, step_per_epoch):
        self.cfg = cfg
        self.program = fluid.default_main_program().clone()
        with fluid.program_guard(self.program):
            model = SPADE_model()
            input = input_label
            if not cfg.no_instance:
                input = fluid.layers.concat([input_label, input_ins], 1)
            self.fake_B = model.network_G(input, "generator", cfg=cfg)
            self.fake_B.persistable = True
            self.infer_program = self.program.clone()
            fake_concat = fluid.layers.concat([input, self.fake_B], 1)
            real_concat = fluid.layers.concat([input, input_img], 1)
            fake_and_real = fluid.layers.concat([fake_concat, real_concat], 0)
            pred = model.network_D(fake_and_real, "discriminator", cfg)
            if type(pred) == list:
                self.pred_fake = []
                self.pred_real = []
                for p in pred:
L
lvmengsi 已提交
49 50 51 52
                    self.pred_fake.append(
                        [tensor[:tensor.shape[0] // 2] for tensor in p])
                    self.pred_real.append(
                        [tensor[tensor.shape[0] // 2:] for tensor in p])
Z
zhumanyu 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
            else:
                self.pred_fake = pred[:pred.shape[0] // 2]
                self.pred_real = pred[pred.shape[0] // 2:]

            ###GAN Loss hinge
            if isinstance(self.pred_fake, list):
                self.gan_loss = 0
                for pred_i in self.pred_fake:
                    if isinstance(pred_i, list):
                        pred_i = pred_i[-1]
                    loss_i = -1 * fluid.layers.reduce_mean(pred_i)
                    self.gan_loss += loss_i
                self.gan_loss /= len(self.pred_fake)
            else:
                self.gan_loss = -1 * fluid.layers.reduce_mean(self.pred_fake)
            self.gan_loss.persistable = True
            #####GAN Feat loss
            num_D = len(self.pred_fake)
            self.gan_feat_loss = 0.0
            for i in range(num_D):
                num_intermediate_outputs = len(self.pred_fake[i]) - 1
                for j in range(num_intermediate_outputs):
L
lvmengsi 已提交
75 76 77 78 79
                    self.gan_feat_loss = fluid.layers.reduce_mean(
                        fluid.layers.abs(
                            fluid.layers.elementwise_sub(
                                x=self.pred_fake[i][j], y=self.pred_real[i][
                                    j]))) * cfg.lambda_feat / num_D
Z
zhumanyu 已提交
80 81
            self.gan_feat_loss.persistable = True
            ########VGG Feat loss
L
lvmengsi 已提交
82
            weights = [1.0 / 32, 1.0 / 16, 1.0 / 8, 1.0 / 4, 1.0]
Z
zhumanyu 已提交
83 84 85 86 87
            self.vgg = vgg.VGG19()
            fake_vgg = self.vgg.net(self.fake_B)
            real_vgg = self.vgg.net(input_img)
            self.vgg_loss = 0.0
            for i in range(len(fake_vgg)):
L
lvmengsi 已提交
88 89 90 91
                self.vgg_loss += weights[i] * fluid.layers.reduce_mean(
                    fluid.layers.abs(
                        fluid.layers.elementwise_sub(
                            x=fake_vgg[i], y=real_vgg[i])))
Z
zhumanyu 已提交
92
            self.vgg_loss.persistable = True
L
lvmengsi 已提交
93 94
            self.g_loss = (
                self.gan_loss + self.gan_feat_loss + self.vgg_loss) / 3
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
            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
            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")
            optimizer.minimize(self.g_loss, parameter_list=vars)


class DTrainer():
L
lvmengsi 已提交
123 124
    def __init__(self, input_label, input_img, input_ins, fake_B, cfg,
                 step_per_epoch):
Z
zhumanyu 已提交
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
        self.program = fluid.default_main_program().clone()
        lr = cfg.learning_rate
        with fluid.program_guard(self.program):
            model = SPADE_model()
            input = input_label
            if not cfg.no_instance:
                input = fluid.layers.concat([input_label, input_ins], 1)
            fake_concat = fluid.layers.concat([input, fake_B], 1)
            real_concat = fluid.layers.concat([input, input_img], 1)
            fake_and_real = fluid.layers.concat([fake_concat, real_concat], 0)
            pred = model.network_D(fake_and_real, "discriminator", cfg)
            if type(pred) == list:
                self.pred_fake = []
                self.pred_real = []
                for p in pred:
L
lvmengsi 已提交
140 141 142 143
                    self.pred_fake.append(
                        [tensor[:tensor.shape[0] // 2] for tensor in p])
                    self.pred_real.append(
                        [tensor[tensor.shape[0] // 2:] for tensor in p])
Z
zhumanyu 已提交
144 145 146 147 148 149 150
            else:
                self.pred_fake = pred[:pred.shape[0] // 2]
                self.pred_real = pred[pred.shape[0] // 2:]

            #####gan loss
            self.gan_loss_fake = 0
            for pred_i in self.pred_fake:
L
lvmengsi 已提交
151 152 153 154 155
                zeros = fluid.layers.fill_constant_batch_size_like(
                    input=pred_i[-1],
                    shape=pred_i[-1].shape,
                    value=0,
                    dtype='float32')
Z
zhumanyu 已提交
156 157
                if isinstance(pred_i, list):
                    pred_i = pred_i[-1]
L
lvmengsi 已提交
158
                minval = fluid.layers.elementwise_min(-1 * pred_i - 1, zeros)
Z
zhumanyu 已提交
159 160 161
                loss_i = -1 * fluid.layers.reduce_mean(minval)
                self.gan_loss_fake += loss_i
            self.gan_loss_fake /= len(self.pred_fake)
L
lvmengsi 已提交
162

Z
zhumanyu 已提交
163 164
            self.gan_loss_real = 0
            for pred_i in self.pred_real:
L
lvmengsi 已提交
165 166 167 168 169
                zeros = fluid.layers.fill_constant_batch_size_like(
                    input=pred_i[-1],
                    shape=pred_i[-1].shape,
                    value=0,
                    dtype='float32')
Z
zhumanyu 已提交
170 171
                if isinstance(pred_i, list):
                    pred_i = pred_i[-1]
L
lvmengsi 已提交
172
                minval = fluid.layers.elementwise_min(pred_i - 1, zeros)
Z
zhumanyu 已提交
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
                loss_i = -1 * fluid.layers.reduce_mean(minval)
                self.gan_loss_real += loss_i
            self.gan_loss_real /= len(self.pred_real)
            self.gan_loss_real.persistable = True
            self.gan_loss_fake.persistable = True

            self.d_loss = 0.5 * (self.gan_loss_real + self.gan_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
            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")

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


class SPADE(object):
    def add_special_args(self, parser):
        parser.add_argument(
            '--vgg19_pretrain',
            type=str,
            default="./VGG19_pretrained",
L
lvmengsi 已提交
213
            help="VGG19 pretrained model for vgg loss")
Z
zhumanyu 已提交
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
        parser.add_argument(
            '--crop_width',
            type=int,
            default=1024,
            help="crop width for training SPADE")
        parser.add_argument(
            '--crop_height',
            type=int,
            default=512,
            help="crop height for training SPADE")
        parser.add_argument(
            '--load_width',
            type=int,
            default=1124,
            help="load width for training SPADE")
        parser.add_argument(
            '--load_height',
            type=int,
            default=612,
            help="load height for training SPADE")
        parser.add_argument(
            '--d_nlayers',
            type=int,
            default=4,
            help="num of discriminator layers for SPADE")
        parser.add_argument(
L
lvmengsi 已提交
240
            '--label_nc', type=int, default=36, help="label numbers of SPADE")
Z
zhumanyu 已提交
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265
        parser.add_argument(
            '--ngf',
            type=int,
            default=64,
            help="base channels of generator in SPADE")
        parser.add_argument(
            '--ndf',
            type=int,
            default=64,
            help="base channels of discriminator in SPADE")
        parser.add_argument(
            '--num_D',
            type=int,
            default=2,
            help="number of discriminators in SPADE")
        parser.add_argument(
            '--lambda_feat',
            type=float,
            default=10,
            help="weight term of feature loss")
        parser.add_argument(
            '--lambda_vgg',
            type=float,
            default=10,
            help="weight term of vgg loss")
L
lvmengsi 已提交
266 267 268 269 270
        parser.add_argument(
            '--no_instance',
            type=bool,
            default=False,
            help="Whether to use instance label.")
Z
zhumanyu 已提交
271 272 273 274 275 276 277

        return parser

    def __init__(self,
                 cfg=None,
                 train_reader=None,
                 test_reader=None,
L
lvmengsi 已提交
278 279
                 batch_num=1,
                 id2name=None):
Z
zhumanyu 已提交
280 281 282 283
        self.cfg = cfg
        self.train_reader = train_reader
        self.test_reader = test_reader
        self.batch_num = batch_num
L
lvmengsi 已提交
284
        self.id2name = id2name
Z
zhumanyu 已提交
285 286 287

    def build_model(self):
        data_shape = [-1, 3, self.cfg.crop_height, self.cfg.crop_width]
L
lvmengsi 已提交
288 289 290
        label_shape = [
            -1, self.cfg.label_nc, self.cfg.crop_height, self.cfg.crop_width
        ]
Z
zhumanyu 已提交
291 292
        edge_shape = [-1, 1, self.cfg.crop_height, self.cfg.crop_width]

293
        input_A = fluid.layers.data(
Z
zhumanyu 已提交
294
            name='input_label', shape=label_shape, dtype='float32')
295
        input_B = fluid.layers.data(
Z
zhumanyu 已提交
296
            name='input_img', shape=data_shape, dtype='float32')
297
        input_C = fluid.layers.data(
Z
zhumanyu 已提交
298
            name='input_ins', shape=edge_shape, dtype='float32')
299
        input_fake = fluid.layers.data(
Z
zhumanyu 已提交
300 301
            name='input_fake', shape=data_shape, dtype='float32')

L
lvmengsi 已提交
302 303
        gen_trainer = GTrainer(input_A, input_B, input_C, self.cfg,
                               self.batch_num)
Z
zhumanyu 已提交
304 305 306 307 308 309 310
        dis_trainer = DTrainer(input_A, input_B, input_C, input_fake, self.cfg,
                               self.batch_num)
        py_reader = fluid.io.PyReader(
            feed_list=[input_A, input_B, input_C],
            capacity=4,  ## batch_size * 4
            iterable=True,
            use_double_buffer=True)
L
lvmengsi 已提交
311
        py_reader.decorate_batch_generator(
Z
zhumanyu 已提交
312 313 314 315 316 317 318 319
            self.train_reader,
            places=fluid.cuda_places()
            if self.cfg.use_gpu else fluid.cpu_places())

        # prepare environment
        place = fluid.CUDAPlace(0) if self.cfg.use_gpu else fluid.CPUPlace()
        exe = fluid.Executor(place)
        exe.run(fluid.default_startup_program())
L
lvmengsi 已提交
320 321 322 323 324 325

        if not os.path.exists(self.cfg.vgg19_pretrain):
            print(
                "directory VGG19_pretrain NOT EXIST!!! Please download VGG19 first."
            )
            sys.exit(1)
L
lvmengsi 已提交
326 327 328
        gen_trainer.vgg.load_vars(exe, gen_trainer.program,
                                  self.cfg.vgg19_pretrain)

Z
zhumanyu 已提交
329 330 331 332 333 334
        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()
L
lvmengsi 已提交
335
        build_strategy.enable_inplace = True
L
lvmengsi 已提交
336
        build_strategy.sync_batch_norm = False
Z
zhumanyu 已提交
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351

        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

        for epoch_id in range(self.cfg.epoch):
            batch_id = 0
            for tensor in py_reader():
L
lvmengsi 已提交
352 353
                data_A, data_B, data_C = tensor[0]['input_label'], tensor[0][
                    'input_img'], tensor[0]['input_ins']
Z
zhumanyu 已提交
354 355 356 357 358
                s_time = time.time()
                # optimize the generator network
                g_loss_gan, g_loss_vgg, g_loss_feat, fake_B_tmp = exe.run(
                    gen_trainer_program,
                    fetch_list=[
L
lvmengsi 已提交
359 360
                        gen_trainer.gan_loss, gen_trainer.vgg_loss,
                        gen_trainer.gan_feat_loss, gen_trainer.fake_B
Z
zhumanyu 已提交
361
                    ],
L
lvmengsi 已提交
362
                    feed={
L
lvmengsi 已提交
363 364 365
                        "input_label": data_A,
                        "input_img": data_B,
                        "input_ins": data_C
L
lvmengsi 已提交
366 367
                    })

Z
zhumanyu 已提交
368
                # optimize the discriminator network
L
lvmengsi 已提交
369 370 371 372 373 374
                d_loss_real, d_loss_fake = exe.run(
                    dis_trainer_program,
                    fetch_list=[
                        dis_trainer.gan_loss_real, dis_trainer.gan_loss_fake
                    ],
                    feed={
L
lvmengsi 已提交
375 376 377
                        "input_label": data_A,
                        "input_img": data_B,
                        "input_ins": data_C,
L
lvmengsi 已提交
378 379
                        "input_fake": fake_B_tmp
                    })
Z
zhumanyu 已提交
380 381 382 383 384 385 386 387 388

                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_vgg: {}; g_loss_feat: {} \n\
                         d_loss_real: {}; d_loss_fake: {}; \n\
                         Batch_time_cost: {:.2f}"
                          .format(epoch_id, batch_id, g_loss_gan[0], g_loss_vgg[
L
lvmengsi 已提交
389 390
                              0], g_loss_feat[0], d_loss_real[0], d_loss_fake[
                                  0], batch_time))
Z
zhumanyu 已提交
391 392 393 394 395 396

                sys.stdout.flush()
                batch_id += 1

            if self.cfg.run_test:
                test_program = gen_trainer.infer_program
397
                image_name = fluid.layers.data(
Z
zhumanyu 已提交
398
                    name='image_name',
399
                    shape=[self.cfg.batch_size],
Z
zhumanyu 已提交
400 401
                    dtype="int32")
                test_py_reader = fluid.io.PyReader(
L
lvmengsi 已提交
402
                    feed_list=[input_A, input_B, input_C, image_name],
Z
zhumanyu 已提交
403 404 405 406 407 408 409
                    capacity=4,  ## batch_size * 4
                    iterable=True,
                    use_double_buffer=True)
                test_py_reader.decorate_batch_generator(
                    self.test_reader,
                    places=fluid.cuda_places()
                    if self.cfg.use_gpu else fluid.cpu_places())
L
lvmengsi 已提交
410 411 412 413 414 415 416 417 418
                utility.save_test_image(
                    epoch_id,
                    self.cfg,
                    exe,
                    place,
                    test_program,
                    gen_trainer,
                    test_py_reader,
                    A_id2name=self.id2name)
Z
zhumanyu 已提交
419 420 421 422 423 424

            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")