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
13.73k stars 2.15k forks source link

DeepFace.analyze: AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects #254

Closed dalvarezdc closed 3 years ago

dalvarezdc commented 3 years ago

Hello! I am using DeepFace to analyze/classify some face images

The process is simple, I download an image into a folder and then I analyze it using DeepFace.analyze

        from deepface import DeepFace

        url = "url_to_imagefile.jpg"  

        # Download image
        file_path = os.path.join(BASE_DIR, "other_files_ignore_this/image.jpg")
        urllib.request.urlretrieve(url, r'{}'.format(save_path))

        print(os.path.isfile(file_path))
        # Analyze image
        obj = DeepFace.analyze(img_path=file_path, actions=['age', 'gender', 'race', 'emotion'])

This process gives me an AttributeError

*** AttributeError: module 'keras.utils.generic_utils' has no attribute 'populate_dict_with_module_objects'

This are my tensorflow packages installed:

tensorboard==2.5.0 tensorboard-data-server==0.6.1 tensorboard-plugin-wit==1.8.0 tensorflow==2.5.0 tensorflow-estimator==2.5.0

serengil commented 3 years ago

downgrade your tf to 2.2.0

dalvarezdc commented 3 years ago

Hello @serengil .

After downgrading I got the following error:

AttributeError: module 'tensorflow.compat.v2' has no attribute '__internal__'

innat commented 3 years ago

@dpresident check this.

davedgd commented 3 years ago

Not to dig this up again, but for anyone interested, the fix for this is simple when using the latest tensorflow (2.5.0). Simply edit the mtcnn/network/network.py file, which will appear in the error message, e.g.,

/home/davedgd/.pyenv/versions/3.9.5/lib/python3.9/site-packages/mtcnn/mtcnn.py

Specifically, edit the following lines to add tensorflow. to the keras imports:

from keras.layers import Input, Dense, Conv2D, MaxPooling2D, PReLU, Flatten, Softmax
from keras.models import Model

Specifically, these should be:

from tensorflow.keras.layers import Input, Dense, Conv2D, MaxPooling2D, PReLU, Flatten, Softmax
from tensorflow.keras.models import Model

Once this change is made, everything should work!

Note that these fixes are already up on the official mtcnn repo. The issue is that the pip package is out of date and does not include these fixes, but I expect Google colab is using a source version, since it works fine with tensorflow 2.5.0 as @dpresident pointed out.