-
Notifications
You must be signed in to change notification settings - Fork 35
/
face.py
216 lines (185 loc) · 7.43 KB
/
face.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
import os
import glob
import time
import keras
from PIL import Image
from os import listdir
from shutil import copyfile
from os.path import isfile, join
from matplotlib import pyplot as plt
from keras_vggface.vggface import VGGFace
from keras.engine import Input
from keras import applications
from keras.models import Model
import tensorflow as tf
import numpy as np
from keras.layers import ZeroPadding2D, Convolution2D, MaxPooling2D, Flatten, Dense, Dropout
from keras_vggface.utils import preprocess_input
from sklearn.metrics import classification_report, confusion_matrix
from MLEXPS.MLEXPS import *
from keras import backend as K
import random
random.seed(42)
tf.random.set_seed(42)
# Providing more training examples within certain distributions of age, gender, and race will increase the model's accuracy.
Height = 224
Width = 224
BatchSize = 24
lr_rate=.0015
Version = 5
load_model = False
model_path = ''
accuracy = 0
accuracyCount = 0
trainableCount = 30
def SaveModelImage(Model, Title):
keras.utils.vis_utils.plot_model(Model, to_file=Title, show_shapes=True, show_layer_names=True)
return
def Summary(Model):
print(Model.summary())
return
def resnet():
BaseModel = applications.resnet50.ResNet50(weights= None, include_top=False, input_shape= (224,224,3))
last_layer = BaseModel.get_layer('activation_49').output
print('here')
return model
def MakeModel(dlsize):
BaseModel = VGGFace(model='senet50', include_top=False, input_shape=(Height, Width, 3), pooling='avg')
last_layer = BaseModel.get_layer('avg_pool').output
x = keras.layers.Flatten(name='flatten')(last_layer)
x = keras.layers.Dense(128, kernel_regularizer = keras.regularizers.l2(l = 0.015), activation='relu')(x)
x = keras.layers.Dropout(rate=.4, seed=42)(x)
out = keras.layers.Dense(2, activation='softmax', name='classifier')(x)
DerivedModel = keras.Model(BaseModel.input, out)
# # Everything is trainingable
# # Weights are used at init
# for layer in DerivedModel.layers:
# layer.trainable = True
#
#
# # Everything in the base model is frozen
# # Only top layers are trainable
# for layer in BaseModel.layers:
# layer.trainable = False
# base
for layer in DerivedModel.layers:
layer.trainable = False
for layer in DerivedModel.layers[-trainableCount:]:
layer.trainable = True
DerivedModel.compile(keras.optimizers.Adam(lr=lr_rate), loss='categorical_crossentropy', metrics=['accuracy'])
return DerivedModel
def clearWeights(model):
weights = model.get_weights()
for weight in weights:
weight = K.zeros(weight.shape, dtype=np.float64)
model.set_weights(weights)
return model
def preprocess_input_new(x):
img = preprocess_input(keras.preprocessing.image.img_to_array(x), version = 2)
return keras.preprocessing.image.array_to_img(img)
class EarlyStoppingAtMinLoss(tf.keras.callbacks.Callback):
def __init__(self, trainableCount=30):
print('working')
super(EarlyStoppingAtMinLoss, self).__init__()
self.epochCount = []
self.trainableCount = trainableCount
self.max = 0
def on_train_begin(self, logs=None):
self.accuracyCount = 0
self.accuracy = 0
def on_epoch_end(self, epoch, logs=None):
self.max = len(self.model.layers)
print("Ending Epoch")
if logs['val_accuracy'] > self.accuracy:
self.accuracy = logs['val_accuracy']
self.accuracyCount = 0
else:
self.accuracyCount+=1
if self.accuracyCount >= 10 * (len(self.epochCount)+1):
self.epochCount.append(epoch)
print('Adding train layers')
self.accuracyCount = 0
self.trainableCount += 10
if self.trainableCount >= self.max:
self.trainableCount = self.max
for layer in self.model.layers:
layer.trainable = False
for layer in self.model.layers[-self.trainableCount:]:
layer.trainable = True
self.model.compile(keras.optimizers.Adam(lr=lr_rate), loss='categorical_crossentropy', metrics=['accuracy'])
print(self.epochCount)
if __name__ == "__main__":
timestr = time.strftime("%Y%m%d-%H%M%S")
model = MakeModel(1024)
# model = resnet()
# model = clearWeights(model)
model.compile(keras.optimizers.Adam(lr=lr_rate), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
TrainPath = 'D:/Autism-Data/Kaggle/v' + str(Version) + '/train'
ValidPath = 'D:/Autism-Data/Kaggle/v' + str(Version) + '/valid'
TestPath = 'D:/Autism-Data/Kaggle/v' + str(Version) + '/test'
TrainGen = keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=preprocess_input_new,
horizontal_flip=True,
rotation_range=45,
width_shift_range=.01,
height_shift_range=.01).flow_from_directory(
TrainPath,
target_size=(Height, Width),
batch_size=BatchSize)
ValidGen = keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=preprocess_input_new).flow_from_directory(
ValidPath,
target_size=(Height, Width),
batch_size=BatchSize,
shuffle=False)
TestGen = keras.preprocessing.image.ImageDataGenerator(
preprocessing_function=preprocess_input_new).flow_from_directory(
TestPath,
target_size=(Height, Width),
batch_size=BatchSize,
shuffle=False)
os.makedirs("models/h5/" + str(timestr), exist_ok=True)
filepath = "models/h5/" + str(timestr) + "/" + "weights-improvement-{epoch:02d}-{val_accuracy:.4f}.hdf5"
SaveModelImage(model, "models/h5/" + str(timestr) + "/" + "Graph.png")
copyfile('face.py', "models/h5/" + str(timestr) + "/face.py")
checkpoint = keras.callbacks.callbacks.ModelCheckpoint(filepath, monitor='val_accuracy', verbose=1, save_best_only=True, mode='max')
reduce_lr = keras.callbacks.callbacks.ReduceLROnPlateau(monitor='val_accuracy', factor=0.9, patience=5, min_lr=0.00001)
ModelCallbacks = keras.callbacks.callbacks.LambdaCallback(
on_epoch_begin=None,
on_epoch_end=None,
on_batch_begin=None,
on_batch_end=None,
on_train_begin=None,
on_train_end=None)
first = 5
if not load_model:
# data = model.fit_generator(
# generator = TrainGen,
# validation_data= ValidGen,
# epochs=first,
# callbacks=[ModelCallbacks, reduce_lr, checkpoint],
# verbose=1)
models = [model]
args = [{'generator':TrainGen,
'validation_data':TestGen,
'epochs':first,
'callbacks':[ModelCallbacks, reduce_lr, EarlyStoppingAtMinLoss()],
'verbose':1}]
ml = MLEXPS()
ml.setTopic('Autism')
ml.setCopyFileList(['face.py'])
ml.setModels(models)
ml.setArgList(args)
ml.generator = True
ml.saveBestOnly = False
ml.startExprQ()
else:
model = load_model(model_path)
Y_pred = model.predict_generator(TestGen)
y_pred = np.argmax(Y_pred, axis=1)
print('Confusion Matrix')
print(confusion_matrix(TestGen.classes, y_pred))
print('Classification Report')
target_names = ['Autistic', 'Non_Autistic']
print(classification_report(TestGen.classes, y_pred, target_names=target_names))