serengil/retinaface

Error when using retinaface with the latest TensorFlow version (2.16.1)

Dobiasd opened this issue ยท 6 comments

When updating TensorFlow to the latest version 2.16.1, retinaface no longer works. Here is a minimal example:

Code:

from retinaface import RetinaFace
RetinaFace.detect_faces('img1.jpg')

Error:

Traceback (most recent call last):
  File "<string>", line 1, in <module>
  File "/usr/local/lib/python3.12/site-packages/retinaface/RetinaFace.py", line 88, in detect_faces
    model = build_model()
            ^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/retinaface/RetinaFace.py", line 46, in build_model
    retinaface_model.build_model(),
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/retinaface/model/retinaface_model.py", line 1027, in build_model
    x1_shape = tf.shape(ssh_c3_up)
               ^^^^^^^^^^^^^^^^^^^
  File "/usr/local/lib/python3.12/site-packages/tensorflow/python/util/traceback_utils.py", line 153, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "/usr/local/lib/python3.12/site-packages/keras/src/backend/common/keras_tensor.py", line 92, in __tf_tensor__
    raise ValueError(
ValueError: A KerasTensor cannot be used as input to a TensorFlow function. A KerasTensor is a symbolic placeholder for a shape and dtype, used when constructing Keras Functional models or Keras Functions. You can only use it as input to a Keras layer or a Keras operation (from the namespaces `keras.layers` and `keras.operations`). You are likely doing something like:


x = Input(...)
...
tf_fn(x)  # Invalid.


What you should do instead is wrap `tf_fn` in a layer:


class MyLayer(Layer):
    def call(self, x):
        return tf_fn(x)

x = MyLayer()(x)

The following Dockerfile can be used (docker build) to reproduce the problem:

FROM python:3.12.2

# libGL is needed to avoid "ImportError: libGL.so.1" in OpenCV
# libglib2.0-0 is needed to avoid "ImportError: libgthread-2.0.so.0" in OpenCV
RUN apt-get update && apt-get install -y \
  libglib2.0-0 \
  libgl1-mesa-glx \
  && rm -rf /var/lib/apt/lists/*

RUN pip3 install tensorflow==2.16.1
RUN pip3 install retina-face==0.0.15

RUN wget https://raw.githubusercontent.com/serengil/retinaface/master/tests/dataset/img3.jpg -O img3.jpg
RUN python3 -c "from retinaface import RetinaFace; RetinaFace.detect_faces('img3.jpg')"

Need to downgrade tf to 2.15

Thanks for the quick response. ๐Ÿ‘

Yeah, retinaface works fine with TF 2.15. However, TF 2.15 requires Python 3.11, i.e., it blocks me from updating to Python 3.12.

Are you planning on implementing a fix regarding retinaface with TF 2.16, or should I look for a different solution?

Not soon even if it is going to be done

@Dobiasd

1- would you please install pip install tf-keras

2- then, would you please add the following line before importing deepface & retinaface

import os
os.environ["TF_USE_LEGACY_KERAS"]="1"

It works, awesome! ๐Ÿš€

Here is the Dockerfile to confirm this:

FROM python:3.12.2

# libGL is needed to avoid "ImportError: libGL.so.1" in OpenCV
# libglib2.0-0 is needed to avoid "ImportError: libgthread-2.0.so.0" in OpenCV
RUN apt-get update && apt-get install -y \
  libglib2.0-0 \
  libgl1-mesa-glx \
  && rm -rf /var/lib/apt/lists/*

RUN pip3 install tensorflow==2.16.1
RUN pip3 install tf-keras==2.16.0
RUN pip3 install retina-face==0.0.15

RUN wget https://raw.githubusercontent.com/serengil/retinaface/master/tests/dataset/img3.jpg -O img3.jpg
RUN python3 -c "import os; os.environ['TF_USE_LEGACY_KERAS']='1'; from retinaface import RetinaFace; RetinaFace.detect_faces('img3.jpg')"

Thanks a lot! ๐Ÿ˜ƒ

I will add this into the source code! Thank you.