test_embeddings.py 4.17 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
6
import pkg_resources

7
8

def test_idiap_inceptionv2_msceleb():
9
    from bob.bio.face.embeddings import InceptionResnetv2_MsCeleb_CenterLoss_2018
10

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

27
    assert np.allclose(output, reference)
28
29
30
31
    assert output.size == 128, output.shape


def test_idiap_inceptionv2_casia():
32
    from bob.bio.face.embeddings import InceptionResnetv2_Casia_CenterLoss_2018
33
34

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


def test_idiap_inceptionv1_msceleb():
49
    from bob.bio.face.embeddings import InceptionResnetv1_MsCeleb_CenterLoss_2018
50
51

    np.random.seed(10)
52
    transformer = InceptionResnetv1_MsCeleb_CenterLoss_2018()
53
    data = np.random.rand(3, 160, 160).astype("uint8")
54
    output = transformer.transform([data])[0]
55
56
57
58
59
60
61
62
63
64
65
    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


def test_idiap_inceptionv1_casia():
66
    from bob.bio.face.embeddings import InceptionResnetv1_Casia_CenterLoss_2018
67
68

    np.random.seed(10)
69
    transformer = InceptionResnetv1_Casia_CenterLoss_2018()
70
    data = np.random.rand(3, 160, 160).astype("uint8")
71
    output = transformer.transform([data])[0]
72
73
74
75
76
77
78
79
80
    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

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
def test_facenet_sanderberg():
    from bob.bio.face.embeddings import FaceNetSanderberg_20170512_110547

    np.random.seed(10)
    transformer = FaceNetSanderberg_20170512_110547()
    data = np.random.rand(3, 160, 160).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]
    assert output.size == 128, output.shape

96

97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
def test_arcface_insight_face():
    from bob.bio.face.embeddings import ArcFaceInsightFace

    transformer = ArcFaceInsightFace()
    data = np.random.rand(3, 112, 112)*255
    data = data.astype("uint8")
    output = transformer.transform([data])
    assert output.size == 512, 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 == 512, output.shape


113
"""
114
115
def test_arface_insight_tf():
    import tensorflow as tf
116

117
118
119
120
121
122
123
    tf.compat.v1.reset_default_graph()

    from bob.bio.face.embeddings import ArcFace_InsightFaceTF

    np.random.seed(10)
    transformer = ArcFace_InsightFaceTF()
    data = np.random.rand(3, 112, 112).astype("uint8")
124
    output = transformer.transform([data])[0]
125
126
127
128
129
130
131
    assert output.size == 512, 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 == 512, output.shape
132
"""