serengil/retinaface

A KerasTensor cannot be used as input to a TensorFlow function

atg1966 opened this issue · 4 comments

I have played around with the DeepFace library and it works like a charm.

However, no luck while I am playing with RetinaFace, even on the most simplest example code.

I´m running this on Thonny 4.1.4:

from retinaface import RetinaFace
resp = RetinaFace.detect_faces(img_path = "group2.jpg")

Which leads to the following error:

Traceback (most recent call last):
File "C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\MyCode\retina_face_test.py", line 3, in
resp = RetinaFace.detect_faces(img_path = "group2.jpg")
File "C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\lib\site-packages\retinaface\RetinaFace.py", line 88, in detect_faces
model = build_model()
File "C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\lib\site-packages\retinaface\RetinaFace.py", line 46, in build_model
retinaface_model.build_model(),
File "C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\lib\site-packages\retinaface\model\retinaface_model.py", line 1027, in build_model
x1_shape = tf.shape(ssh_c3_up)
File "C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\lib\site-packages\tensorflow\python\util\traceback_utils.py", line 153, in error_handler
raise e.with_traceback(filtered_tb) from None
File "C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\lib\site-packages\keras\src\backend\common\keras_tensor.py", line 91, 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)

Installed libraries:

(RetinaFaceEnv) C:\Users\atger\Documents\PythonCode\RetinaFaceEnv\MyCode>pip freeze
absl-py==2.1.0
astunparse==1.6.3
beautifulsoup4==4.12.3
certifi==2024.2.2
charset-normalizer==3.3.2
colorama==0.4.6
contourpy==1.2.0
cycler==0.12.1
filelock==3.13.3
flatbuffers==24.3.25
fonttools==4.50.0
gast==0.5.4
gdown==5.1.0
google-pasta==0.2.0
grpcio==1.62.1
h5py==3.10.0
idna==3.6
keras==3.1.1
kiwisolver==1.4.5
libclang==18.1.1
Markdown==3.6
markdown-it-py==3.0.0
MarkupSafe==2.1.5
matplotlib==3.8.3
mdurl==0.1.2
ml-dtypes==0.3.2
namex==0.0.7
numpy==1.26.4
opencv-python==4.9.0.80
opt-einsum==3.3.0
optree==0.11.0
packaging==24.0
pillow==10.2.0
protobuf==4.25.3
Pygments==2.17.2
pyparsing==3.1.2
PySocks==1.7.1
python-dateutil==2.9.0.post0
requests==2.31.0
retina-face==0.0.15
rich==13.7.1
six==1.16.0
soupsieve==2.5
tensorboard==2.16.2
tensorboard-data-server==0.7.2
tensorflow==2.16.1
tensorflow-intel==2.16.1
tensorflow-io-gcs-filesystem==0.31.0
termcolor==2.4.0
tqdm==4.66.2
typing_extensions==4.10.0
urllib3==2.2.1
Werkzeug==3.0.1
wrapt==1.16.0

Any idea, please, about what I´m doing wrong ?

Many thanks !
ATG

Okay I know this issue, we basically need to do some changes similar to this PR: serengil/deepface#1126

Meanwhile, you can downgrade your tensorflow to 2.15

Or if you still want to use tensorflow 2.16, install tf_keras with pip install tf_keras, and then set the following environment variable before importing deepface (and tensorflow if you import it manually)

import os
os.environ["TF_USE_LEGACY_KERAS"] = "1"
from deepface import DeepFace

Serengil,

both solutions worked, and I kept the one using tensorflow 2.16.
I really appreciated the prompt reply and solution.

Thank you
ATG

@atg1966 I will sort this in the source code, after that release you do not have to set that env var anymore - because i will set it already.

still it is good to know nothing is blocking you right now. will close this ticket when i publish that release.

sorted with PR - #99