From cc4cc182580b0fb3e458dc0e848957640d87ad87 Mon Sep 17 00:00:00 2001
From: Laurent COLBOIS <lcolbois@.idiap.ch>
Date: Wed, 12 May 2021 13:53:31 +0200
Subject: [PATCH] Refactoring DNN baselines which only differ by the specific
 extractor

---
 .../config/baseline/arcface_insightface.py    | 31 ++---------
 .../config/baseline/facenet_sanderberg.py     | 28 ++--------
 bob/bio/face/config/baseline/helpers.py       |  2 -
 .../inception_resnetv1_casiawebface.py        | 27 ++--------
 .../baseline/inception_resnetv1_msceleb.py    | 28 ++--------
 .../inception_resnetv2_casiawebface.py        | 28 ++--------
 .../baseline/inception_resnetv2_msceleb.py    | 27 ++--------
 .../mobilenetv2_msceleb_arcface_2021.py       | 32 ++---------
 .../baseline/resnet50_msceleb_arcface_2021.py | 32 ++---------
 .../baseline/resnet50_vgg2_arcface_2021.py    | 33 ++----------
 bob/bio/face/config/baseline/templates.py     | 53 +++++++++++++++++++
 .../config/baseline/tf2_inception_resnet.py   | 31 ++---------
 12 files changed, 97 insertions(+), 255 deletions(-)
 create mode 100644 bob/bio/face/config/baseline/templates.py

