test_picklability.py 5.02 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
10
import numpy as np
import pickle
import nose

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

11
12
13
14
15
16
17
18
19
# 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)


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
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)
    nose.tools.assert_equal(sorted(list(obj.keys())), sorted(list(new_obj.keys())))
    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:
                nose.tools.assert_equal(v, new_obj[k])
    return True

53

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

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

57
58
59
60
61
62
63
64
65
66
67
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",
    )
68
    assert assert_picklable_with_exceptions(cropper)
69
70
71


def test_face_detect():
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}
    )

    face_detect = bob.bio.face.preprocessor.FaceDetect(
      face_cropper = face_cropper,
      use_flandmark = False
    )

82
    assert_picklable_with_exceptions(face_detect)
83
84

    face_detect = bob.bio.face.preprocessor.FaceDetect(
85
86
      face_cropper = face_cropper,
      use_flandmark = True
87
    )
88
    assert assert_picklable_with_exceptions(face_detect)
89
90
91


def test_INormLBP():
92
93
94
95
96
97
98
99
100
101
102
    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)
103
104
105


def test_TanTriggs():
106
107
108
109
110
111
112
113
    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)
114
115
116


def test_SQI():
117
118
119
120
121
122
    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
123
    )
124
125

    assert assert_picklable_with_exceptions(preprocessor)
126
127
128


def test_HistogramEqualization():
129
130
131
132
    
    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
133
    )
134
135
136
137
138
    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
139

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

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
141
142
### Extractors

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

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
144
145
def test_DCT():
    extractor = bob.bio.face.extractor.DCTBlocks()
146
    assert_picklable_with_exceptions(extractor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
147

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

149
def test_GridGraph():
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
150
    extractor = bob.bio.face.extractor.GridGraph(node_distance=24)
151
    fake_image = np.arange(64 * 80).reshape(64, 80).astype("float")
152
    extractor(fake_image)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
153

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

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
155
156
def test_LGBPHS():
    import math
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
157

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
158
    extractor = bob.bio.face.extractor.LGBPHS(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
159
160
161
162
163
164
        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
165
166
    )

167
    assert assert_picklable_with_exceptions(extractor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
168
169
170
171
172
173
174
175
176
177


## Algorithms


def test_GaborJet():

    algorithm = bob.bio.face.algorithm.GaborJet(
        "PhaseDiffPlusCanberra", multiple_feature_scoring="average_model"
    )
178
    assert assert_picklable_with_exceptions(algorithm)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
179
180
181
182


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