-
Notifications
You must be signed in to change notification settings - Fork 3
/
GAN.py
635 lines (526 loc) · 28.4 KB
/
GAN.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
# -*- coding: utf-8 -*-
"""
Created on Thu Apr 23 19:59:19 2020
@author: Bipasha
"""
import tensorflow as tf
import tensorflow.keras as K
import numpy as np
from sklearn.metrics import accuracy_score, confusion_matrix, classification_report, average_precision_score
from skimage.transform import resize
import scipy.io
import matplotlib.pyplot as plt
import itertools
import time
import shutil
import os
class GAN():
def __init__(self):
"""
-----------------------------------------------------------------------------------------------
Define image shape for TMI data(32x32x3)
-----------------------------------------------------------------------------------------------
"""
self.img_rows = 32
self.img_cols = 32
self.channels = 3
self.img_shape = (self.img_rows, self.img_cols, self.channels)
"""
-----------------------------------------------------------------------------------------------
Define number of class
-----------------------------------------------------------------------------------------------
"""
self.latent_dim = 100
self.num_classes = 2
"""
-----------------------------------------------------------------------------------------------
Define training history
-----------------------------------------------------------------------------------------------
"""
self.training_history = {
'D_loss': [],
'D_acc': [],
'G_loss': [],
'G_acc': [],
}
"""
-----------------------------------------------------------------------------------------------
Build discriminator model
-----------------------------------------------------------------------------------------------
"""
self.discriminator = self.build_discriminator()
"""
-----------------------------------------------------------------------------------------------
Compile discriminator using Adam optimizer
binary crossentropy is used to distinguish among real or fake samples
categorical entropy is to distinguish among which real category is (nuclei or non-nuclei)
-----------------------------------------------------------------------------------------------
"""
optimizer = K.optimizers.Adam(learning_rate=0.0002)
self.discriminator.compile(
loss=['binary_crossentropy', 'categorical_crossentropy'],
optimizer=optimizer,
metrics=['accuracy'])
"""
-----------------------------------------------------------------------------------------------
Build the generator
-----------------------------------------------------------------------------------------------
"""
self.generator = self.build_generator()
"""
-----------------------------------------------------------------------------------------------
The generator takes noise as input and generates imgs
-----------------------------------------------------------------------------------------------
"""
z = K.layers.Input(shape=(self.latent_dim,))
img = self.generator(z)
"""
-----------------------------------------------------------------------------------------------
For the combined model only generator is trained
-----------------------------------------------------------------------------------------------
"""
self.discriminator.trainable = False
"""
-----------------------------------------------------------------------------------------------
The discriminator takes generated images as input and determines validity
-----------------------------------------------------------------------------------------------
"""
validity, _ = self.discriminator(img)
"""
-----------------------------------------------------------------------------------------------
The combined model (generator and discriminator) takes
noise as input => generates images => determines validity
-----------------------------------------------------------------------------------------------
"""
self.combined = Model(z, validity)
self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
def build_generator(self):
"""
-----------------------------------------------------------------------------------------------
Model creation for generator
-----------------------------------------------------------------------------------------------
"""
model = K.Sequential()
"""
-----------------------------------------------------------------------------------------------
Add layers
-----------------------------------------------------------------------------------------------
"""
model.add(K.layers.Dense(128 * 8 * 8, activation="relu", input_dim=100))
model.add(K.layers.Dense(256, input_dim=self.latent_dim))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.BatchNormalization(momentum=0.8))
model.add(K.layers.Dense(512))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.BatchNormalization(momentum=0.8))
model.add(K.layers.Dense(1024))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.BatchNormalization(momentum=0.8))
model.add(K.layers.Dense(np.prod(self.img_shape), activation='tanh'))
model.add(K.layers.Reshape(self.img_shape))
"""
-----------------------------------------------------------------------------------------------
Generate noise and Image
-----------------------------------------------------------------------------------------------
"""
noise = K.layers.Input(shape=(self.latent_dim,))
img = model(noise)
"""
-----------------------------------------------------------------------------------------------
Return Model
-----------------------------------------------------------------------------------------------
"""
return Model(noise, img)
def build_discriminator(self):
"""
-----------------------------------------------------------------------------------------------
Model creation for discriminator
-----------------------------------------------------------------------------------------------
"""
model = K.Sequential()
"""
-----------------------------------------------------------------------------------------------
Add layers
-----------------------------------------------------------------------------------------------
"""
model.add(K.layers.Conv2D(32, kernel_size=3, strides=2, input_shape= self.img_shape, padding="same"))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.Dropout(0.25))
model.add(K.layers.Conv2D(64, kernel_size=3, strides=2, padding="same"))
model.add(K.layers.ZeroPadding2D(padding=((0,1),(0,1))))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.Dropout(0.25))
model.add(K.layers.BatchNormalization(momentum=0.8))
model.add(K.layers.Conv2D(128, kernel_size=3, strides=2, padding="same"))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.Dropout(0.25))
model.add(K.layers.BatchNormalization(momentum=0.8))
model.add(K.layers.Conv2D(256, kernel_size=3, strides=1, padding="same"))
model.add(K.layers.LeakyReLU(alpha=0.2))
model.add(K.layers.Dropout(0.25))
model.add(K.layers.Flatten())
"""
-----------------------------------------------------------------------------------------------
Gereate image and features
-----------------------------------------------------------------------------------------------
"""
img = K.layers.Input(shape=self.img_shape)
features = model(img)
"""
-----------------------------------------------------------------------------------------------
valid indicates if the image is real or fake
-----------------------------------------------------------------------------------------------
"""
valid = K.layers.Dense(1, activation="sigmoid")(features)
"""
-----------------------------------------------------------------------------------------------
label indicates which type of image it is
-----------------------------------------------------------------------------------------------
"""
label = K.layers.Dense(self.num_classes+1, activation="softmax")(features)
"""
-----------------------------------------------------------------------------------------------
Return model
-----------------------------------------------------------------------------------------------
"""
return Model(img, [valid, label])
def train(self, X_train, y_train, X_test, y_test, epochs, batch_size, save_interval):
"""
-----------------------------------------------------------------------------------------------
Delete directory if exist and create it
-----------------------------------------------------------------------------------------------
"""
shutil.rmtree('GAN_generators_output', ignore_errors=True)
os.makedirs("GAN_generators_output")
"""
-----------------------------------------------------------------------------------------------
Adversarial ground truths
-----------------------------------------------------------------------------------------------
"""
valid = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
for epoch in range(epochs):
"""
-----------------------------------------------------------------------------------------------
Training the Discriminator
Select a random batch of images
-----------------------------------------------------------------------------------------------
"""
idx = np.random.randint(0, X_train.shape[0], batch_size)
imgs = X_train[idx]
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
"""
-----------------------------------------------------------------------------------------------
Generate a batch of new images
-----------------------------------------------------------------------------------------------
"""
gen_imgs = self.generator.predict(noise)
"""
-----------------------------------------------------------------------------------------------
Convert labels to categorical one-hot encoding
-----------------------------------------------------------------------------------------------
"""
labels = tf.keras.utils.to_categorical(y_train[idx], num_classes=self.num_classes+1)
fake_labels = tf.keras.utils.to_categorical(np.full((batch_size, 1),
self.num_classes),
num_classes=self.num_classes+1)
"""
-----------------------------------------------------------------------------------------------
Train the discriminator
-----------------------------------------------------------------------------------------------
"""
d_loss_real = self.discriminator.train_on_batch(imgs, [valid, labels])
d_loss_fake = self.discriminator.train_on_batch(gen_imgs, [fake, fake_labels])
d_loss = 0.5 * np.add(d_loss_real, d_loss_fake)
"""
-----------------------------------------------------------------------------------------------
Train Generator
-----------------------------------------------------------------------------------------------
"""
noise = np.random.normal(0, 1, (batch_size, self.latent_dim))
"""
-----------------------------------------------------------------------------------------------
Train the generator (to have the discriminator label samples as valid)
-----------------------------------------------------------------------------------------------
"""
g_loss = self.combined.train_on_batch(noise, valid)
"""
-----------------------------------------------------------------------------------------------
Add history data
-----------------------------------------------------------------------------------------------
"""
self.training_history["D_loss"].append(d_loss[0]);
self.training_history["D_acc"].append(100*d_loss[3]);
self.training_history["G_loss"].append(g_loss);
self.training_history["G_acc"].append(100*d_loss[4]);
"""
-----------------------------------------------------------------------------------------------
Print the result
-----------------------------------------------------------------------------------------------
"""
print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss[0], 100*d_loss[3], g_loss))
"""
-----------------------------------------------------------------------------------------------
Evaluate test data for each epoch
-----------------------------------------------------------------------------------------------
"""
self.evaluate_discriminator(X_test, y_test)
"""
-----------------------------------------------------------------------------------------------
If at save interval => save generated image samples
-----------------------------------------------------------------------------------------------
"""
if epoch % save_interval == 0:
self.save_images(epoch)
def evaluate_discriminator(self, X_test, y_test):
valid = np.ones((y_test.shape[0], 1))
"""
-----------------------------------------------------------------------------------------------
Convert labels to categorical one-hot encoding
-----------------------------------------------------------------------------------------------
"""
labels = tf.keras.utils.to_categorical(y_test, num_classes=self.num_classes+1)
"""
-----------------------------------------------------------------------------------------------
Evaluating the trained Discriminator and print outputs
-----------------------------------------------------------------------------------------------
"""
scores = self.discriminator.evaluate(X_test, [valid, labels], verbose=0)
print("Evaluating D [loss: %.4f, bi-loss: %.4f, cat-loss: %.4f, bi-acc: %.2f%%, cat-acc: %.2f%%]\n" %
(scores[0], scores[1], scores[2], scores[3]*100, scores[4]*100))
return (scores[0], scores[3]*100)
def save_images(self, epoch):
r, c = 5, 5
noise = np.random.normal(0, 1, (r * c, self.latent_dim))
gen_imgs = self.generator.predict(noise)
"""
-----------------------------------------------------------------------------------------------
Rescale images from [-1..1] to [0..1] just to display purposes.
-----------------------------------------------------------------------------------------------
"""
gen_imgs = 0.5 * gen_imgs + 0.5
fig, axs = plt.subplots(r, c)
cnt = 0
for i in range(r):
for j in range(c):
axs[i,j].imshow(gen_imgs[cnt, :,:,0])
axs[i,j].axis('off')
cnt += 1
"""
-----------------------------------------------------------------------------------------------
Save generator image for each epoch
-----------------------------------------------------------------------------------------------
"""
fig.savefig("GAN_generators_output/gan_%d.png" % epoch)
plt.close()
def save_model(self):
def save(model, model_name):
"""
-----------------------------------------------------------------------------------------------
Save weights
-----------------------------------------------------------------------------------------------
"""
model_path = "./CGAN_saved_models/%s.json" % model_name
weights_path = "./CGAN_saved_models/%s_weights.hdf5" % model_name
options = {"file_arch": model_path,
"file_weight": weights_path}
json_string = model.to_json()
open(options['file_arch'], 'w').write(json_string)
model.save_weights(options['file_weight'])
shutil.rmtree('CGAN_saved_models', ignore_errors=True)
os.makedirs("CGAN_saved_models")
"""
-----------------------------------------------------------------------------------------------
Save models
-----------------------------------------------------------------------------------------------
"""
save(self.generator, "CGAN_gan_generator")
save(self.discriminator, "CGAN_gan_discriminator")
save(self.combined, "CGAN_gan_adversarial")
def plot_training_history(self):
"""
-----------------------------------------------------------------------------------------------
Plot training history
-----------------------------------------------------------------------------------------------
"""
fig, axs = plt.subplots(1,2,figsize=(15,5))
plt.title('Training History')
"""
-----------------------------------------------------------------------------------------------
Summarize history for G and D accuracy
-----------------------------------------------------------------------------------------------
"""
axs[0].plot(range(1,len(self.training_history['D_acc'])+1),self.training_history['D_acc'])
axs[0].plot(range(1,len(self.training_history['G_acc'])+1),self.training_history['G_acc'])
axs[0].set_title('D and G Accuracy')
axs[0].set_ylabel('Accuracy')
axs[0].set_xlabel('Epoch')
axs[0].set_xticks(np.arange(1,len(self.training_history['D_acc'])+1),len(self.training_history['D_acc'])/10)
axs[0].set_yticks([n for n in range(0, 101,10)])
axs[0].legend(['Discriminator', 'Generator'], loc='best')
"""
-----------------------------------------------------------------------------------------------
Summarize history for G and D loss
-----------------------------------------------------------------------------------------------
"""
axs[1].plot(range(1,len(self.training_history['D_loss'])+1),self.training_history['D_loss'])
axs[1].plot(range(1,len(self.training_history['G_loss'])+1),self.training_history['G_loss'])
axs[1].set_title('D and G Loss')
axs[1].set_ylabel('Loss')
axs[1].set_xlabel('Epoch')
axs[1].set_xticks(np.arange(1,len(self.training_history['G_loss'])+1),len(self.training_history['G_loss'])/10)
axs[1].legend(['Discriminator', 'Generator'], loc='best')
"""
-----------------------------------------------------------------------------------------------
Plot graphs
-----------------------------------------------------------------------------------------------
"""
plt.show()
def predict(self, X_test, y_test):
"""
-----------------------------------------------------------------------------------------------
Generating a predictions from the discriminator over the testing dataset
-----------------------------------------------------------------------------------------------
"""
y_pred = self.discriminator.predict(X_test)
"""
-----------------------------------------------------------------------------------------------
Formating predictions to remove the one_hot_encoding format
-----------------------------------------------------------------------------------------------
"""
y_pred = np.argmax(y_pred[1][:,:-1], axis=1)
"""
-----------------------------------------------------------------------------------------------
Calculating and ploting a Classification Report
-----------------------------------------------------------------------------------------------
"""
print ('\nOverall accuracy: %f%% \n' % (accuracy_score(y_test, y_pred) * 100))
print ('\nAveP: %f%% \n' % (average_precision_score(y_test, y_pred) * 100))
class_names = ['Non-nuclei', 'Nuclei']
print("Classification report:\n %s\n"
% (classification_report(y_test, y_pred, target_names=class_names)))
"""
-----------------------------------------------------------------------------------------------
Output Confusion matrix
-----------------------------------------------------------------------------------------------
"""
cm = confusion_matrix(y_test, y_pred)
plt.figure()
plot_confusion_matrix(cm, class_names, title='Confusion matrix')
def plot_confusion_matrix(cm, classes,
normalize=False,
title='Confusion matrix',
cmap=plt.cm.Blues):
"""
-----------------------------------------------------------------------------------------------
Prints and plots the confusion matrix
-----------------------------------------------------------------------------------------------
"""
if normalize:
cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
plt.imshow(cm, interpolation='nearest', cmap=cmap)
plt.title(title)
plt.colorbar()
tick_marks = np.arange(len(classes))
plt.xticks(tick_marks, classes, rotation=45)
plt.yticks(tick_marks, classes)
fmt = '.2f' if normalize else 'd'
thresh = cm.max() / 2.
for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
plt.text(j, i, format(cm[i, j], fmt),
horizontalalignment="center",
color="white" if cm[i, j] > thresh else "black")
plt.tight_layout()
plt.ylabel('True label')
plt.xlabel('Predicted label')
def load_TMI_data():
"""
-----------------------------------------------------------------------------------------------
Load the dataset
-----------------------------------------------------------------------------------------------
"""
dataset = scipy.io.loadmat('TMI2015/training/training.mat')
"""
-----------------------------------------------------------------------------------------------
Split into train and test. Values are in range [0..1] as float64
-----------------------------------------------------------------------------------------------
"""
X_train = np.transpose(dataset['train_x'], (3, 0, 1, 2))
y_train = list(dataset['train_y'][0])
X_test = np.transpose(dataset['test_x'], (3, 0, 1, 2))
y_test = list(dataset['test_y'][0])
"""
-----------------------------------------------------------------------------------------------
Change shape and range.
-----------------------------------------------------------------------------------------------
"""
y_train = np.asarray(y_train).reshape(-1, 1)
y_test = np.asarray(y_test).reshape(-1, 1)
"""
-----------------------------------------------------------------------------------------------
1-> 0 : Non-nucleus. 2 -> 1: Nucleus
-----------------------------------------------------------------------------------------------
"""
y_test -= 1
y_train -= 1
"""
-----------------------------------------------------------------------------------------------
Resize to 32x32
-----------------------------------------------------------------------------------------------
"""
X_train_resized = np.empty([X_train.shape[0], 32, 32, X_train.shape[3]])
for i in range(X_train.shape[0]):
X_train_resized[i] = resize(X_train[i], (32, 32, 3), mode='reflect')
X_test_resized = np.empty([X_test.shape[0], 32, 32, X_test.shape[3]])
for i in range(X_test.shape[0]):
X_test_resized[i] = resize(X_test[i], (32, 32, 3), mode='reflect')
"""
-----------------------------------------------------------------------------------------------
Normalize images from [0..1] to [-1..1]
-----------------------------------------------------------------------------------------------
"""
X_train_resized = 2 * X_train_resized - 1
X_test_resized = 2 * X_test_resized - 1
return X_train_resized, y_train, X_test_resized, y_test
if __name__ == '__main__':
Model = tf.keras.Model
"""
-----------------------------------------------------------------------------------------------
Load Data
-----------------------------------------------------------------------------------------------
"""
X_train, y_train, X_test, y_test = load_TMI_data()
"""
-----------------------------------------------------------------------------------------------
Instanciate a compiled model
-----------------------------------------------------------------------------------------------
"""
gan = GAN()
"""
-----------------------------------------------------------------------------------------------
Fit/Train the model
-----------------------------------------------------------------------------------------------
"""
start = time.time()
gan.train(X_train, y_train, X_test, y_test, epochs=200, batch_size=32, save_interval=5)
end = time.time()
print ("\nTraining time: %0.1f minutes \n" % ((end-start) / 60))
"""
-----------------------------------------------------------------------------------------------
saved the trained model
-----------------------------------------------------------------------------------------------
"""
gan.save_model()
"""
-----------------------------------------------------------------------------------------------
plot training graph
-----------------------------------------------------------------------------------------------
"""
gan.plot_training_history()
"""
-----------------------------------------------------------------------------------------------
Evaluate the trained D model w.r.t unseen data (i.e. testing set)
-----------------------------------------------------------------------------------------------
"""
gan.evaluate_discriminator(X_test, y_test)
gan.predict(X_test, y_test)