test_picklability.py 4.41 KB
Newer Older
1
import bob.bio.face
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
2
import bob.bio.base
3
4
5
6
7
8
9
import numpy as np
import pickle

import bob.ip.base
import bob.ip.flandmark
import bob.ip.gabor

10
11
12
13
14
15
16
17
18
# Cropping
CROPPED_IMAGE_HEIGHT = 80
CROPPED_IMAGE_WIDTH = CROPPED_IMAGE_HEIGHT * 4 // 5

# eye positions for frontal images
RIGHT_EYE_POS = (CROPPED_IMAGE_HEIGHT // 5, CROPPED_IMAGE_WIDTH // 4 - 1)
LEFT_EYE_POS = (CROPPED_IMAGE_HEIGHT // 5, CROPPED_IMAGE_WIDTH // 4 * 3)


19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
def assert_picklable_with_exceptions(obj):
    """Test if an object is picklable or not."""

    # some bob C bind objects doesn't have __eq__ properly implemented
    # and we'll not implement it
    # therefore, exception list has been done, so we can skip
    # those objects

    exception_list = [
        bob.ip.base.LBP,
        bob.bio.face.preprocessor.FaceCrop,
        bob.ip.facedetect.detector.sampler.Sampler,
        bob.ip.facedetect.detector.cascade.Cascade,
        bob.ip.flandmark.Flandmark,
        bob.ip.gabor.Similarity,
        bob.ip.gabor.Transform,
        bob.ip.gabor.Graph,
    ]

    string = pickle.dumps(obj)
    new_obj = pickle.loads(string)
    obj = obj.__dict__
    new_obj = new_obj.__dict__
    assert len(obj) == len(new_obj)
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
43
    assert sorted(list(obj.keys())) == sorted(list(new_obj.keys()))
44
45
46
47
48
    for k, v in obj.items():
        if isinstance(v, np.ndarray):
            np.testing.assert_equal(v, new_obj[k])
        else:
            if type(v) not in exception_list:
Amir MOHAMMADI's avatar
Amir MOHAMMADI committed
49
                assert v == new_obj[k]
50
51
    return True

52

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
53
### Preprocessors
54

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
55

56
57
58
59
60
61
62
63
64
65
66
def test_face_crop():
    CROPPED_IMAGE_HEIGHT = 64
    CROPPED_IMAGE_WIDTH = 64
    RIGHT_EYE_POS = (16.0, 15.5)
    LEFT_EYE_POS = (16.0, 48.0)
    cropper = bob.bio.face.preprocessor.FaceCrop(
        cropped_image_size=(CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH),
        cropped_positions={"leye": LEFT_EYE_POS, "reye": RIGHT_EYE_POS},
        color_channel="rgb",
        dtype="uint8",
    )
67
    assert assert_picklable_with_exceptions(cropper)
68
69
70


def test_INormLBP():
71
72
73
74
75
76
77
78
79
80
81
    face_cropper = bob.bio.face.preprocessor.FaceCrop(
      cropped_image_size=(CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH),
      cropped_positions={'leye': LEFT_EYE_POS, 'reye': RIGHT_EYE_POS}
    )

    preprocessor = bob.bio.face.preprocessor.INormLBP(
      face_cropper = face_cropper,
      dtype = np.float64
    )

    assert assert_picklable_with_exceptions(preprocessor)
82
83
84


def test_TanTriggs():
85
86
87
88
89
90
91
92
    face_cropper = bob.bio.face.preprocessor.FaceCrop(
      cropped_image_size=(CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH),
      cropped_positions={'leye': LEFT_EYE_POS, 'reye': RIGHT_EYE_POS}
    )

    preprocessor = bob.bio.face.preprocessor.TanTriggs(face_cropper=face_cropper)

    assert assert_picklable_with_exceptions(preprocessor)
93
94
95


def test_SQI():
96
97
98
99
100
101
    face_cropper = bob.bio.face.preprocessor.FaceCrop(
      cropped_image_size=(CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH),
      cropped_positions={'leye': LEFT_EYE_POS, 'reye': RIGHT_EYE_POS}
    )
    preprocessor = bob.bio.face.preprocessor.SelfQuotientImage(
      face_cropper = face_cropper
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
102
    )
103
104

    assert assert_picklable_with_exceptions(preprocessor)
105
106
107


def test_HistogramEqualization():
108

109
110
111
    face_cropper = bob.bio.face.preprocessor.FaceCrop(
      cropped_image_size=(CROPPED_IMAGE_HEIGHT, CROPPED_IMAGE_WIDTH),
      cropped_positions={'leye': LEFT_EYE_POS, 'reye': RIGHT_EYE_POS}
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
112
    )
113
114
115
116
117
    preprocessor = bob.bio.face.preprocessor.HistogramEqualization(
      face_cropper = face_cropper
    )

    #assert assert_picklable_with_exceptions(preprocessor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
118

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
119

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
120
121
### Extractors

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
122

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
123
124
def test_DCT():
    extractor = bob.bio.face.extractor.DCTBlocks()
125
    assert_picklable_with_exceptions(extractor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
126

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
127

128
def test_GridGraph():
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
129
    extractor = bob.bio.face.extractor.GridGraph(node_distance=24)
130
    fake_image = np.arange(64 * 80).reshape(64, 80).astype("float")
131
    extractor(fake_image)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
132

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
133

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
134
135
def test_LGBPHS():
    import math
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
136

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
137
    extractor = bob.bio.face.extractor.LGBPHS(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
138
139
140
141
142
143
        block_size=8,
        block_overlap=0,
        gabor_directions=4,
        gabor_scales=2,
        gabor_sigma=math.sqrt(2.0) * math.pi,
        sparse_histogram=True,
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
144
145
    )

146
    assert assert_picklable_with_exceptions(extractor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
147
148
149
150
151
152
153
154
155
156


## Algorithms


def test_GaborJet():

    algorithm = bob.bio.face.algorithm.GaborJet(
        "PhaseDiffPlusCanberra", multiple_feature_scoring="average_model"
    )
157
    assert assert_picklable_with_exceptions(algorithm)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
158
159
160
161


def test_Histogram():
    algorithm = bob.bio.face.algorithm.Histogram()
162
    assert assert_picklable_with_exceptions(algorithm)