AttributeError: module ‘keras.preprocessing.image’ has no attribute ‘load_img’ error occurs when you “try to access ‘load_img’ attribute from ‘keras.preprocessing.image’ module which is deprecated.”
ImportError: cannot import name ‘load_img’ from ‘keras.preprocessing.image'” error is similar to AttributeError also occurs because the keras preprocessing API has been deprecated.
To fix the AttributeError: module ‘keras.preprocessing.image’ has no attribute ‘load_img’ error, “import the load_img function from tensorflow.keras.utils.load_img module.”
Reproducing the error
from keras.preprocessing import load_img
img = load_img(
'krunal.jpg',
target_size=(300, 600),
color_mode="grayscale"
)
img.show()
Output
ImportError: cannot import name 'load_img' from 'keras.preprocessing'
Since the API is deprecated, we got the error.
How to Fix it?
To fix the error, import the library like this: “from tensorflow.keras.utils import load_img”.
from tensorflow.keras.utils import load_img
img = load_img(
'krunal.jpg',
target_size=(300, 600),
color_mode="grayscale"
)
img.show()
Output
Alternate solutions
Solution 1: Upgrade your versions of tensorflow and keras
pip install tensorflow keras --upgrade
pip3 install tensorflow keras --upgrade
# 👇️ for Anaconda
conda install -c conda-forge tensorflow
conda update tensorflow
conda install -c conda-forge keras
conda update keras
# 👇️ for Jupyter Notebook
!pip install tensorflow keras --upgrade
Solution 2: Installation Issue
There could be some issues with your installation of Keras or TensorFlow. Try reinstalling these packages.
I faced a similar issue, and I solved it using the above solutions, and I hope you resolve it too.
Further reading
AttributeError: module ‘keras.utils’ has no attribute ‘get_value’
AttributeError: module ‘keras.utils’ has no attribute ‘to_categorical’
AttributeError: module ‘keras.utils’ has no attribute ‘plot_model’
AttributeError: module ‘keras.utils’ has no attribute ‘sequence’
AttributeError: module ‘keras.engine’ has no attribute ‘layer’
AttributeError: module ‘keras.utils’ has no attribute ‘get_file’

Krunal Lathiya is a seasoned Computer Science expert with over eight years in the tech industry. He boasts deep knowledge in Data Science and Machine Learning. Versed in Python, JavaScript, PHP, R, and Golang. Skilled in frameworks like Angular and React and platforms such as Node.js. His expertise spans both front-end and back-end development. His proficiency in the Python language stands as a testament to his versatility and commitment to the craft.