Problems when I implemented my own Face Recognition projects

Hi,

I have finished Course 4 couple weeks ago and the Face Recognition assignment inspired me a lot. So after a while learning how facenet works, I decided to do an IoT project related to this field with my friends. But when I load the pretrained facenet_keras model, it’s said that “EOFError: EOF read where object expected” and when I tried with another computer, it’s said “bad marshal data”. I’m using tensorflow and keras version 2.13, python version 3.11. My code below:

from mtcnn.mtcnn import MTCNN

import tensorflow as tf

from PIL import Image

import numpy as np

import os

from sklearn.preprocessing import LabelEncoder

from sklearn.preprocessing import Normalizer

from sklearn.svm import SVC

from keras.models import load_model
class FaceRecognition:
    def __init__(self, path = r"C:\facenet_keras.h5"): 
        self.detector = MTCNN()
        self.__model = load_model(path)
        self.clf = SVC(kernel='linear', probability=True)
        self.__embedding = []
        self.out_encoder = None
        self.in_encoder = None

I don’t know if I can ask something not too relate to this course assignment but still hope you could help me with this problem. I searched a lot but have no idea.

Thanks for spending time.
Have a nice day.

Hello @QUY_LE_MINH

Usually people need to see the full error traceback. Can you provide it?

Raymond

sorry for this inconvenience, here is my full error traceback:

Traceback (most recent call last):
  File "c:\face_detection.py", line 16, in __init__
    self.__model = load_model(self.__path)
                   ^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\saving\saving_api.py", line 238, in load_model
    return legacy_sm_saving_lib.load_model(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\Admin\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.11_qbz5n2kfra8p0\LocalCache\local-packages\Python311\site-packages\keras\src\utils\generic_utils.py", line 102, in func_load
    code = marshal.loads(raw_code)
           ^^^^^^^^^^^^^^^^^^^^^^^
EOFError: EOF read where object expected

Thanks. Where did you get that h5 model file?

I got it in here (model folder): keras-facenet - Google Drive

How did you find this google drive? My ultimate question is, what is the link to the webpage that describes the creation of this model?

I got from here: https://machinelearningmastery.com/how-to-develop-a-face-recognition-system-using-facenet-in-keras-and-an-svm-classifier/

I think you can check out the author’s environment here

1 Like

I see. But far as I know python 3.6 is no longer available. Even google colab, . . . don’t support it anymore :cry:

Hi @QUY_LE_MINH,

“bad Marshall” error usually occurs when there are compatibility issues with python and other packages.

As for python3.6, if 3.6.0 is not available, you can try 3.6.15 as available here, or any other 3.6.x. Also, be sure to use the TF version which worked with 3.6.x

1 Like

I have tried it before but it didn’t work :

But surprisingly there are a lot of developers out there have the same problem with me. So far I know that since 2022 we can’t load that pretrained model because python only support version 3.7 to 3.11 while it need loading using version <3.7. Instead, we can install keras-facenet via pip. It is more straightforward to do face recognition but sadly I can’t play some coding like the assignment.

Thanks for spending time helping me.
Have a nice day.

1 Like

Hi @QUY_LE_MINH,

The link I shared, you can find other versions of 3.6 in there as well. They do support 3.7 to 3.11, but the previous versions still remain (as in, they are no longer actively looked after), and can be used.

I’m having the same issue. Sadly it is not practical to switch across python environments for every single issue and project :frowning: There are various dependencies in my environment (GPU, etc). Is there any other fix? Is there an updated facenet model h5 file we can leverage here in place?

I was able to find a workaround and fix this issue to get it to work. I imported the Inception ResNet python file that manually built the model. And then I imported the weights. This seems to be running for now. Thank you to @rmwkwok for providing the github reference. I used that to identify the tf/keras code used to build the model and basically just rebuilt the same model manually.

#Step 1
import inception_resnet_v1

#Step 2
model = inception_resnet_v1.InceptionResNetV1()
model.load_weights('keras-facenet-h5/facenet_keras_weights.h5')