From tensorflow.keras.models import load_model ModuleNotFoundError: No module named 'tensorflow.keras'

import tensorflow as tf
from tensorflow.keras.models import load_model

import cv2 # Install opencv-python
import numpy as np

Disable scientific notation for clarity

np.set_printoptions(suppress=True)

Load the model

model = load_model(“keras_Model.h5”, compile=False)

Load the labels

class_names = open(“labels.txt”, “r”).readlines()

CAMERA can be 0 or 1 based on default camera of your computer

camera = cv2.VideoCapture(0)

while True:
# Grab the webcamera’s image.
ret, image = camera.read()

# Resize the raw image into (224-height,224-width) pixels
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)

# Show the image in a window
cv2.imshow("Webcam Image", image)

# Make the image a numpy array and reshape it to the models input shape.
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)

# Normalize the image array
image = (image / 127.5) - 1

# Predicts the model
prediction = model.predict(image)
index = np.argmax(prediction)
class_name = class_names[index]
confidence_score = prediction[0][index]

# Print prediction and confidence score
print("Class:", class_name[2:], end="")
print("Confidence Score:", str(np.round(confidence_score * 100))[:-2], "%")

# Listen to the keyboard for presses.
keyboard_input = cv2.waitKey(1)

# 27 is the ASCII for the esc key on your keyboard.
if keyboard_input == 27:
    break

camera.release()
cv2.destroyAllWindows()

This is my code I’m getting the error
from tensorflow.keras.models import load_model ModuleNotFoundError: No module named ‘tensorflow.keras’

can anyone help Thank you

The root cause of the error ModuleNotFoundError: No module named 'tensorflow.keras' is likely due to one of the following issues:

  1. TensorFlow Not Installed: TensorFlow might not be installed in your Python environment.
  2. Environment Issue: You might be running your script in a different Python environment where TensorFlow is not installed.
  3. Outdated TensorFlow Version: You might be using an older version of TensorFlow that does not include the tensorflow.keras submodule.
  4. Corrupted TensorFlow Installation: Your TensorFlow installation could be corrupted or incomplete.

To resolve this, ensure TensorFlow is installed and up-to-date in your active Python environment.

Hi @Sahil_006, Could you please try to import keras directly instead of importing keras from tensorflow, and from keras.model you can load load_model method

import keras
from keras.models import load_model

Thank You.

1 Like

I have the same issue with keras and tensorflow.

I have MacPro M1.
I had to delete and reinstall tensorflow. Here is what I did:

conda install -y -c apple tensorflow-deps
python -m pip install tensorflow-macos
python -m pip install tensorflow-metal

After the installation:
(base) tolynovik@ ~ % pip list | grep tensorflow
tensorflow 2.16.1
tensorflow-datasets 4.8.3+nightly
tensorflow-estimator 2.12.0
tensorflow-hub 0.8.0
tensorflow-io-gcs-filesystem 0.36.0
tensorflow-macos 2.16.1
tensorflow-metadata 0.14.0
tensorflow-metal 1.1.0
tensorflow-probability 0.14.0

(base) tolynovik@ ~ % pip list | grep keras
keras 3.1.1

But when I run the code I get these errors:

ModuleNotFoundError Traceback (most recent call last)
Cell In[18], line 4
2 import pandas as pd
3 import tensorflow as tf
----> 4 import tensorflow.keras as keras
5 from tensorflow.keras.models import Sequential, Model
6 from matplotlib import pyplot as plt

ModuleNotFoundError: No module named ‘tensorflow.keras’

ModuleNotFoundError Traceback (most recent call last)
Cell In[20], line 4
2 import pandas as pd
3 import tensorflow as tf
----> 4 import keras
6 from matplotlib import pyplot as plt

File ~/anaconda3/lib/python3.10/site-packages/keras/engine/functional.py:24
21 import itertools
22 import warnings
—> 24 import tensorflow.compat.v2 as tf
26 from keras import backend
27 from keras.dtensor import layout_map as layout_map_lib

ModuleNotFoundError: No module named ‘tensorflow.compat’

It is very frustrating!

I do not want to reinstall everything again (conda, python, etc.)
Please, help