test_embeddings.py 4.3 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
7

8

9
@is_library_available("tensorflow")
10
def test_idiap_inceptionv2_msceleb():
11
12
13
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv2_MsCeleb_CenterLoss_2018,
    )
14

15
16
17
18
19
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
            "bob.bio.face.test", "data/inception_resnet_v2_rgb.hdf5"
        )
    )
20
    np.random.seed(10)
21
    transformer = InceptionResnetv2_MsCeleb_CenterLoss_2018()
22
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
23
    output = transformer.transform([data])[0]
24
25
26
27
28
29
30
    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]

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


35
@is_library_available("tensorflow")
36
def test_idiap_inceptionv2_casia():
37
    from bob.bio.face.embeddings import InceptionResnetv2_Casia_CenterLoss_2018
38
39

    np.random.seed(10)
40
    transformer = InceptionResnetv2_Casia_CenterLoss_2018()
41
    data = np.random.rand(3, 160, 160).astype("uint8")
42
    output = transformer.transform([data])[0]
43
44
45
46
47
48
49
50
51
52
    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]

    assert output.size == 128, output.shape


53
@is_library_available("tensorflow")
54
def test_idiap_inceptionv1_msceleb():
55
56
57
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv1_MsCeleb_CenterLoss_2018,
    )
58
59

    np.random.seed(10)
60
    transformer = InceptionResnetv1_MsCeleb_CenterLoss_2018()
61
    data = np.random.rand(3, 160, 160).astype("uint8")
62
    output = transformer.transform([data])[0]
63
64
65
66
67
68
69
70
71
72
    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]

    assert output.size == 128, output.shape


73
@is_library_available("tensorflow")
74
def test_idiap_inceptionv1_casia():
75
76
77
    from bob.bio.face.embeddings.tf2_inception_resnet import (
        InceptionResnetv1_Casia_CenterLoss_2018,
    )
78
79

    np.random.seed(10)
80
    transformer = InceptionResnetv1_Casia_CenterLoss_2018()
81
    data = np.random.rand(3, 160, 160).astype("uint8")
82
    output = transformer.transform([data])[0]
83
84
85
86
87
88
89
90
91
    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]

    assert output.size == 128, output.shape

92

93
@is_library_available("tensorflow")
94
def test_facenet_sanderberg():
95
    from bob.bio.face.embeddings.tf2_inception_resnet import (
96
        FaceNetSanderberg_20170512_110547
97
    )
98

99
100
101
102
103
    reference = bob.io.base.load(
        pkg_resources.resource_filename(
            "bob.bio.face.test", "data/facenet_sandberg_20170512-110547.hdf5"
        )
    )
104
105
    np.random.seed(10)
    transformer = FaceNetSanderberg_20170512_110547()
106
    data = (np.random.rand(3, 160, 160) * 255).astype("uint8")
107
108
    output = transformer.transform([data])[0]
    assert output.size == 128, output.shape
109
    
110
111
112
113
    # Sample Batch
    sample = Sample(data)
    transformer_sample = wrap(["sample"], transformer)
    output = [s.data for s in transformer_sample.transform([sample])][0]
114
115

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

118

119
@is_library_available("mxnet")
120
def test_arcface_insight_face():
121
    from bob.bio.face.embeddings.mxnet_models import ArcFaceInsightFace
122
123

    transformer = ArcFaceInsightFace()
124
    data = np.random.rand(3, 112, 112) * 255
125
126
127
    data = data.astype("uint8")
    output = transformer.transform([data])
    assert output.size == 512, output.shape
128
129
130
131
132
133

    # 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