diff --git a/bob/bio/face/config/baseline/arcface_insightface.py b/bob/bio/face/config/baseline/arcface_insightface.py
index db164c30..4e2975fb 100644
--- a/bob/bio/face/config/baseline/arcface_insightface.py
+++ b/bob/bio/face/config/baseline/arcface_insightface.py
@@ -1,41 +1,18 @@
 from bob.bio.face.embeddings.mxnet_models import ArcFaceInsightFace
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import arcface_baseline
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (112, 112)
-    if annotation_type == "eyes-center":
-        # Hard coding eye positions for backward consistency
-        cropped_positions = {
-            "leye": (55, 81),
-            "reye": (55, 42),
-        }
-    else:
-        cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
 
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return arcface_baseline(
         embedding=ArcFaceInsightFace(memory_demanding=memory_demanding),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/facenet_sanderberg.py b/bob/bio/face/config/baseline/facenet_sanderberg.py
index bdefacb8..3197d061 100644
--- a/bob/bio/face/config/baseline/facenet_sanderberg.py
+++ b/bob/bio/face/config/baseline/facenet_sanderberg.py
@@ -1,39 +1,19 @@
 from bob.bio.face.embeddings.tf2_inception_resnet import (
     FaceNetSanderberg_20170512_110547,
 )
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import facenet_baseline
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (160, 160)
-    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    # ASSEMBLE TRANSFORMER
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return facenet_baseline(
         embedding=FaceNetSanderberg_20170512_110547(memory_demanding=memory_demanding),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
-        annotator="mtcnn",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/helpers.py b/bob/bio/face/config/baseline/helpers.py
index 0daeaf09..1ea0668e 100644
--- a/bob/bio/face/config/baseline/helpers.py
+++ b/bob/bio/face/config/baseline/helpers.py
@@ -1,6 +1,4 @@
-import bob.bio.face
 from sklearn.pipeline import make_pipeline
-from bob.bio.base.wrappers import wrap_sample_preprocessor
 from bob.pipelines import wrap
 from bob.bio.face.helpers import face_crop_solver
 import numpy as np
diff --git a/bob/bio/face/config/baseline/inception_resnetv1_casiawebface.py b/bob/bio/face/config/baseline/inception_resnetv1_casiawebface.py
index b532c815..0d88bbad 100644
--- a/bob/bio/face/config/baseline/inception_resnetv1_casiawebface.py
+++ b/bob/bio/face/config/baseline/inception_resnetv1_casiawebface.py
@@ -1,40 +1,21 @@
 from bob.bio.face.embeddings.tf2_inception_resnet import (
     InceptionResnetv1_Casia_CenterLoss_2018,
 )
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import facenet_baseline
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (160, 160)
-    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    # ASSEMBLE TRANSFORMER
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return facenet_baseline(
         embedding=InceptionResnetv1_Casia_CenterLoss_2018(
             memory_demanding=memory_demanding
         ),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
-        annotator="mtcnn",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/inception_resnetv1_msceleb.py b/bob/bio/face/config/baseline/inception_resnetv1_msceleb.py
index db94eddd..766f1cf2 100644
--- a/bob/bio/face/config/baseline/inception_resnetv1_msceleb.py
+++ b/bob/bio/face/config/baseline/inception_resnetv1_msceleb.py
@@ -1,40 +1,22 @@
 from bob.bio.face.embeddings.tf2_inception_resnet import (
     InceptionResnetv1_MsCeleb_CenterLoss_2018,
 )
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import facenet_baseline
+
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (160, 160)
-    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    # ASSEMBLE TRANSFORMER
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return facenet_baseline(
         embedding=InceptionResnetv1_MsCeleb_CenterLoss_2018(
             memory_demanding=memory_demanding
         ),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
-        annotator="mtcnn",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/inception_resnetv2_casiawebface.py b/bob/bio/face/config/baseline/inception_resnetv2_casiawebface.py
index c20d856c..1f56b451 100644
--- a/bob/bio/face/config/baseline/inception_resnetv2_casiawebface.py
+++ b/bob/bio/face/config/baseline/inception_resnetv2_casiawebface.py
@@ -1,40 +1,22 @@
 from bob.bio.face.embeddings.tf2_inception_resnet import (
     InceptionResnetv2_Casia_CenterLoss_2018,
 )
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import facenet_baseline
+
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (160, 160)
-    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    # ASSEMBLE TRANSFORMER
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return facenet_baseline(
         embedding=InceptionResnetv2_Casia_CenterLoss_2018(
             memory_demanding=memory_demanding
         ),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
-        annotator="mtcnn",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/inception_resnetv2_msceleb.py b/bob/bio/face/config/baseline/inception_resnetv2_msceleb.py
index d44585eb..2a4bd3d1 100644
--- a/bob/bio/face/config/baseline/inception_resnetv2_msceleb.py
+++ b/bob/bio/face/config/baseline/inception_resnetv2_msceleb.py
@@ -1,40 +1,21 @@
 from bob.bio.face.embeddings.tf2_inception_resnet import (
     InceptionResnetv2_MsCeleb_CenterLoss_2018,
 )
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import facenet_baseline
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (160, 160)
-    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    # ASSEMBLE TRANSFORMER
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return facenet_baseline(
         embedding=InceptionResnetv2_MsCeleb_CenterLoss_2018(
             memory_demanding=memory_demanding
         ),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
-        annotator="mtcnn",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/mobilenetv2_msceleb_arcface_2021.py b/bob/bio/face/config/baseline/mobilenetv2_msceleb_arcface_2021.py
index 2ca17ebe..61b9db98 100644
--- a/bob/bio/face/config/baseline/mobilenetv2_msceleb_arcface_2021.py
+++ b/bob/bio/face/config/baseline/mobilenetv2_msceleb_arcface_2021.py
@@ -1,41 +1,19 @@
 from bob.bio.face.embeddings.mobilenet_v2 import MobileNetv2_MsCeleb_ArcFace_2021
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import arcface_baseline
+
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (112, 112)
-    if annotation_type == "eyes-center":
-        # Hard coding eye positions for backward consistency
-        cropped_positions = {
-            "leye": (55, 81),
-            "reye": (55, 42),
-        }
-    else:
-        cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
 
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return arcface_baseline(
         embedding=MobileNetv2_MsCeleb_ArcFace_2021(memory_demanding=memory_demanding),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/resnet50_msceleb_arcface_2021.py b/bob/bio/face/config/baseline/resnet50_msceleb_arcface_2021.py
index c5056662..442247b5 100644
--- a/bob/bio/face/config/baseline/resnet50_msceleb_arcface_2021.py
+++ b/bob/bio/face/config/baseline/resnet50_msceleb_arcface_2021.py
@@ -1,42 +1,18 @@
 from bob.bio.face.embeddings.resnet50 import Resnet50_MsCeleb_ArcFace_2021
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import arcface_baseline
 
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (112, 112)
-    if annotation_type == "eyes-center":
-        # Hard coding eye positions for backward consistency
-        cropped_positions = {
-            "leye": (55, 81),
-            "reye": (55, 42),
-        }
-    else:
-        cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return arcface_baseline(
         embedding=Resnet50_MsCeleb_ArcFace_2021(memory_demanding=memory_demanding),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/resnet50_vgg2_arcface_2021.py b/bob/bio/face/config/baseline/resnet50_vgg2_arcface_2021.py
index 05eaa49b..95a3a95b 100644
--- a/bob/bio/face/config/baseline/resnet50_vgg2_arcface_2021.py
+++ b/bob/bio/face/config/baseline/resnet50_vgg2_arcface_2021.py
@@ -1,41 +1,18 @@
 from bob.bio.face.embeddings.resnet50 import Resnet50_VGG2_ArcFace_2021
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import arcface_baseline
+
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (112, 112)
-    if annotation_type == "eyes-center":
-        # Hard coding eye positions for backward consistency
-        cropped_positions = {
-            "leye": (55, 81),
-            "reye": (55, 42),
-        }
-    else:
-        cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return arcface_baseline(
         embedding=Resnet50_VGG2_ArcFace_2021(memory_demanding=memory_demanding),
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
diff --git a/bob/bio/face/config/baseline/templates.py b/bob/bio/face/config/baseline/templates.py
new file mode 100644
index 00000000..bf6c6c4f
--- /dev/null
+++ b/bob/bio/face/config/baseline/templates.py
@@ -0,0 +1,53 @@
+from bob.bio.face.config.baseline.helpers import (
+    dnn_default_cropping,
+    embedding_transformer,
+)
+from bob.bio.base.pipelines.vanilla_biometrics import (
+    Distance,
+    VanillaBiometricsPipeline,
+)
+
+
+def arcface_baseline(embedding, annotation_type, fixed_positions=None):
+    # DEFINE CROPPING
+    cropped_image_size = (112, 112)
+    if annotation_type == "eyes-center":
+        # Hard coding eye positions for backward consistency
+        cropped_positions = {
+            "leye": (55, 81),
+            "reye": (55, 42),
+        }
+    else:
+        cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
+
+    transformer = embedding_transformer(
+        cropped_image_size=cropped_image_size,
+        embedding=embedding,
+        cropped_positions=cropped_positions,
+        fixed_positions=fixed_positions,
+        color_channel="rgb",
+    )
+
+    algorithm = Distance()
+
+    return VanillaBiometricsPipeline(transformer, algorithm)
+
+
+def facenet_baseline(embedding, annotation_type, fixed_positions=None):
+    # DEFINE CROPPING
+    cropped_image_size = (160, 160)
+    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
+
+    # ASSEMBLE TRANSFORMER
+    transformer = embedding_transformer(
+        cropped_image_size=cropped_image_size,
+        embedding=embedding,
+        cropped_positions=cropped_positions,
+        fixed_positions=fixed_positions,
+        color_channel="rgb",
+        annotator="mtcnn",
+    )
+
+    algorithm = Distance()
+
+    return VanillaBiometricsPipeline(transformer, algorithm)
diff --git a/bob/bio/face/config/baseline/tf2_inception_resnet.py b/bob/bio/face/config/baseline/tf2_inception_resnet.py
index 619bcf82..87862c7a 100644
--- a/bob/bio/face/config/baseline/tf2_inception_resnet.py
+++ b/bob/bio/face/config/baseline/tf2_inception_resnet.py
@@ -1,45 +1,22 @@
 from bob.extension import rc
 from bob.bio.face.embeddings.tf2_inception_resnet import InceptionResnetv2
-from bob.bio.face.preprocessor import FaceCrop
-from bob.bio.face.config.baseline.helpers import (
-    lookup_config_from_database,
-    dnn_default_cropping,
-    embedding_transformer,
-)
-
-from sklearn.pipeline import make_pipeline
-from bob.pipelines.wrappers import wrap
-from bob.bio.base.pipelines.vanilla_biometrics import (
-    Distance,
-    VanillaBiometricsPipeline,
-)
+from bob.bio.face.config.baseline.helpers import lookup_config_from_database
+from bob.bio.face.config.baseline.templates import facenet_baseline
 
 annotation_type, fixed_positions, memory_demanding = lookup_config_from_database()
 
 
 def load(annotation_type, fixed_positions=None):
-    # DEFINE CROPPING
-    cropped_image_size = (160, 160)
-    cropped_positions = dnn_default_cropping(cropped_image_size, annotation_type)
-
     extractor_path = rc["bob.bio.face.tf2.casia-webface-inception-v2"]
     embedding = InceptionResnetv2(
         checkpoint_path=extractor_path, memory_demanding=memory_demanding
     )
-    # ASSEMBLE TRANSFORMER
-    transformer = embedding_transformer(
-        cropped_image_size=cropped_image_size,
+    return facenet_baseline(
         embedding=embedding,
-        cropped_positions=cropped_positions,
+        annotation_type=annotation_type,
         fixed_positions=fixed_positions,
-        color_channel="rgb",
-        annotator="mtcnn",
     )
 
-    algorithm = Distance()
-
-    return VanillaBiometricsPipeline(transformer, algorithm)
-
 
 pipeline = load(annotation_type, fixed_positions)
 transformer = pipeline.transformer
-- 
GitLab