-
Notifications
You must be signed in to change notification settings - Fork 2
/
models.py
110 lines (102 loc) · 3.85 KB
/
models.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
61
62
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
import keras.backend as K
from keras.models import Model
from keras.layers import Input
from keras.layers.core import Dense, Flatten, Reshape, Dropout, Lambda
from keras.layers.merge import Concatenate
from keras.layers.convolutional import Conv2D, Conv2DTranspose
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.normalization import BatchNormalization
from layers import ConvMaxout
def create_xgenerater():
input = Input(shape=(1,1,64))
x = Conv2DTranspose(256, (4,4), strides=(1,1))(input)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2DTranspose(128, (4,4), strides=(2,2))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2DTranspose(64, (4,4), strides=(1,1))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2DTranspose(32, (4,4), strides=(2,2))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2DTranspose(32, (5,5), strides=(1,1))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2D(32, (1,1), strides=(1,1))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
output = Conv2D(3, (1,1), strides=(1,1), activation='sigmoid')(x)
return Model(input, output, name='xgenerater')
def create_zgenerater():
input = Input(shape=(32,32,3))
x = Conv2D(32, (5,5), strides=(1,1))(input)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2D(64, (4,4), strides=(2,2))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2D(128, (4,4), strides=(1,1))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2D(256, (4,4), strides=(2,2))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2D(512, (4,4), strides=(1,1))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
x = Conv2D(512, (1,1), strides=(1,1))(x)
x = BatchNormalization()(x)
x = LeakyReLU(0.1)(x)
mu = Conv2D(64, (1,1), strides=(1,1))(x)
sigma = Conv2D(64, (1,1), strides=(1,1))(x)
concatenated = Concatenate(axis=-1)([mu, sigma])
output = Lambda(
function=lambda x: x[:,:,:,:64] + K.exp(x[:,:,:,64:]) * K.random_normal(shape=K.shape(x[:,:,:,64:])),
output_shape=(1,1,64)
)(concatenated)
return Model(input, output, name='zgenerater')
def create_discriminater():
input_x = Input(shape=(32,32,3))
input_z = Input(shape=(1,1,64))
x = Dropout(0.2)(input_x)
x = Conv2D(32, (5,5), strides=(1,1))(x)
x = ConvMaxout(n_piece=2)(x)
x = Dropout(0.5)(x)
x = Conv2D(64, (4,4), strides=(2,2))(x)
x = ConvMaxout(n_piece=2)(x)
x = Dropout(0.5)(x)
x = Conv2D(128, (4,4), strides=(1,1))(x)
x = ConvMaxout(n_piece=2)(x)
x = Dropout(0.5)(x)
x = Conv2D(256, (4,4), strides=(2,2))(x)
x = ConvMaxout(n_piece=2)(x)
x = Dropout(0.5)(x)
x = Conv2D(512, (4,4), strides=(1,1))(x)
x = ConvMaxout(n_piece=2)(x)
z = Dropout(0.2)(input_z)
z = Conv2D(512, (1,1), strides=(1,1))(z)
z = ConvMaxout(n_piece=2)(z)
z = Dropout(0.5)(z)
z = Conv2D(512, (1,1), strides=(1,1))(z)
z = ConvMaxout(n_piece=2)(z)
concatenated = Concatenate(axis=-1)([x, z])
c = Dropout(0.5)(concatenated)
c = Conv2D(1024, (1,1), strides=(1,1))(c)
c = ConvMaxout(n_piece=2)(c)
c = Dropout(0.5)(c)
c = Conv2D(1024, (1,1), strides=(1,1))(c)
c = ConvMaxout(n_piece=2)(c)
c = Dropout(0.5)(c)
c = Conv2D(1, (1,1), strides=(1,1), activation='sigmoid')(c)
return Model([input_x, input_z], c, name='discriminater')
def create_gan(xgenerater, zgenerater, discriminater):
input_x = Input(shape=(32,32,3))
input_z = Input(shape=(1,1,64))
x_gen = xgenerater(input_z)
z_gen = zgenerater(input_x)
p = discriminater([x_gen, input_z])
q = discriminater([input_x, z_gen])
concatenated = Concatenate(axis=-1)([p,q])
return Model([input_x, input_z], concatenated, name='gan')