Python
Keras
ImportError
load_img
Deep Learning

Cannot import name 'load_img' from 'keras.preprocessing.image'

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

The Cannot import name 'load_img' from 'keras.preprocessing.image' error usually comes from version mismatch between standalone Keras and TensorFlow-integrated Keras. The import path changed across releases, so old tutorials can fail in modern environments. The fix is selecting the import path that matches your installed package versions.

Use the Correct Import for Your Stack

In most current TensorFlow workflows, import from tensorflow.keras.utils:

python
from tensorflow.keras.utils import load_img, img_to_array

For some environments, tensorflow.keras.preprocessing.image may still work, but utils is a safer modern default for portability across recent releases.

Minimal image load example:

python
1from tensorflow.keras.utils import load_img, img_to_array
2
3img = load_img("cat.jpg", target_size=(224, 224))
4arr = img_to_array(img)
5
6print(arr.shape)  # (224, 224, 3)
7print(arr.dtype)

If this succeeds, your import path is aligned with installed TensorFlow.

Diagnose Version Conflicts Quickly

Many failures happen because keras and tensorflow packages are installed with incompatible versions. Check what is actually installed in the active environment.

bash
1python - <<'PY'
2import sys
3print("Python:", sys.version)
4
5try:
6    import tensorflow as tf
7    print("TensorFlow:", tf.__version__)
8except Exception as e:
9    print("TensorFlow import error:", e)
10
11try:
12    import keras
13    print("Keras:", keras.__version__)
14except Exception as e:
15    print("Keras import error:", e)
16PY

If both packages exist but versions differ significantly, choose one API style and align dependencies.

Prefer One Keras Source in a Project

Mixing imports like from keras... and from tensorflow.keras... in the same codebase can cause subtle runtime issues, including incompatible object types and serialization errors.

Pick one convention:

  • TensorFlow-centric project: use tensorflow.keras imports everywhere
  • standalone Keras project: use keras imports consistently and pin matching backend setup

Example cleanup:

python
1# Avoid mixed style
2# from keras.models import Sequential
3# from tensorflow.keras.layers import Dense
4
5# Prefer consistent TensorFlow Keras style
6from tensorflow.keras.models import Sequential
7from tensorflow.keras.layers import Dense
8from tensorflow.keras.utils import load_img

Consistency reduces debugging time when moving between training and serving scripts.

Rebuild a Clean Environment if Needed

When dependency drift is severe, rebuilding the virtual environment is often faster than patching packages in place.

bash
1python -m venv .venv
2source .venv/bin/activate
3pip install --upgrade pip
4pip install "tensorflow==2.16.*"

Then run a smoke test:

python
from tensorflow.keras.utils import load_img
print("Import OK")

Lock versions in a dependency file so teammates and CI use the same stack.

Legacy Code Migration Pattern

If you inherit older notebooks, search and replace outdated imports carefully.

Common migration:

  • old: from keras.preprocessing.image import load_img
  • new: from tensorflow.keras.utils import load_img

After migration, rerun preprocessing pipelines and verify output shapes and dtypes. Import fixes can still hide behavior changes if downstream defaults differ.

Common Pitfalls

  • Installing both keras and tensorflow without version alignment.
  • Mixing standalone Keras imports with TensorFlow Keras imports in one script.
  • Assuming tutorial import paths from older releases still apply unchanged.
  • Updating one package only and leaving stale transitive dependencies.
  • Running code in a different interpreter than the one where packages were installed.

Summary

  • The load_img import error is usually a package-version path mismatch.
  • For modern TensorFlow workflows, use tensorflow.keras.utils imports.
  • Check installed versions and keep one consistent Keras API style.
  • Rebuild virtual environments when dependency drift becomes hard to reason about.
  • Pin versions and run smoke tests to prevent recurring import regressions.
  • Add a small startup diagnostic script in CI that verifies critical imports before model training begins.
  • Keep notebook and production environments synced with one lock file to avoid hidden package drift.
  • Re-test image preprocessing output after migration to confirm unchanged data semantics.

Course illustration
Course illustration

All Rights Reserved.