Faheel Mohammad
Faheel Mohammad

Reputation: 115

Unable to load facenet_keras.h5 model in python

I am fairly new to tensorflow and dont have any idea what is going wrong. It keeps me showing this "bad marshall error" which i cant seem to understand is caused by what. version: python: 3.8 tensorflow:2.5.0 keras: 2.4.3

below is my code

import os
import tensorflow as tf
from tensorflow.keras import layers
from keras.models import load_model
from tensorflow.keras.models import Model
from tensorflow.python.keras.backend import set_session 
from flask import Flask, request
from flask_cors import CORS
import cv2
import json
import numpy as np
import base64
from datetime import datetime

database = {}
graph = tf.compat.v1.get_default_graph()
app = Flask(__name__)
CORS(app)
sess = tf.compat.v1.Session()
set_session(sess)


#loading model
model = load_model('facenet_keras.h5')
model.summary()

And below is the error that i am encountering:

Traceback (most recent call last):
  File "index.py", line 24, in <module>
    model = load_model('facenet_keras.h5')
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/saving/save.py", line 201, in load_model
    return hdf5_format.load_model_from_hdf5(filepath, custom_objects,
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/saving/hdf5_format.py", line 180, in load_model_from_hdf5
    model = model_config_lib.model_from_config(model_config,
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/saving/model_config.py", line 59, in model_from_config
    return deserialize(config, custom_objects=custom_objects)
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/layers/serialization.py", line 159, in deserialize
    return generic_utils.deserialize_keras_object(
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/utils/generic_utils.py", line 668, in deserialize_keras_object
    deserialized_obj = cls.from_config(
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/engine/training.py", line 2332, in from_config
    functional.reconstruct_from_config(config, custom_objects))
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/engine/functional.py", line 1274, in reconstruct_from_config
    process_layer(layer_data)
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/engine/functional.py", line 1256, in process_layer
    layer = deserialize_layer(layer_data, custom_objects=custom_objects)
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/layers/serialization.py", line 159, in deserialize
    return generic_utils.deserialize_keras_object(
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/utils/generic_utils.py", line 668, in deserialize_keras_object
    deserialized_obj = cls.from_config(
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/layers/core.py", line 1001, in from_config
    function = cls._parse_function_from_config(
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/layers/core.py", line 1053, in _parse_function_from_config
    function = generic_utils.func_load(
  File "/home/faheel/.local/lib/python3.8/site-packages/keras/utils/generic_utils.py", line 783, in func_load
    code = marshal.loads(raw_code)
ValueError: bad marshal data (unknown type code)

Upvotes: 2

Views: 9842

Answers (1)

Joshi Omkaar
Joshi Omkaar

Reputation: 41

Use keras-facenet library instead:

pip install keras-facenet

from keras_facenet import FaceNet

embedder = FaceNet()

Gets a detection dict for each face in an image. Each one has the bounding box and face landmarks (from mtcnn.MTCNN) along with the embedding from FaceNet.

detections = embedder.extract(image, threshold=0.95)

If you have pre-cropped images, you can skip the detection step.

embeddings = embedder.embeddings(images)

Upvotes: 4

Related Questions