質問編集履歴
1
コードを載せました
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,3 +1,451 @@
|
|
1
|
-
現在autoencoderに画像を入力し、autoencoderから出力された画像で教師データどうかの判定を行
|
1
|
+
現在autoencoderに画像を入力し、autoencoderから出力された画像で教師データどうかの判定を行いautoencoderを学習させるといったGANの開発を行なっています。
|
2
|
+
|
3
|
+
標準のGANならばランダムノイズから画像生成するコードの部分の改良を行なったため、エラーが起こってるのではないかと思います。
|
2
4
|
|
3
5
|
しかし実行しようとすると以下のようなエラーが発生しています。
|
6
|
+
|
7
|
+
```
|
8
|
+
|
9
|
+
Traceback (most recent call last):
|
10
|
+
|
11
|
+
File "keras_dcgan_copy.py", line 192, in <module>
|
12
|
+
|
13
|
+
main()
|
14
|
+
|
15
|
+
File "keras_dcgan_copy.py", line 150, in main
|
16
|
+
|
17
|
+
discriminator_on_generator = generator_containing_discriminator(generator, discriminator)
|
18
|
+
|
19
|
+
File "keras_dcgan_copy.py", line 107, in generator_containing_discriminator
|
20
|
+
|
21
|
+
model.add(discriminator)
|
22
|
+
|
23
|
+
File "/usr/local/lib/python3.6/dist-packages/keras/engine/sequential.py", line 181, in add
|
24
|
+
|
25
|
+
output_tensor = layer(self.outputs[0])
|
26
|
+
|
27
|
+
File "/usr/local/lib/python3.6/dist-packages/keras/engine/base_layer.py", line 457, in __call__
|
28
|
+
|
29
|
+
output = self.call(inputs, **kwargs)
|
30
|
+
|
31
|
+
File "/usr/local/lib/python3.6/dist-packages/keras/engine/network.py", line 564, in call
|
32
|
+
|
33
|
+
output_tensors, _, _ = self.run_internal_graph(inputs, masks)
|
34
|
+
|
35
|
+
File "/usr/local/lib/python3.6/dist-packages/keras/engine/network.py", line 721, in run_internal_graph
|
36
|
+
|
37
|
+
layer.call(computed_tensor, **kwargs))
|
38
|
+
|
39
|
+
File "/usr/local/lib/python3.6/dist-packages/keras/layers/convolutional.py", line 171, in call
|
40
|
+
|
41
|
+
dilation_rate=self.dilation_rate)
|
42
|
+
|
43
|
+
File "/usr/local/lib/python3.6/dist-packages/keras/backend/tensorflow_backend.py", line 3650, in conv2d
|
44
|
+
|
45
|
+
data_format=tf_data_format)
|
46
|
+
|
47
|
+
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py", line 779, in convolution
|
48
|
+
|
49
|
+
data_format=data_format)
|
50
|
+
|
51
|
+
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/ops/nn_ops.py", line 839, in __init__
|
52
|
+
|
53
|
+
filter_shape[num_spatial_dims]))
|
54
|
+
|
55
|
+
ValueError: number of input channels does not match corresponding dimension of filter, 32 != 3
|
56
|
+
|
57
|
+
```
|
58
|
+
|
59
|
+
機械学習初心者で汚いコードかもしれませんが、ご教授お願いします。
|
60
|
+
|
61
|
+
以上がコードの全文です
|
62
|
+
|
63
|
+
```python Keras
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
from keras.models import Sequential
|
68
|
+
|
69
|
+
from keras.layers import Dense
|
70
|
+
|
71
|
+
from keras.layers import Reshape
|
72
|
+
|
73
|
+
from keras.layers.core import Activation
|
74
|
+
|
75
|
+
from keras.layers.normalization import BatchNormalization
|
76
|
+
|
77
|
+
from keras.layers.convolutional import UpSampling2D
|
78
|
+
|
79
|
+
from keras.layers.convolutional import Conv2D, MaxPooling2D
|
80
|
+
|
81
|
+
from keras.layers.advanced_activations import LeakyReLU
|
82
|
+
|
83
|
+
from keras.layers.core import Flatten,Dropout
|
84
|
+
|
85
|
+
from keras.optimizers import Adam
|
86
|
+
|
87
|
+
import numpy as np
|
88
|
+
|
89
|
+
from PIL import Image
|
90
|
+
|
91
|
+
import os
|
92
|
+
|
93
|
+
import glob
|
94
|
+
|
95
|
+
import random
|
96
|
+
|
97
|
+
|
98
|
+
|
99
|
+
n_colors = 3
|
100
|
+
|
101
|
+
|
102
|
+
|
103
|
+
'''
|
104
|
+
|
105
|
+
def generator_model():
|
106
|
+
|
107
|
+
model = Sequential()
|
108
|
+
|
109
|
+
|
110
|
+
|
111
|
+
model.add(Dense(1024, input_shape=(100,)))
|
112
|
+
|
113
|
+
model.add(Activation('tanh'))
|
114
|
+
|
115
|
+
|
116
|
+
|
117
|
+
model.add(Dense(128 * 16 * 16))
|
118
|
+
|
119
|
+
model.add(BatchNormalization())
|
120
|
+
|
121
|
+
model.add(Activation('tanh'))
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
model.add(Reshape((16, 16, 128)))
|
126
|
+
|
127
|
+
model.add(UpSampling2D(size=(2, 2)))
|
128
|
+
|
129
|
+
model.add(Conv2D(64, (5, 5), padding='same'))
|
130
|
+
|
131
|
+
model.add(Activation('tanh'))
|
132
|
+
|
133
|
+
|
134
|
+
|
135
|
+
model.add(UpSampling2D(size=(2, 2)))
|
136
|
+
|
137
|
+
model.add(Conv2D(n_colors, (5, 5), padding='same'))
|
138
|
+
|
139
|
+
model.add(Activation('tanh'))
|
140
|
+
|
141
|
+
return model
|
142
|
+
|
143
|
+
'''
|
144
|
+
|
145
|
+
|
146
|
+
|
147
|
+
def generator_model():
|
148
|
+
|
149
|
+
model = Sequential()
|
150
|
+
|
151
|
+
#encode
|
152
|
+
|
153
|
+
model.add(Conv2D(32, (5, 5),input_shape=(64, 64, n_colors),activation='relu', padding='same'))
|
154
|
+
|
155
|
+
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
|
156
|
+
|
157
|
+
|
158
|
+
|
159
|
+
model.add(Conv2D(16, (5, 5),activation='relu', padding='same'))
|
160
|
+
|
161
|
+
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
|
162
|
+
|
163
|
+
|
164
|
+
|
165
|
+
model.add(Conv2D(16, (5, 5),activation='relu', padding='same'))
|
166
|
+
|
167
|
+
model.add(MaxPooling2D(pool_size=(2, 2),padding='same'))
|
168
|
+
|
169
|
+
#decode
|
170
|
+
|
171
|
+
model.add(Conv2D(16,(5,5),activation='relu',padding='same'))
|
172
|
+
|
173
|
+
model.add(UpSampling2D(size=(2, 2)))
|
174
|
+
|
175
|
+
|
176
|
+
|
177
|
+
model.add(Conv2D(16,(5,5),activation='relu',padding='same'))
|
178
|
+
|
179
|
+
model.add(UpSampling2D(size=(2, 2)))
|
180
|
+
|
181
|
+
|
182
|
+
|
183
|
+
model.add(Conv2D(32,(5,5),activation='relu'))
|
184
|
+
|
185
|
+
model.add(UpSampling2D(size=(2, 2)))
|
186
|
+
|
187
|
+
return model
|
188
|
+
|
189
|
+
|
190
|
+
|
191
|
+
|
192
|
+
|
193
|
+
def discriminator_model():
|
194
|
+
|
195
|
+
model = Sequential()
|
196
|
+
|
197
|
+
|
198
|
+
|
199
|
+
model.add(Conv2D(64, (5, 5), input_shape=(64, 64, n_colors), padding='same'))
|
200
|
+
|
201
|
+
model.add(LeakyReLU(alpha=0.2))
|
202
|
+
|
203
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
204
|
+
|
205
|
+
|
206
|
+
|
207
|
+
model.add(Conv2D(128, (5, 5)))
|
208
|
+
|
209
|
+
model.add(LeakyReLU(alpha=0.2))
|
210
|
+
|
211
|
+
model.add(MaxPooling2D(pool_size=(2, 2)))
|
212
|
+
|
213
|
+
model.add(Flatten())
|
214
|
+
|
215
|
+
|
216
|
+
|
217
|
+
model.add(Dense(1024))
|
218
|
+
|
219
|
+
model.add(LeakyReLU(alpha=0.2))
|
220
|
+
|
221
|
+
|
222
|
+
|
223
|
+
model.add(Dense(1))
|
224
|
+
|
225
|
+
model.add(Activation('sigmoid'))
|
226
|
+
|
227
|
+
return model
|
228
|
+
|
229
|
+
|
230
|
+
|
231
|
+
'''
|
232
|
+
|
233
|
+
def discriminator_model():
|
234
|
+
|
235
|
+
model = Sequential()
|
236
|
+
|
237
|
+
|
238
|
+
|
239
|
+
model.add(Conv2D(64, (5,5), strides=(2, 2), input_shape=(64, 64, 3), padding="same"))
|
240
|
+
|
241
|
+
model.add(LeakyReLU(0.2))
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
model.add(Conv2D(128, (5,5), strides=(2, 2)))
|
246
|
+
|
247
|
+
model.add(LeakyReLU(0.2))
|
248
|
+
|
249
|
+
|
250
|
+
|
251
|
+
model.add(Flatten())
|
252
|
+
|
253
|
+
model.add(Dense(256))
|
254
|
+
|
255
|
+
|
256
|
+
|
257
|
+
model.add(LeakyReLU(0.2))
|
258
|
+
|
259
|
+
model.add(Dropout(0.5))
|
260
|
+
|
261
|
+
|
262
|
+
|
263
|
+
model.add(Dense(1))
|
264
|
+
|
265
|
+
model.add(Activation('sigmoid'))
|
266
|
+
|
267
|
+
return model
|
268
|
+
|
269
|
+
'''
|
270
|
+
|
271
|
+
def generator_containing_discriminator(generator, discriminator):
|
272
|
+
|
273
|
+
model = Sequential()
|
274
|
+
|
275
|
+
model.add(generator)
|
276
|
+
|
277
|
+
model.add(discriminator)
|
278
|
+
|
279
|
+
return model
|
280
|
+
|
281
|
+
|
282
|
+
|
283
|
+
def image_batch(batch_size):
|
284
|
+
|
285
|
+
files = glob.glob("./in_images/**/*.jpg", recursive=True)
|
286
|
+
|
287
|
+
files = random.sample(files, batch_size)
|
288
|
+
|
289
|
+
# print(files)
|
290
|
+
|
291
|
+
res = []
|
292
|
+
|
293
|
+
for path in files:
|
294
|
+
|
295
|
+
img = Image.open(path)
|
296
|
+
|
297
|
+
img = img.resize((64, 64))
|
298
|
+
|
299
|
+
arr = np.array(img)
|
300
|
+
|
301
|
+
arr = (arr - 127.5) / 127.5
|
302
|
+
|
303
|
+
arr.resize((64, 64, n_colors))
|
304
|
+
|
305
|
+
res.append(arr)
|
306
|
+
|
307
|
+
return np.array(res)
|
308
|
+
|
309
|
+
|
310
|
+
|
311
|
+
def combine_images(generated_images, cols=5, rows=5):
|
312
|
+
|
313
|
+
shape = generated_images.shape
|
314
|
+
|
315
|
+
h = shape[1]
|
316
|
+
|
317
|
+
w = shape[2]
|
318
|
+
|
319
|
+
image = np.zeros((rows * h, cols * w, n_colors))
|
320
|
+
|
321
|
+
for index, img in enumerate(generated_images):
|
322
|
+
|
323
|
+
if index >= cols * rows:
|
324
|
+
|
325
|
+
break
|
326
|
+
|
327
|
+
i = index // cols
|
328
|
+
|
329
|
+
j = index % cols
|
330
|
+
|
331
|
+
image[i*h:(i+1)*h, j*w:(j+1)*w, :] = img[:, :, :]
|
332
|
+
|
333
|
+
image = image * 127.5 + 127.5
|
334
|
+
|
335
|
+
image = Image.fromarray(image.astype(np.uint8))
|
336
|
+
|
337
|
+
return image
|
338
|
+
|
339
|
+
|
340
|
+
|
341
|
+
def set_trainable(model, trainable):
|
342
|
+
|
343
|
+
model.trainable = trainable
|
344
|
+
|
345
|
+
for layer in model.layers:
|
346
|
+
|
347
|
+
layer.trainable = trainable
|
348
|
+
|
349
|
+
|
350
|
+
|
351
|
+
def main():
|
352
|
+
|
353
|
+
batch_size = 64
|
354
|
+
|
355
|
+
|
356
|
+
|
357
|
+
discriminator = discriminator_model()
|
358
|
+
|
359
|
+
generator = generator_model()
|
360
|
+
|
361
|
+
|
362
|
+
|
363
|
+
discriminator_on_generator = generator_containing_discriminator(generator, discriminator)
|
364
|
+
|
365
|
+
|
366
|
+
|
367
|
+
set_trainable(discriminator, False)
|
368
|
+
|
369
|
+
discriminator_on_generator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))
|
370
|
+
|
371
|
+
|
372
|
+
|
373
|
+
print(generator.summary())
|
374
|
+
|
375
|
+
print(discriminator_on_generator.summary())
|
376
|
+
|
377
|
+
|
378
|
+
|
379
|
+
set_trainable(discriminator, True)
|
380
|
+
|
381
|
+
discriminator.compile(loss='binary_crossentropy', optimizer=Adam(lr=0.0002, beta_1=0.5))
|
382
|
+
|
383
|
+
|
384
|
+
|
385
|
+
print(discriminator.summary())
|
386
|
+
|
387
|
+
|
388
|
+
|
389
|
+
for i in range(30 * 1000):
|
390
|
+
|
391
|
+
batch_images = image_batch(batch_size)
|
392
|
+
|
393
|
+
|
394
|
+
|
395
|
+
# generator更新
|
396
|
+
|
397
|
+
noise = np.random.uniform(size=[batch_size, batch_images], low=-1.0, high=1.0)
|
398
|
+
|
399
|
+
|
400
|
+
|
401
|
+
generated_images = generator.predict(image_batch)
|
402
|
+
|
403
|
+
|
404
|
+
|
405
|
+
# discriminatorを更新
|
406
|
+
|
407
|
+
X = np.concatenate((batch_images, generated_images))
|
408
|
+
|
409
|
+
# 訓練データのラベルが1、生成画像のラベルが0になるよう学習する
|
410
|
+
|
411
|
+
y = [1] * batch_size + [0] * batch_size
|
412
|
+
|
413
|
+
d_loss = discriminator.train_on_batch(X, y)
|
414
|
+
|
415
|
+
|
416
|
+
|
417
|
+
# generator更新
|
418
|
+
|
419
|
+
noise = np.random.uniform(size=[batch_size, batch_images], low=-1.0, high=1.0)
|
420
|
+
|
421
|
+
|
422
|
+
|
423
|
+
# 生成画像をdiscriminatorにいれたときに
|
424
|
+
|
425
|
+
# 出力が1に近くなる(訓練画像と識別される確率が高くなる)ように学習する
|
426
|
+
|
427
|
+
g_loss = discriminator_on_generator.train_on_batch(noise, [1] * batch_size)
|
428
|
+
|
429
|
+
|
430
|
+
|
431
|
+
if i % 100 == 0:
|
432
|
+
|
433
|
+
print("step %d d_loss, g_loss : %g %g" % (i, d_loss, g_loss))
|
434
|
+
|
435
|
+
image = combine_images(generated_images)
|
436
|
+
|
437
|
+
os.system('mkdir -p ./gen_images')
|
438
|
+
|
439
|
+
image.save("./gen_images/gen%05d.jpg" % i)
|
440
|
+
|
441
|
+
generator.save_weights('generator.h5', True)
|
442
|
+
|
443
|
+
discriminator.save_weights('discriminator.h5', True)
|
444
|
+
|
445
|
+
|
446
|
+
|
447
|
+
main()
|
448
|
+
|
449
|
+
|
450
|
+
|
451
|
+
```
|