trainer.py 3.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
# Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
#
# 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.

X
xiaoting 已提交
15 16 17 18 19 20 21 22 23 24 25 26 27
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from model import *
import paddle.fluid as fluid

step_per_epoch = 2974
lambda_A = 10.0
lambda_B = 10.0
lambda_identity = 0.5


class Cycle_Gan(fluid.dygraph.Layer):
28 29
    def __init__(self, input_channel, istrain=True):
        super (Cycle_Gan, self).__init__()
X
xiaoting 已提交
30

31 32
        self.build_generator_resnet_9blocks_a = build_generator_resnet_9blocks(input_channel)
        self.build_generator_resnet_9blocks_b = build_generator_resnet_9blocks(input_channel)
X
xiaoting 已提交
33
        if istrain:
34 35
            self.build_gen_discriminator_a = build_gen_discriminator(input_channel)
            self.build_gen_discriminator_b = build_gen_discriminator(input_channel)
X
xiaoting 已提交
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 61

    def forward(self,input_A,input_B,is_G,is_DA,is_DB):

        if is_G:
            fake_B = self.build_generator_resnet_9blocks_a(input_A)
            fake_A = self.build_generator_resnet_9blocks_b(input_B)
            cyc_A = self.build_generator_resnet_9blocks_b(fake_B)
            cyc_B = self.build_generator_resnet_9blocks_a(fake_A)

            diff_A = fluid.layers.abs(
                fluid.layers.elementwise_sub(
                    x=input_A,y=cyc_A))
            diff_B = fluid.layers.abs(
                fluid.layers.elementwise_sub(
                    x=input_B, y=cyc_B))
            cyc_A_loss = fluid.layers.reduce_mean(diff_A) * lambda_A
            cyc_B_loss = fluid.layers.reduce_mean(diff_B) * lambda_B
            cyc_loss = cyc_A_loss + cyc_B_loss

            fake_rec_A = self.build_gen_discriminator_a(fake_B)
            g_A_loss = fluid.layers.reduce_mean(fluid.layers.square(fake_rec_A-1))
         
            fake_rec_B = self.build_gen_discriminator_b(fake_A)
            g_B_loss = fluid.layers.reduce_mean(fluid.layers.square(fake_rec_B-1))
            G = g_A_loss + g_B_loss
            idt_A = self.build_generator_resnet_9blocks_a(input_B)
X
xiaoting 已提交
62
            idt_loss_A = fluid.layers.reduce_mean(fluid.layers.abs(fluid.layers.elementwise_sub(x = input_B , y = idt_A))) * lambda_B * lambda_identity
X
xiaoting 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87

            idt_B = self.build_generator_resnet_9blocks_b(input_A)
            idt_loss_B = fluid.layers.reduce_mean(fluid.layers.abs(fluid.layers.elementwise_sub(x = input_A , y = idt_B))) * lambda_A * lambda_identity
            idt_loss = fluid.layers.elementwise_add(idt_loss_A,idt_loss_B)
            g_loss = cyc_loss + G + idt_loss
            return fake_A,fake_B,cyc_A,cyc_B,g_A_loss,g_B_loss,idt_loss_A,idt_loss_B,cyc_A_loss,cyc_B_loss,g_loss


        if is_DA:

            ### D
            rec_B = self.build_gen_discriminator_a(input_A)
            fake_pool_rec_B = self.build_gen_discriminator_a(input_B)
            
            return rec_B, fake_pool_rec_B

        if is_DB:

            rec_A = self.build_gen_discriminator_b(input_A)

            fake_pool_rec_A = self.build_gen_discriminator_b(input_B)


        return rec_A, fake_pool_rec_A