-
Notifications
You must be signed in to change notification settings - Fork 12
/
ex7_1_gan_dnn_prob.py
233 lines (180 loc) · 6.2 KB
/
ex7_1_gan_dnn_prob.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
import numpy as np
import matplotlib.pyplot as plt
from keras import models
from keras.layers import Dense, Conv1D, Reshape, Flatten, Lambda
from keras.optimizers import Adam
from keras import backend as K
def add_decorate(x):
"""
axis = -1 --> last dimension in an array
"""
m = K.mean(x, axis=-1, keepdims=True)
d = K.square(x - m)
return K.concatenate([x, d], axis=-1)
def add_decorate_shape(input_shape):
shape = list(input_shape)
assert len(shape) == 2
shape[1] *= 2
return tuple(shape)
# model.add(Lambda(antirectifier, output_shape=antirectifier_output_shape))
lr = 2e-4 # 0.0002
adam = Adam(lr=lr, beta_1=0.9, beta_2=0.999)
def model_compile(model):
return model.compile(loss='binary_crossentropy', optimizer=adam, metrics=['accuracy'])
class GAN:
def __init__(self, ni_D, nh_D, nh_G):
self.ni_D = ni_D
self.nh_D = nh_D
self.nh_G = nh_G
self.D = self.gen_D()
self.G = self.gen_G()
self.GD = self.make_GD()
def gen_D(self):
ni_D = self.ni_D
nh_D = self.nh_D
D = models.Sequential()
D.add(Lambda(add_decorate, output_shape=add_decorate_shape, input_shape=(ni_D,)))
D.add(Dense(nh_D, activation='relu'))
D.add(Dense(nh_D, activation='relu'))
D.add(Dense(1, activation='sigmoid'))
model_compile(D)
return D
def gen_G(self):
ni_D = self.ni_D
nh_G = self.nh_D
G = models.Sequential() # (Batch, ni_D)
G.add(Reshape((ni_D, 1), input_shape=(ni_D,))) # (Batch, steps=ni_D, input_dim=1)
G.add(Conv1D(nh_G, 1, activation='relu')) # (Batch, ni_D, nh_G)
G.add(Conv1D(nh_G, 1, activation='sigmoid')) # (Batch, ni_D, nh_G)
G.add(Conv1D(1, 1)) # (Batch, ni_D, 1)
G.add(Flatten()) # (Batch, ni_D)
model_compile(G)
return G
def make_GD(self):
G, D = self.G, self.D
GD = models.Sequential()
GD.add(G)
GD.add(D)
D.trainable = False
model_compile(GD)
D.trainable = True
return GD
def D_train_on_batch(self, Real, Gen):
D = self.D
X = np.concatenate([Real, Gen], axis=0)
y = np.array([1] * Real.shape[0] + [0] * Gen.shape[0])
D.train_on_batch(X, y)
def GD_train_on_batch(self, Z):
GD = self.GD
y = np.array([1] * Z.shape[0])
GD.train_on_batch(Z, y)
class Data:
def __init__(self, mu, sigma, ni_D):
self.real_sample = lambda n_batch: np.random.normal(mu, sigma, (n_batch, ni_D))
self.in_sample = lambda n_batch: np.random.rand(n_batch, ni_D)
class Machine:
def __init__(self, n_batch=10, ni_D=100):
data_mean = 4
data_stddev = 1.25
self.n_iter_D = 1
self.n_iter_G = 5
self.data = Data(data_mean, data_stddev, ni_D)
self.gan = GAN(ni_D=ni_D, nh_D=50, nh_G=50)
self.n_batch = n_batch
# self.ni_D = ni_D
def train_D(self):
gan = self.gan
n_batch = self.n_batch
data = self.data
# Real data
Real = data.real_sample(n_batch) # (n_batch, ni_D)
# print(Real.shape)
# Generated data
Z = data.in_sample(n_batch) # (n_batch, ni_D)
Gen = gan.G.predict(Z) # (n_batch, ni_D)
# print(Gen.shape)
gan.D.trainable = True
gan.D_train_on_batch(Real, Gen)
def train_GD(self):
gan = self.gan
n_batch = self.n_batch
data = self.data
# Seed data for data generation
Z = data.in_sample(n_batch)
gan.D.trainable = False
gan.GD_train_on_batch(Z)
def train_each(self):
for it in range(self.n_iter_D):
self.train_D()
for it in range(self.n_iter_G):
self.train_GD()
def train(self, epochs):
for epoch in range(epochs):
self.train_each()
def test(self, n_test):
"""
generate a new image
"""
gan = self.gan
data = self.data
Z = data.in_sample(n_test)
Gen = gan.G.predict(Z)
return Gen, Z
def show_hist(self, Real, Gen, Z):
plt.hist(Real.reshape(-1), histtype='step', label='Real')
plt.hist(Gen.reshape(-1), histtype='step', label='Generated')
plt.hist(Z.reshape(-1), histtype='step', label='Input')
plt.legend(loc=0)
def test_and_show(self, n_test):
data = self.data
Gen, Z = self.test(n_test)
Real = data.real_sample(n_test)
self.show_hist(Real, Gen, Z)
Machine.print_stat(Real, Gen)
def run_epochs(self, epochs, n_test):
"""
train GAN and show the results
for showing, the original and the artificial results will be compared
"""
self.train(epochs)
self.test_and_show(n_test)
def run(self, n_repeat=200, n_show=200, n_test=100):
for ii in range(n_repeat):
print('Stage', ii, '(Epoch: {})'.format(ii * n_show))
self.run_epochs(n_show, n_test)
plt.show()
@staticmethod
def print_stat(Real, Gen):
def stat(d):
return (np.mean(d), np.std(d))
print('Mean and Std of Real:', stat(Real))
print('Mean and Std of Gen:', stat(Gen))
class GAN_Pure(GAN):
def __init__(self, ni_D, nh_D, nh_G):
'''
Discriminator input is not added
'''
super().__init__(ni_D, nh_D, nh_G)
def gen_D(self):
ni_D = self.ni_D
nh_D = self.nh_D
D = models.Sequential()
# D.add(Lambda(add_decorate, output_shape=add_decorate_shape, input_shape=(ni_D,)))
D.add(Dense(nh_D, activation='relu', input_shape=(ni_D,)))
D.add(Dense(nh_D, activation='relu'))
D.add(Dense(1, activation='sigmoid'))
model_compile(D)
return D
class Machine_Pure(Machine):
def __init__(self, n_batch=10, ni_D=100):
data_mean = 4
data_stddev = 1.25
self.data = Data(data_mean, data_stddev, ni_D)
self.gan = GAN_Pure(ni_D=ni_D, nh_D=50, nh_G=50)
self.n_batch = n_batch
# self.ni_D = ni_D
def main():
machine = Machine(n_batch=1, ni_D=100)
machine.run(n_repeat=200, n_show=200, n_test=100)
if __name__ == '__main__':
main()