-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDCGAN.py
287 lines (217 loc) · 8.5 KB
/
DCGAN.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
#!/usr/bin/env python
# coding: utf-8
import numpy
from numpy import zeros
from numpy import ones
from numpy.random import randn
from numpy.random import randint
from tensorflow.keras.datasets.cifar10 import load_data
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Reshape
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Conv2D
from tensorflow.keras.layers import Conv2DTranspose
from tensorflow.keras.layers import LeakyReLU
from tensorflow.keras.layers import Dropout
from matplotlib import pyplot
import os
from PIL import Image
import numpy as np
def define_discriminator(in_shape=(256, 256, 3)):
model = Sequential()
# normal
model.add(Conv2D(64, (3, 3), padding='same', input_shape=in_shape))
model.add(LeakyReLU(alpha=0.2))
# downsample
model.add(Conv2D(128, (3, 3), strides=(2, 2), padding='same'))
model.add(LeakyReLU(alpha=0.2))
# downsample
model.add(Conv2D(128, (3, 3), strides=(2, 2), padding='same'))
model.add(LeakyReLU(alpha=0.2))
# downsample
model.add(Conv2D(256, (3, 3), strides=(2, 2), padding='same'))
model.add(LeakyReLU(alpha=0.2))
# classifier
model.add(Flatten())
model.add(Dropout(0.2))
model.add(Dense(1, activation='sigmoid'))
# compile model
opt = Adam(learning_rate=0.00001, beta_1=0.2)
model.compile(loss='binary_crossentropy', optimizer=opt, metrics=['accuracy'])
return model
d_model = define_discriminator(in_shape = (256,256,3))
# In[24]:
d_model.summary()
from tensorflow.keras.layers import BatchNormalization
from keras.models import Sequential
from keras.layers import Dense, Reshape, Conv2DTranspose, BatchNormalization, Activation
def define_generator(latent_dim):
model = Sequential()
# Foundation for 8x8 image
n_nodes = 128 * 8 * 8
model.add(Dense(n_nodes, input_dim=latent_dim))
model.add(Reshape((8, 8, 128)))
model.add(BatchNormalization())
model.add(Activation('relu'))
# Upsample to 16x16
model.add(Conv2DTranspose(64, (4, 4), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
# Upsample to 32x32
model.add(Conv2DTranspose(32, (4, 4), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
# Upsample to 64x64
model.add(Conv2DTranspose(16, (4, 4), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
# Upsample to 128x128
model.add(Conv2DTranspose(8, (4, 4), strides=(2, 2), padding='same'))
model.add(BatchNormalization())
model.add(Activation('relu'))
# Upsample to 256x256
model.add(Conv2DTranspose(3, (4, 4), strides=(2, 2), padding='same', activation='tanh'))
return model
return model
g_model = define_generator(500)
g_model.summary()
# In[ ]:
# In[ ]:
# define the combined generator and discriminator model, for updating the generator
def define_gan(g_model, d_model):
# make weights in the discriminator not trainable
d_model.trainable = False
# connect them
model = Sequential()
# add generator
model.add(g_model)
# add the discriminator
model.add(d_model)
# compile model
opt = Adam(lr=0.00001, beta_1=0.5)
model.compile(loss='binary_crossentropy', optimizer=opt)
return model
# load and prepare cifar10 training images
def load_real_samples():
# load cifar10 dataset
trainX = []
for i in os.listdir("/N/slate/athshah/Breast_cancer/train"):
if str(i)[-3:] == "png":
image = Image.open(f"/N/slate/athshah/Breast_cancer/train/{i}")
# Convert the resized image to a NumPy array
image = image.resize((256,256))
image_array = np.array(image)
trainX.append(image_array)
# convert from unsigned ints to floats
trainX = np.array(trainX)
X = trainX.astype('float32')
# scale from [0,255] to [-1,1]
X = (X - 127.5) / 127.5
return X
# select real samples
def generate_real_samples(dataset, n_samples):
# choose random instances
ix = randint(0, dataset.shape[0], n_samples)
# retrieve selected images
X = dataset[ix]
# generate 'real' class labels (1)
y = ones((n_samples, 1))
return X, y
# generate points in latent space as input for the generator
def generate_latent_points(latent_dim, n_samples):
# generate points in the latent space
x_input = numpy.random.normal(0, 3, (n_samples, latent_dim))
# reshape into a batch of inputs for the network
return x_input
# use the generator to generate n fake examples, with class labels
def generate_fake_samples(g_model, latent_dim, n_samples):
# generate points in latent space
x_input = generate_latent_points(latent_dim, n_samples)
# predict outputs
X = g_model.predict(x_input,verbose = 0)
# create 'fake' class labels (0)
y = zeros((n_samples, 1))
return X, y
# create and save a plot of generated images
def save_plot(examples, epoch, n=2):
# scale from [-1,1] to [0,1]
examples = (examples + 1) / 2.0
# plot images
for i in range(n * n):
# define subplot
pyplot.subplot(n, n, 1 + i)
# turn off axis
pyplot.axis('off')
# plot raw pixel data
pyplot.imshow(examples[i*10])
# save plot to file
filename = 'images_new/generated_plot_e%03d.png' % (epoch+1)
pyplot.savefig(filename)
pyplot.show()
# evaluate the discriminator, plot generated images, save generator model
def summarize_performance(epoch, g_model, d_model, dataset, latent_dim,acc_re,acc_fa,n_samples = 150):
# prepare real samples
X_real, y_real = generate_real_samples(dataset, n_samples)
# evaluate discriminator on real examples
_, acc_real = d_model.evaluate(X_real, y_real, verbose=0)
# prepare fake examples
x_fake, y_fake = generate_fake_samples(g_model, latent_dim, n_samples)
# evaluate discriminator on fake examples
_, acc_fake = d_model.evaluate(x_fake, y_fake, verbose=0)
# summarize discriminator performance
print('>Accuracy real: %.0f%%, fake: %.0f%%' % (acc_real * 100, acc_fake * 100))
acc_re.append(acc_real)
acc_fa.append(1- acc_fake)
# save plot
save_plot(x_fake, epoch)
# save the generator model tile file
if epoch > 50:
filename = f'generator_model_DCGAN{epoch}.h5'
g_model.save(filename)
# train the generator and discriminator
def train(g_model, d_model, gan_model, dataset, latent_dim, n_epochs=100, n_batch=50):
disc_real = []
disc_fake = []
bat_per_epo = int(dataset.shape[0] / n_batch)
half_batch = int(n_batch / 2)
# manually enumerate epochs
for i in range(n_epochs):
# enumerate batches over the training set
for j in range(bat_per_epo):
# get randomly selected 'real' samples
X_real, y_real = generate_real_samples(dataset, half_batch)
# update discriminator model weights
d_loss1, _ = d_model.train_on_batch(X_real, y_real)
# generate 'fake' examples
X_fake, y_fake = generate_fake_samples(g_model, latent_dim, half_batch)
# update discriminator model weights
d_loss2, _ = d_model.train_on_batch(X_fake, y_fake)
# prepare points in latent space as input for the generator
X_gan = generate_latent_points(latent_dim, n_batch)
# create inverted labels for the fake samples
y_gan = ones((n_batch, 1))
# update the generator via the discriminator's error
g_loss = gan_model.train_on_batch(X_gan, y_gan)
# summarize loss on this batch
if j % 100 == 0:
print('>%d, %d/%d, d1=%.3f, d2=%.3f g=%.3f' % (i + 1, j + 1, bat_per_epo, d_loss1, d_loss2, g_loss))
# evaluate the model performance, sometimes
# if (i+1) % 10 == 0:
summarize_performance(i, g_model, d_model, dataset, latent_dim,disc_real,disc_fake)
from keras.models import load_model
# size of the latent space
latent_dim = 1000
# create the discriminator
d_model = define_discriminator()
# create the generator
g_model = define_generator(latent_dim)
#g_model = load_model("generator_model.h5")
# create the gan
gan_model = define_gan(g_model, d_model)
# load image data
dataset = load_real_samples()
# train model
train(g_model, d_model, gan_model, dataset, latent_dim, 100, 64)
# In[ ]: