ImportError: cannot import name 'LocallyConnected2D' from 'tensorflow.keras.layers'

557 views Asked by At

I installed deepface by running:

pip install deepface

and running very simple code sample:

from deepface import DeepFace

result = DeepFace.verify(model_name='VGG-Face', detector_backend='yolov8', img1_path="img1.jpg", img2_path="img2.jpg")

facing this error:

. . . . . . . . . .
File "/home/real/miniconda3/envs/face_recognition_v1/lib/python3.9/site-packages/deepface/basemodels/FbDeepFace.py", line 29, in from tensorflow.keras.layers import ( ImportError: cannot import name 'LocallyConnected2D' from 'tensorflow.keras.layers'

How to solve this issue?

3

There are 3 answers

0
Scott On

I discovered that the LocallyConnected2D layer is no longer available in TensorFlow 2.x Keras API. To fix the issue, I downgraded TensorFlow to version 2.12 using the command:

pip install tensorflow==2.12
0
x89 On

For me, installing this version worked:

tensorflow==2.15.1

2.12 was not found

0
sefiks On

Seems LocallyConnected2D is retired or broken in tensorflow 2.16. In DeepFace 0.0.86, tf version is now be checked when model is set to (Facebook's) DeepFace because just that model requires LocallyConnected2D. On the other hand, earlier versions that dependency is tried to be imported independent from the model.

TLDR: upgrade deepface to its latest version not to get this exception