-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeepface.py
More file actions
109 lines (92 loc) · 3.28 KB
/
deepface.py
File metadata and controls
109 lines (92 loc) · 3.28 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
from os import path
from tensorflow import keras
IMAGE_SIZE = (152, 152)
CHANNELS = 3
NUM_CLASSES = 8631
LEARN_RATE = 0.01
MOMENTUM = 0.9
def create_deepface(
image_size=IMAGE_SIZE,
channels=CHANNELS,
num_classes=NUM_CLASSES,
learn_rate=LEARN_RATE,
momentum=MOMENTUM,
):
"""
Deep CNN architecture primarily for Face Recognition,
Face Verification and Face Representation (feature extraction) purposes
"DeepFace: Closing the Gap to Human-Level Performance in Face Verification"
CNN architecture proposed by Taigman et al. (CVPR 2014)
"""
wt_init = keras.initializers.RandomNormal(mean=0, stddev=0.01)
bias_init = keras.initializers.Constant(value=0.5)
"""
Construct certain functions
for using some common parameters
with network layers
"""
def conv2d_layer(**args):
return keras.layers.Conv2D(
**args,
kernel_initializer=wt_init,
bias_initializer=bias_init,
activation=keras.activations.relu
)
def lc2d_layer(**args):
return keras.layers.LocallyConnected2D(
**args,
kernel_initializer=wt_init,
bias_initializer=bias_init,
activation=keras.activations.relu
)
def dense_layer(**args):
return keras.layers.Dense(
**args, kernel_initializer=wt_init, bias_initializer=bias_init
)
"""
Create the network using
tf.keras.layers.Layer(s)
"""
deepface = keras.models.Sequential(
[
keras.layers.InputLayer(input_shape=(*image_size, channels), name="I0"),
conv2d_layer(filters=32, kernel_size=11, name="C1"),
keras.layers.MaxPooling2D(
pool_size=3, strides=2, padding="same", name="M2"
),
conv2d_layer(filters=16, kernel_size=9, name="C3"),
lc2d_layer(filters=16, kernel_size=9, name="L4"),
lc2d_layer(filters=16, kernel_size=7, strides=2, name="L5"),
lc2d_layer(filters=16, kernel_size=5, name="L6"),
keras.layers.Flatten(name="F0"),
dense_layer(units=4096, activation=keras.activations.relu, name="F7"),
keras.layers.Dropout(rate=0.5, name="D0"),
dense_layer(
units=num_classes, activation=keras.activations.softmax, name="F8"
),
],
name="DeepFace",
)
deepface.summary()
"""
A tf.keras.optimizers.SGD will
be used for training,
and compile the model
"""
sgd_opt = keras.optimizers.SGD(lr=learn_rate, momentum=momentum)
cce_loss = keras.losses.categorical_crossentropy
deepface.compile(optimizer=sgd_opt, loss=cce_loss, metrics=["accuracy"])
return deepface
DOWNLOAD_PATH = "https://github.com/swghosh/DeepFace/releases/download/weights-vggface2-2d-aligned/VGGFace2_DeepFace_weights_val-0.9034.h5.zip"
MD5_HASH = "0b21fb70cd6901c96c19ac14c9ea8b89"
def get_weights():
filename = "deepface.zip"
downloaded_file_path = keras.utils.get_file(
filename, DOWNLOAD_PATH, md5_hash=MD5_HASH, extract=True
)
downloaded_h5_file = path.join(
path.dirname(downloaded_file_path), path.basename(DOWNLOAD_PATH).rstrip(".zip")
)
return downloaded_h5_file
if __name__ == "__main__":
print("load deepface")