Make FaceNet pickalable

parent 00042f9a
Pipeline #39234 passed with stage
in 13 minutes and 42 seconds
...@@ -9,6 +9,7 @@ from bob.io.image import to_matplotlib ...@@ -9,6 +9,7 @@ from bob.io.image import to_matplotlib
from bob.extension import rc from bob.extension import rc
import bob.extension.download import bob.extension.download
import bob.io.base import bob.io.base
import multiprocessing
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -45,7 +46,7 @@ def get_model_filenames(model_dir): ...@@ -45,7 +46,7 @@ def get_model_filenames(model_dir):
ckpt_file = step_str.groups()[0] ckpt_file = step_str.groups()[0]
return meta_file, ckpt_file return meta_file, ckpt_file
_semaphore = multiprocessing.Semaphore()
class FaceNet(object): class FaceNet(object):
"""Wrapper for the free FaceNet variant: """Wrapper for the free FaceNet variant:
https://github.com/davidsandberg/facenet https://github.com/davidsandberg/facenet
...@@ -85,9 +86,19 @@ class FaceNet(object): ...@@ -85,9 +86,19 @@ class FaceNet(object):
super(FaceNet, self).__init__() super(FaceNet, self).__init__()
self.model_path = model_path self.model_path = model_path
self.image_size = image_size self.image_size = image_size
self.layer_name = layer_name
self._clean_unpicklables()
def _clean_unpicklables(self):
self.session = None self.session = None
self.embeddings = None self.embeddings = None
self.layer_name = layer_name self.graph = None
self.images_placeholder = None
self.embeddings = None
self.phase_train_placeholder = None
self.session = None
def _check_feature(self, img): def _check_feature(self, img):
img = numpy.ascontiguousarray(img) img = numpy.ascontiguousarray(img)
...@@ -142,17 +153,18 @@ class FaceNet(object): ...@@ -142,17 +153,18 @@ class FaceNet(object):
logger.info("Successfully loaded the model.") logger.info("Successfully loaded the model.")
def __call__(self, img): def __call__(self, img):
images = self._check_feature(img) with _semaphore:
if self.session is None: images = self._check_feature(img)
self.graph = tf.Graph() if self.session is None:
self.session = tf.compat.v1.Session(graph=self.graph) self.graph = tf.Graph()
if self.embeddings is None: self.session = tf.compat.v1.Session(graph=self.graph)
self.load_model() if self.embeddings is None:
feed_dict = { self.load_model()
self.images_placeholder: images, feed_dict = {
self.phase_train_placeholder: False, self.images_placeholder: images,
} self.phase_train_placeholder: False,
features = self.session.run(self.embeddings, feed_dict=feed_dict) }
features = self.session.run(self.embeddings, feed_dict=feed_dict)
return features.flatten() return features.flatten()
@staticmethod @staticmethod
...@@ -176,3 +188,13 @@ class FaceNet(object): ...@@ -176,3 +188,13 @@ class FaceNet(object):
) )
return model_path return model_path
def __setstate__(self, d):
# Handling unpicklable objects
self.__dict__ = d
def __getstate__(self):
# Handling unpicklable objects
with _semaphore:
self._clean_unpicklables()
return self.__dict__
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