-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathneural_net.py
More file actions
273 lines (205 loc) · 11.3 KB
/
neural_net.py
File metadata and controls
273 lines (205 loc) · 11.3 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
263
264
265
266
267
268
269
270
271
272
273
from time import time
import numpy as np
from absl import flags, app
FLAGS = flags.FLAGS
flags.DEFINE_string("model_name", None, "Path for Autoencoder model without extension")
flags.DEFINE_boolean("anomaly_blank_label", True, "True if the use of blank labels for anomalous impurity is desired")
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, UpSampling2D, Reshape, Flatten, Dropout, Activation
from tensorflow.keras.models import Model, Sequential
from tensorflow.keras import backend as K
# from keras.layers.normalization import BatchNormalization
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.callbacks import TensorBoard
def conv_autoencoder(input_shape, WIDTH, HEIGHT):
input_img = Input(shape=input_shape)
# encode with a deep net
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Dropout(0.3)(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Dropout(0.3)(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Dropout(0.3)(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Dropout(0.3)(encoded)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = UpSampling2D((2, 2))(x)
x = Dropout(0.3)(x)
x = Conv2D(256, (5, 5), activation='relu', kernel_initializer='random_uniform')(x)
x = UpSampling2D((2, 2))(x)
x = Dropout(0.3)(x)
x = Conv2D(256, (5, 5), activation='relu', kernel_initializer='random_uniform')(x)
x = UpSampling2D((2, 2))(x)
x = Dropout(0.3)(x)
x = Conv2D(256, (5, 5), activation='relu', kernel_initializer='random_uniform')(x)
decoded = UpSampling2D((2, 2))(x)
x = Flatten()(decoded)
x = Dense(500, activation='relu')(x)
x = Dense(1*WIDTH*HEIGHT, activation='sigmoid')(x)
result = Reshape(input_shape)(x)
ae = Model(input_img, result)
optimizer = Adam(lr=1e-05, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
ae.compile(optimizer=optimizer, loss='mse', metrics=['accuracy'])
return ae
def conv_autoencoder_no_drop(input_shape, WIDTH, HEIGHT):
input_img = Input(shape=input_shape)
# encode with a deep net
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(input_img)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
encoded = MaxPooling2D((2, 2), padding='same')(x)
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(encoded)
x = UpSampling2D((2, 2))(x)
x = Conv2D(256, (5, 5), activation='relu', kernel_initializer='random_uniform')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(256, (5, 5), activation='relu', kernel_initializer='random_uniform')(x)
x = UpSampling2D((2, 2))(x)
x = Conv2D(256, (5, 5), activation='relu', kernel_initializer='random_uniform')(x)
decoded = UpSampling2D((2, 2))(x)
x = Flatten()(decoded)
x = Dense(500, activation='relu')(x)
x = Dense(1*WIDTH*HEIGHT, activation='sigmoid')(x)
result = Reshape(input_shape)(x)
ae = Model(input_img, result)
optimizer = Adam(lr=1e-05, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
ae.compile(optimizer=optimizer, loss='mse', metrics=['accuracy'])
return ae
def smaller_conv_autoencoder(input_shape, WIDTH, HEIGHT):
input_img = Input(shape=input_shape)
x = input_img
depth = 4
for encoding_layer in range(depth):
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = MaxPooling2D((2, 2), padding='same')(x)
# x = Dropout(0.3)(x)
for decoding_layer in range(depth-1):
x = Conv2D(256, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
x = UpSampling2D((2, 2))(x)
# x = Dropout(0.3)(x)
# x = Conv2D(128, (5, 5), activation='relu', padding='same', kernel_initializer='random_uniform')(x)
# decoded = UpSampling2D((2, 2))(x)
x = Flatten()(x)
x = Dense(500, activation='relu')(x)
x = Dense(1*WIDTH*HEIGHT, activation='sigmoid')(x)
result = Reshape(input_shape)(x)
ae = Model(input_img, result)
optimizer = Adam(lr=1e-05, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=0.0)
ae.compile(optimizer=optimizer, loss='mse', metrics=['accuracy'])
return ae
def fixed_generator(generator):
"""
Modifies the data generator, such that normal data will get itself as label, and anomal data will get blank image
as label. By that, the network learns to reconstruct successfully normal impurities, and will fail to reconstruct
anomalous impurities.
"""
for batch in generator:
fixed_x = np.empty(shape=batch[0].shape, dtype="float32")
fixed_y = np.empty(shape=batch[0].shape, dtype="float32")
data_pice_counter = 0
for (x, y) in zip(batch[0], batch[1]):
fixed_x[data_pice_counter] = x
if y == 0: # anomaly, give blank image as label. Thus, the auto encoder won't be able to reconstruct.
blank = np.full(fill_value=1, shape=x.shape, dtype="float32")
fixed_y[data_pice_counter] = blank
else: # normal, give the input image as label so the auto encoder will be able to reconstruct.
fixed_y[data_pice_counter] = x
data_pice_counter += 1
yield (fixed_x, fixed_y)
def fixed_generator_none(generator):
"""
"""
for batch in generator:
yield (batch, batch)
# Plot the training and validation loss + accuracy
def plot_training(history):
import matplotlib.pyplot as plt
acc = history.history['acc']
val_acc = history.history['val_acc']
loss = history.history['loss']
val_loss = history.history['val_loss']
epochs = range(len(loss))
plt.figure()
plt.plot(epochs, acc, 'r.')
plt.plot(epochs, val_acc, 'r')
plt.title('Training and validation accuracy')
plt.show()
plt.savefig('acc_vs_epochs.png')
plt.figure()
plt.plot(epochs, loss, 'r.')
plt.plot(epochs, val_loss, 'r-')
plt.title('Training and validation loss')
plt.show()
plt.savefig('loss_vs_epochs.png')
def main(_):
if FLAGS.model_name is None:
print("Please provide a name for the model by providing --model_name=NAME without extension")
return
HEIGHT = 100
WIDTH = 100
BATCH_SIZE = 32
EPOCHS_NUM = 500
# create generator
datagen = ImageDataGenerator(rescale=1. / 255, horizontal_flip=True, vertical_flip=True, rotation_range=360)
# datagen = ImageDataGenerator()
# prepare an iterators for each dataset
if FLAGS.anomaly_blank_label:
train_it = datagen.flow_from_directory('data/rescaled_extended_2_classes/train/', target_size=(HEIGHT, WIDTH),
class_mode="binary", batch_size=BATCH_SIZE, color_mode='grayscale')
val_it = datagen.flow_from_directory('data/rescaled_extended_2_classes/validation/',
target_size=(HEIGHT, WIDTH),
class_mode="binary", batch_size=BATCH_SIZE, color_mode='grayscale')
else:
train_it = datagen.flow_from_directory('data/rescaled_extended_1_class/train/', target_size=(HEIGHT, WIDTH),
class_mode=None, batch_size=BATCH_SIZE, color_mode='grayscale')
val_it = datagen.flow_from_directory('data/rescaled_extended_1_class/validation/', target_size=(HEIGHT, WIDTH),
class_mode=None, batch_size=BATCH_SIZE, color_mode='grayscale')
if K.image_data_format() == 'channels_first':
input_shape = (1, WIDTH, HEIGHT)
else:
input_shape = (WIDTH, HEIGHT, 1)
# model = conv_autoencoder(input_shape, WIDTH, HEIGHT)
model = conv_autoencoder_no_drop(input_shape, WIDTH, HEIGHT)
tbCallBack = TensorBoard(log_dir='./Graph/{}'.format(time()), histogram_freq=0, write_graph=True, write_images=True)
if FLAGS.anomaly_blank_label:
history = model.fit_generator(fixed_generator(train_it), epochs=EPOCHS_NUM,
validation_data=fixed_generator(val_it),
validation_steps=8, steps_per_epoch=16, callbacks=[tbCallBack])
# history = model.fit_generator(fixed_generator(train_it), epochs=EPOCHS_NUM,
# validation_data=fixed_generator(val_it),
# validation_steps=8,
# steps_per_epoch=16, workers=8, use_multiprocessing=True, callbacks=[tbCallBack])
else:
history = model.fit_generator(fixed_generator_none(train_it), epochs=EPOCHS_NUM,
validation_data=fixed_generator_none(val_it),
validation_steps=8, steps_per_epoch=16, callbacks=[tbCallBack])
# history = model.fit_generator(fixed_generator_none(train_it), epochs=EPOCHS_NUM,
# validation_data=fixed_generator_none(val_it),
# validation_steps=8,
# steps_per_epoch=16, workers=8, use_multiprocessing=True, callbacks=[tbCallBack])
test_it_normal = datagen.flow_from_directory('data/test_rescaled_extended/normal/', target_size=(HEIGHT, WIDTH),
class_mode=None, batch_size=BATCH_SIZE, color_mode='grayscale')
test_it_anomaly = datagen.flow_from_directory('data/test_rescaled_extended/anomaly/', target_size=(HEIGHT, WIDTH),
class_mode=None, batch_size=BATCH_SIZE, color_mode='grayscale')
# test_it_combined = datagen.flow_from_directory('data/test_with_2_classes/', target_size=(HEIGHT, WIDTH),
# class_mode="binary", batch_size=BATCH_SIZE)
score = model.evaluate_generator(fixed_generator_none(test_it_normal), steps=24)
print("Normal: Loss: ", score[0], "Accuracy: ", score[1])
score = model.evaluate_generator(fixed_generator_none(test_it_anomaly), steps=24)
print("Anoamly: Loss: ", score[0], "Accuracy: ", score[1])
model.save(FLAGS.model_name + ".h5")
# Write the net summary
with open(FLAGS.model_name + '_summary.txt', 'w') as fh:
# Pass the file handle in as a lambda function to make it callable
model.summary(print_fn=lambda x: fh.write(x + '\n'))
print("Saved model to disk")
plot_training(history)
if __name__ == "__main__":
app.run(main)