resnet50.py 3.77 KB
Newer Older
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
from bob.bio.face.embeddings import download_model


from .tf2_inception_resnet import TransformTensorflow
import pkg_resources
import os
from bob.extension import rc
import tensorflow as tf


class Resnet50_MsCeleb_ArcFace_2021(TransformTensorflow):
    """
    Resnet50 Backbone trained with the MSCeleb 1M database.

    The bottleneck layer (a.k.a embedding) has 512d.

    The configuration file used to trained is:

    ```yaml
    batch-size: 128
    face-size: 112
    face-output_size: 112
    n-classes: 85742


    ## Backbone
    backbone: 'resnet50'
    head: 'arcface'
    s: 10
    bottleneck: 512
    m: 0.5

    # Training parameters
    solver: "sgd"
    lr: 0.01
    dropout-rate: 0.5
    epochs: 500


    train-tf-record-path: "<PATH>"
    validation-tf-record-path: "<PATH>"

    ```


    """

    def __init__(self, memory_demanding=False):
        internal_path = pkg_resources.resource_filename(
            __name__, os.path.join("data", "resnet50_msceleb_arcface_2021"),
        )

        checkpoint_path = (
            internal_path
            if rc["bob.bio.face.models.resnet50_msceleb_arcface_2021"] is None
            else rc["bob.bio.face.models.resnet50_msceleb_arcface_2021"]
        )

        urls = [
            "https://www.idiap.ch/software/bob/data/bob/bob.bio.face/master/tf2/resnet50_msceleb_arcface_2021.tar.gz",
            "http://www.idiap.ch/software/bob/data/bob/bob.bio.face/master/tf2/resnet50_msceleb_arcface_2021.tar.gz",
        ]

        download_model(checkpoint_path, urls, "resnet50_msceleb_arcface_2021.tar.gz")

        super(Resnet50_MsCeleb_ArcFace_2021, self).__init__(
            checkpoint_path,
            preprocessor=lambda X: X / 255.0,
            memory_demanding=memory_demanding,
        )

    def inference(self, X):
        if self.preprocessor is not None:
            X = self.preprocessor(tf.cast(X, "float32"))

        prelogits = self.model.predict_on_batch(X)[0]
        embeddings = tf.math.l2_normalize(prelogits, axis=-1)
        return embeddings

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
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

class Resnet50_VGG2_ArcFace_2021(TransformTensorflow):
    """
    Resnet50 Backbone trained with the VGG2 database.

    The bottleneck layer (a.k.a embedding) has 512d.

    The configuration file used to trained is:

    ```yaml
    batch-size: 128
    face-size: 112
    face-output_size: 112
    n-classes: 8631


    ## Backbone
    backbone: 'resnet50'
    head: 'arcface'
    s: 64
    bottleneck: 512
    m: 0.5

    # Training parameters
    solver: "sgd"
    lr: 0.1
    dropout-rate: 0.5
    epochs: 1047


    train-tf-record-path: "<PATH>"
    validation-tf-record-path: "<PATH>"

    ```


    """

    def __init__(self, memory_demanding=False):
        internal_path = pkg_resources.resource_filename(
            __name__, os.path.join("data", "resnet50_vgg2_arcface_2021"),
        )

        checkpoint_path = (
            internal_path
            if rc["bob.bio.face.models.resnet50_vgg2_arcface_2021"] is None
            else rc["bob.bio.face.models.resnet50_vgg2_arcface_2021"]
        )

        urls = [
            "https://www.idiap.ch/software/bob/data/bob/bob.bio.face/master/tf2/resnet50_vgg2_arcface_2021.tar.gz",
            "http://www.idiap.ch/software/bob/data/bob/bob.bio.face/master/tf2/resnet50_vgg2_arcface_2021.tar.gz",
        ]

        download_model(checkpoint_path, urls, "resnet50_vgg2_arcface_2021.tar.gz")

        super(Resnet50_VGG2_ArcFace_2021, self).__init__(
            checkpoint_path,
            preprocessor=lambda X: X / 255.0,
            memory_demanding=memory_demanding,
        )

    def inference(self, X):
        if self.preprocessor is not None:
            X = self.preprocessor(tf.cast(X, "float32"))

        prelogits = self.model.predict_on_batch(X)
        embeddings = tf.math.l2_normalize(prelogits, axis=-1)
        return embeddings