-
Notifications
You must be signed in to change notification settings - Fork 2
/
bagan.py
665 lines (551 loc) · 26 KB
/
bagan.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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
from collections import defaultdict, Counter
import keras.backend as K
import tensorflow as tf
import keras
from keras.layers.advanced_activations import LeakyReLU
from keras.layers.convolutional import (
UpSampling2D, Convolution2D,
Conv2D, Conv2DTranspose
)
from keras.models import Sequential, Model, model_from_json
from keras.optimizers import Adam
from keras.losses import mean_squared_error, cosine_similarity, KLDivergence
from keras.layers import (
Input, Dense, Reshape,
Flatten, Embedding, Dropout,
BatchNormalization, Activation,
Lambda,Layer, Add, Concatenate,
Average,GaussianNoise,
MaxPooling2D, AveragePooling2D,
RepeatVector,GlobalAveragePooling2D,
)
from keras_contrib.losses import DSSIMObjective
from keras_contrib.layers.normalization.instancenormalization import InstanceNormalization
from tensorflow.python.ops import array_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.framework import dtypes
from keras.applications.vgg16 import VGG16
from keras.utils import np_utils
import sklearn.metrics as metrics
from sklearn.model_selection import train_test_split
from mlxtend.plotting import plot_confusion_matrix
import matplotlib.pyplot as plt
import seaborn as sns
import os
import sys
import re
import numpy as np
import datetime
import pickle
import cv2
import utils
import logger
from PIL import Image
K.common.set_image_dim_ordering('tf')
class BalancingGAN:
def plot_loss_his(self):
train_d = self.train_history['disc_loss']
train_g = self.train_history['gen_loss']
test_d = self.test_history['disc_loss']
test_g = self.test_history['gen_loss']
plt.plot(train_d, label='train_d_loss')
plt.plot(train_g, label='train_g_loss')
plt.plot(test_d, label='test_d_loss')
plt.plot(test_g, label='test_g_loss')
plt.ylabel('loss')
plt.xlabel('epoch')
plt.legend()
plt.show()
def plot_acc_his(self):
train_d = self.train_history['disc_acc']
train_g = self.train_history['gen_acc']
test_d = self.test_history['disc_acc']
test_g = self.test_history['gen_acc']
plt.plot(train_d, label='train_d_acc')
plt.plot(train_g, label='train_g_acc')
plt.plot(test_d, label='test_d_acc')
plt.plot(test_g, label='test_g_acc')
plt.ylabel('accuracy')
plt.xlabel('epoch')
plt.legend()
plt.show()
def build_generator(self, latent_size, init_resolution=8):
resolution = self.resolution
channels = self.channels
init_channels = 256
cnn = Sequential()
cnn.add(Dense(init_channels * init_resolution * init_resolution, input_dim=latent_size))
cnn.add(BatchNormalization())
cnn.add(LeakyReLU())
cnn.add(Reshape((init_resolution, init_resolution, init_channels)))
crt_res = init_resolution
# upsample
while crt_res < resolution/2:
cnn.add(Conv2DTranspose(
init_channels, kernel_size = 5, strides = 2, padding='same'))
cnn.add(LeakyReLU(alpha=0.02))
init_channels //= 2
crt_res = crt_res * 2
assert crt_res <= resolution,\
"Error: final resolution [{}] must equal i*2^n. Initial resolution i is [{}]. n must be a natural number.".format(resolution, init_resolution)
cnn.add(Conv2DTranspose(
1, kernel_size = 5,
strides = 2, padding='same',
activation='tanh'))
latent = Input(shape=(latent_size, ))
fake_image_from_latent = cnn(latent)
self.generator = Model(inputs=latent, outputs=fake_image_from_latent)
def _build_common_encoder(self, image, min_latent_res=8):
resolution = self.resolution
channels = self.channels
# build a relatively standard conv net, with LeakyReLUs as suggested in ACGAN
cnn = Sequential()
cnn.add(Conv2D(128, (5, 5), padding='same', strides=(2, 2)))
cnn.add(LeakyReLU(alpha=0.2))
cnn.add(Dropout(0.3))
cnn.add(Conv2D(256, (5, 5), padding='same', strides=(2, 2)))
cnn.add(LeakyReLU(alpha=0.2))
cnn.add(Dropout(0.3))
cnn.add(Conv2D(512, (5, 5), padding='same', strides=(2, 2)))
cnn.add(LeakyReLU(alpha=0.2))
cnn.add(Dropout(0.3))
if resolution == 128:
cnn.add(Conv2D(1024, (5, 5), padding='same', strides=(2, 2)))
cnn.add(LeakyReLU(alpha=0.2))
cnn.add(Dropout(0.3))
cnn.add(Flatten())
features = cnn(image)
return features
# latent_size is the innermost latent vector size; min_latent_res is latent resolution (before the dense layer).
def build_reconstructor(self, latent_size, min_latent_res=8):
resolution = self.resolution
channels = self.channels
image = Input(shape=(resolution, resolution, channels))
features = self._build_common_encoder(image, min_latent_res)
# Reconstructor specific
latent = Dense(latent_size, activation='linear')(features)
self.reconstructor = Model(inputs=image, outputs=latent)
def build_discriminator(self, min_latent_res=8):
resolution = self.resolution
channels = self.channels
image = Input(shape=(resolution, resolution, channels))
features = self._build_common_encoder(image, min_latent_res)
# Discriminator specific
features = Dropout(0.4)(features)
aux = Dense(self.nclasses+1,
activation='softmax',
name='auxiliary')(features)
self.discriminator = Model(inputs=image, outputs=aux)
def generate_from_latent(self, latent):
return self.generator(latent)
def generate(self, c, bg=None): # c is a vector of classes
latent = self.generate_latent(c, bg)
return self.generator.predict(latent)
def evaluate_g(self, test_x, test_y):
y_pre = self.combined.predict(test_x)
y_pre = np.argmax(y_pre, axis=1)
cm = metrics.confusion_matrix(y_true=test_y, y_pred=y_pre) # shape=(12, 12)
plt.figure()
plot_confusion_matrix(cm, hide_ticks=True,cmap=plt.cm.Blues,figsize=(8,8))
plt.show()
def generate_latent(self, c, bg=None, n_mix=10): # c is a vector of classes
return np.array([
np.random.multivariate_normal(self.means[e], self.covariances[e])
for e in c
])
def gen_for_class(self, bg, classid,size=1000):
print("Gen for class {}, size: {}".format(classid, size))
total = None
for i in range(1000):
labels = [classid] * 2000
labels = np.array(labels)
latent = self.generate_latent(labels)
gen = self.generator.predict(latent)
d_outputs = self.discriminator.predict(gen)
d_outputs = np.argmax(d_outputs, axis=1)
to_keep = np.where(labels == d_outputs)[0]
gen = gen[to_keep]
if total is None:
total = gen
else:
total = np.concatenate([total, gen], axis=0)
if len(total) >= size:
total = total[:size]
break
print("{}/{}".format(len(total), size))
print("done class {}, size {}".format(classid, len(total)))
return total, np.array([classid] * len(total))
def gen_augment_data(self, bg, size=1000):
total = None
labels = None
counter = dict(Counter(bg.dataset_y))
max_ = max(counter.values())
for i in bg.classes:
acctual_size = max((max_ - counter[i]), 0)
print(acctual_size, size, max_, counter[i])
if acctual_size == 0:
print("Skip class", i)
continue
gen , label = self.gen_for_class(bg, i, acctual_size)
if total is None:
total = gen
labels = label
else:
total = np.concatenate([total, gen], axis=0)
labels = np.concatenate([labels, label], axis=0)
print("Done all ", len(total))
return total, labels
def discriminate(self, image):
return self.discriminator(image)
def __init__(self, classes, target_class_id,
# Set dratio_mode, and gratio_mode to 'rebalance' to bias the sampling toward the minority class
# No relevant difference noted
dratio_mode="uniform", gratio_mode="uniform",
adam_lr=0.00005, latent_size=100,
res_dir = "./res-tmp", image_shape=[3,32,32], min_latent_res=8,
autoenc_epochs=100):
self.gratio_mode = gratio_mode
self.dratio_mode = dratio_mode
self.classes = classes
self.target_class_id = target_class_id # target_class_id is used only during saving, not to overwrite other class results.
self.nclasses = len(classes)
self.latent_size = latent_size
self.res_dir = res_dir
self.channels = image_shape[-1]
self.resolution = image_shape[1]
self.autoenc_epochs =autoenc_epochs
self.min_latent_res = min_latent_res
# Initialize learning variables
self.adam_lr = adam_lr
self.adam_beta_1 = 0.5
# Initialize stats
self.train_history = defaultdict(list)
self.test_history = defaultdict(list)
self.trained = False
# Build generator
self.build_generator(latent_size, init_resolution=min_latent_res)
latent_gen = Input(shape=(latent_size, ))
# Build discriminator
self.build_discriminator(min_latent_res=min_latent_res)
self.discriminator.compile(
optimizer=Adam(lr=self.adam_lr, beta_1=self.adam_beta_1),
metrics=['accuracy'],
loss='sparse_categorical_crossentropy'
)
# Build reconstructor
self.build_reconstructor(latent_size, min_latent_res=min_latent_res)
self.reconstructor.compile(
optimizer=Adam(lr=self.adam_lr, beta_1=self.adam_beta_1),
loss='mean_squared_error'
)
# Define combined for training generator.
fake = self.generator(latent_gen)
self.discriminator.trainable = False
self.reconstructor.trainable = False
self.generator.trainable = True
aux = self.discriminate(fake)
self.combined = Model(inputs=latent_gen, outputs=aux)
self.combined.compile(
optimizer=Adam(lr=self.adam_lr, beta_1=self.adam_beta_1),
metrics=['accuracy'],
loss='sparse_categorical_crossentropy'
)
# Define initializer for autoencoder
self.discriminator.trainable = False
self.generator.trainable = True
self.reconstructor.trainable = True
img_for_reconstructor = Input(shape=(self.resolution, self.resolution,self.channels))
img_reconstruct = self.generator(self.reconstructor(img_for_reconstructor))
self.autoenc_0 = Model(inputs=img_for_reconstructor, outputs=img_reconstruct)
self.autoenc_0.compile(
optimizer=Adam(lr=self.adam_lr, beta_1=self.adam_beta_1),
loss='mean_squared_error'
)
def _biased_sample_labels(self, samples, target_distribution="uniform"):
distribution = self.class_uratio
if target_distribution == "d":
distribution = self.class_dratio
elif target_distribution == "g":
distribution = self.class_gratio
sampled_labels = np.full(samples,0)
sampled_labels_p = np.random.normal(0, 1, samples)
for c in list(range(self.nclasses)):
mask = np.logical_and((sampled_labels_p > 0), (sampled_labels_p <= distribution[c]))
sampled_labels[mask] = self.classes[c]
sampled_labels_p = sampled_labels_p - distribution[c]
return sampled_labels
def _train_one_epoch(self, bg_train):
epoch_disc_loss = []
epoch_gen_loss = []
epoch_disc_acc = []
epoch_gen_acc = []
for image_batch, label_batch in bg_train.next_batch():
crt_batch_size = label_batch.shape[0]
################## Train Discriminator ##################
fake_size = int(np.ceil(crt_batch_size * 1.0/self.nclasses))
# sample some labels from p_c, then latent and images
sampled_labels = self._biased_sample_labels(fake_size, "d")
latent_gen = self.generate_latent(sampled_labels, bg_train)
generated_images = self.generator.predict(latent_gen, verbose=0)
X = np.concatenate((image_batch, generated_images))
aux_y = np.concatenate((label_batch, np.full(len(sampled_labels) , self.nclasses )), axis=0)
loss, acc = self.discriminator.train_on_batch(X, aux_y)
epoch_disc_loss.append(loss)
epoch_disc_acc.append(acc)
################## Train Generator ##################
sampled_labels = self._biased_sample_labels(fake_size + crt_batch_size, "g")
latent_gen = self.generate_latent(sampled_labels, bg_train)
loss, acc = self.combined.train_on_batch(latent_gen, sampled_labels)
epoch_gen_loss.append(loss)
epoch_gen_acc.append(acc)
# return statistics: generator loss,
return (
np.mean(np.array(epoch_disc_loss), axis=0),
np.mean(np.array(epoch_gen_loss), axis=0),
np.mean(np.array(epoch_disc_acc), axis=0),
np.mean(np.array(epoch_gen_acc), axis=0),
)
def _set_class_ratios(self):
self.class_dratio = np.full(self.nclasses, 0.0)
# Set uniform
target = 1/self.nclasses
self.class_uratio = np.full(self.nclasses, target)
# Set gratio
self.class_gratio = np.full(self.nclasses, 0.0)
for c in range(self.nclasses):
if self.gratio_mode == "uniform":
self.class_gratio[c] = target
elif self.gratio_mode == "rebalance":
self.class_gratio[c] = 2 * target - self.class_aratio[c]
else:
print("Error while training bgan, unknown gmode " + self.gratio_mode)
exit()
# Set dratio
self.class_dratio = np.full(self.nclasses, 0.0)
for c in range(self.nclasses):
if self.dratio_mode == "uniform":
self.class_dratio[c] = target
elif self.dratio_mode == "rebalance":
self.class_dratio[c] = 2 * target - self.class_aratio[c]
else:
print("Error while training bgan, unknown dmode " + self.dratio_mode)
exit()
# if very unbalanced, the gratio might be negative for some classes.
# In this case, we adjust..
if self.gratio_mode == "rebalance":
self.class_gratio[self.class_gratio < 0] = 0
self.class_gratio = self.class_gratio / sum(self.class_gratio)
# if very unbalanced, the dratio might be negative for some classes.
# In this case, we adjust..
if self.dratio_mode == "rebalance":
self.class_dratio[self.class_dratio < 0] = 0
self.class_dratio = self.class_dratio / sum(self.class_dratio)
def init_autoenc(self, bg_train, gen_fname=None, rec_fname=None):
if gen_fname is None:
generator_fname = "{}/{}_decoder.h5".format(self.res_dir, self.target_class_id)
else:
generator_fname = gen_fname
if rec_fname is None:
reconstructor_fname = "{}/{}_encoder.h5".format(self.res_dir, self.target_class_id)
else:
reconstructor_fname = rec_fname
multivariate_prelearnt = False
# Preload the autoencoders
if os.path.exists(generator_fname) and os.path.exists(reconstructor_fname):
print("BAGAN: loading autoencoder: ", generator_fname, reconstructor_fname)
self.generator.load_weights(generator_fname)
self.reconstructor.load_weights(reconstructor_fname)
# load the learned distribution
if os.path.exists("{}/{}_means.npy".format(self.res_dir, self.target_class_id)) \
and os.path.exists("{}/{}_covariances.npy".format(self.res_dir, self.target_class_id)):
multivariate_prelearnt = True
cfname = "{}/{}_covariances.npy".format(self.res_dir, self.target_class_id)
mfname = "{}/{}_means.npy".format(self.res_dir, self.target_class_id)
print("BAGAN: loading multivariate: ", cfname, mfname)
self.covariances = np.load(cfname)
self.means = np.load(mfname)
else:
print("BAGAN: training autoencoder")
autoenc_train_loss = []
for e in range(self.autoenc_epochs):
print('Autoencoder train epoch: {}/{}'.format(e+1, self.autoenc_epochs))
autoenc_train_loss_crt = []
for image_batch, label_batch in bg_train.next_batch():
autoenc_train_loss_crt.append(self.autoenc_0.train_on_batch(image_batch, image_batch))
autoenc_train_loss.append(np.mean(np.array(autoenc_train_loss_crt), axis=0))
autoenc_loss_fname = "{}/{}_autoencoder.csv".format(self.res_dir, self.target_class_id)
with open(autoenc_loss_fname, 'w') as csvfile:
for item in autoenc_train_loss:
csvfile.write("%s\n" % item)
self.generator.save(generator_fname)
self.reconstructor.save(reconstructor_fname)
layers_r = self.reconstructor.layers
layers_d = self.discriminator.layers
for l in range(1, len(layers_r)-1):
layers_d[l].set_weights( layers_r[l].get_weights() )
# Organize multivariate distribution
if not multivariate_prelearnt:
print("BAGAN: computing multivariate")
self.covariances = []
self.means = []
for c in range(self.nclasses):
imgs = bg_train.dataset_x[bg_train.per_class_ids[c]]
latent = self.reconstructor.predict(imgs)
self.covariances.append(np.cov(np.transpose(latent)))
self.means.append(np.mean(latent, axis=0))
self.covariances = np.array(self.covariances)
self.means = np.array(self.means)
# save the learned distribution
cfname = "{}/{}_covariances.npy".format(self.res_dir, self.target_class_id)
mfname = "{}/{}_means.npy".format(self.res_dir, self.target_class_id)
print("BAGAN: saving multivariate: ", cfname, mfname)
np.save(cfname, self.covariances)
np.save(mfname, self.means)
print("BAGAN: saved multivariate")
def _get_lst_bck_name(self, element):
# Find last bck name
files = [
f for f in os.listdir(self.res_dir)
if re.match(r'bck_' + element, f)
]
if len(files) > 0:
fname = files[0]
epoch = 0
return epoch, fname
else:
return 0, None
def init_gan(self):
# Find last bck name
epoch, generator_fname = self._get_lst_bck_name("generator")
new_e, discriminator_fname = self._get_lst_bck_name("discriminator")
# Load last bck
try:
self.generator.load_weights(os.path.join(self.res_dir, generator_fname))
self.discriminator.load_weights(os.path.join(self.res_dir, discriminator_fname))
print('GAN weight initialized, train from epoch ', epoch)
return epoch
except Exception as e:
e = str(e)
logger.warn('Reload error, restart from scratch ' + e)
return 0
def backup_point(self, epoch):
# Bck
if epoch == 0:
return
print('Save weights at epochs : ', epoch)
generator_fname = "{}/bck_generator.h5".format(self.res_dir)
discriminator_fname = "{}/bck_discriminator.h5".format(self.res_dir)
self.generator.save(generator_fname)
self.discriminator.save(discriminator_fname)
def train(self, bg_train, bg_test, epochs=50):
if not self.trained:
# Class actual ratio
self.class_aratio = bg_train.get_class_probability()
# Class balancing ratio
self._set_class_ratios()
# Initialization
print("BAGAN init_autoenc")
self.init_autoenc(bg_train)
print("BAGAN autoenc initialized, init gan")
start_e = self.init_gan()
print("BAGAN gan initialized, start_e: ", start_e)
crt_c = 0
act_img_samples = bg_train.get_samples_for_class(crt_c, 10)
img_samples = np.array([
[
act_img_samples,
self.generator.predict(
self.reconstructor.predict(
act_img_samples
)
),
self.generate_samples(crt_c, 10, bg_train)
]
])
for crt_c in range(1, 3):
act_img_samples = bg_train.get_samples_for_class(crt_c, 10)
new_samples = np.array([
[
act_img_samples,
self.generator.predict(
self.reconstructor.predict(
act_img_samples
)
),
self.generate_samples(crt_c, 10, bg_train)
]
])
img_samples = np.concatenate((img_samples, new_samples), axis=0)
shape = img_samples.shape
img_samples = img_samples.reshape((-1, shape[-4], shape[-3], shape[-2], shape[-1]))
utils.show_samples(img_samples)
# Train
for e in range(start_e, epochs):
start_time = datetime.datetime.now()
print('GAN train epoch: {}/{}'.format(e+1, epochs))
train_disc_loss, train_gen_loss, train_disc_acc, train_gen_acc = self._train_one_epoch(bg_train)
if False:
# Test: # generate a new batch of noise
nb_test = bg_test.get_num_samples()
fake_size = int(np.ceil(nb_test * 1.0/self.nclasses))
sampled_labels = self._biased_sample_labels(nb_test, "d")
latent_gen = self.generate_latent(sampled_labels, bg_test)
# sample some labels from p_c and generate images from them
generated_images = self.generator.predict(
latent_gen, verbose=False)
X = np.concatenate( (bg_test.dataset_x, generated_images) )
aux_y = np.concatenate((bg_test.dataset_y, np.full(len(sampled_labels), self.nclasses )), axis=0)
# see if the discriminator can figure itself out...
test_disc_loss, test_disc_acc = self.discriminator.evaluate(
X, aux_y, verbose=False)
# make new latent
sampled_labels = self._biased_sample_labels(fake_size + nb_test, "g")
latent_gen = self.generate_latent(sampled_labels, bg_test)
test_gen_loss, test_gen_acc = self.combined.evaluate(
latent_gen,
sampled_labels, verbose=False)
# generate an epoch report on performance
self.train_history['disc_loss'].append(train_disc_loss)
self.train_history['gen_loss'].append(train_gen_loss)
# self.test_history['disc_loss'].append(test_disc_loss)
# self.test_history['gen_loss'].append(test_gen_loss)
# accuracy
self.train_history['disc_acc'].append(train_disc_acc)
self.train_history['gen_acc'].append(train_gen_acc)
# self.test_history['disc_acc'].append(test_disc_acc)
# self.test_history['gen_acc'].append(test_gen_acc)
print("D_loss {}, G_loss {}, D_acc {}, G_acc {} - {}".format(
train_disc_loss, train_gen_loss, train_disc_acc, train_gen_acc,
datetime.datetime.now() - start_time
))
# self.plot_his()
# Save sample images
if e % 15 == 0:
img_samples = np.array([
self.generate_samples(c, 10, bg_train)
for c in range(0,self.nclasses)
])
utils.show_samples(img_samples)
# Generate whole evaluation plot (real img, autoencoded img, fake img)
if e % 10 == 5:
self.plot_loss_his()
self.plot_acc_his()
self.backup_point(e)
crt_c = 0
sample_size = 700
labels = np.zeros(sample_size)
img_samples = self.generate_samples(crt_c, sample_size, bg_train)
five_imgs = img_samples[:5]
for crt_c in range(1, 3):
new_samples = self.generate_samples(crt_c, sample_size, bg_train)
img_samples = np.concatenate((img_samples, new_samples), axis=0)
labels = np.concatenate((np.ones(sample_size), labels), axis=0)
five_imgs = np.concatenate((five_imgs, new_samples[:5]), axis=0)
labels = np_utils.to_categorical(labels, self.nclasses)
img_samples = np.transpose(img_samples, axes=(0, 2, 3, 1))
# shape = img_samples.shape
# img_samples = img_samples.reshape((-1, shape[-4], shape[-3], shape[-2], shape[-1]))
utils.show_samples(five_imgs)
self.trained = True
def generate_samples(self, c, samples, bg = None):
return self.generate(np.full(samples, c), bg)