test_picklability.py 4.06 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
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import numpy as np
import pickle
import nose

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

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

44

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
45
### Preprocessors
46

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

48
49
50
51
52
53
54
55
56
57
58
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",
    )
59
    assert assert_picklable_with_exceptions(cropper)
60
61
62
63


def test_face_detect():
    face_detect = bob.bio.face.preprocessor.FaceDetect(face_cropper="face-crop-eyes")
64
    assert_picklable_with_exceptions(face_detect)
65
66
67
68

    face_detect = bob.bio.face.preprocessor.FaceDetect(
        face_cropper="face-crop-eyes", use_flandmark=True
    )
69
    assert assert_picklable_with_exceptions(face_detect)
70
71
72
73


def test_INormLBP():
    face_crop = bob.bio.face.preprocessor.INormLBP(face_cropper="face-crop-eyes")
74
    assert assert_picklable_with_exceptions(face_crop)
75
76
77
78


def test_TanTriggs():
    face_crop = bob.bio.face.preprocessor.TanTriggs(face_cropper="face-crop-eyes")
79
    assert assert_picklable_with_exceptions(face_crop)
80
81
82


def test_SQI():
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
83
84
85
    face_crop = bob.bio.face.preprocessor.SelfQuotientImage(
        face_cropper="face-crop-eyes"
    )
86
    assert assert_picklable_with_exceptions(face_crop)
87
88
89


def test_HistogramEqualization():
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
90
91
92
    face_crop = bob.bio.face.preprocessor.HistogramEqualization(
        face_cropper="face-crop-eyes"
    )
93
    assert assert_picklable_with_exceptions(face_crop)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
94

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

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
96
97
### Extractors

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

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
99
100
def test_DCT():
    extractor = bob.bio.face.extractor.DCTBlocks()
101
    assert_picklable_with_exceptions(extractor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
102

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

104
def test_GridGraph():
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
105
    extractor = bob.bio.face.extractor.GridGraph(node_distance=24)
106
    #assert assert_picklable_with_exceptions(extractor)
107

108
    fake_image = np.arange(64 * 80).reshape(64, 80).astype("float")
109
    extractor(fake_image)
110
    assert assert_picklable_with_exceptions(extractor)
111

112
113
114
115
116
117
    #cropper = bob.bio.base.load_resource(
    #    "face-crop-eyes", "preprocessor", preferred_package="bob.bio.face"
    #)
    #eyes = cropper.cropped_positions
    #extractor = bob.bio.face.extractor.GridGraph(eyes=eyes)
    #assert assert_picklable_with_exceptions(extractor)
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
def test_LGBPHS():
    import math
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
122

Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
123
    extractor = bob.bio.face.extractor.LGBPHS(
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
124
125
126
127
128
129
        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
130
131
    )

132
    assert assert_picklable_with_exceptions(extractor)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
133
134
135
136
137
138
139
140
141
142


## Algorithms


def test_GaborJet():

    algorithm = bob.bio.face.algorithm.GaborJet(
        "PhaseDiffPlusCanberra", multiple_feature_scoring="average_model"
    )
143
    assert assert_picklable_with_exceptions(algorithm)
Tiago de Freitas Pereira's avatar
Tiago de Freitas Pereira committed
144
145
146
147


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