DCGAN_network.py 3.1 KB
Newer Older
L
lvmengsi 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
#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

L
lvmengsi 已提交
19
from .base_network import norm_layer, deconv2d, linear, conv_and_pool
L
lvmengsi 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33

import paddle.fluid as fluid
import numpy as np
import os


class DCGAN_model(object):
    def __init__(self, batch_size=1):
        self.batch_size = batch_size
        self.img_dim = 28
        self.gfc_dim = 2048
        self.dfc_dim = 1024
        self.gf_dim = 64
        self.df_dim = 64
L
lvmengsi 已提交
34 35 36 37
        if self.batch_size == 1:
            self.norm = None
        else:
            self.norm = "batch_norm"
L
lvmengsi 已提交
38 39

    def network_G(self, input, name="generator"):
L
lvmengsi 已提交
40 41 42 43 44 45
        o_l1 = linear(
            input,
            self.gfc_dim,
            norm=self.norm,
            activation_fn='relu',
            name=name + '_l1')
L
lvmengsi 已提交
46
        o_l2 = linear(
L
lvmengsi 已提交
47 48
            input=o_l1,
            output_size=self.gf_dim * 2 * self.img_dim // 4 * self.img_dim // 4,
L
lvmengsi 已提交
49
            norm=self.norm,
L
lvmengsi 已提交
50
            activation_fn='relu',
L
lvmengsi 已提交
51 52 53 54
            name=name + '_l2')
        o_r1 = fluid.layers.reshape(
            o_l2, [-1, self.df_dim * 2, self.img_dim // 4, self.img_dim // 4])
        o_dc1 = deconv2d(
L
lvmengsi 已提交
55 56 57 58 59
            input=o_r1,
            num_filters=self.gf_dim * 2,
            filter_size=5,
            stride=2,
            padding=2,
L
lvmengsi 已提交
60 61
            activation_fn='relu',
            output_size=[self.img_dim // 2, self.img_dim // 2],
L
lvmengsi 已提交
62
            use_bias=True,
L
lvmengsi 已提交
63 64
            name=name + '_dc1')
        o_dc2 = deconv2d(
L
lvmengsi 已提交
65 66 67 68 69
            input=o_dc1,
            num_filters=1,
            filter_size=5,
            stride=2,
            padding=2,
L
lvmengsi 已提交
70
            activation_fn='tanh',
L
lvmengsi 已提交
71
            use_bias=True,
L
lvmengsi 已提交
72 73 74 75 76 77 78 79
            output_size=[self.img_dim, self.img_dim],
            name=name + '_dc2')
        out = fluid.layers.reshape(o_dc2, shape=[-1, 28 * 28])
        return out

    def network_D(self, input, name="discriminator"):
        o_r1 = fluid.layers.reshape(
            input, shape=[-1, 1, self.img_dim, self.img_dim])
L
lvmengsi 已提交
80 81 82 83 84 85
        o_c1 = conv_and_pool(
            o_r1, self.df_dim, name=name + '_c1', act='leaky_relu')
        o_c2_1 = conv_and_pool(o_c1, self.df_dim * 2, name=name + '_c2')
        o_c2_2 = norm_layer(
            o_c2_1, norm_type='batch_norm', name=name + '_c2_bn')
        o_c2 = fluid.layers.leaky_relu(o_c2_2, name=name + '_c2_leaky_relu')
L
lvmengsi 已提交
86
        o_l1 = linear(
L
lvmengsi 已提交
87 88
            input=o_c2,
            output_size=self.dfc_dim,
L
lvmengsi 已提交
89
            norm=self.norm,
L
lvmengsi 已提交
90 91
            activation_fn='leaky_relu',
            name=name + '_l1')
C
ceci3 已提交
92
        out = linear(o_l1, 1, activation_fn=None, name=name + '_l2')
L
lvmengsi 已提交
93
        return out