解决AttributeError: module ‘keras.backend‘ has no attribute ‘image_dim_ordering‘错误

遇到的错误:

D:\Python-programing\PycharmProjects\pythonProject\venv\Scripts\python.exe D:/Python-programing/PycharmProjects/pythonProject/faceRecognition/c4_face_CNN_keras.py
2022-07-20 19:45:00.558490: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found
2022-07-20 19:45:00.558701: 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 "D:\Python-programing\PycharmProjects\pythonProject\faceRecognition\c4_face_CNN_keras.py", line 205, in <module>
    dataset.load()
  File "D:\Python-programing\PycharmProjects\pythonProject\faceRecognition\c4_face_CNN_keras.py", line 78, in load
    if (K.image_dim_ordering() == 'th'):  
AttributeError: module 'keras.backend' has no attribute 'image_dim_ordering'

看错误信息得出的信息:

解决办法:
经查资料,得出是 Keras的版本不同造成的,代码if K.image_dim_ordering() == "th": 是Keras旧版本使用的代码,如果你使用的是新版本,那么将 K.image_dim_ordering() == "th"代码改为 K.image_data_format() == "channels_first" 即可解决这个问题。
根本原因:版本不同导致的错误。

改正之后的代码:
在这里插入图片描述

你可能感兴趣的:(问题,keras,tensorflow,深度学习,python,pycharm)