forked from PINTO0309/DeepLearningMugenKnock
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dcgan_keras.py
270 lines (223 loc) · 8.38 KB
/
dcgan_keras.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
234
235
236
237
238
239
240
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
266
267
268
269
270
import keras
import cv2
import numpy as np
import argparse
from glob import glob
import matplotlib.pyplot as plt
# GPU config
import os
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
import tensorflow as tf
from keras import backend as K
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
config.gpu_options.visible_device_list="0"
sess = tf.Session(config=config)
K.set_session(sess)
# network
from keras.models import Sequential, Model
from keras.layers import Dense, Dropout, Activation, Flatten, Conv2D, MaxPooling2D, Input, BatchNormalization, Reshape, UpSampling2D, LeakyReLU, Conv2DTranspose
num_classes = 2
img_height, img_width = 64, 64
channel = 3
from keras.regularizers import l1_l2
from keras.initializers import RandomNormal as RN, Constant
def G_model():
inputs = Input((100,))
in_h = int(img_height / 16)
in_w = int(img_width / 16)
d_dim = 256
base = 128
x = Dense(in_h * in_w * d_dim, name='g_dense1',
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(inputs)
x = Reshape((in_h, in_w, d_dim), input_shape=(d_dim * in_h * in_w,))(x)
x = Activation('relu')(x)
x = BatchNormalization(momentum=0.9, epsilon=1e-5, name='g_dense1_bn')(x)
# 1/8
x = Conv2DTranspose(base*4, (5, 5), name='g_conv1', padding='same', strides=(2,2),
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(x)
x = Activation('relu')(x)
x = BatchNormalization(momentum=0.9, epsilon=1e-5, name='g_conv1_bn')(x)
# 1/4
x = Conv2DTranspose(base*2, (5, 5), name='g_conv2', padding='same', strides=(2,2),
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(x)
x = Activation('relu')(x)
x = BatchNormalization(momentum=0.9, epsilon=1e-5, name='g_conv2_bn')(x)
# 1/2
x = Conv2DTranspose(base, (5, 5), name='g_conv3', padding='same', strides=(2,2),
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(x)
x = Activation('relu')(x)
x = BatchNormalization(momentum=0.9, epsilon=1e-5, name='g_conv3_bn')(x)
# 1/1
x = Conv2DTranspose(channel, (5, 5), name='g_out', padding='same', strides=(2,2),
kernel_initializer=RN(mean=0.0, stddev=0.02), bias_initializer=Constant())(x)
x = Activation('tanh')(x)
model = Model(inputs=inputs, outputs=x, name='G')
return model
def D_model():
base = 32
inputs = Input((img_height, img_width, channel))
x = Conv2D(base, (5, 5), padding='same', strides=(2,2), name='d_conv1',
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(inputs)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(base*2, (5, 5), padding='same', strides=(2,2), name='d_conv2',
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(base*4, (5, 5), padding='same', strides=(2,2), name='d_conv3',
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(x)
x = LeakyReLU(alpha=0.2)(x)
x = Conv2D(base*8, (5, 5), padding='same', strides=(2,2), name='d_conv4',
kernel_initializer=RN(mean=0.0, stddev=0.02), use_bias=False)(x)
x = LeakyReLU(alpha=0.2)(x)
x = Flatten()(x)
x = Dense(1, activation='sigmoid', name='d_out',
kernel_initializer=RN(mean=0.0, stddev=0.02), bias_initializer=Constant())(x)
model = Model(inputs=inputs, outputs=x, name='D')
return model
def Combined_model(g, d):
model = Sequential()
model.add(g)
model.add(d)
return model
CLS = {'background': [0,0,0],
'akahara': [0,0,128],
'madara': [0,128,0]}
# get train data
def data_load(path, hf=False, vf=False, rot=False):
xs = []
ts = []
paths = []
data_num = 0
for dir_path in glob(path + '/*'):
data_num += len(glob(dir_path + "/*"))
pbar = tqdm(total = data_num)
for dir_path in glob(path + '/*'):
for path in glob(dir_path + '/*'):
x = cv2.imread(path)
if channel == 1:
x = cv2.cvtColor(x, cv2.COLOR_BGR2GRAY)
x = cv2.resize(x, (img_width, img_height)).astype(np.float32)
x = x / 127.5 - 1
if channel == 1:
x = x[..., None]
else:
x = x[..., ::-1]
xs.append(x)
for i, cls in enumerate(CLS):
if cls in path:
t = i
ts.append(t)
paths.append(path)
if hf:
xs.append(x[:, ::-1])
ts.append(t)
paths.append(path)
if vf:
xs.append(x[::-1])
ts.append(t)
paths.append(path)
if hf and vf:
xs.append(x[::-1, ::-1])
ts.append(t)
paths.append(path)
if rot != False:
angle = 0
scale = 1
while angle < 360:
angle += rot
_h, _w, _c = x.shape
max_side = max(_h, _w)
tmp = np.zeros((max_side, max_side, _c))
tx = int((max_side - _w) / 2)
ty = int((max_side - _h) / 2)
tmp[ty: ty+_h, tx: tx+_w] = x.copy()
M = cv2.getRotationMatrix2D((max_side/2, max_side/2), angle, scale)
_x = cv2.warpAffine(tmp, M, (max_side, max_side))
_x = _x[tx:tx+_w, ty:ty+_h]
xs.append(_x)
ts.append(t)
paths.append(path)
pbar.update(1)
xs = np.array(xs, dtype=np.float32)
ts = np.array(ts, dtype=np.int)
#xs = np.transpose(xs, (0,3,1,2))
pbar.close()
return xs, paths
# train
def train():
g = G_model()
d = D_model()
gan = Combined_model(g=g, d=d)
g_opt = keras.optimizers.Adam(lr=0.0002, beta_1=0.5)
d_opt = keras.optimizers.Adam(lr=0.0002, beta_1=0.5)
d.trainable = True
for layer in d.layers:
layer.trainable = True
d.compile(loss='binary_crossentropy', optimizer=d_opt)
g.compile(loss='binary_crossentropy', optimizer=d_opt)
d.trainable = False
for layer in d.layers:
layer.trainable = False
gan = Combined_model(g=g, d=d)
gan.compile(loss='binary_crossentropy', optimizer=g_opt)
xs, paths = data_load('../Dataset/train/images/', hf=True, vf=True, rot=1)
# training
mb = 32
mbi = 0
train_ind = np.arange(len(xs))
np.random.seed(0)
np.random.shuffle(train_ind)
for i in range(10000):
if mbi + mb > len(xs):
mb_ind = train_ind[mbi:]
np.random.shuffle(train_ind)
mb_ind = np.hstack((mb_ind, train_ind[:(mb-(len(xs)-mbi))]))
mbi = mb - (len(xs) - mbi)
else:
mb_ind = train_ind[mbi: mbi+mb]
mbi += mb
x = xs[mb_ind]
input_noise = np.random.uniform(-1, 1, size=(mb, 100))
g_output = g.predict(input_noise, verbose=0)
X = np.concatenate((x, g_output))
Y = [1] * mb + [0] * mb
d_loss = d.train_on_batch(X, Y)
# Generator training
input_noise = np.random.uniform(-1, 1, size=(mb, 100))
g_loss = gan.train_on_batch(input_noise, [1] * mb)
print("iter >>", i+1, ",g_loss >>", g_loss, ',d_loss >>', d_loss)
g.save('model.h5')
# test
def test():
# load trained model
g = G_model()
g.load_weights('model.h5', by_name=True)
np.random.seed(100)
for i in range(3):
input_noise = np.random.uniform(-1, 1, size=(10, 100))
g_output = g.predict(input_noise, verbose=0)
g_output = (g_output + 1 ) / 2
for i in range(10):
gen = g_output[i]
plt.subplot(1,10,i+1)
plt.imshow(gen)
plt.axis('off')
plt.show()
def arg_parse():
parser = argparse.ArgumentParser(description='CNN implemented with Keras')
parser.add_argument('--train', dest='train', action='store_true')
parser.add_argument('--test', dest='test', action='store_true')
args = parser.parse_args()
return args
# main
if __name__ == '__main__':
args = arg_parse()
if args.train:
train()
if args.test:
test()
if not (args.train or args.test):
print("please select train or test flag")
print("train: python main.py --train")
print("test: python main.py --test")
print("both: python main.py --train --test")