serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://bit.ly/deepface-py
MIT License
14.7k stars 2.22k forks source link

AttributeError: 'KerasHistory' object has no attribute 'layer' #1086

Closed FabioCanavarro closed 8 months ago

FabioCanavarro commented 8 months ago

I am running the following code but it keeps telling me that AttributeError: 'KerasHistory' object has no attribute 'layer', are there anything i can use to fix this issue. this is the code:

TF_ENABLE_ONEDNN_OPTS=0 import cv2 import pandas as pd import keras from deepface import DeepFace as df cap = cv2.VideoCapture(0) cascade = cv2.CascadeClassifier(r"D:\FaceDetect\haarcascade_frontalface_default.xml") while True: face = [] ret, frame = cap.read() if cv2.waitKey(1) and (0XFF == ord("q")): break for (x,y,w,h) in cascade.detectMultiScale(frame,1.15,3): face.append(frame[x:x+w,y:y+h]) cv2.rectangle(frame,(x,y),(x+w,y+h),(255,255,255),1) for img in face: df.find(img,r"D:\FaceDetect\database","ArcFace")

cv2.imshow("frame",frame)

cap.release() cv2.destroyAllWindows()

my deepface is in its latest version 0.086 this is the error message:

Traceback (most recent call last): File "d:\FaceDetect\FaceDetect.py", line 16, in df.find(img,r"D:\FaceDetect\database","ArcFace") File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\DeepFace.py", line 301, in find return recognition.find( ^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\modules\recognition.py", line 96, in find model: FacialRecognition = modeling.build_model(model_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\modules\modeling.py", line 46, in build_model model_obj[model_name] = model() ^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\basemodels\ArcFace.py", line 54, in init self.model = load_model() ^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\basemodels\ArcFace.py", line 80, in load_model base_model = ResNet34() ^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\basemodels\ArcFace.py", line 130, in ResNet34 model = training.Model(img_input, x, name="ResNet34") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\trackable\base.py", line 204, in _method_wrapper result = method(self, *args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\keras\engine\functional.py", line 116, in init self._init_graph_network(inputs, outputs) File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\trackable\base.py", line 204, in _method_wrapper result = method(self, *args, *kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\keras\engine\functional.py", line 152, in _init_graph_network self._validate_graph_inputs_and_outputs() File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\keras\engine\functional.py", line 694, in _validate_graph_inputs_and_outputs layer = x._keras_history.layer ^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'KerasHistory' object has no attribute 'layer' PS C:\Users\ASUS> ^C PS C:\Users\ASUS> & C:/Users/ASUS/AppData/Local/Microsoft/WindowsApps/python3.12.exe d:/FaceDetect/FaceDetect.py 2024-03-10 16:51:33.062190: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2024-03-10 16:51:33.441281: I tensorflow/core/util/port.cc:113] oneDNN custom operations are on. You may see slightly different numerical results due to floating-point round-off errors from different computation orders. To turn them off, set the environment variable TF_ENABLE_ONEDNN_OPTS=0. 2024-03-10 16:51:37.024612: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations. To enable the following instructions: AVX2 AVX_VNNI FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags. Traceback (most recent call last): File "d:\FaceDetect\FaceDetect.py", line 17, in df.find(img,r"D:\FaceDetect\database","ArcFace") File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\DeepFace.py", line 301, in find return recognition.find( ^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\modules\recognition.py", line 96, in find model: FacialRecognition = modeling.build_model(model_name) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\modules\modeling.py", line 46, in build_model model_obj[model_name] = model() ^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\basemodels\ArcFace.py", line 54, in init self.model = load_model() ^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\basemodels\ArcFace.py", line 80, in load_model base_model = ResNet34() ^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\deepface\basemodels\ArcFace.py", line 130, in ResNet34 model = training.Model(img_input, x, name="ResNet34") ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\trackable\base.py", line 204, in _method_wrapper result = method(self, args, kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\keras\engine\functional.py", line 116, in init self._init_graph_network(inputs, outputs) File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\trackable\base.py", line 204, in _method_wrapper result = method(self, *args, **kwargs) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\keras\engine\functional.py", line 152, in _init_graph_network self._validate_graph_inputs_and_outputs() File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\tensorflow\python\keras\engine\functional.py", line 694, in _validate_graph_inputs_and_outputs layer = x._keras_history.layer ^^^^^^^^^^^^^^^^^^^^^^ AttributeError: 'KerasHistory' object has no attribute 'layer'

serengil commented 8 months ago

Which tensorflow version?

FabioCanavarro commented 8 months ago

sorry for the late reply

2.16.1 is my tensorflow version 3.0.5 is my keras version i dont use opencv but i use opencv-contib-python 4.9.0.80

serengil commented 8 months ago

would you please downgrade tensorflow?

e.g.

 pip uninstall tensorflow && pip install tensorflow==2.7.0
FabioCanavarro commented 8 months ago

ERROR: Could not find a version that satisfies the requirement tensorflow==2.7.0 (from versions: 2.16.0rc0, 2.16.1) ERROR: No matching distribution found for tensorflow==2.7.0

may i know the version of python and pip, i am sorry for the inconvinience

serengil commented 8 months ago

Python 3.8.12 pip 21.2.4

FabioCanavarro commented 8 months ago

i am so sorry but are there anyways to switch tensorflow to a lower version since after vigorous testing python 3.12, 3.11 and 3.8 doest let me download older version of tensorflow and i fi wanna change my pip this happened: PS C:\Users\ASUS> pip install tensorflow==1 Traceback (most recent call last): File "", line 198, in _run_module_as_main File "", line 88, in _run_code File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip__main.py", line 29, in from pip._internal.cli.main import main as _main File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\cli\main.py", line 9, in from pip._internal.cli.autocompletion import autocomplete File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\cli\autocompletion.py", line 10, in from pip._internal.cli.main_parser import create_main_parser File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\cli\main_parser.py", line 8, in from pip._internal.cli import cmdoptions File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\cli\cmdoptions.py", line 23, in from pip._internal.cli.parser import ConfigOptionParser File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\cli\parser.py", line 12, in from pip._internal.configuration import Configuration, ConfigurationError File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\configuration.py", line 21, in from pip._internal.exceptions import ( File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_internal\exceptions.py", line 7, in from pip._vendor.pkg_resources import Distribution File "C:\Users\ASUS\AppData\Local\Packages\PythonSoftwareFoundation.Python.3.12_qbz5n2kfra8p0\LocalCache\local-packages\Python312\site-packages\pip_vendor\pkg_resources\init__.py", line 2164, in register_finder(pkgutil.ImpImporter, find_on_path) ^^^^^^^^^^^^^^^^^^^ AttributeError: module 'pkgutil' has no attribute 'ImpImporter'. Did you mean: 'zipimporter'?

serengil commented 8 months ago

@FabioCanavarro recommend to create a fresh virtual environment with my python version. You may consider to use conda or pipenv.

FabioCanavarro commented 8 months ago

thank you i will try to do so

FabioCanavarro commented 8 months ago

its working thank you so much

serengil commented 8 months ago

Cheers!