forked from OeslleLucena/FASNet
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathFASNet.py
282 lines (214 loc) · 9.35 KB
/
FASNet.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
# coding: utf-8
# In[ ]:
import os, time
import h5py
import numpy as np
from keras.preprocessing.image import ImageDataGenerator,load_img,img_to_array
from keras import optimizers
from keras.models import Sequential
from keras.layers import Convolution2D, MaxPooling2D, ZeroPadding2D
from keras.layers import Activation, Dropout, Flatten, Dense
from keras import callbacks
from keras import backend as K
os.environ["CUDA_DEVICE_ORDER"]="PCI_BUS_ID"
os.environ["CUDA_VISIBLE_DEVICES"]="0"
K.set_image_dim_ordering('th')
# # Train
# In[ ]:
# path to the model weights files.
weights_path = './weights/REPLAY-ftweights18.h5'
top_model_weights_path = './model'
# dimensions of images. (less than 224x 224)
img_width, img_height = 96,96
# number of layers to freeze
nFreeze = 0
train_data_dir = './train'
validation_data_dir = './val'
nb_train_samples = 10524
nb_validation_samples = 2090
nb_epoch = 10
def get_tr_vgg_model(weights_path, img_width, img_height):
# build the VGG16 network
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
assert os.path.exists(weights_path), 'Model weights not found (see "weights_path" variable in script).'
if weights_path:
model.load_weights(weights_path, by_name=True)
print('Model loaded.')
return model
#print ('Model loaded.')
else:
print('model not loaded...')
def add_top_layers(model):
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
# add the model on top of the convolutional base
model.add(top_model)
return model
def run_train(model):
start_time = time.time()
# freeze layers
for layer in model.layers[:nFreeze]:
layer.trainable = False
# compile model
model.compile(loss='binary_crossentropy',
optimizer=optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=1e-6),
metrics=['accuracy'])
print ('Model Compiled.')
train_datagen = ImageDataGenerator(
rescale=1./255,
rotation_range=40,
width_shift_range=0.2,
height_shift_range=0.2,
shear_range=0.2,
zoom_range=0.2,
horizontal_flip=True,
vertical_flip = True,
fill_mode='nearest')
test_datagen = ImageDataGenerator(rescale=1./255)
train_generator = train_datagen.flow_from_directory(
train_data_dir,
target_size=(img_height, img_width),
batch_size=256,
class_mode='binary')
validation_generator = test_datagen.flow_from_directory(
validation_data_dir,
target_size=(img_height, img_width),
batch_size=256,
class_mode='binary')
print ('Fine-tuning top layers...')
earlyStopping = callbacks.EarlyStopping(monitor='val_acc',
patience=10,
verbose=0, mode='auto')
#fit model
model.fit_generator(
train_generator,
callbacks=[earlyStopping],
samples_per_epoch=nb_train_samples,
steps_per_epoch=nb_train_samples/256,
nb_epoch=nb_epoch,
validation_data=validation_generator,
nb_val_samples=nb_validation_samples)
#model.save_weights(top_model_weights_path)
model_structure = model.to_json()
open('model.json','w').write(model_structure)
model.save_weights('model.h5',overwrite=True)
print ('Done fine-tuning, have a nice day!')
print("Execution time %s seconds" % (time.time() - start_time))
# In[ ]:
if __name__ == "__main__":
vgg16_tr_model = get_tr_vgg_model(weights_path, img_width, img_height)
vgg16_tr_model = add_top_layers(vgg16_tr_model)
# fine-tuning the model
run_train(vgg16_tr_model)
# # Test
# In[ ]:
def load_model(weightsPath,img_width,img_height):
#VGG-16 model
model = Sequential()
model.add(ZeroPadding2D((1, 1), input_shape=(3, img_width, img_height)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(64, 3, 3, activation='relu', name='conv1_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(128, 3, 3, activation='relu', name='conv2_2'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(256, 3, 3, activation='relu', name='conv3_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv4_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_1'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_2'))
model.add(ZeroPadding2D((1, 1)))
model.add(Convolution2D(512, 3, 3, activation='relu', name='conv5_3'))
model.add(MaxPooling2D((2, 2), strides=(2, 2)))
# Top-model for anti-spoofing
top_model = Sequential()
top_model.add(Flatten(input_shape=model.output_shape[1:]))
top_model.add(Dense(256, activation='relu'))
top_model.add(Dropout(0.5))
top_model.add(Dense(1, activation='sigmoid'))
#
model.add(top_model)
if weightsPath:
model.load_weights(weightsPath)
else:
print ('Could not load model!')
return model
def read_preprocess_image(imgPath,img_width,img_height):
img = load_img(imgPath,target_size=(img_width,img_height))
imgArray = img_to_array(img)
imgArray = imgArray.reshape(1,3,img_width, img_height)
imgArray = imgArray/float(255)
return imgArray
# In[ ]:
if __name__ == "__main__":
# load Parameters
imgPath = './11010219830125307X_20160913160757097826.png'
test_model_path = './model.json'
ori_model_path = 'weights/REPLAY-ftweights18.h5'
img_width,img_height = 96,96
# read and Pre-processing image
img = read_preprocess_image(imgPath,img_width,img_height)
# load weights
ori_model = load_model(ori_model_path,img_width,img_height)
model_path_json = 'model.json'
model_path_h5 = 'model.h5'
with open(model_path_json) as file_constant:
model = model_from_json(file_constant.read())
model.load_weights(model_path_h5)
# predict Class
opt = optimizers.Adam(lr=0.0001, beta_1=0.9, beta_2=0.999, epsilon=1e-08, decay=1e-6)
ori_model.compile(loss='binary_crossentropy',
optimizer=opt,
metrics=['accuracy'])
outLabel = int(ori_model.predict_classes(img,verbose=0))
print outLabel