Commit 9a106e8c authored by Tiago de Freitas Pereira's avatar Tiago de Freitas Pereira
Browse files

Serialization of bob.ip.gabor.Jet

parent 34a6ea0a
Pipeline #39302 failed with stage
in 4 minutes and 17 seconds
......@@ -9,6 +9,7 @@ import numpy
import math
from bob.bio.base.algorithm import Algorithm
from bob.bio.face.extractor import GridGraph
class GaborJet(Algorithm):
......@@ -72,21 +73,20 @@ class GaborJet(Algorithm):
multiple_probe_scoring=None,
)
self.gabor_jet_similarity_type=gabor_jet_similarity_type
self.multiple_feature_scoring=multiple_feature_scoring
self.gabor_directions=gabor_directions
self.gabor_scales=gabor_scales
self.gabor_sigma=gabor_sigma
self.gabor_maximum_frequency=gabor_maximum_frequency
self.gabor_frequency_step=gabor_frequency_step
self.gabor_power_of_k=gabor_power_of_k
self.gabor_dc_free=gabor_dc_free
self.gabor_jet_similarity_type = gabor_jet_similarity_type
self.multiple_feature_scoring = multiple_feature_scoring
self.gabor_directions = gabor_directions
self.gabor_scales = gabor_scales
self.gabor_sigma = gabor_sigma
self.gabor_maximum_frequency = gabor_maximum_frequency
self.gabor_frequency_step = gabor_frequency_step
self.gabor_power_of_k = gabor_power_of_k
self.gabor_dc_free = gabor_dc_free
self.gabor_jet_similarity_type = gabor_jet_similarity_type
self._init_non_pickables()
def _init_non_pickables(self):
# the Gabor wavelet transform; used by (some of) the Gabor jet similarities
self.gwt = bob.ip.gabor.Transform(
......@@ -128,9 +128,10 @@ class GaborJet(Algorithm):
}[self.multiple_feature_scoring]
def _check_feature(self, feature):
# import ipdb; ipdb.set_trace()
assert isinstance(feature, list) or isinstance(feature, numpy.ndarray)
assert len(feature)
feature = GridGraph.serialize_jets(feature)
assert all(isinstance(f, bob.ip.gabor.Jet) for f in feature)
def enroll(self, enroll_features):
......@@ -216,7 +217,7 @@ class GaborJet(Algorithm):
for g in range(count):
name = "Node-" + str(g + 1)
f.cd(name)
model.append(bob.ip.gabor.load_jets(f))
model.append(GridGraph.serialize_jets(bob.ip.gabor.load_jets(f)))
f.cd("..")
return model
......@@ -354,4 +355,3 @@ class GaborJet(Algorithm):
def __setstate__(self, d):
self.__dict__ = d
self._init_non_pickables()
......@@ -140,7 +140,7 @@ class GridGraph(Extractor):
"Please specify either 'eyes' or the grid parameters 'node_distance' (and 'first_node')!"
)
self._aligned_graph = None
self._last_image_resolution = None
self._last_image_resolution = None
if isinstance(self.node_distance, (int, float)):
self.node_distance = (int(self.node_distance), int(self.node_distance))
......@@ -229,7 +229,7 @@ class GridGraph(Extractor):
[j.normalize() for j in jets]
# return the extracted face graph
return jets
return self.__class__.serialize_jets(jets)
def write_feature(self, feature, feature_file):
"""Writes the feature extracted by the `__call__` function to the given file.
......@@ -264,7 +264,9 @@ class GridGraph(Extractor):
feature : [:py:class:`bob.ip.gabor.Jet`]
The list of Gabor jets read from file.
"""
return bob.ip.gabor.load_jets(bob.io.base.HDF5File(feature_file))
return self.__class__.serialize_jets(
bob.ip.gabor.load_jets(bob.io.base.HDF5File(feature_file))
)
# re-define the train function to get it non-documented
def train(*args, **kwargs):
......@@ -279,8 +281,19 @@ class GridGraph(Extractor):
d = dict(self.__dict__)
d.pop("gwt")
d.pop("_aligned_graph")
if "_graph" in d:
d.pop("_graph")
return d
def __setstate__(self, d):
self.__dict__ = d
self._init_non_pickables()
@staticmethod
def serialize_jets(jets):
serialize_jets = []
for jet in jets:
sj = bob.ip.gabor.Jet(jet.length)
sj.jet = jet.jet
serialize_jets.append(sj)
return serialize_jets
import bob.bio.face
import bob.bio.base
from bob.pipelines.utils import assert_picklable
import numpy
### Preprocessors
......@@ -16,7 +17,7 @@ def test_face_crop():
color_channel="rgb",
dtype="uint8",
)
assert_picklable(cropper)
assert assert_picklable(cropper)
def test_face_detect():
......@@ -26,31 +27,31 @@ def test_face_detect():
face_detect = bob.bio.face.preprocessor.FaceDetect(
face_cropper="face-crop-eyes", use_flandmark=True
)
assert_picklable(face_detect)
assert assert_picklable(face_detect)
def test_INormLBP():
face_crop = bob.bio.face.preprocessor.INormLBP(face_cropper="face-crop-eyes")
assert_picklable(face_crop)
assert assert_picklable(face_crop)
def test_TanTriggs():
face_crop = bob.bio.face.preprocessor.TanTriggs(face_cropper="face-crop-eyes")
assert_picklable(face_crop)
assert assert_picklable(face_crop)
def test_SQI():
face_crop = bob.bio.face.preprocessor.SelfQuotientImage(
face_cropper="face-crop-eyes"
)
assert_picklable(face_crop)
assert assert_picklable(face_crop)
def test_HistogramEqualization():
face_crop = bob.bio.face.preprocessor.HistogramEqualization(
face_cropper="face-crop-eyes"
)
assert_picklable(face_crop)
assert assert_picklable(face_crop)
### Extractors
......@@ -61,16 +62,24 @@ def test_DCT():
assert_picklable(extractor)
def test_GridGraph():
def test_GridGraph():
extractor = bob.bio.face.extractor.GridGraph(node_distance=24)
assert_picklable(extractor)
assert assert_picklable(extractor)
fake_image = numpy.arange(64*80).reshape(64,80).astype("float")
extractor(fake_image)
assert assert_picklable(extractor)
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_picklable(extractor)
assert assert_picklable(extractor)
def test_LGBPHS():
......@@ -85,7 +94,7 @@ def test_LGBPHS():
sparse_histogram=True,
)
assert_picklable(extractor)
assert assert_picklable(extractor)
## Algorithms
......@@ -96,9 +105,9 @@ def test_GaborJet():
algorithm = bob.bio.face.algorithm.GaborJet(
"PhaseDiffPlusCanberra", multiple_feature_scoring="average_model"
)
assert_picklable(algorithm)
assert assert_picklable(algorithm)
def test_Histogram():
algorithm = bob.bio.face.algorithm.Histogram()
assert_picklable(algorithm)
assert assert_picklable(algorithm)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment