-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathinsight_face_models.py
1462 lines (1251 loc) · 71.6 KB
/
insight_face_models.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
import numpy as np
import tensorflow as tf
from tensorflow import keras
import tensorflow.keras.backend as K
def print_buildin_models():
print(
"""
>>>> buildin_models
MXNet version resnet: mobilenet_m1, r18, r34, r50, r100, r101, se_r34, se_r50, se_r100
Keras application: mobilenet, mobilenetv2, resnet50, resnet50v2, resnet101, resnet101v2, resnet152, resnet152v2
EfficientNet: efficientnetb[0-7], efficientnetl2, efficientnetv2b[1-3], efficientnetv2[t, s, m, l, xl]
Custom 1: ghostnet, mobilefacenet, mobilenetv3_small, mobilenetv3_large, se_mobilefacenet, se_resnext, vargface
Or other names from keras.applications like DenseNet121 / InceptionV3 / NASNetMobile / VGG19.
""",
end="",
)
def __init_model_from_name__(name, input_shape=(112, 112, 3), weights="imagenet", **kwargs):
name_lower = name.lower()
""" Basic model """
if name_lower == "mobilenet":
xx = keras.applications.MobileNet(input_shape=input_shape, include_top=False, weights=weights, **kwargs)
elif name_lower == "mobilenet_m1":
from backbones import mobilenet_m1
xx = mobilenet_m1.MobileNet(input_shape=input_shape, include_top=False, weights=None, **kwargs)
elif name_lower == "mobilenetv2":
xx = keras.applications.MobileNetV2(input_shape=input_shape, include_top=False, weights=weights, **kwargs)
elif "r18" in name_lower or "r34" in name_lower or "r50" in name_lower or "r100" in name_lower or "r101" in name_lower:
from backbones import resnet # MXNet insightface version resnet
use_se = True if name_lower.startswith("se_") else False
model_name = "ResNet" + name_lower[4:] if use_se else "ResNet" + name_lower[1:]
use_se = kwargs.pop("use_se", use_se)
model_class = getattr(resnet, model_name)
xx = model_class(input_shape=input_shape, classes=0, use_se=use_se, model_name=model_name, **kwargs)
elif name_lower.startswith("resnet"): # keras.applications.ResNetxxx
if name_lower.endswith("v2"):
model_name = "ResNet" + name_lower[len("resnet") : -2] + "V2"
else:
model_name = "ResNet" + name_lower[len("resnet") :]
model_class = getattr(keras.applications, model_name)
xx = model_class(weights=weights, include_top=False, input_shape=input_shape, **kwargs)
elif name_lower.startswith("efficientnetv2"):
import keras_efficientnet_v2
model_name = "EfficientNetV2" + name_lower[len("EfficientNetV2") :].upper()
model_class = getattr(keras_efficientnet_v2, model_name)
xx = model_class(pretrained=weights, num_classes=0, input_shape=input_shape, **kwargs)
elif name_lower.startswith("efficientnet"):
import keras_efficientnet_v2
model_name = "EfficientNetV1" + name_lower[-2:].upper()
model_class = getattr(keras_efficientnet_v2, model_name)
xx = model_class(pretrained=weights, num_classes=0, input_shape=input_shape, **kwargs)
elif name_lower.startswith("se_resnext"):
from keras_squeeze_excite_network import se_resnext
if name_lower.endswith("101"): # se_resnext101
depth = [3, 4, 23, 3]
else: # se_resnext50
depth = [3, 4, 6, 3]
xx = se_resnext.SEResNextImageNet(weights=weights, input_shape=input_shape, include_top=False, depth=depth)
elif name_lower.startswith("mobilenetv3"):
model_class = keras.applications.MobileNetV3Small if "small" in name_lower else keras.applications.MobileNetV3Large
xx = model_class(input_shape=input_shape, include_top=False, weights=weights, include_preprocessing=False)
elif "mobilefacenet" in name_lower or "mobile_facenet" in name_lower:
from backbones import mobile_facenet
use_se = True if "se" in name_lower else False
xx = mobile_facenet.MobileFaceNet(input_shape=input_shape, include_top=False, name=name, use_se=use_se)
elif name_lower == "ghostnet":
from backbones import ghost_model
xx = ghost_model.GhostNet(input_shape=input_shape, include_top=False, width=1.3, **kwargs)
elif name_lower == "vargface":
from backbones import vargface
xx = vargface.VargFace(input_shape=input_shape, **kwargs)
elif hasattr(keras.applications, name):
model_class = getattr(keras.applications, name)
xx = model_class(weights=weights, include_top=False, input_shape=input_shape, **kwargs)
else:
return None
xx.trainable = True
return xx
# MXNET: bn_momentum=0.9, bn_epsilon=2e-5, TF default: bn_momentum=0.99, bn_epsilon=0.001, PyTorch default: momentum=0.1, eps=1e-05
# MXNET: use_bias=True, scale=False, cavaface.pytorch: use_bias=False, scale=True
def buildin_models(
stem_model,
dropout=1,
emb_shape=512,
input_shape=(112, 112, 3),
output_layer="GDC",
bn_momentum=0.99,
bn_epsilon=0.001,
add_pointwise_conv=False,
pointwise_conv_act="relu",
use_bias=False,
scale=True,
weights="imagenet",
**kwargs
):
if isinstance(stem_model, str):
xx = __init_model_from_name__(stem_model, input_shape, weights, **kwargs)
name = stem_model
else:
name = stem_model.name
xx = stem_model
if bn_momentum != 0.99 or bn_epsilon != 0.001:
print(">>>> Change BatchNormalization momentum and epsilon default value.")
for ii in xx.layers:
if isinstance(ii, keras.layers.BatchNormalization):
ii.momentum, ii.epsilon = bn_momentum, bn_epsilon
xx = keras.models.clone_model(xx)
inputs = xx.inputs[0]
nn = xx.outputs[0]
if add_pointwise_conv: # Model using `pointwise_conv + GDC` / `pointwise_conv + E` is smaller than `E`
filters = nn.shape[-1] // 2 if add_pointwise_conv == -1 else 512 # Compitable with previous models...
nn = keras.layers.Conv2D(filters, 1, use_bias=False, padding="valid", name="pw_conv")(nn)
# nn = keras.layers.Conv2D(nn.shape[-1] // 2, 1, use_bias=False, padding="valid", name="pw_conv")(nn)
nn = keras.layers.BatchNormalization(momentum=bn_momentum, epsilon=bn_epsilon, name="pw_bn")(nn)
if pointwise_conv_act.lower() == "prelu":
nn = keras.layers.PReLU(shared_axes=[1, 2], name="pw_" + pointwise_conv_act)(nn)
else:
nn = keras.layers.Activation(pointwise_conv_act, name="pw_" + pointwise_conv_act)(nn)
if output_layer == "E":
"""Fully Connected"""
nn = keras.layers.BatchNormalization(momentum=bn_momentum, epsilon=bn_epsilon, name="E_batchnorm")(nn)
if dropout > 0 and dropout < 1:
nn = keras.layers.Dropout(dropout)(nn)
nn = keras.layers.Flatten(name="E_flatten")(nn)
nn = keras.layers.Dense(emb_shape, use_bias=use_bias, kernel_initializer="glorot_normal", name="E_dense")(nn)
elif output_layer == "GAP":
"""GlobalAveragePooling2D"""
nn = keras.layers.BatchNormalization(momentum=bn_momentum, epsilon=bn_epsilon, name="GAP_batchnorm")(nn)
nn = keras.layers.GlobalAveragePooling2D(name="GAP_pool")(nn)
if dropout > 0 and dropout < 1:
nn = keras.layers.Dropout(dropout)(nn)
nn = keras.layers.Dense(emb_shape, use_bias=use_bias, kernel_initializer="glorot_normal", name="GAP_dense")(nn)
elif output_layer == "GDC":
"""GDC"""
nn = keras.layers.DepthwiseConv2D(nn.shape[1], use_bias=False, name="GDC_dw")(nn)
# nn = keras.layers.Conv2D(nn.shape[-1], nn.shape[1], use_bias=False, padding="valid", groups=nn.shape[-1])(nn)
nn = keras.layers.BatchNormalization(momentum=bn_momentum, epsilon=bn_epsilon, name="GDC_batchnorm")(nn)
if dropout > 0 and dropout < 1:
nn = keras.layers.Dropout(dropout)(nn)
nn = keras.layers.Conv2D(emb_shape, 1, use_bias=use_bias, kernel_initializer="glorot_normal", name="GDC_conv")(nn)
nn = keras.layers.Flatten(name="GDC_flatten")(nn)
# nn = keras.layers.Dense(emb_shape, activation=None, use_bias=use_bias, kernel_initializer="glorot_normal", name="GDC_dense")(nn)
elif output_layer == "F":
"""F, E without first BatchNormalization"""
if dropout > 0 and dropout < 1:
nn = keras.layers.Dropout(dropout)(nn)
nn = keras.layers.Flatten(name="F_flatten")(nn)
nn = keras.layers.Dense(emb_shape, use_bias=use_bias, kernel_initializer="glorot_normal", name="F_dense")(nn)
# `fix_gamma=True` in MXNet means `scale=False` in Keras
embedding = keras.layers.BatchNormalization(momentum=bn_momentum, epsilon=bn_epsilon, scale=scale, name="pre_embedding")(nn)
embedding_fp32 = keras.layers.Activation("linear", dtype="float32", name="embedding")(embedding)
basic_model = keras.models.Model(inputs, embedding_fp32, name=xx.name)
return basic_model
@keras.utils.register_keras_serializable(package="keras_insightface")
class NormDense(keras.layers.Layer):
def __init__(self, units=1000, kernel_regularizer=None, loss_top_k=1, append_norm=False, partial_fc_split=0, **kwargs):
super(NormDense, self).__init__(**kwargs)
# self.init = keras.initializers.VarianceScaling(scale=2.0, mode="fan_out", distribution="truncated_normal")
self.init = keras.initializers.glorot_normal()
# self.init = keras.initializers.TruncatedNormal(mean=0, stddev=0.01)
self.units, self.loss_top_k, self.append_norm, self.partial_fc_split = units, loss_top_k, append_norm, partial_fc_split
self.kernel_regularizer = keras.regularizers.get(kernel_regularizer)
self.supports_masking = False
def build(self, input_shape):
if self.partial_fc_split > 1:
self.cur_id = self.add_weight(name="cur_id", shape=(), initializer="zeros", dtype="int64", trainable=False)
self.sub_weights = self.add_weight(
name="norm_dense_w_subs",
shape=(self.partial_fc_split, input_shape[-1], self.units * self.loss_top_k),
initializer=self.init,
trainable=True,
regularizer=self.kernel_regularizer,
)
else:
self.w = self.add_weight(
name="norm_dense_w",
shape=(input_shape[-1], self.units * self.loss_top_k),
initializer=self.init,
trainable=True,
regularizer=self.kernel_regularizer,
)
super(NormDense, self).build(input_shape)
def call(self, inputs, **kwargs):
# tf.print("tf.reduce_mean(self.w):", tf.reduce_mean(self.w))
if self.partial_fc_split > 1:
# self.sub_weights.scatter_nd_update([[(self.cur_id - 1) % self.partial_fc_split]], [self.w])
# self.w.assign(tf.gather(self.sub_weights, self.cur_id))
self.w = tf.gather(self.sub_weights, self.cur_id)
self.cur_id.assign((self.cur_id + 1) % self.partial_fc_split)
norm_w = tf.nn.l2_normalize(self.w, axis=0, epsilon=1e-5)
norm_inputs = tf.nn.l2_normalize(inputs, axis=1, epsilon=1e-5)
output = K.dot(norm_inputs, norm_w)
if self.loss_top_k > 1:
output = K.reshape(output, (-1, self.units, self.loss_top_k))
output = K.max(output, axis=2)
if self.append_norm:
# Keep norm value low by * -1, so will not affect accuracy metrics.
output = tf.concat([output, tf.norm(inputs, axis=1, keepdims=True) * -1], axis=-1)
return output
def compute_output_shape(self, input_shape):
return (input_shape[0], self.units)
def get_config(self):
config = super(NormDense, self).get_config()
config.update(
{
"units": self.units,
"loss_top_k": self.loss_top_k,
"append_norm": self.append_norm,
"partial_fc_split": self.partial_fc_split,
"kernel_regularizer": keras.regularizers.serialize(self.kernel_regularizer),
}
)
return config
@classmethod
def from_config(cls, config):
return cls(**config)
@keras.utils.register_keras_serializable(package="keras_insightface")
class NormDenseVPL(NormDense):
def __init__(self, batch_size, units=1000, kernel_regularizer=None, vpl_lambda=0.15, start_iters=8000, allowed_delta=200, **kwargs):
super().__init__(units, kernel_regularizer, **kwargs)
self.vpl_lambda, self.batch_size = vpl_lambda, batch_size # Need the actual batch_size here, for storing inputs
# self.start_iters, self.allowed_delta = 8000 * 128 // batch_size, 200 * 128 // batch_size # adjust according to batch_size
self.start_iters, self.allowed_delta = start_iters, allowed_delta
# print(">>>> [NormDenseVPL], vpl_lambda={}, start_iters={}, allowed_delta={}".format(vpl_lambda, start_iters, allowed_delta))
def build(self, input_shape):
# self.queue_features in same shape format as self.norm_features, for easier calling tf.tensor_scatter_nd_update
self.norm_features = self.add_weight(name="norm_features", shape=(self.batch_size, input_shape[-1]), dtype=self.compute_dtype, trainable=False)
self.queue_features = self.add_weight(name="queue_features", shape=(self.units, input_shape[-1]), initializer=self.init, trainable=False)
self.queue_iters = self.add_weight(name="queue_iters", shape=(self.units,), initializer="zeros", dtype="int64", trainable=False)
self.zero_queue_lambda = tf.zeros((self.units,), dtype=self.compute_dtype)
self.iters = self.add_weight(name="iters", shape=(), initializer="zeros", dtype="int64", trainable=False)
super().build(input_shape)
def call(self, inputs, **kwargs):
# tf.print("tf.reduce_mean(self.w):", tf.reduce_mean(self.w))
self.iters.assign_add(1)
queue_lambda = tf.cond(
self.iters > self.start_iters,
lambda: tf.where(self.iters - self.queue_iters <= self.allowed_delta, self.vpl_lambda, 0.0), # prepare_queue_lambda
lambda: self.zero_queue_lambda,
)
tf.print(" - vpl_sample_ratio:", tf.reduce_mean(tf.cast(queue_lambda > 0, "float32")), end="")
# self.queue_lambda = queue_lambda
if self.partial_fc_split > 1:
self.w = tf.gather(self.sub_weights, self.cur_id)
self.cur_id.assign((self.cur_id + 1) % self.partial_fc_split)
norm_w = K.l2_normalize(self.w, axis=0)
injected_weight = norm_w * (1 - queue_lambda) + tf.transpose(self.queue_features) * queue_lambda
injected_norm_weight = K.l2_normalize(injected_weight, axis=0)
# set_queue needs actual input labels, it's done in callback VPLUpdateQueue
norm_inputs = K.l2_normalize(inputs, axis=1)
self.norm_features.assign(norm_inputs)
output = K.dot(norm_inputs, injected_norm_weight)
if self.append_norm:
# Keep norm value low by * -1, so will not affect accuracy metrics.
output = tf.concat([output, tf.norm(inputs, axis=1, keepdims=True) * -1], axis=-1)
return output
def get_config(self):
config = super().get_config()
config.update({"batch_size": self.batch_size, "vpl_lambda": self.vpl_lambda})
return config
def add_l2_regularizer_2_model(model, weight_decay, custom_objects={}, apply_to_batch_normal=False, apply_to_bias=False):
# https://github.com/keras-team/keras/issues/2717#issuecomment-456254176
if 0:
regularizers_type = {}
for layer in model.layers:
rrs = [kk for kk in layer.__dict__.keys() if "regularizer" in kk and not kk.startswith("_")]
if len(rrs) != 0:
# print(layer.name, layer.__class__.__name__, rrs)
if layer.__class__.__name__ not in regularizers_type:
regularizers_type[layer.__class__.__name__] = rrs
print(regularizers_type)
for layer in model.layers:
attrs = []
if isinstance(layer, keras.layers.Dense) or isinstance(layer, keras.layers.Conv2D):
# print(">>>> Dense or Conv2D", layer.name, "use_bias:", layer.use_bias)
attrs = ["kernel_regularizer"]
if apply_to_bias and layer.use_bias:
attrs.append("bias_regularizer")
elif isinstance(layer, keras.layers.DepthwiseConv2D):
# print(">>>> DepthwiseConv2D", layer.name, "use_bias:", layer.use_bias)
attrs = ["depthwise_regularizer"]
if apply_to_bias and layer.use_bias:
attrs.append("bias_regularizer")
elif isinstance(layer, keras.layers.SeparableConv2D):
# print(">>>> SeparableConv2D", layer.name, "use_bias:", layer.use_bias)
attrs = ["pointwise_regularizer", "depthwise_regularizer"]
if apply_to_bias and layer.use_bias:
attrs.append("bias_regularizer")
elif apply_to_batch_normal and isinstance(layer, keras.layers.BatchNormalization):
# print(">>>> BatchNormalization", layer.name, "scale:", layer.scale, ", center:", layer.center)
if layer.center:
attrs.append("beta_regularizer")
if layer.scale:
attrs.append("gamma_regularizer")
elif apply_to_batch_normal and isinstance(layer, keras.layers.PReLU):
# print(">>>> PReLU", layer.name)
attrs = ["alpha_regularizer"]
for attr in attrs:
if hasattr(layer, attr) and layer.trainable:
setattr(layer, attr, keras.regularizers.L2(weight_decay / 2))
# So far, the regularizers only exist in the model config. We need to
# reload the model so that Keras adds them to each layer's losses.
# temp_weight_file = "tmp_weights.h5"
# model.save_weights(temp_weight_file)
# out_model = keras.models.model_from_json(model.to_json(), custom_objects=custom_objects)
# out_model.load_weights(temp_weight_file, by_name=True)
# os.remove(temp_weight_file)
# return out_model
return keras.models.clone_model(model)
def replace_ReLU_with_PReLU(model, target_activation="PReLU", **kwargs):
from tensorflow.keras.layers import ReLU, PReLU, Activation
def convert_ReLU(layer):
# print(layer.name)
if isinstance(layer, ReLU) or (isinstance(layer, Activation) and layer.activation == keras.activations.relu):
if target_activation == "PReLU":
layer_name = layer.name.replace("_relu", "_prelu")
print(">>>> Convert ReLU:", layer.name, "-->", layer_name)
# Default initial value in mxnet and pytorch is 0.25
return PReLU(shared_axes=[1, 2], alpha_initializer=tf.initializers.Constant(0.25), name=layer_name, **kwargs)
elif isinstance(target_activation, str):
layer_name = layer.name.replace("_relu", "_" + target_activation)
print(">>>> Convert ReLU:", layer.name, "-->", layer_name)
return Activation(activation=target_activation, name=layer_name, **kwargs)
else:
act_class_name = target_activation.__name__
layer_name = layer.name.replace("_relu", "_" + act_class_name)
print(">>>> Convert ReLU:", layer.name, "-->", layer_name)
return target_activation(**kwargs)
return layer
input_tensors = keras.layers.Input(model.input_shape[1:])
return keras.models.clone_model(model, input_tensors=input_tensors, clone_function=convert_ReLU)
@keras.utils.register_keras_serializable(package="keras_insightface")
class AconC(keras.layers.Layer):
"""
- [Github nmaac/acon](https://github.com/nmaac/acon/blob/main/acon.py)
- [Activate or Not: Learning Customized Activation, CVPR 2021](https://arxiv.org/pdf/2009.04759.pdf)
"""
def __init__(self, p1=1, p2=0, beta=1, **kwargs):
super(AconC, self).__init__(**kwargs)
self.p1_init = tf.initializers.Constant(p1)
self.p2_init = tf.initializers.Constant(p2)
self.beta_init = tf.initializers.Constant(beta)
self.supports_masking = False
def build(self, input_shape):
self.p1 = self.add_weight(name="p1", shape=(1, 1, 1, input_shape[-1]), initializer=self.p1_init, trainable=True)
self.p2 = self.add_weight(name="p2", shape=(1, 1, 1, input_shape[-1]), initializer=self.p2_init, trainable=True)
self.beta = self.add_weight(name="beta", shape=(1, 1, 1, input_shape[-1]), initializer=self.beta_init, trainable=True)
super(AconC, self).build(input_shape)
def call(self, inputs, **kwargs):
p1 = inputs * self.p1
p2 = inputs * self.p2
beta = inputs * self.beta
return p1 * tf.nn.sigmoid(beta) + p2
def compute_output_shape(self, input_shape):
return input_shape
def get_config(self):
return super(AconC, self).get_config()
@classmethod
def from_config(cls, config):
return cls(**config)
class SAMModel(tf.keras.models.Model):
"""
Arxiv article: [Sharpness-Aware Minimization for Efficiently Improving Generalization](https://arxiv.org/pdf/2010.01412.pdf)
Implementation by: [Keras SAM (Sharpness-Aware Minimization)](https://qiita.com/T-STAR/items/8c3afe3a116a8fc08429)
Usage is same with `keras.modeols.Model`: `model = SAMModel(inputs, outputs, rho=sam_rho, name=name)`
"""
def __init__(self, *args, rho=0.05, **kwargs):
super().__init__(*args, **kwargs)
self.rho = tf.constant(rho, dtype=tf.float32)
def train_step(self, data):
if len(data) == 3:
x, y, sample_weight = data
else:
sample_weight = None
x, y = data
# 1st step
with tf.GradientTape() as tape:
y_pred = self(x, training=True)
loss = self.compiled_loss(y, y_pred, sample_weight=sample_weight, regularization_losses=self.losses)
trainable_vars = self.trainable_variables
gradients = tape.gradient(loss, trainable_vars)
norm = tf.linalg.global_norm(gradients)
scale = self.rho / (norm + 1e-12)
e_w_list = []
for v, grad in zip(trainable_vars, gradients):
e_w = grad * scale
v.assign_add(e_w)
e_w_list.append(e_w)
# 2nd step
with tf.GradientTape() as tape:
y_pred_adv = self(x, training=True)
loss_adv = self.compiled_loss(y, y_pred_adv, sample_weight=sample_weight, regularization_losses=self.losses)
gradients_adv = tape.gradient(loss_adv, trainable_vars)
for v, e_w in zip(trainable_vars, e_w_list):
v.assign_sub(e_w)
# optimize
self.optimizer.apply_gradients(zip(gradients_adv, trainable_vars))
self.compiled_metrics.update_state(y, y_pred, sample_weight=sample_weight)
return_metrics = {}
for metric in self.metrics:
result = metric.result()
if isinstance(result, dict):
return_metrics.update(result)
else:
return_metrics[metric.name] = result
return return_metrics
def replace_add_with_stochastic_depth(model, survivals=(1, 0.8)):
"""
- [Deep Networks with Stochastic Depth](https://arxiv.org/pdf/1603.09382.pdf)
- [tfa.layers.StochasticDepth](https://www.tensorflow.org/addons/api_docs/python/tfa/layers/StochasticDepth)
"""
from tensorflow_addons.layers import StochasticDepth
add_layers = [ii.name for ii in model.layers if isinstance(ii, keras.layers.Add)]
total_adds = len(add_layers)
if isinstance(survivals, float):
survivals = [survivals] * total_adds
elif isinstance(survivals, (list, tuple)) and len(survivals) == 2:
start, end = survivals
survivals = [start - (1 - end) * float(ii) / total_adds for ii in range(total_adds)]
survivals_dict = dict(zip(add_layers, survivals))
def __replace_add_with_stochastic_depth__(layer):
if isinstance(layer, keras.layers.Add):
layer_name = layer.name
new_layer_name = layer_name.replace("_add", "_stochastic_depth")
new_layer_name = layer_name.replace("add_", "stochastic_depth_")
survival_probability = survivals_dict[layer_name]
if survival_probability < 1:
print("Converting:", layer_name, "-->", new_layer_name, ", survival_probability:", survival_probability)
return StochasticDepth(survival_probability, name=new_layer_name)
else:
return layer
return layer
input_tensors = keras.layers.Input(model.input_shape[1:])
return keras.models.clone_model(model, input_tensors=input_tensors, clone_function=__replace_add_with_stochastic_depth__)
def replace_stochastic_depth_with_add(model, drop_survival=False):
from tensorflow_addons.layers import StochasticDepth
def __replace_stochastic_depth_with_add__(layer):
if isinstance(layer, StochasticDepth):
layer_name = layer.name
new_layer_name = layer_name.replace("_stochastic_depth", "_lambda")
survival = layer.survival_probability
print("Converting:", layer_name, "-->", new_layer_name, ", survival_probability:", survival)
if drop_survival or not survival < 1:
return keras.layers.Add(name=new_layer_name)
else:
return keras.layers.Lambda(lambda xx: xx[0] + xx[1] * survival, name=new_layer_name)
return layer
input_tensors = keras.layers.Input(model.input_shape[1:])
return keras.models.clone_model(model, input_tensors=input_tensors, clone_function=__replace_stochastic_depth_with_add__)
def convert_to_mixed_float16(model, convert_batch_norm=False):
policy = keras.mixed_precision.Policy("mixed_float16")
policy_config = keras.utils.serialize_keras_object(policy)
from tensorflow.keras.layers import InputLayer, Activation
from tensorflow.keras.activations import linear, softmax
def do_convert_to_mixed_float16(layer):
if not convert_batch_norm and isinstance(layer, keras.layers.BatchNormalization):
return layer
if isinstance(layer, InputLayer):
return layer
if isinstance(layer, NormDense):
return layer
if isinstance(layer, Activation) and layer.activation == softmax:
return layer
if isinstance(layer, Activation) and layer.activation == linear:
return layer
aa = layer.get_config()
aa.update({"dtype": policy_config})
bb = layer.__class__.from_config(aa)
bb.build(layer.input_shape)
bb.set_weights(layer.get_weights())
return bb
input_tensors = keras.layers.Input(model.input_shape[1:])
mm = keras.models.clone_model(model, input_tensors=input_tensors, clone_function=do_convert_to_mixed_float16)
if model.built:
mm.compile(optimizer=model.optimizer, loss=model.compiled_loss, metrics=model.compiled_metrics)
# mm.optimizer, mm.compiled_loss, mm.compiled_metrics = model.optimizer, model.compiled_loss, model.compiled_metrics
# mm.built = True
return mm
def convert_mixed_float16_to_float32(model):
from tensorflow.keras.layers import InputLayer, Activation
from tensorflow.keras.activations import linear
def do_convert_to_mixed_float16(layer):
if not isinstance(layer, InputLayer) and not (isinstance(layer, Activation) and layer.activation == linear):
aa = layer.get_config()
aa.update({"dtype": "float32"})
bb = layer.__class__.from_config(aa)
bb.build(layer.input_shape)
bb.set_weights(layer.get_weights())
return bb
return layer
input_tensors = keras.layers.Input(model.input_shape[1:])
return keras.models.clone_model(model, input_tensors=input_tensors, clone_function=do_convert_to_mixed_float16)
def convert_to_batch_renorm(model):
def do_convert_to_batch_renorm(layer):
if isinstance(layer, keras.layers.BatchNormalization):
aa = layer.get_config()
aa.update({"renorm": True, "renorm_clipping": {}, "renorm_momentum": aa["momentum"]})
bb = layer.__class__.from_config(aa)
bb.build(layer.input_shape)
bb.set_weights(layer.get_weights() + bb.get_weights()[-3:])
return bb
return layer
input_tensors = keras.layers.Input(model.input_shape[1:])
return keras.models.clone_model(model, input_tensors=input_tensors, clone_function=do_convert_to_batch_renorm)
# margin_softmax class wrapper
@keras.utils.register_keras_serializable(package="keras_insightface")
class MarginSoftmax(tf.keras.losses.Loss):
def __init__(self, power=2, scale=0.4, scale_all=1.0, from_logits=False, label_smoothing=0, **kwargs):
super(MarginSoftmax, self).__init__(**kwargs)
self.power, self.scale, self.from_logits, self.label_smoothing = power, scale, from_logits, label_smoothing
self.scale_all = scale_all
if power != 1 and scale == 0:
self.logits_reduction_func = lambda xx: xx**power
elif power == 1 and scale != 0:
self.logits_reduction_func = lambda xx: xx * scale
else:
self.logits_reduction_func = lambda xx: (xx**power + xx * scale) / 2
def call(self, y_true, y_pred):
# margin_soft = tf.where(tf.cast(y_true, dtype=tf.bool), (y_pred ** self.power + y_pred * self.scale) / 2, y_pred)
margin_soft = tf.where(tf.cast(y_true, dtype=tf.bool), self.logits_reduction_func(y_pred), y_pred) * self.scale_all
return tf.keras.losses.categorical_crossentropy(y_true, margin_soft, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
def get_config(self):
config = super(MarginSoftmax, self).get_config()
config.update(
{
"power": self.power,
"scale": self.scale,
"scale_all": self.scale_all,
"from_logits": self.from_logits,
"label_smoothing": self.label_smoothing,
}
)
return config
@classmethod
def from_config(cls, config):
return cls(**config)
# ArcfaceLoss class
@keras.utils.register_keras_serializable(package="keras_insightface")
class ArcfaceLoss(tf.keras.losses.Loss):
def __init__(self, margin1=1.0, margin2=0.5, margin3=0.0, scale=64.0, from_logits=True, label_smoothing=0, **kwargs):
# reduction = tf.keras.losses.Reduction.NONE if tf.distribute.has_strategy() else tf.keras.losses.Reduction.AUTO
# super(ArcfaceLoss, self).__init__(**kwargs, reduction=reduction)
super(ArcfaceLoss, self).__init__(**kwargs)
self.margin1, self.margin2, self.margin3, self.scale = margin1, margin2, margin3, scale
self.from_logits, self.label_smoothing = from_logits, label_smoothing
self.threshold = np.cos((np.pi - margin2) / margin1) # grad(theta) == 0
self.theta_min = (-1 - margin3) * 2
self.batch_labels_back_up = None
# self.reduction_func = tf.keras.losses.CategoricalCrossentropy(
# from_logits=from_logits, label_smoothing=label_smoothing, reduction=reduction
# )
# self.norm_logits = tf.Variable(tf.zeros([512, 93431]), dtype="float32", trainable=False)
# self.y_true = tf.Variable(tf.zeros([512, 93431], dtype="int32"), dtype="int32", trainable=False)
def build(self, batch_size):
self.batch_labels_back_up = tf.Variable(tf.zeros([batch_size], dtype="int64"), dtype="int64", trainable=False)
def call(self, y_true, norm_logits):
if self.batch_labels_back_up is not None:
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
# norm_logits = y_pred
# self.norm_logits.assign(norm_logits)
# self.y_true.assign(y_true)
# pick_cond = tf.cast(y_true, dtype=tf.bool)
# y_pred_vals = norm_logits[pick_cond]
pick_cond = tf.where(y_true != 0)
y_pred_vals = tf.gather_nd(norm_logits, pick_cond)
# tf.print(", y_true.sum:", tf.reduce_sum(y_true), ", y_pred_vals.shape:", K.shape(y_pred_vals), ", y_true.shape:", K.shape(y_true), end="")
# tf.assert_equal(tf.reduce_sum(y_true), K.shape(y_true)[0])
# tf.assert_equal(K.shape(y_pred_vals)[0], K.shape(y_true)[0])
# y_pred_vals = tf.clip_by_value(y_pred_vals, -1, 1)
if self.margin1 == 1.0 and self.margin2 == 0.0 and self.margin3 == 0.0:
theta = y_pred_vals
elif self.margin1 == 1.0 and self.margin3 == 0.0:
theta = tf.cos(tf.acos(y_pred_vals) + self.margin2)
else:
theta = tf.cos(tf.acos(y_pred_vals) * self.margin1 + self.margin2) - self.margin3
# Grad(theta) == 0
# ==> np.sin(np.math.acos(xx) * margin1 + margin2) == 0
# ==> np.math.acos(xx) * margin1 + margin2 == np.pi
# ==> xx == np.cos((np.pi - margin2) / margin1)
# ==> theta_min == theta(xx) == -1 - margin3
theta_valid = tf.where(y_pred_vals > self.threshold, theta, self.theta_min - theta)
# theta_one_hot = tf.expand_dims(theta_valid - y_pred_vals, 1) * tf.cast(y_true, dtype=tf.float32)
# arcface_logits = (theta_one_hot + norm_logits) * self.scale
# theta_one_hot = tf.expand_dims(theta_valid, 1) * tf.cast(y_true, dtype=tf.float32)
# arcface_logits = tf.where(pick_cond, theta_one_hot, norm_logits) * self.scale
arcface_logits = tf.tensor_scatter_nd_update(norm_logits, pick_cond, theta_valid) * self.scale
# tf.assert_equal(tf.math.is_nan(tf.reduce_mean(arcface_logits)), False)
# arcface_logits = tf.cond(tf.math.is_finite(tf.reduce_mean(arcface_logits)), lambda: arcface_logits, lambda: tf.cast(y_true, "float32"))
# arcface_logits = tf.where(tf.math.is_finite(arcface_logits), arcface_logits, tf.zeros_like(arcface_logits))
# cond = tf.repeat(tf.math.is_finite(tf.reduce_sum(arcface_logits, axis=-1, keepdims=True)), arcface_logits.shape[-1], axis=-1)
# arcface_logits = tf.where(cond, arcface_logits, tf.zeros_like(arcface_logits))
return tf.keras.losses.categorical_crossentropy(y_true, arcface_logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
# return self.reduction_func(y_true, arcface_logits)
def get_config(self):
config = super(ArcfaceLoss, self).get_config()
config.update(
{
"margin1": self.margin1,
"margin2": self.margin2,
"margin3": self.margin3,
"scale": self.scale,
"from_logits": self.from_logits,
"label_smoothing": self.label_smoothing,
}
)
return config
@classmethod
def from_config(cls, config):
return cls(**config)
# ArcfaceLoss simple
@keras.utils.register_keras_serializable(package="keras_insightface")
class ArcfaceLossSimple(tf.keras.losses.Loss):
def __init__(self, margin=0.5, scale=64.0, from_logits=True, label_smoothing=0, **kwargs):
super(ArcfaceLossSimple, self).__init__(**kwargs)
self.margin, self.scale, self.from_logits, self.label_smoothing = margin, scale, from_logits, label_smoothing
self.margin_cos, self.margin_sin = tf.cos(margin), tf.sin(margin)
self.threshold = tf.cos(np.pi - margin)
# self.low_pred_punish = tf.sin(np.pi - margin) * margin
self.theta_min = -2
self.batch_labels_back_up = None
def build(self, batch_size):
self.batch_labels_back_up = tf.Variable(tf.zeros([batch_size], dtype="int64"), dtype="int64", trainable=False)
def call(self, y_true, norm_logits):
if self.batch_labels_back_up is not None:
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
pick_cond = tf.where(y_true != 0)
y_pred_vals = tf.gather_nd(norm_logits, pick_cond)
theta = y_pred_vals * self.margin_cos - tf.sqrt(1 - tf.pow(y_pred_vals, 2)) * self.margin_sin
theta_valid = tf.where(y_pred_vals > self.threshold, theta, self.theta_min - theta)
arcface_logits = tf.tensor_scatter_nd_update(norm_logits, pick_cond, theta_valid) * self.scale
return tf.keras.losses.categorical_crossentropy(y_true, arcface_logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
def get_config(self):
config = super(ArcfaceLossSimple, self).get_config()
config.update(
{
"margin": self.margin,
"scale": self.scale,
"from_logits": self.from_logits,
"label_smoothing": self.label_smoothing,
}
)
return config
@classmethod
def from_config(cls, config):
return cls(**config)
# [CurricularFace: Adaptive Curriculum Learning Loss for Deep Face Recognition](https://arxiv.org/pdf/2004.00288.pdf)
@keras.utils.register_keras_serializable(package="keras_insightface")
class CurricularFaceLoss(ArcfaceLossSimple):
def __init__(self, margin=0.5, scale=64.0, from_logits=True, label_smoothing=0, hard_scale=0, **kwargs):
super(CurricularFaceLoss, self).__init__(margin, scale, from_logits, label_smoothing, **kwargs)
self.hard_scale = tf.Variable(hard_scale, dtype="float32", trainable=False)
def call(self, y_true, norm_logits):
if self.batch_labels_back_up is not None:
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
pick_cond = tf.where(y_true != 0)
y_pred_vals = tf.gather_nd(norm_logits, pick_cond)
theta = y_pred_vals * self.margin_cos - tf.sqrt(1 - tf.pow(y_pred_vals, 2)) * self.margin_sin
theta_valid = tf.where(y_pred_vals > self.threshold, theta, self.theta_min - theta)
self.hard_scale.assign(tf.reduce_mean(y_pred_vals) * 0.01 + (1 - 0.01) * self.hard_scale)
tf.print(", hard_scale:", self.hard_scale, end="")
hard_norm_logits = tf.where(norm_logits > tf.expand_dims(theta_valid, 1), norm_logits * (self.hard_scale + norm_logits), norm_logits)
arcface_logits = tf.tensor_scatter_nd_update(hard_norm_logits, pick_cond, theta_valid) * self.scale
return tf.keras.losses.categorical_crossentropy(y_true, arcface_logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
def get_config(self):
config = super(CurricularFaceLoss, self).get_config()
config.update({"hard_scale": K.get_value(self.hard_scale)})
return config
# [AirFace:Lightweight and Efficient Model for Face Recognition](https://arxiv.org/pdf/1907.12256.pdf)
@keras.utils.register_keras_serializable(package="keras_insightface")
class AirFaceLoss(ArcfaceLossSimple):
def __init__(self, margin=0.4, scale=64.0, from_logits=True, label_smoothing=0, **kwargs):
super(AirFaceLoss, self).__init__(margin, scale, from_logits, label_smoothing, **kwargs)
# theta = (np.pi - 2 * (tf.acos(y_pred_vals).numpy() + margin)) / np.pi
# ==> theta = 1 - tf.acos(y_pred_vals).numpy() * 2 / np.pi - 2 * margin / np.pi
# ==> theta = 1 - 2 * margin / np.pi - tf.acos(y_pred_vals).numpy() * 2 / np.pi
self.margin_head = 1 - 2 * margin / np.pi
self.margin_scale = 2 / np.pi
def call(self, y_true, norm_logits):
if self.batch_labels_back_up is not None:
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
# norm_logits = tf.acos(norm_logits) * self.margin_scale
# logits = tf.where(tf.cast(y_true, dtype=tf.bool), self.margin_head - norm_logits, 1 - norm_logits) * self.scale
pick_cond = tf.where(y_true != 0)
y_pred_vals = tf.gather_nd(norm_logits, pick_cond)
theta = self.margin_head - tf.acos(y_pred_vals) * self.margin_scale
logits = tf.tensor_scatter_nd_update(norm_logits, pick_cond, theta) * self.scale
return tf.keras.losses.categorical_crossentropy(y_true, logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
# [CosFace: Large Margin Cosine Loss for Deep Face Recognition](https://arxiv.org/pdf/1801.09414.pdf)
@keras.utils.register_keras_serializable(package="keras_insightface")
class CosFaceLoss(ArcfaceLossSimple):
def __init__(self, margin=0.35, scale=64.0, from_logits=True, label_smoothing=0, **kwargs):
super(CosFaceLoss, self).__init__(margin, scale, from_logits, label_smoothing, **kwargs)
def call(self, y_true, norm_logits):
if self.batch_labels_back_up is not None:
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
pick_cond = tf.cast(y_true, dtype=tf.bool)
logits = tf.where(pick_cond, norm_logits - self.margin, norm_logits) * self.scale
return tf.keras.losses.categorical_crossentropy(y_true, logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
# [MagFace: A Universal Representation for Face Recognition and Quality Assessment](https://arxiv.org/pdf/2103.06627.pdf)
@keras.utils.register_keras_serializable(package="keras_insightface")
class MagFaceLoss(ArcfaceLossSimple):
"""Another set for fine-tune is: min_feature_norm, max_feature_norm, min_margin, max_margin, regularizer_loss_lambda = 1, 51, 0.45, 1, 5"""
def __init__(
self,
min_feature_norm=10.0,
max_feature_norm=110.0,
min_margin=0.45,
max_margin=0.8,
scale=64.0,
regularizer_loss_lambda=35.0,
use_cosface_margin=False,
curricular_hard_scale=-1,
from_logits=True,
label_smoothing=0,
**kwargs
):
super(MagFaceLoss, self).__init__(scale=scale, from_logits=from_logits, label_smoothing=label_smoothing, **kwargs)
# l_a, u_a, lambda_g
self.min_feature_norm, self.max_feature_norm, self.regularizer_loss_lambda = min_feature_norm, max_feature_norm, regularizer_loss_lambda
# l_margin, u_margin
self.min_margin, self.max_margin = min_margin, max_margin
self.use_cosface_margin, self.curricular_hard_scale = use_cosface_margin, curricular_hard_scale
self.margin_scale = (max_margin - min_margin) / (max_feature_norm - min_feature_norm)
self.regularizer_loss_scale = 1.0 / (self.max_feature_norm**2)
self.use_curricular_scale = False
self.epislon = 1e-3
if curricular_hard_scale >= 0:
self.curricular_hard_scale = tf.Variable(curricular_hard_scale, dtype="float32", trainable=False)
self.use_curricular_scale = True
# np.set_printoptions(precision=4)
# self.precission_4 = lambda xx: tf.math.round(xx * 10000) / 10000
def call(self, y_true, norm_logits_with_norm):
if self.batch_labels_back_up is not None:
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
# feature_norm is multiplied with -1 in NormDense layer, keeping low for not affecting accuracy metrics.
norm_logits, feature_norm = norm_logits_with_norm[:, :-1], norm_logits_with_norm[:, -1] * -1
norm_logits = tf.clip_by_value(norm_logits, -1 + self.epislon, 1 - self.epislon)
feature_norm = tf.clip_by_value(feature_norm, self.min_feature_norm, self.max_feature_norm)
# margin = (self.u_margin-self.l_margin) / (self.u_a-self.l_a)*(x-self.l_a) + self.l_margin
margin = self.margin_scale * (feature_norm - self.min_feature_norm) + self.min_margin
margin = tf.expand_dims(margin, 1)
pick_cond = tf.where(y_true != 0)
y_pred_vals = tf.gather_nd(norm_logits, pick_cond)
if self.use_cosface_margin:
# Cosface process
arcface_logits = tf.where(tf.cast(y_true, dtype=tf.bool), norm_logits - margin, norm_logits) * self.scale
# theta_valid = y_pred_vals - margin
else:
# Arcface process
margin_cos, margin_sin = tf.cos(margin), tf.sin(margin)
# XLA after TF > 2.7.0 not supporting this gather_nd -> tensor_scatter_nd_update method...
# threshold = tf.cos(np.pi - margin)
# theta = y_pred_vals * margin_cos - tf.sqrt(tf.maximum(1 - tf.pow(y_pred_vals, 2), 0.0)) * margin_sin
# theta_valid = tf.where(y_pred_vals > threshold, theta, self.theta_min - theta)
# arcface_logits = tf.tensor_scatter_nd_update(norm_logits, pick_cond, theta_valid) * self.scale
arcface_logits = tf.where(
tf.cast(y_true, dtype=tf.bool),
norm_logits * margin_cos - tf.sqrt(tf.maximum(1 - tf.pow(norm_logits, 2), 0.0)) * margin_sin,
norm_logits,
)
arcface_logits = tf.minimum(arcface_logits, norm_logits) * self.scale
# if self.use_curricular_scale:
# self.curricular_hard_scale.assign(tf.reduce_mean(y_pred_vals) * 0.01 + (1 - 0.01) * self.curricular_hard_scale)
# tf.print(", hard_scale:", self.curricular_hard_scale, end="")
# norm_logits = tf.where(norm_logits > tf.expand_dims(theta_valid, 1), norm_logits * (self.curricular_hard_scale + norm_logits), norm_logits)
arcface_loss = tf.keras.losses.categorical_crossentropy(y_true, arcface_logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
# MegFace loss_G, g = 1/(self.u_a**2) * x_norm + 1/(x_norm)
regularizer_loss = self.regularizer_loss_scale * feature_norm + 1.0 / feature_norm
tf.print(
# ", regularizer_loss: ",
# tf.reduce_mean(regularizer_loss),
", arcface: ",
tf.reduce_mean(arcface_loss),
", margin: ",
tf.reduce_mean(margin),
# ", min: ",
# tf.reduce_min(margin),
# ", max: ",
# tf.reduce_max(margin),
", feature_norm: ",
tf.reduce_mean(feature_norm),
# ", min: ",
# tf.reduce_min(feature_norm),
# ", max: ",
# tf.reduce_max(feature_norm),
sep="",
end="\r",
)
return arcface_loss + regularizer_loss * self.regularizer_loss_lambda
def get_config(self):
config = super(MagFaceLoss, self).get_config()
config.update(
{
"min_feature_norm": self.min_feature_norm,
"max_feature_norm": self.max_feature_norm,
"min_margin": self.min_margin,
"max_margin": self.max_margin,
"regularizer_loss_lambda": self.regularizer_loss_lambda,
"use_cosface_margin": self.use_cosface_margin,
"curricular_hard_scale": K.get_value(self.curricular_hard_scale),
}
)
return config
# [AdaFace: Quality Adaptive Margin for Face Recognition](https://arxiv.org/pdf/2204.00964.pdf)
@keras.utils.register_keras_serializable(package="keras_insightface")
class AdaFaceLoss(ArcfaceLossSimple):
"""
margin_alpha:
- When margin_alpha=0.33, the model performs the best. For 0.22 or 0.66, the performance is still higher.
- As long as h is set such that ∥dzi∥ has some variation, margin_alpha is not very sensitive.
margin:
- The performance is best for HQ datasets when margin=0.4, for LQ datasets when margin=0.75.
- Large margin results in large angular margin variation based on the image quality, resulting in more adaptivity.
mean_std_alpha: Update pace for batch_mean and batch_std.
"""
def __init__(self, margin=0.4, margin_alpha=0.333, mean_std_alpha=0.01, scale=64.0, from_logits=True, label_smoothing=0, **kwargs):
super().__init__(scale=scale, from_logits=from_logits, label_smoothing=label_smoothing, **kwargs)
self.min_feature_norm, self.max_feature_norm, self.epislon = 0.001, 100, 1e-3
self.margin, self.margin_alpha, self.mean_std_alpha = margin, margin_alpha, mean_std_alpha
self.batch_mean = tf.Variable(20, dtype="float32", trainable=False)
self.batch_std = tf.Variable(100, dtype="float32", trainable=False)
self.cos_max_epislon = tf.acos(-1.0) - self.epislon # pi - epislon
def __to_scaled_margin__(self, feature_norm):
norm_mean = tf.math.reduce_mean(feature_norm)
samples = tf.cast(tf.maximum(1, feature_norm.shape[0] - 1), feature_norm.dtype)
norm_std = tf.sqrt(tf.math.reduce_sum((feature_norm - norm_mean) ** 2) / samples) # Torch std
self.batch_mean.assign(self.mean_std_alpha * norm_mean + (1.0 - self.mean_std_alpha) * self.batch_mean)
self.batch_std.assign(self.mean_std_alpha * norm_std + (1.0 - self.mean_std_alpha) * self.batch_std)
margin_scaler = (feature_norm - self.batch_mean) / (self.batch_std + self.epislon) # 66% between -1, 1
margin_scaler = tf.clip_by_value(margin_scaler * self.margin_alpha, -1, 1) # 68% between -0.333 ,0.333 when h:0.333
return tf.expand_dims(self.margin * margin_scaler, 1)
def call(self, y_true, norm_logits_with_norm):
if self.batch_labels_back_up is not None: # For VPL mode
self.batch_labels_back_up.assign(tf.argmax(y_true, axis=-1))
# feature_norm is multiplied with -1 in NormDense layer, keeping low for not affecting accuracy metrics.
norm_logits, feature_norm = norm_logits_with_norm[:, :-1], norm_logits_with_norm[:, -1] * -1
norm_logits = tf.clip_by_value(norm_logits, -1 + self.epislon, 1 - self.epislon)
feature_norm = tf.clip_by_value(feature_norm, self.min_feature_norm, self.max_feature_norm)
scaled_margin = tf.stop_gradient(self.__to_scaled_margin__(feature_norm))
# tf.print(", margin: ", tf.reduce_mean(scaled_margin), sep="", end="\r")
tf.print(", margin hist: ", tf.histogram_fixed_width(scaled_margin, [-self.margin, self.margin], nbins=3), sep="", end="\r")
# ex: m=0.5, h:0.333
# range
# (66% range)
# -1 -0.333 0.333 1 (margin_scaler)
# -0.5 -0.166 0.166 0.5 (m * margin_scaler)
# XLA after TF > 2.7.0 not supporting gather_nd -> tensor_scatter_nd_update method...
arcface_logits = tf.where(
tf.cast(y_true, dtype=tf.bool),
tf.cos(tf.clip_by_value(tf.acos(norm_logits) - scaled_margin, self.epislon, self.cos_max_epislon)) - (self.margin + scaled_margin),
norm_logits,
)
# arcface_logits = tf.minimum(arcface_logits, norm_logits) * self.scale
arcface_logits *= self.scale
# return arcface_logits
return tf.keras.losses.categorical_crossentropy(y_true, arcface_logits, from_logits=self.from_logits, label_smoothing=self.label_smoothing)
def get_config(self):
config = super().get_config()