diff --git a/bob/learn/tensorflow/layers.py b/bob/learn/tensorflow/layers.py index ba63ca9521aaab3c0c8f4a0dc92e60a9745d93bf..e15d36e5ffaedc4f321296486350b6248c18d252 100644 --- a/bob/learn/tensorflow/layers.py +++ b/bob/learn/tensorflow/layers.py @@ -1,7 +1,11 @@ +import math import numbers import tensorflow as tf -import math +from tensorflow.keras.layers import BatchNormalization +from tensorflow.keras.layers import Dense +from tensorflow.keras.layers import Dropout +from tensorflow.keras.layers import GlobalAvgPool2D def _check_input( @@ -164,7 +168,7 @@ def Normalize(mean, std=1.0, **kwargs): class SphereFaceLayer(tf.keras.layers.Layer): - """ + r""" Implements the SphereFace loss from equation (7) of SphereFace: Deep Hypersphere Embedding for Face Recognition _ If the parameter original is set to True it will computes exactly what's written in eq (7): :math:\\text{soft}(x_i) = \\frac{exp(||x_i||\\text{cos}(\\psi(\\theta_{yi})))}{exp(||x_i||\\text{cos}(\\psi(\\theta_{yi}))) + \sum_{j;j\\neq yi} exp(||x_i||\\text{cos}(\psi(\\theta_{j}))) }. @@ -178,7 +182,7 @@ class SphereFaceLayer(tf.keras.layers.Layer): m: float Margin - + """ def __init__(self, n_classes=10, m=0.5): @@ -227,7 +231,7 @@ class ModifiedSoftMaxLayer(tf.keras.layers.Layer): Parameters ---------- - + n_classes: int Number of classes for the new logit function """ @@ -256,15 +260,6 @@ class ModifiedSoftMaxLayer(tf.keras.layers.Layer): return logits -from tensorflow.keras.layers import ( - BatchNormalization, - Dropout, - Dense, - Concatenate, - GlobalAvgPool2D, -) - - def add_bottleneck(model, bottleneck_size=128, dropout_rate=0.2): """ Amend a bottleneck layer to a Keras Model diff --git a/bob/learn/tensorflow/models/__init__.py b/bob/learn/tensorflow/models/__init__.py index b5cd87bf26c41a6ad1418c78bbefa1ac221640b6..4265c99d7b58fe0bf723afb04883d48a09633df1 100644 --- a/bob/learn/tensorflow/models/__init__.py +++ b/bob/learn/tensorflow/models/__init__.py @@ -1,10 +1,13 @@ from .alexnet import AlexNet_simplified +from .arcface import ArcFaceLayer +from .arcface import ArcFaceLayer3Penalties +from .arcface import ArcFaceModel from .densenet import DeepPixBiS from .densenet import DenseNet from .densenet import densenet161 # noqa: F401 -from .mine import MineModel from .embedding_validation import EmbeddingValidation -from .arcface import ArcFaceLayer, ArcFaceLayer3Penalties, ArcFaceModel +from .mine import MineModel + # gets sphinx autodoc done right - don't remove it def __appropriate__(*args): diff --git a/bob/learn/tensorflow/models/arcface.py b/bob/learn/tensorflow/models/arcface.py index 59f8ff68a1396598fa14bf8c6958e225e92a7592..8bbb7a7d65657d63909fe226e2ecbc60053d6633 100644 --- a/bob/learn/tensorflow/models/arcface.py +++ b/bob/learn/tensorflow/models/arcface.py @@ -1,7 +1,10 @@ +import math + import tensorflow as tf -from .embedding_validation import EmbeddingValidation + from bob.learn.tensorflow.metrics.embedding_accuracy import accuracy_from_embeddings -import math + +from .embedding_validation import EmbeddingValidation class ArcFaceModel(EmbeddingValidation): @@ -48,10 +51,10 @@ class ArcFaceLayer(tf.keras.layers.Layer): Number of classes m: float - Margin + Margin s: int - Scale + Scale """ def __init__(self, n_classes=10, s=30, m=0.5): @@ -102,9 +105,9 @@ class ArcFaceLayer(tf.keras.layers.Layer): class ArcFaceLayer3Penalties(tf.keras.layers.Layer): """ Implements the ArcFace loss from equation (4) of ArcFace: Additive Angular Margin Loss for Deep Face Recognition _ - + Defined as: - + :math:s(cos(m_1\\theta_i + m_2) -m_3 """ @@ -148,4 +151,3 @@ class ArcFaceLayer3Penalties(tf.keras.layers.Layer): logits = self.s * logits return logits - diff --git a/bob/learn/tensorflow/models/embedding_validation.py b/bob/learn/tensorflow/models/embedding_validation.py index 701f3084e74267bdfb0e300b57a723b3beef4ac3..be342d1f5c22404b0145f78c8cfc71e33e18c792 100644 --- a/bob/learn/tensorflow/models/embedding_validation.py +++ b/bob/learn/tensorflow/models/embedding_validation.py @@ -1,16 +1,18 @@ import tensorflow as tf + from bob.learn.tensorflow.metrics.embedding_accuracy import accuracy_from_embeddings class EmbeddingValidation(tf.keras.Model): """ Use this model if the validation step should validate the accuracy with respect to embeddings. - + In this model, the test_step runs the function bob.learn.tensorflow.metrics.embedding_accuracy.accuracy_from_embeddings """ def compile( - self, **kwargs, + self, + **kwargs, ): """ Compile @@ -29,7 +31,6 @@ class EmbeddingValidation(tf.keras.Model): logits, _ = self(X, training=True) loss = self.loss(y, logits) - trainable_vars = self.trainable_variables self.optimizer.minimize(loss, self.trainable_variables, tape=tape) self.compiled_metrics.update_state(y, logits, sample_weight=None) diff --git a/bob/learn/tensorflow/tests/test_arcface.py b/bob/learn/tensorflow/tests/test_arcface.py index ac9152003a86751e51415d3e58a9ef4f7d68685e..4ebfe63e29677cddf54251949eef90beaab6712d 100644 --- a/bob/learn/tensorflow/tests/test_arcface.py +++ b/bob/learn/tensorflow/tests/test_arcface.py @@ -1,16 +1,10 @@ -from bob.learn.tensorflow.models import ( - EmbeddingValidation, - ArcFaceLayer, - ArcFaceModel, - ArcFaceLayer3Penalties, -) -from bob.learn.tensorflow.layers import ( - SphereFaceLayer, - ModifiedSoftMaxLayer, -) - import numpy as np +from bob.learn.tensorflow.layers import ModifiedSoftMaxLayer +from bob.learn.tensorflow.layers import SphereFaceLayer +from bob.learn.tensorflow.models import ArcFaceLayer +from bob.learn.tensorflow.models import ArcFaceLayer3Penalties + def test_arcface_layer():