serengil / deepface

A Lightweight Face Recognition and Facial Attribute Analysis (Age, Gender, Emotion and Race) Library for Python
https://www.youtube.com/watch?v=WnUVYQP4h44&list=PLsS_1RYmYQQFdWqxQggXHynP1rqaYXv_E&index=1
MIT License
10.98k stars 1.93k forks source link

[BUG]: ValueError: The layer sequential_4 has never been called and thus has no defined input. #1272

Closed mohibovais79 closed 2 days ago

mohibovais79 commented 1 week ago

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

DeepFace's version

0.0.92

Python version

3.12.3

Operating System

windows 11

Dependencies

tensorflow==2.16.2 keras==3.4.1

Reproducible example

from deepface import DeepFace
objs = DeepFace.analyze(
  img_path = "images.jpg", 
  actions = ['age', 'gender', 'race', 'emotion'],
)

Relevant Log Output

{ "name": "ValueError", "message": "The layer sequential_4 has never been called and thus has no defined input.", "stack": "--------------------------------------------------------------------------- ValueError Traceback (most recent call last) Cell In[24], line 2 1 from deepface import DeepFace ----> 2 objs = DeepFace.analyze( 3 img_path = \"images.jpg\", 4 actions = ['age', 'gender', 'race', 'emotion'], 5 )

File d:\conda\envs\comp3420\Lib\site-packages\deepface\DeepFace.py:247, in analyze(img_path, actions, enforce_detection, detector_backend, align, expand_percentage, silent, anti_spoofing) 160 def analyze( 161 img_path: Union[str, np.ndarray], 162 actions: Union[tuple, list] = (\"emotion\", \"age\", \"gender\", \"race\"), (...) 168 anti_spoofing: bool = False, 169 ) -> List[Dict[str, Any]]: 170 \"\"\" 171 Analyze facial attributes such as age, gender, emotion, and race in the provided image. 172 Args: (...) 245 - 'white': Confidence score for White ethnicity. 246 \"\"\" --> 247 return demography.analyze( 248 img_path=img_path, 249 actions=actions, 250 enforce_detection=enforce_detection, 251 detector_backend=detector_backend, 252 align=align, 253 expand_percentage=expand_percentage, 254 silent=silent, 255 anti_spoofing=anti_spoofing, 256 )

File d:\conda\envs\comp3420\Lib\site-packages\deepface\modules\demography.py:172, in analyze(img_path, actions, enforce_detection, detector_backend, align, expand_percentage, silent, anti_spoofing) 169 obj[\"dominant_emotion\"] = Emotion.labels[np.argmax(emotion_predictions)] 171 elif action == \"age\": --> 172 apparent_age = modeling.build_model(\"Age\").predict(img_content) 173 # int cast is for exception - object of type 'float32' is not JSON serializable 174 obj[\"age\"] = int(apparent_age)

File d:\conda\envs\comp3420\Lib\site-packages\deepface\modules\modeling.py:59, in build_model(model_name) 57 model = models.get(model_name) 58 if model: ---> 59 model_obj[model_name] = model() 60 else: 61 raise ValueError(f\"Invalid model_name passed - {model_name}\")

File d:\conda\envs\comp3420\Lib\site-packages\deepface\extendedmodels\Age.py:32, in ApparentAgeClient.init(self) 31 def init(self): ---> 32 self.model = load_model() 33 self.model_name = \"Age\"

File d:\conda\envs\comp3420\Lib\site-packages\deepface\extendedmodels\Age.py:61, in load_model(url) 57 base_model_output = Activation(\"softmax\")(base_model_output) 59 # -------------------------- ---> 61 age_model = Model(inputs=model.input, outputs=base_model_output) 63 # -------------------------- 64 65 # load weights 67 home = folder_utils.get_deepface_home()

File d:\conda\envs\comp3420\Lib\site-packages\keras\src\ops\operation.py:254, in Operation.input(self) 244 @property 245 def input(self): 246 \"\"\"Retrieves the input tensor(s) of a symbolic operation. 247 248 Only returns the tensor(s) corresponding to the first time (...) 252 Input tensor or list of input tensors. 253 \"\"\" --> 254 return self._get_node_attribute_at_index(0, \"input_tensors\", \"input\")

File d:\conda\envs\comp3420\Lib\site-packages\keras\src\ops\operation.py:285, in Operation._get_node_attribute_at_index(self, node_index, attr, attr_name) 269 \"\"\"Private utility to retrieves an attribute (e.g. inputs) from a node. 270 271 This is used to implement the properties: (...) 282 The operation's attribute attr at the node of index node_index. 283 \"\"\" 284 if not self._inbound_nodes: --> 285 raise ValueError( 286 f\"The layer {self.name} has never been called \" 287 f\"and thus has no defined {attr_name}.\" 288 ) 289 if not len(self._inbound_nodes) > node_index: 290 raise ValueError( 291 f\"Asked to get {attr_name} at node \" 292 f\"{node_index}, but the operation has only \" 293 f\"{len(self._inbound_nodes)} inbound nodes.\" 294 )

ValueError: The layer sequential_4 has never been called and thus has no defined input." }

Expected Result

No response

What happened instead?

No response

Additional Info

No response

serengil commented 1 week ago

Try to downgrade tf to 2.15

sanjeev-bhandari commented 4 days ago

hi, @mohibovais79
cc: @serengil Downgrading tf to 2.15 didn't work for me.

I had Linux Ubuntu 22.04 I had to downgrade tf to 2.8.0

mohibovais79 commented 4 days ago
from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, PReLU, Flatten, Softmax

ModuleNotFoundError: No module named 'tensorflow.keras' now getting the above error after downgrading to tensorflow==2.15.0

serengil commented 3 days ago

when you are using tf 2.16, you need to install tf_keras as highlighted here: https://github.com/serengil/deepface/blob/master/deepface/commons/package_utils.py#L37

if you insist to use tf 2.16, try to install this manually

serengil commented 3 days ago

@mohibovais79 i think your tf installation is not okay because tf.keras comes with tf 2.15

mohibovais79 commented 2 days ago

Thanks it worked. I want to know when using this result = DeepFace.analyze(face, actions=['emotion', 'gender', 'age'], enforce_detection=False) can we define model and backend in analyze function? like in verify

serengil commented 2 days ago

No, analyze does not cover many models like verify