serengil / retinaface

RetinaFace: Deep Face Detection Library for Python
https://www.youtube.com/watch?v=Wm1DucuQk70&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=3
MIT License
1.15k stars 150 forks source link

ValueError: A KerasTensor cannot be used as input to a TensorFlow function. (TensorFlow 2.17.0) #107

Closed szy-kenn closed 2 weeks ago

szy-kenn commented 2 weeks ago

Before You Report a Bug, Please Confirm You Have Done The Following...

RetinaFace's Version

0.0.17

Python Version

3.10.11

Operating System

Windows 11

Dependencies

absl-py==2.1.0 asttokens==2.4.1 astunparse==1.6.3 beautifulsoup4==4.12.3 certifi==2024.7.4 charset-normalizer==3.3.2 colorama==0.4.6 comm==0.2.2 debugpy==1.8.5 decorator==5.1.1 exceptiongroup==1.2.2 executing==2.0.1 filelock==3.15.4 flatbuffers==24.3.25 gast==0.6.0 gdown==5.2.0 google-pasta==0.2.0 grpcio==1.66.0 h5py==3.11.0 idna==3.8 ipykernel==6.29.5 ipython==8.26.0 jedi==0.19.1 jupyter_client==8.6.2 jupyter_core==5.7.2 keras==3.5.0 libclang==18.1.1 Markdown==3.7 markdown-it-py==3.0.0 MarkupSafe==2.1.5 matplotlib-inline==0.1.7 mdurl==0.1.2 ml-dtypes==0.4.0 namex==0.0.8 nest-asyncio==1.6.0 numpy==1.26.4 opencv-python==4.10.0.84 opt-einsum==3.3.0 optree==0.12.1 packaging==24.1 parso==0.8.4 pillow==10.4.0 platformdirs==4.2.2 prompt_toolkit==3.0.47 protobuf==4.25.4 psutil==6.0.0 pure_eval==0.2.3 Pygments==2.18.0 PySocks==1.7.1 python-dateutil==2.9.0.post0 pywin32==306 pyzmq==26.2.0 requests==2.32.3 retina-face==0.0.17 rich==13.7.1 six==1.16.0 soupsieve==2.6 stack-data==0.6.3 tensorboard==2.17.1 tensorboard-data-server==0.7.2 tensorflow==2.17.0 tensorflow-intel==2.17.0 tensorflow-io-gcs-filesystem==0.31.0 termcolor==2.4.0 tf_keras==2.17.0 tornado==6.4.1 tqdm==4.66.5 traitlets==5.14.3 typing_extensions==4.12.2 urllib3==2.2.2 wcwidth==0.2.13 Werkzeug==3.0.4 wrapt==1.16.0

Reproducible example

ret, frame = cap.read()
response = RetinaFace.detect_faces(frame)
print(response)

Relevant Log Output

Error during model build: 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)

Expected Result

To accept the argument and perform the face detection.

What happened instead?

An error occured, probably has something to do with the dependencies in tensorflow and tf-keras.

Additional Info

The provided error messages that I'm encountering are similar to those described in issues #93 and #97, where it has tensorflow v2.16 and no tf_keras dependency. It is also related in issue #106 where we have the similar tf version but installing tf_keras unfortunately didn't resolve the issue on my end. Downgrading tensorflow to 2.15 also didn't work.

Strange thing though is that the code works perfectly in Google Colab which has the exact same versions of the relevant modules/packages, but I am trying to make this work locally so if any solutions are available, I'd be glad to have that.

PedroRubinstein commented 2 weeks ago

Had a similar issue today on Linux also with TensorFlow 2.17.0. My problem was that TensorFlow got imported in a different file before RetinaFace was called. This meant TensorFlow was initialized without the legacy Keras support. Maybe you've got the same.

Fix:

Just added this line before importing TensorFlow, and it solved the issue:

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

serengil commented 2 weeks ago

@szy-kenn can you try @PedroRubinstein 's comment?

szy-kenn commented 2 weeks ago

Hey, sorry for the delay in getting back to this. Thanks for the suggestion btw @PedroRubinstein, but I eventually figured out the issue was with my GPU configuration - drivers, software, packages, and such. I'm still new to this so it was a bit of a rookie mistake on my part. Once my GPU has been detected, all is working as intended (I already installed tf_keras so I encountered no error on this part).

I'll be closing this issue now, thank you!