-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathContextNet.py
More file actions
262 lines (204 loc) · 9.61 KB
/
ContextNet.py
File metadata and controls
262 lines (204 loc) · 9.61 KB
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
from random import randint
from turtle import shape
from keras.layers import Input, Dense, Reshape, Flatten, Conv2DTranspose
from keras.layers import BatchNormalization, Activation, MaxPool2D,ReLU
from keras.layers.convolutional import Conv2D
from keras.models import Sequential, Model
from keras.optimizers import adam_v2
import tensorflow as tf
import keras.layers.merge as merge
import ds
import numpy as np
import matplotlib.pyplot as plt
import os
class GAN():
def __init__(self):
self.img_rows = 227
self.img_cols = 227
self.channels = 1
self.img_shape = (self.img_rows, self.img_cols, self.channels)
self.img_gen_shape = (192,192,1)
optimizer = adam_v2.Adam(0.0002, 0.5)
# Create Dirs
if(not os.path.exists('models')):
os.mkdir('models')
if(not os.path.exists('images')):
os.mkdir('images')
self.epo = 0
if(os.path.exists('models/epoch')):
with open('models/epoch','r') as f:
self.epo = int(f.read())
# Build the discriminator
self.discriminator = self.build_discriminator()
self.discriminator.compile(loss='binary_crossentropy',
optimizer=optimizer,
metrics=['accuracy'])
# Load the discriminator weights
if(os.path.exists('models/dis.h5')):
self.discriminator.load_weights('models/dis.h5')
# Build the generator
self.generator = self.build_generator()
# Load the generator weights
if(os.path.exists('models/gen.h5')):
self.generator.load_weights('models/gen.h5')
# The generator takes noise as input and generates imgs
z = Input(shape=self.img_shape)
label = Input(shape=self.img_shape)
label_gen = Input(shape=self.img_gen_shape)
img = self.generator([z,label])
# For the combined model we will only train the generator
self.discriminator.trainable = False
# The discriminator takes generated images as input and determines validity
validity = self.discriminator([img,label_gen])
# The combined model (stacked generator and discriminator)
# Trains the generator to fool the discriminator
self.combined = Model([z,label, label_gen], validity)
self.combined.compile(loss='binary_crossentropy', optimizer=optimizer)
def build_generator(self):
model = Sequential()
model.add(Input(shape=self.img_shape))
model.add(Conv2D(96,(11,11),strides=(4,4),activation='relu',name='conv1'))
model.add(MaxPool2D((3,3),strides=(2,2),name='maxpool1'))
model.add(Conv2D(256,(5,5),strides=(1,1),padding='same',activation='relu',name="conv2"))
model.add(MaxPool2D((3,3),strides=(2,2),name='maxpool2'))
model.add(Conv2D(384,(3,3),strides=(1,1),padding='same',activation='relu',name='conv3'))
model.add(Conv2D(384,(3,3),strides=(1,1),padding='same',activation='relu',name='conv4'))
model.add(Conv2D(256,(3,3),strides=(1,1),padding='same',activation='relu',name='conv5'))
model.add(MaxPool2D((3,3),strides=(2,2),name='maxpool3'))
model.add(Flatten())
model.add(Dense(9216))
model.add(Dense(9216))
model.add(Dense(9216))
model.add(Dense(9216))
model.add(Reshape((6,6,256)))
model.add(Conv2DTranspose(128,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2DTranspose(64,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2DTranspose(64,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2DTranspose(32,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2DTranspose(1,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(Activation('sigmoid'))
noise = Input(shape=self.img_shape)
label = Input(shape=self.img_shape)
model_input = merge.multiply([noise, label])
img = model(model_input)
return Model([noise,label], img)
def build_discriminator(self):
model = Sequential()
model.add(Input(shape=self.img_gen_shape))
model.add(Conv2D(32,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2D(64,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2D(128,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Conv2D(256,(5,5),strides=(2,2),padding='same'))
model.add(BatchNormalization())
model.add(ReLU())
model.add(Flatten())
model.add(Dense(1, activation="sigmoid"))
img = Input(shape=self.img_gen_shape)
label = Input(shape=self.img_gen_shape)
tmp = merge.minimum([label,np.zeros((1,)+self.img_gen_shape)])
tmp = merge.multiply([tmp,img])
tmp = merge.multiply([tmp,np.multiply(np.ones((1,)+self.img_gen_shape),-1)])
tmp2 = merge.maximum([label,np.zeros((1,)+self.img_gen_shape)])
comp = merge.add([tmp,tmp2])
validity = model(comp)
return Model([img, label], validity)
def train(self, epochs, batch_size=128, sample_interval=50):
# Load the dataset
(_,_,_),(self.X_test, self.Y_test, self.y_test) = ds.load_data()
# (XX, 227, 227) -> (XX, 227, 227, 1)
ds.epo = self.epo
ds.batch_size = batch_size
ds.epochs = epochs
ds.init_data()
# Load TF dataset
tfXtrain = tf.data.Dataset.from_generator(ds._gen_Xtrain,output_signature=(tf.TensorSpec(shape=self.img_gen_shape,dtype=tf.float32)),args=())
tfYtrain = tf.data.Dataset.from_generator(ds._gen_Ytrain,output_signature=(tf.TensorSpec(shape=self.img_shape,dtype=tf.float32)),args=())
tfytrain = tf.data.Dataset.from_generator(ds._gen_ytrain,output_signature=(tf.TensorSpec(shape=self.img_gen_shape,dtype=tf.float32)),args=())
dataset = tf.data.Dataset.zip((tfXtrain,tfYtrain,tfytrain)).batch(batch_size).prefetch(tf.data.AUTOTUNE)
# Adversarial ground truths
valid = np.ones((batch_size, 1))
fake = np.zeros((batch_size, 1))
epoch = self.epo
for item in dataset:
# Interrupt training
# if(keyboard.is_pressed('q')):
# break
# ---------------------
# Train Discriminator
# ---------------------
# Select a random batch of images
imgs = item[0]
labels = item[1]
labels_gen = item[2]
noise = np.random.normal(0, 1, (batch_size,)+self.img_shape)
# Generate a batch of new images
gen_imgs = self.generator.predict([noise,labels])
# Train the discriminator
d_loss_real = self.discriminator.fit([imgs,labels_gen], valid, epochs=2, verbose=0)
d_loss_fake = self.discriminator.fit([gen_imgs,labels_gen], fake, epochs=2, verbose=0)
d_loss = 0.5 * (d_loss_real.history['loss'][-1] + d_loss_fake.history['loss'][-1])
d_loss_acc = 0.5 * (d_loss_real.history['accuracy'][-1] + d_loss_fake.history['accuracy'][-1])
# ---------------------
# Train Generator
# ---------------------
noise = np.random.normal(0, 1, (batch_size,)+self.img_shape)
# Train the generator (to have the discriminator label samples as valid)
g_loss = self.combined.fit([noise,labels,labels_gen], valid, epochs=10, verbose=0)
# Plot the progress
print ("%d [D loss: %f, acc.: %.2f%%] [G loss: %f]" % (epoch, d_loss, 100*d_loss_acc, g_loss.history['loss'][-1]))
self.cur_iter = epoch
# If at save interval => save generated image samples
if epoch % sample_interval == 0:
self.sample_images(epoch)
# Save Models
self.save_models()
epoch += 1
def sample_images(self, epoch):
idx = np.random.randint(0, self.X_test.shape[0], 1)
noise = np.random.normal(0, 1, (1,)+self.img_shape)
label = self.Y_test[idx]
gen_img = self.generator.predict([noise,label])
label_gen = self.y_test[idx]
# Rescale images 0 - 1
gen_img = np.maximum(gen_img,0)
# Shaped images
tmp = np.minimum(label_gen[0,:,:],0)
tmp = np.multiply(tmp,gen_img[0,:,:,0])
tmp = np.multiply(tmp,-1)
tmp2 = np.maximum(label_gen[0,:,:],0)
shaped_img = np.add(tmp,tmp2)
fig, axs = plt.subplots(1, 4)
axs[0].imshow(self.X_test[idx][0,:,:], cmap='gray')
axs[1].imshow(label[0,:,:], cmap='gray')
axs[2].imshow(gen_img[0,:,:,0], cmap='gray')
axs[3].imshow(shaped_img, cmap='gray')
axs[0].axis('off')
axs[1].axis('off')
axs[2].axis('off')
axs[3].axis('off')
fig.savefig("images/%d.png" % epoch)
plt.close()
def save_models(self):
self.generator.save_weights('models/gen.h5')
self.discriminator.trainable = True
self.discriminator.save_weights('models/dis.h5')
with open('models/epoch','w') as f:
f.write(str(self.cur_iter))
if __name__ == '__main__':
gan = GAN()
gan. train(epochs=100000, batch_size=256, sample_interval=50)