-
Notifications
You must be signed in to change notification settings - Fork 7
/
utils.py
executable file
·341 lines (279 loc) · 11.7 KB
/
utils.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
# coding=utf-8
import numpy as np
import math
from tensorflow.python.eager import context
from tensorflow.python.framework import constant_op
from tensorflow.python.framework import dtypes
from tensorflow.python.framework import ops
from tensorflow.python.ops import control_flow_ops
from tensorflow.python.ops import math_ops
from tensorflow.python.ops import random_ops
from tensorflow.python.util.tf_export import tf_export
def e_distance(a, b):
return np.linalg.norm(a - b)
def find_closest(matrix_list, target_matrix):
min_distance = 999999
closest_matrix = None
closest_id = None
for i, m in enumerate(matrix_list):
if len(m) > 0:
dist = e_distance(m, target_matrix)
if dist < min_distance:
closest_matrix = m
closest_id = i
min_distance = dist
return closest_matrix, closest_id
def range_to_landmark_nb(landmark_list, landmark_dict):
landmark_numbers = []
for x,y in zip(landmark_list[0:][::2], landmark_list[1:][::2]):
landmark_numbers.append(landmark_dict[int(x),int(y)])
return landmark_numbers
def filter_landmarks(landmarks, mode='all'):
if mode == 'all':
return landmarks
elif mode == 'no_outline':
nb_to_remove = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16]
elif mode == 'eyes_mouth':
nb_to_remove = list(range(0,36))
else:
raise ValueError('Wrong method to filter landmarks!')
indices_to_remove = [landmark_to_index(a) for a in nb_to_remove]
indices_flattened = [v for s in indices_to_remove for v in s]
return np.delete(landmarks, indices_flattened)
def landmark_to_index(lnd_nb):
# 0 -> 0,1
# 1 -> 2,3
# 2 -> 4,5
return [lnd_nb*2, lnd_nb*2+1]
def get_landmark_nb_dict(all_landmarks):
xy_to_landmark = {}
x_s = all_landmarks[0:][::2]
y_s = all_landmarks[1:][::2]
i_s = range(len(x_s))
for i, x, y in zip(i_s, x_s, y_s):
xy_to_landmark[int(x),int(y)] = i
return xy_to_landmark
def loadFromPts(filename):
landmarks = np.genfromtxt(filename, skip_header=3, skip_footer=1)
landmarks = landmarks - 1
return landmarks
def saveToPts(filename, landmarks):
pts = landmarks + 1
header = 'version: 1\nn_points: {}\n{{'.format(pts.shape[0])
np.savetxt(filename, pts, delimiter=' ', header=header,
footer='}', fmt='%.3f', comments='')
def bestFitRect(points, meanS, box=None):
if box is None:
box = np.array([points[:, 0].min(), points[:, 1].min(),
points[:, 0].max(), points[:, 1].max()])
boxCenter = np.array([(box[0] + box[2]) / 2, (box[1] + box[3]) / 2])
boxWidth = box[2] - box[0]
boxHeight = box[3] - box[1]
meanShapeWidth = meanS[:, 0].max() - meanS[:, 0].min()
meanShapeHeight = meanS[:, 1].max() - meanS[:, 1].min()
scaleWidth = boxWidth / meanShapeWidth
scaleHeight = boxHeight / meanShapeHeight
scale = (scaleWidth + scaleHeight) / 2
S0 = meanS * scale
S0Center = [(S0[:, 0].min() + S0[:, 0].max()) / 2,
(S0[:, 1].min() + S0[:, 1].max()) / 2]
S0 += boxCenter - S0Center
return S0
def bestFit(destination, source, returnTransform=False):
destMean = np.mean(destination, axis=0)
srcMean = np.mean(source, axis=0)
srcVec = (source - srcMean).flatten()
destVec = (destination - destMean).flatten()
a = np.dot(srcVec, destVec) / np.linalg.norm(srcVec)**2
b = 0
for i in range(destination.shape[0]):
b += srcVec[2 * i] * destVec[2 * i + 1] - \
srcVec[2 * i + 1] * destVec[2 * i]
b = b / np.linalg.norm(srcVec)**2
T = np.array([[a, b], [-b, a]])
srcMean = np.dot(srcMean, T)
if returnTransform:
return T, destMean - srcMean
else:
return np.dot(srcVec.reshape((-1, 2)), T) + destMean
def mirrorShape(shape, imgShape=None):
imgShapeTemp = np.array(imgShape)
shape2 = mirrorShapes(shape.reshape((1, -1, 2)),
imgShapeTemp.reshape((1, -1)))[0]
return shape2
def mirrorShapes(shapes, imgShapes=None):
shapes2 = shapes.copy()
for i in range(shapes.shape[0]):
if imgShapes is None:
shapes2[i, :, 0] = -shapes2[i, :, 0]
else:
shapes2[i, :, 0] = -shapes2[i, :, 0] + imgShapes[i][1]
lEyeIndU = list(range(36, 40))
lEyeIndD = [40, 41]
rEyeIndU = list(range(42, 46))
rEyeIndD = [46, 47]
lBrowInd = list(range(17, 22))
rBrowInd = list(range(22, 27))
uMouthInd = list(range(48, 55))
dMouthInd = list(range(55, 60))
uInnMouthInd = list(range(60, 65))
dInnMouthInd = list(range(65, 68))
noseInd = list(range(31, 36))
beardInd = list(range(17))
lEyeU = shapes2[i, lEyeIndU].copy()
lEyeD = shapes2[i, lEyeIndD].copy()
rEyeU = shapes2[i, rEyeIndU].copy()
rEyeD = shapes2[i, rEyeIndD].copy()
lBrow = shapes2[i, lBrowInd].copy()
rBrow = shapes2[i, rBrowInd].copy()
uMouth = shapes2[i, uMouthInd].copy()
dMouth = shapes2[i, dMouthInd].copy()
uInnMouth = shapes2[i, uInnMouthInd].copy()
dInnMouth = shapes2[i, dInnMouthInd].copy()
nose = shapes2[i, noseInd].copy()
beard = shapes2[i, beardInd].copy()
lEyeIndU.reverse()
lEyeIndD.reverse()
rEyeIndU.reverse()
rEyeIndD.reverse()
lBrowInd.reverse()
rBrowInd.reverse()
uMouthInd.reverse()
dMouthInd.reverse()
uInnMouthInd.reverse()
dInnMouthInd.reverse()
beardInd.reverse()
noseInd.reverse()
shapes2[i, rEyeIndU] = lEyeU
shapes2[i, rEyeIndD] = lEyeD
shapes2[i, lEyeIndU] = rEyeU
shapes2[i, lEyeIndD] = rEyeD
shapes2[i, rBrowInd] = lBrow
shapes2[i, lBrowInd] = rBrow
shapes2[i, uMouthInd] = uMouth
shapes2[i, dMouthInd] = dMouth
shapes2[i, uInnMouthInd] = uInnMouth
shapes2[i, dInnMouthInd] = dInnMouth
shapes2[i, noseInd] = nose
shapes2[i, beardInd] = beard
return shapes2
def cyclic_learning_rate(global_step,
learning_rate=0.01,
max_lr=0.1,
step_size=20.,
gamma=0.99994,
mode='triangular',
name=None):
"""Applies cyclic learning rate (CLR).
From the paper:
Smith, Leslie N. "Cyclical learning
rates for training neural networks." 2017.
[https://arxiv.org/pdf/1506.01186.pdf]
This method lets the learning rate cyclically
vary between reasonable boundary values
achieving improved classification accuracy and
often in fewer iterations.
This code varies the learning rate linearly between the
minimum (learning_rate) and the maximum (max_lr).
It returns the cyclic learning rate. It is computed as:
```python
cycle = floor( 1 + global_step /
( 2 * step_size ) )
x = abs( global_step / step_size – 2 * cycle + 1 )
clr = learning_rate +
( max_lr – learning_rate ) * max( 0 , 1 - x )
```
Polices:
'triangular':
Default, linearly increasing then linearly decreasing the
learning rate at each cycle.
'triangular2':
The same as the triangular policy except the learning
rate difference is cut in half at the end of each cycle.
This means the learning rate difference drops after each cycle.
'exp_range':
The learning rate varies between the minimum and maximum
boundaries and each boundary value declines by an exponential
factor of: gamma^global_step.
Example: 'triangular2' mode cyclic learning rate.
'''python
...
global_step = tf.Variable(0, trainable=False)
optimizer = tf.train.AdamOptimizer(learning_rate=
clr.cyclic_learning_rate(global_step=global_step, mode='triangular2'))
train_op = optimizer.minimize(loss_op, global_step=global_step)
...
with tf.Session() as sess:
sess.run(init)
for step in range(1, num_steps+1):
assign_op = global_step.assign(step)
sess.run(assign_op)
...
'''
Args:
global_step: A scalar `int32` or `int64` `Tensor` or a Python number.
Global step to use for the cyclic computation. Must not be negative.
learning_rate: A scalar `float32` or `float64` `Tensor` or a
Python number. The initial learning rate which is the lower bound
of the cycle (default = 0.1).
max_lr: A scalar. The maximum learning rate boundary.
step_size: A scalar. The number of iterations in half a cycle.
The paper suggests step_size = 2-8 x training iterations in epoch.
gamma: constant in 'exp_range' mode:
gamma**(global_step)
mode: one of {triangular, triangular2, exp_range}.
Default 'triangular'.
Values correspond to policies detailed above.
name: String. Optional name of the operation. Defaults to
'CyclicLearningRate'.
Returns:
A scalar `Tensor` of the same type as `learning_rate`. The cyclic
learning rate.
Raises:
ValueError: if `global_step` is not supplied.
@compatibility(eager)
When eager execution is enabled, this function returns
a function which in turn returns the decayed learning
rate Tensor. This can be useful for changing the learning
rate value across different invocations of optimizer functions.
@end_compatibility
"""
if global_step is None:
raise ValueError(
"global_step is required for cyclic_learning_rate.")
with ops.name_scope(name, "CyclicLearningRate",
[learning_rate, global_step]) as name:
learning_rate = ops.convert_to_tensor(
learning_rate, name="learning_rate")
dtype = learning_rate.dtype
global_step = math_ops.cast(global_step, dtype)
step_size = math_ops.cast(step_size, dtype)
def cyclic_lr():
"""Helper to recompute learning rate; most helpful in eager-mode."""
# computing: cycle = floor( 1 + global_step / ( 2 * step_size ) )
double_step = math_ops.multiply(2., step_size)
global_div_double_step = math_ops.divide(
global_step, double_step)
cycle = math_ops.floor(
math_ops.add(
1., global_div_double_step))
# computing: x = abs( global_step / step_size – 2 * cycle + 1 )
double_cycle = math_ops.multiply(2., cycle)
global_div_step = math_ops.divide(global_step, step_size)
tmp = math_ops.subtract(global_div_step, double_cycle)
x = math_ops.abs(math_ops.add(1., tmp))
# computing: clr = learning_rate + ( max_lr – learning_rate ) *
# max( 0, 1 - x )
a1 = math_ops.maximum(0., math_ops.subtract(1., x))
a2 = math_ops.subtract(max_lr, learning_rate)
clr = math_ops.multiply(a1, a2)
if mode == 'triangular2':
clr = math_ops.divide(clr, math_ops.cast(math_ops.pow(2, math_ops.cast(
cycle - 1, tf.int32)), tf.float32))
if mode == 'exp_range':
clr = math_ops.multiply(
math_ops.pow(gamma, global_step), clr)
return math_ops.add(clr, learning_rate, name=name)
if not context.executing_eagerly():
cyclic_lr = cyclic_lr()
return cyclic_lr