Search code examples
pythontensorflowmachine-learningdeep-learningdeepface

the layer sequential has never been called and thus has no defined input


I am running a simple script within my Anaconda virtual env

from deepface import DeepFace

face_analysis = DeepFace.analyze(img_path = "face3.jpeg")
print(face_analysis)

But I keep getting this error.

Action: age:  25%|██████████████████████████▊                                                                                | 1/4 [00:02<00:06,  2.08s/it]
Traceback (most recent call last):
  File "C:\Users\Ctrend.pk\Cheer-Check\test2.py", line 9, in <module>
    analysis = DeepFace.analyze(img_path)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\DeepFace.py", line 222, in analyze
    return demography.analyze(
           ^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\modules\demography.py", line 157, in analyze
    apparent_age = modeling.build_model("Age").predict(img_content)
                   ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\modules\modeling.py", line 57, in build_model
    model_obj[model_name] = model()
                            ^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\extendedmodels\Age.py", line 32, in __init__
    self.model = load_model()
                 ^^^^^^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\extendedmodels\Age.py", line 61, in load_model
    age_model = Model(inputs=model.input, outputs=base_model_output)
                             ^^^^^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\keras\src\ops\operation.py", line 228, in input
    return self._get_node_attribute_at_index(0, "input_tensors", "input")
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\Ctrend.pk\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\keras\src\ops\operation.py", line 259, in _get_node_attribute_at_index
    raise ValueError(
ValueError: The layer sequential_1 has never been called and thus has no defined input.

Deepface version: 0.0.87

tensorflow Version: 2.16.1

I think it fetches the age but then doesn't proceed. What am I missing out on?


Solution

  • From tensorflow 2.16, keras 3 is the default keras version. Legacy keras functions are not working anymore. A workaround is to set TF_USE_LEGACY_KERAS environment variable to 1 before importing tensorflow and install tf_keras package to be able to use the legacy keras.

    That environment variable is set and tf_keras installation is enforced for users who are using tf 2.16 or later in the deepface's latest 0.0.89 version.

    TLDR: if you upgrade your deepface package to latest, then you should not have this trouble anymore.