test_embeddings.py 6.16 KB
Newer Older
1
import bob.bio.face
2
import bob.io.base
3
4
import numpy as np
from bob.pipelines import Sample, wrap
5
import pkg_resources
6
from bob.bio.base.test.utils import is_library_available
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
7
import pytest
8

9

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
10
@pytest.mark.slow
11
@is_library_available("tensorflow")
12
def test_idiap_inceptionv2_msceleb():
13
14
15
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv2_MsCeleb_CenterLoss_2018,
    )
16

17
18
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
19
            "bob.bio.face.test", "data/inception_resnet_v2_msceleb_rgb.hdf5"
20
21
        )
    )
22
    np.random.seed(10)
23
    transformer = InceptionResnetv2_MsCeleb_CenterLoss_2018()
24
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
25
    output = transformer.transform([data])[0]
26
27
28
29
30
31
32
    assert output.size == 128, output.shape

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]

33
    np.testing.assert_allclose(output, reference.flatten(), rtol=1e-5, atol=1e-4)
34
    assert output.size == 128, output.shape
35
36


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
37
@pytest.mark.slow
38
39
40
41
42
43
44
45
@is_library_available("tensorflow")
def test_idiap_inceptionv2_msceleb_memory_demanding():
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv2_MsCeleb_CenterLoss_2018,
    )

    reference = bob.io.base.load(
        pkg_resources.resource_filename(
46
            "bob.bio.face.test", "data/inception_resnet_v2_msceleb_rgb.hdf5"
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
        )
    )
    np.random.seed(10)

    transformer = InceptionResnetv2_MsCeleb_CenterLoss_2018(memory_demanding=True)
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
    output = transformer.transform([data])[0]
    assert output.size == 128, output.shape

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]

    np.testing.assert_allclose(output[0], reference.flatten(), rtol=1e-5, atol=1e-4)
    assert output.size == 128, output.shape
63
64


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
65
@pytest.mark.slow
66
@is_library_available("tensorflow")
67
def test_idiap_inceptionv2_casia():
68
69
70
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv2_Casia_CenterLoss_2018,
    )
71

72
73
74
75
76
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
            "bob.bio.face.test", "data/inception_resnet_v2_casia_rgb.hdf5"
        )
    )
77
    np.random.seed(10)
78
    transformer = InceptionResnetv2_Casia_CenterLoss_2018()
79
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
80
    output = transformer.transform([data])[0]
81
82
83
84
85
86
87
    assert output.size == 128, output.shape

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]

88
    np.testing.assert_allclose(output, reference.flatten(), rtol=1e-5, atol=1e-4)
89
90
91
    assert output.size == 128, output.shape


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
92
@pytest.mark.slow
93
@is_library_available("tensorflow")
94
def test_idiap_inceptionv1_msceleb():
95
96
97
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv1_MsCeleb_CenterLoss_2018,
    )
98

99
100
101
102
103
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
            "bob.bio.face.test", "data/inception_resnet_v1_msceleb_rgb.hdf5"
        )
    )
104
    np.random.seed(10)
105
    transformer = InceptionResnetv1_MsCeleb_CenterLoss_2018()
106
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
107
    output = transformer.transform([data])[0]
108
109
110
111
112
113
114
    assert output.size == 128, output.shape

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]

115
    np.testing.assert_allclose(output, reference.flatten(), rtol=1e-5, atol=1e-4)
116
117
118
    assert output.size == 128, output.shape


Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
119
@pytest.mark.slow
120
@is_library_available("tensorflow")
121
def test_idiap_inceptionv1_casia():
122
123
124
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv1_Casia_CenterLoss_2018,
    )
125

126
127
128
129
130
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
            "bob.bio.face.test", "data/inception_resnet_v1_casia_rgb.hdf5"
        )
    )
131
    np.random.seed(10)
132
    transformer = InceptionResnetv1_Casia_CenterLoss_2018()
133
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
134
    output = transformer.transform([data])[0]
135
136
137
138
139
140
141
    assert output.size == 128, output.shape

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]

142
    np.testing.assert_allclose(output, reference.flatten(), rtol=1e-5, atol=1e-4)
143
144
    assert output.size == 128, output.shape

145

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
146
@pytest.mark.slow
147
@is_library_available("tensorflow")
148
def test_facenet_sanderberg():
149
150
151
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        FaceNetSanderberg_20170512_110547,
    )
152

153
154
155
156
157
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
            "bob.bio.face.test", "data/facenet_sandberg_20170512-110547.hdf5"
        )
    )
158
159
    np.random.seed(10)
    transformer = FaceNetSanderberg_20170512_110547()
160
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
161
162
163
164
165
166
167
    output = transformer.transform([data])[0]
    assert output.size == 128, output.shape

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]
168
169

    np.testing.assert_allclose(output, reference.flatten(), rtol=1e-5, atol=1e-4)
170
171
    assert output.size == 128, output.shape

172

Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
173
@pytest.mark.slow
174
@is_library_available("mxnet")
175
def test_arcface_insight_face():
176
    from bob.bio.face.embeddings.mxnet_models import ArcFaceInsightFace
177
178

    transformer = ArcFaceInsightFace()
179
    data = np.random.rand(3, 112, 112) * 255
180
181
182
    data = data.astype("uint8")
    output = transformer.transform([data])
    assert output.size == 512, output.shape
183
184
185
186
187
188

    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]
    assert output.size == 512, output.shape