-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathanogan.py
200 lines (168 loc) · 6.9 KB
/
anogan.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
from __future__ import print_function
from keras.models import Sequential, Model
from keras.layers import Input, Reshape, Dense, Dropout, MaxPooling2D, Conv2D, Flatten
from keras.layers import Conv2DTranspose, LeakyReLU
from keras.layers.core import Activation
from keras.layers.normalization import BatchNormalization
from keras.optimizers import Adam, RMSprop
from keras import backend as K
from keras import initializers
import tensorflow as tf
import numpy as np
from tqdm import tqdm
import cv2
import math
from keras.utils. generic_utils import Progbar
### combine images for visualization
def combine_images(generated_images):
num = generated_images.shape[0]
width = int(math.sqrt(num))
height = int(math.ceil(float(num)/width))
shape = generated_images.shape[1:4]
image = np.zeros((height*shape[0], width*shape[1], shape[2]),
dtype=generated_images.dtype)
for index, img in enumerate(generated_images):
i = int(index/width)
j = index % width
image[i*shape[0]:(i+1)*shape[0], j*shape[1]:(j+1)*shape[1],:] = img[:, :, :]
return image
### generator model define
def generator_model():
inputs = Input((10,))
fc1 = Dense(input_dim=10, units=128*7*7)(inputs)
fc1 = BatchNormalization()(fc1)
fc1 = LeakyReLU(0.2)(fc1)
fc2 = Reshape((7, 7, 128), input_shape=(128*7*7,))(fc1)
up1 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(fc2)
conv1 = Conv2D(64, (3, 3), padding='same')(up1)
conv1 = BatchNormalization()(conv1)
conv1 = Activation('relu')(conv1)
up2 = Conv2DTranspose(64, (2, 2), strides=(2, 2), padding='same')(conv1)
conv2 = Conv2D(1, (5, 5), padding='same')(up2)
outputs = Activation('tanh')(conv2)
model = Model(inputs=[inputs], outputs=[outputs])
return model
### discriminator model define
def discriminator_model():
inputs = Input((28, 28, 1))
conv1 = Conv2D(64, (5, 5), padding='same')(inputs)
conv1 = LeakyReLU(0.2)(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, (5, 5), padding='same')(pool1)
conv2 = LeakyReLU(0.2)(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
fc1 = Flatten()(pool2)
fc1 = Dense(1)(fc1)
outputs = Activation('sigmoid')(fc1)
model = Model(inputs=[inputs], outputs=[outputs])
return model
### d_on_g model for training generator
def generator_containing_discriminator(g, d):
d.trainable = False
ganInput = Input(shape=(10,))
x = g(ganInput)
ganOutput = d(x)
gan = Model(inputs=ganInput, outputs=ganOutput)
# gan.compile(loss='binary_crossentropy', optimizer='adam')
return gan
def load_model():
d = discriminator_model()
g = generator_model()
d_optim = RMSprop()
g_optim = RMSprop(lr=0.0002)
g.compile(loss='binary_crossentropy', optimizer=g_optim)
d.compile(loss='binary_crossentropy', optimizer=d_optim)
d.load_weights('./weights/discriminator.h5')
g.load_weights('./weights/generator.h5')
return g, d
### train generator and discriminator
def train(BATCH_SIZE, X_train):
### model define
d = discriminator_model()
g = generator_model()
d_on_g = generator_containing_discriminator(g, d)
d_optim = RMSprop(lr=0.0004)
g_optim = RMSprop(lr=0.0002)
g.compile(loss='mse', optimizer=g_optim)
d_on_g.compile(loss='mse', optimizer=g_optim)
d.trainable = True
d.compile(loss='mse', optimizer=d_optim)
for epoch in range(10):
print ("Epoch is", epoch)
n_iter = int(X_train.shape[0]/BATCH_SIZE)
progress_bar = Progbar(target=n_iter)
for index in range(n_iter):
# create random noise -> U(0,1) 10 latent vectors
noise = np.random.uniform(0, 1, size=(BATCH_SIZE, 10))
# load real data & generate fake data
image_batch = X_train[index*BATCH_SIZE:(index+1)*BATCH_SIZE]
generated_images = g.predict(noise, verbose=0)
# visualize training results
if index % 20 == 0:
image = combine_images(generated_images)
image = image*127.5+127.5
cv2.imwrite('./result/'+str(epoch)+"_"+str(index)+".png", image)
# attach label for training discriminator
X = np.concatenate((image_batch, generated_images))
y = np.array([1] * BATCH_SIZE + [0] * BATCH_SIZE)
# training discriminator
d_loss = d.train_on_batch(X, y)
# training generator
d.trainable = False
g_loss = d_on_g.train_on_batch(noise, np.array([1] * BATCH_SIZE))
d.trainable = True
progress_bar.update(index, values=[('g',g_loss), ('d',d_loss)])
print ('')
# save weights for each epoch
g.save_weights('weights/generator.h5', True)
d.save_weights('weights/discriminator.h5', True)
return d, g
### generate images
def generate(BATCH_SIZE):
g = generator_model()
g.load_weights('weights/generator.h5')
noise = np.random.uniform(0, 1, (BATCH_SIZE, 10))
generated_images = g.predict(noise)
return generated_images
### anomaly loss function
def sum_of_residual(y_true, y_pred):
return K.sum(K.abs(y_true - y_pred))
### discriminator intermediate layer feautre extraction
def feature_extractor(d=None):
if d is None:
d = discriminator_model()
d.load_weights('weights/discriminator.h5')
intermidiate_model = Model(inputs=d.layers[0].input, outputs=d.layers[-7].output)
intermidiate_model.compile(loss='binary_crossentropy', optimizer='rmsprop')
return intermidiate_model
### anomaly detection model define
def anomaly_detector(g=None, d=None):
if g is None:
g = generator_model()
g.load_weights('weights/generator.h5')
intermidiate_model = feature_extractor(d)
intermidiate_model.trainable = False
g = Model(inputs=g.layers[1].input, outputs=g.layers[-1].output)
g.trainable = False
# Input layer cann't be trained. Add new layer as same size & same distribution
aInput = Input(shape=(10,))
gInput = Dense((10), trainable=True)(aInput)
gInput = Activation('sigmoid')(gInput)
# G & D feature
G_out = g(gInput)
D_out= intermidiate_model(G_out)
model = Model(inputs=aInput, outputs=[G_out, D_out])
model.compile(loss=sum_of_residual, loss_weights= [0.90, 0.10], optimizer='rmsprop')
# batchnorm learning phase fixed (test) : make non trainable
K.set_learning_phase(0)
return model
### anomaly detection
def compute_anomaly_score(model, x, iterations=500, d=None):
z = np.random.uniform(0, 1, size=(1, 10))
intermidiate_model = feature_extractor(d)
d_x = intermidiate_model.predict(x)
# learning for changing latent
loss = model.fit(z, [x, d_x], batch_size=1, epochs=iterations, verbose=0)
similar_data, _ = model.predict(z)
loss = loss.history['loss'][-1]
return loss, similar_data