import mtcnn cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’

import mtcnn 报错

ImportError: cannot import name ‘get_config’ from 'tensorflow.python.eager.context’

环境: tensorflow2.2, tensorflow2.3, mtcnn0.1.0

>>> import mtcnn
D:\anaconda\lib\site-packages\numpy\_distributor_init.py:30: UserWarning: loaded more than 1 DLL from .libs:
D:\anaconda\lib\site-packages\numpy\.libs\libopenblas.PYQHXLVVQ7VESDPUVUADXEVJOBGHJPAY.gfortran-win_amd64.dll
D:\anaconda\lib\site-packages\numpy\.libs\libopenblas.WCDJNK7YVMPZQ2ME2ZZHJJRJ3JIKNDB7.gfortran-win_amd64.dll
  warnings.warn("loaded more than 1 DLL from .libs:\n%s" %
2021-05-25 16:16:10.232287: W tensorflow/stream_executor/platform/default/dso_loader.cc:59] Could not load dynamic library 'cudart64_101.dll'; dlerror: cudart64_101.dll not found
2021-05-25 16:16:10.232414: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine.
Traceback (most recent call last):
  File "", line 1, in 
  File "D:\anaconda\lib\site-packages\mtcnn\__init__.py", line 26, in 
    from mtcnn.mtcnn import MTCNN
  File "D:\anaconda\lib\site-packages\mtcnn\mtcnn.py", line 37, in 
    from mtcnn.network.factory import NetworkFactory
  File "D:\anaconda\lib\site-packages\mtcnn\network\factory.py", line 26, in 
    from keras.layers import Input, Dense, Conv2D, MaxPooling2D, PReLU, Flatten, Softmax
  File "C:\Users\XXX\AppData\Roaming\Python\Python38\site-packages\keras\__init__.py", line 25, in 
    from keras import models
  File "C:\Users\XXX\AppData\Roaming\Python\Python38\site-packages\keras\models.py", line 19, in 
    from keras import backend
  File "C:\Users\XXX\AppData\Roaming\Python\Python38\site-packages\keras\backend.py", line 37, in 
    from tensorflow.python.eager.context import get_config
ImportError: cannot import name 'get_config' from 'tensorflow.python.eager.context' (D:\anaconda\lib\site-packages\tensorflow\python\eager\context.py)

问题原因:

该问题是因为mtcnn包中D:\anaconda\lib\site-packages\mtcnn\network\factory.py这个py文件中导入keras时使用如下代码,在 tensorflow2.X 中导入keras模块代码使用下述方法会报错

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

解决办法:

将上述py文件中代码修改为:

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

ImportError: cannot import name ‘get_config’ from ‘tensorflow.python.eager.context’ 这个类型报错的问题我遇到的都可以通过导入包时使用tensorflow.keras解决

你可能感兴趣的:(python,mtcnn,Bug,tensorflow,debug)