-
Notifications
You must be signed in to change notification settings - Fork 6
/
InverseFaceNetEncoder.py
171 lines (138 loc) · 7.42 KB
/
InverseFaceNetEncoder.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
from __future__ import absolute_import, division, print_function, unicode_literals
import tensorflow as tf
from keras import backend as K
import numpy as np
from FaceNet3D import FaceNet3D as Helpers
from SemanticCodeVector import SemanticCodeVector
from keras_applications.resnet import ResNet101
tf.compat.v1.enable_eager_execution()
class InverseFaceNetEncoder(Helpers):
def __init__(self):
"""
Class initializer
"""
super().__init__()
# Model
self.model = self.build_model()
# Print a model summary
# self.model.summary()
# Custom Loss Function
self.loss_func = self.model_loss()
self.data = SemanticCodeVector()
self.shape_std, self.color_std, self.expression_std = self.data.get_bases_std()
self.scale_shape = self.shape_dim / np.sum(self.shape_std)
self.scale_color = self.color_dim / np.sum(self.color_std)
self.scale_expression = self.expression_dim / np.sum(self.expression_std)
self.early = tf.keras.callbacks.EarlyStopping(monitor='loss', min_delta=0, patience=2, verbose=1,
mode='min', baseline=None, restore_best_weights=True)
def build_model(self):
"""
Create a Keras model
:return: Keras.model()
"""
base_model = tf.keras.applications.resnet50.ResNet50(include_top=False,
weights='imagenet',
input_tensor=None,
# input_shape=self.IMG_SHAPE,
pooling='avg')
# base_model = ResNet101(input_tensor=None,
# include_top=False,
# weights='imagenet',
# backend=tf.keras.backend,
# layers=tf.keras.layers,
# models=tf.keras.models,
# utils=tf.keras.utils,
# input_shape=self.IMG_SHAPE,
# pooling='avg')
# base_model = tf.keras.applications.xception.Xception(include_top=False,
# weights='imagenet',
# input_tensor=None,
# input_shape=self.IMG_SHAPE,
# pooling='avg')
# base_model = tf.keras.applications.inception_resnet_v2.InceptionResNetV2(include_top=False,
# weights='imagenet',
# input_tensor=None,
# input_shape=self.IMG_SHAPE,
# pooling='avg')
# base_model = tf.keras.applications.inception_v3.InceptionV3(include_top=False,
# weights='imagenet',
# input_tensor=None,
# input_shape=self.IMG_SHAPE,
# pooling='avg')
base_model.trainable = True
# base_model.summary()
weights_init = tf.keras.initializers.RandomNormal(mean=0.0, stddev=0.01, seed=None)
# Create prediction layer
prediction_layer = tf.keras.layers.Dense(self.scv_length, activation=None, use_bias=True,
kernel_initializer=weights_init, bias_initializer='zeros')
# Stack model layers
model_o = tf.keras.Sequential([
base_model,
prediction_layer
])
model_o.summary()
"""
Model: "sequential"
_________________________________________________________________
Layer (type) Output Shape Param #
== == == == == == == == == == == == == == == == == == == == == ==
resnet50(Model) (None, 2048) 23587712
_________________________________________________________________
dense(Dense) (None, 231) 473319
== == == == == == == == == == == == == == == == == == == == == ==
Total params: 24,061,031
Trainable params: 24,007,911
Non - trainable params: 53,120 (from resnet50)
_________________________________________________________________
"""
return model_o
def model_space_parameter_loss(self, y):
"""
Custom loss function that incorporates weights for each variable in the
Semantic Code Vector
:param y: Tensor, y_pred - y_true with shape (Batch_size, 257)
:return: Float32, mean loss
"""
std_shape = tf.constant(self.shape_std, dtype=tf.float32)
std_shape = tf.compat.v1.reshape(std_shape, shape=(self.shape_dim,))
# weight
shape = tf.math.scalar_mul(self.scale_shape, std_shape, name='shape_std')
std_expression = tf.constant(self.expression_std, dtype=tf.float32)
std_expression = tf.compat.v1.reshape(std_expression, shape=(self.expression_dim,))
# weight
expression = tf.math.scalar_mul(self.scale_expression, std_expression, name='expression_std')
std_color = tf.constant(self.color_std, dtype=tf.float32)
std_color = tf.compat.v1.reshape(std_color, shape=(self.color_dim,))
# weight
color = tf.math.scalar_mul(self.scale_color, std_color, name='color_std')
rotation = tf.constant(1, shape=(1,), dtype=tf.float32)
rotation = K.tile(rotation, self.rotation_dim)
sigma = tf.compat.v1.concat([shape, expression, color, rotation],
axis=0)
alpha = tf.math.multiply(sigma, y)
beta = K.mean(alpha)
return beta
def model_loss(self):
"""
Wrapper function which calculates auxiliary values for the complete loss function.
Returns a *function* which calculates the complete loss given only the input and target output
"""
# Model space parameter loss
model_space_loss = self.model_space_parameter_loss
def custom_loss(y_true, y_pred):
# with tf.device('/device:GPU:1'):
y = K.square(y_pred - y_true)
# Model Space Parameter Loss
# with tf.device('/device:CPU:0'):
model_loss = model_space_loss(y)
return model_loss
return custom_loss
def compile(self):
"""
Compiles the Keras model. Includes metrics to differentiate between the two main loss terms
"""
self.model.compile(optimizer=tf.keras.optimizers.Adadelta(lr=self.BASE_LEARNING_RATE,
rho=0.95, epsilon=None, decay=self.WEIGHT_DECAY),
loss=self.loss_func,
metrics=[tf.keras.losses.mean_squared_error, tf.keras.losses.mean_absolute_error])
print('Model Compiled!')