Are you familiar with the error attributeerror: module ‘keras.preprocessing.image’ has no attribute ‘load_img’? Have you ever come across one like this? Why does this error occur?
We know how frustrating it is to encounter an error while working on a project. That is why we are here to help you solve such an error. In this article, we will show you how to solve the error
attributeerror: module 'keras.preprocessing.image' has no attribute 'load_img'.
Aside from that, we will also explain to you why this error occurs and, for additional information, what Python is. So, without further ado, let us enhance our knowledge with this information:
Why does this error occur? The reason why this error occurs is due to the deprecated Keras preprocessing API. The other possible reason is that Keras is outdated and does not have the load_img() function, or it was incorrectly installed. What is Python? Python is one of the most popular programming languages. It is used for developing a wide range of applications. It is a high-level programming language that is usually used by developers nowadays due to its flexibility.
Returning to our issue, we must take a few actions to fix this error. Here’s our “how to fix this error” tutorial.
How to solve “module ‘keras.preprocessing.image’ has no attribute ‘load_img'” in Python
Time needed: 3 minutes.
Here’s how to resolve the error message stating
attributeerror: module 'keras.preprocessing.image' has no attribute 'load_img' in Python.
- Import the load_img() function.
Resolving the error
attributeerror: module 'keras.preprocessing.image' has no attribute 'load_img'is an easy task. All you have to do is import the
Here’s an example:
from tensorflow.keras.utils import load_img
img = load_img(
- Import the whole TensorFlow module.
The following code below is the correct import of TensorFlow module.
import tensorflow as tf
img = tf.keras.utils.load_img(
Upgrade the Keras module
If the error still exists, it might be because the Keras module installed in your system is outdated and does not have the
load_img() function. Try upgrading it by using the command
pip install keras --upgrade.
This command will upgrade the Keras module to its latest version.
Reinstall the Keras module
Another way, if the error still exists after upgrading it, is to reinstall the Keras module. To do so, follow the steps below.
- Uninstall the Keras module.
To uninstall the Keras module, input the
pip uninstall kerascommand, then press the Enter key.
After inputting the
pip uninstall kerascommand, results will come out, and this question will also appear (Proceed (Y/n)?). Once that appears, just type Y, then click the Enter key.
- Install the Keras module.
After uninstalling the Keras module, install it again. To do so, enter the
pip install kerascommand.
Note: If you’re using Python 3, use the command
pip3 install keras. Use the command
!pip install kerasif you’re using Jupyter Notebook; however, use the command
conda install -c conda-forge kerasif you’re using Anaconda.
Commands you might need
This command will display all the packages installed on your system, including their versions.
If you’re using Jupyter Notebook, use the
!pip listcommand instead of
pip list. However, if you’re using Anaconda, use the command
Include this command in your
command if you get an error message stating that “pip” cannot be found. (example:
pip install keras
python -m pip install keras)
However, if you’re using Python 3, use the command
python3 -m pip install kerasinstead of
python -m pip install keras.
pip install --upgrade pip
Use this command to upgrade the pip package manager to its newest version. If your pip is already in the latest version, this will come out: “Requirement already satisfied.”
Use this command if you want to check what version of pip you have or have installed on your system.
Use this command if you want to check what version of Python you have.
pip show keras
Use this command to display information about your Keras module, including its location.
If you’re using Jupyter Notebook, use the command
!pip show kerasinstead of
pip show keras.
In conclusion, the error attributeerror: module ‘keras.preprocessing.image’ has no attribute ‘load_img’ can be easily solved by importing the
load_img() function from
tensorflow.keras.utils.load_img. You can also simply upgrade the Keras module or reinstall it.
By following the guide above, there’s no doubt that you’ll be able to resolve this error quickly.
We hope you’ve learned a lot from this. If you have any questions or suggestions, please leave a comment below, and for more Python tutorials, visit our website.
Thank you for reading!