serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
11.75k stars 2.02k forks source link

UnknownError: Exception encountered when calling layer 'norm_layer' (type Lambda) #957

Closed blevlabs closed 7 months ago

blevlabs commented 8 months ago
    198 """Check if face2_rgb_image matches the person in the image at face1_path."""
    199 # DeepFace usage
--> 200 verification = DeepFace.verify(img1_path=face1_path, img2_path=face2_rgb_image,
    201                                enforce_detection=False)
    202 return verification['verified']

File ~/deepface/deepface/DeepFace.py:114, in verify(img1_path, img2_path, model_name, detector_backend, distance_metric, enforce_detection, align, normalization)
     59 def verify(
     60     img1_path: Union[str, np.ndarray],
     61     img2_path: Union[str, np.ndarray],
   (...)
     67     normalization: str = "base",
     68 ) -> Dict[str, Any]:
     69     """
     70     This function verifies an image pair is same person or different persons. In the background,
     71     verification function represents facial images as vectors and then calculates the similarity
   (...)
    111 
    112     """
--> 114     return verification.verify(
    115         img1_path=img1_path,
    116         img2_path=img2_path,
    117         model_name=model_name,
    118         detector_backend=detector_backend,
    119         distance_metric=distance_metric,
    120         enforce_detection=enforce_detection,
    121         align=align,
    122         normalization=normalization,
    123     )

File ~/deepface/deepface/modules/verification.py:97, in verify(img1_path, img2_path, model_name, detector_backend, distance_metric, enforce_detection, align, normalization)
     95 for img1_content, img1_region, _ in img1_objs:
     96     for img2_content, img2_region, _ in img2_objs:
---> 97         img1_embedding_obj = representation.represent(
     98             img_path=img1_content,
     99             model_name=model_name,
    100             enforce_detection=enforce_detection,
    101             detector_backend="skip",
    102             align=align,
    103             normalization=normalization,
    104         )
    106         img2_embedding_obj = representation.represent(
    107             img_path=img2_content,
    108             model_name=model_name,
   (...)
    112             normalization=normalization,
    113         )
    115         img1_representation = img1_embedding_obj[0]["embedding"]

File ~/deepface/deepface/modules/representation.py:115, in represent(img_path, model_name, enforce_detection, detector_backend, align, normalization)
    110 # represent
    111 # if "keras" in str(type(model)):
    112 if isinstance(model, Model):
    113     # model.predict causes memory issue when it is called in a for loop
    114     # embedding = model.predict(img, verbose=0)[0].tolist()
--> 115     embedding = model(img, training=False).numpy()[0].tolist()
    116     # if you still get verbose logging. try call
    117     # - `tf.keras.utils.disable_interactive_logging()`
    118     # in your main program
    119 else:
    120     # SFace and Dlib are not keras models and no verbose arguments
    121     embedding = model.predict(img)[0].tolist()

File ~/anaconda3/envs/companioncore/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py:70, in filter_traceback.<locals>.error_handler(*args, **kwargs)
     67     filtered_tb = _process_traceback_frames(e.__traceback__)
     68     # To get the full stack trace, call:
     69     # `tf.debugging.disable_traceback_filtering()`
---> 70     raise e.with_traceback(filtered_tb) from None
     71 finally:
     72     del filtered_tb

File ~/deepface/deepface/basemodels/VGGFace.py:114, in loadModel.<locals>.<lambda>(x)
    112 base_model_output = Sequential()
    113 base_model_output = Flatten()(model.layers[-5].output)
--> 114 base_model_output = Lambda(lambda x: K.l2_normalize(x, axis=1), name="norm_layer")(
    115     base_model_output
    116 )
    117 vgg_face_descriptor = Model(inputs=model.input, outputs=base_model_output)
    119 return vgg_face_descriptor

UnknownError: Exception encountered when calling layer 'norm_layer' (type Lambda).

{{function_node __wrapped__Rsqrt_device_/job:localhost/replica:0/task:0/device:GPU:0}} JIT compilation failed. [Op:Rsqrt] name: 

Call arguments received by layer 'norm_layer' (type Lambda):
  • inputs=tf.Tensor(shape=(1, 4096), dtype=float32)
  • mask=None
  • training=False

Getting this issue. I referenced other issue tickets on this repo, and tried to implement the following

tf.keras.backend.set_image_data_format('channels_last')

But this did not help at all. Keras, tensorflow, and deepface package details are below:

Name: keras
Version: 2.15.0
Summary: Deep learning for humans.
Home-page: https://keras.io/
Author: Keras team
Author-email: keras-users@googlegroups.com
License: Apache 2.0
Location: /home/blabs/anaconda3/envs/companioncore/lib/python3.9/site-packages
Requires: 
Required-by: deepface, mtcnn, tensorflow
---
Name: tensorflow
Version: 2.15.0.post1
Summary: TensorFlow is an open source machine learning framework for everyone.
Home-page: https://www.tensorflow.org/
Author: Google Inc.
Author-email: packages@tensorflow.org
License: Apache 2.0
Location: /home/blabs/anaconda3/envs/companioncore/lib/python3.9/site-packages
Requires: absl-py, astunparse, flatbuffers, gast, google-pasta, grpcio, h5py, keras, libclang, ml-dtypes, numpy, opt-einsum, packaging, protobuf, setuptools, six, tensorboard, tensorflow-estimator, tensorflow-io-gcs-filesystem, termcolor, typing-extensions, wrapt
Required-by: deepface, retina-face
---
Name: deepface
Version: 0.0.81
Summary: A Lightweight Face Recognition and Facial Attribute Analysis Framework (Age, Gender, Emotion, Race) for Python
Home-page: https://github.com/serengil/deepface
Author: Sefik Ilkin Serengil
Author-email: serengil@gmail.com
License: 
Location: /home/blabs/deepface
Editable project location: /home/blabs/deepface
Requires: Deprecated, fire, Flask, gdown, gunicorn, keras, mtcnn, numpy, opencv-python, pandas, Pillow, retina-face, tensorflow, tqdm
serengil commented 8 months ago

I recommend you to downgrade tf and keras. I am using 2.9.0 for both, and it is fine.

BTW, I plan to get rid of the normalization layer of VGG-Face model (default model of deepface) in the next release. Meanwhile, you can try to use another model such as Facenet.

verification = DeepFace.verify(img1_path=face1_path, img2_path=face2_rgb_image,
                                model_name="Facenet", enforce_detection=False)
serengil commented 8 months ago

Meanwhile, ChatGPT recommends to disable JIT compilation

import os
os.environ['TF_XLA_FLAGS'] = '--tf_xla_enable_xla_devices'
serengil commented 7 months ago

Additionally, disabling GPU will help you in the worst case

import os
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"