test_picklability.py 4.45 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_INormLBP():
72
73
74
75
76
77
78
79
80
81
82
    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)
83
84
85


def test_TanTriggs():
86
87
88
89
90
91
92
93
    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)
94
95
96


def test_SQI():
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.SelfQuotientImage(
      face_cropper = face_cropper
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
103
    )
104
105

    assert assert_picklable_with_exceptions(preprocessor)
106
107
108


def test_HistogramEqualization():
109

110
111
112
    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
113
    )
114
115
116
117
118
    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
119

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

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

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

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

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

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

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

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

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
138
    extractor = bob.bio.face.extractor.LGBPHS(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
139
140
141
142
143
144
        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
145
146
    )

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


## Algorithms


def test_GaborJet():

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


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