ModuleNotFoundError: No module named 'keras.engine'

Hello , can someone help me ? ModuleNotFoundError: No module named ‘keras.engine’ This is the message I received anaytime. in colab in pycharm . I try to make a model by training , I use few github mask rcnn and no way. I tried different Tensorflow & tensorflow2 and and… Someone have an idea ??? Thanks a lot

Hi @Leo_Verheyden, There is no module keras.engine. From tensorflow 2.x onwords all of the sub modules under the keras.engine are under different modules within the tf.keras. You can import keras using import keras directly or from tensorflow import keras. Thank You.

1 Like

Still have error keras.engine

Try checking the compatabilty of keras and tensorflow. Import statements have been changes a lot and try to import all the packages specifically you are using. Check the tensorflow documentation for more detailed help.

Would love to help you if you could provide the whole code if possible.

Hi everyone,
I’m running into the same problem.
Actually, we might have used the same MRCNN GitHub repository : GitHub - matterport/Mask_RCNN: Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow.
I am not that fluent with TensorFlow, it’s my first time trying Deep Learning.
I have been able to clone the repository, install the requirements using requirements.txt and launch setup.py.
But indeed, I do have the “No module named ‘keras.engine’” error. I tried several steps, including downgrading my python version within my environment to an older one, and it still doesn’t work.
The import statements that cause troubles are as follow :

import keras.backend as K
import keras.layers as KL
import keras.engine as KE
import keras.models as KM

Within model.py.

As an information, if it can help you, my packages within my environment are :
Package Version


absl-py 2.1.0
asttokens 2.4.1
astunparse 1.6.3
certifi 2024.2.2
charset-normalizer 3.3.2
contourpy 1.2.1
cycler 0.12.1
Cython 3.0.10
decorator 5.1.1
executing 2.0.1
flatbuffers 24.3.25
fonttools 4.51.0
gast 0.5.4
google-pasta 0.2.0
grpcio 1.62.1
h5py 3.10.0
idna 3.6
imageio 2.34.0
imgaug 0.4.0
ipython 8.23.0
jedi 0.19.1
keras 3.1.1
kiwisolver 1.4.5
lazy_loader 0.4
libclang 18.1.1
Markdown 3.6
markdown-it-py 3.0.0
MarkupSafe 2.1.5
matplotlib 3.8.4
matplotlib-inline 0.1.6
mdurl 0.1.2
ml-dtypes 0.3.2
namex 0.0.7
networkx 3.3
numpy 1.26.4
opencv-python 4.9.0.80
opt-einsum 3.3.0
optree 0.11.0
packaging 24.0
parso 0.8.4
pexpect 4.9.0
pillow 10.3.0
pip 24.0
prompt-toolkit 3.0.43
protobuf 4.25.3
ptyprocess 0.7.0
pure-eval 0.2.2
pycocotools 2.0.7
Pygments 2.17.2
pyparsing 3.1.2
python-dateutil 2.9.0.post0
PyYAML 6.0.1
requests 2.31.0
rich 13.7.1
scikit-image 0.22.0
scipy 1.13.0
setuptools 69.2.0
shapely 2.0.3
six 1.16.0
stack-data 0.6.3
tensorboard 2.16.2
tensorboard-data-server 0.7.2
tensorflow 2.16.1
termcolor 2.4.0
tifffile 2024.2.12
traitlets 5.14.2
typing_extensions 4.11.0
urllib3 2.2.1
wcwidth 0.2.13
Werkzeug 3.0.2
wheel 0.43.0
wrapt 1.16.0

If by any chance you have an idea on how to proceed to solve this issue, I would be more than happy…

Thanks for your time :wink:

Remove the import for keras.engine and replace all KE.Layer in the model.py file with KL.Layer

I got this when I tried what you told →
priate compiler flags.
Traceback (most recent call last):
File “/Users/karthikkaralgikar/Desktop/Aedes/custom.py”, line 173, in
model = modellib.MaskRCNN(mode=“training”, config=config,
(venv) karthikkaralgikar@Karthiks-MacBook-Pro Aedes % python custom.py
2024-04-28 21:31:53.746209: I tensorflow/core/platform/cpu_feature_guard.cc:210] This TensorFlow binary is optimized to use available CPU instructions in performance-critical operations.
To enable the following instructions: AVX2 FMA, in other operations, rebuild TensorFlow with the appropriate compiler flags.
Traceback (most recent call last):
File “/Users/karthikkaralgikar/Desktop/Aedes/custom.py”, line 173, in
model = modellib.MaskRCNN(mode=“training”, config=config,
File “/Users/karthikkaralgikar/Desktop/Aedes/mrcnn/model.py”, line 1837, in init
self.keras_model = self.build(mode=mode, config=config)
File “/Users/karthikkaralgikar/Desktop/Aedes/mrcnn/model.py”, line 1875, in build
gt_boxes = KL.Lambda(lambda x: norm_boxes_graph(
File “/Users/karthikkaralgikar/Desktop/Aedes/venv/lib/python3.9/site-packages/keras/src/utils/traceback_utils.py”, line 122, in error_handler
raise e.with_traceback(filtered_tb) from None
File “/Users/karthikkaralgikar/Desktop/Aedes/venv/lib/python3.9/site-packages/keras/src/layers/core/lambda_layer.py”, line 95, in compute_output_shape
raise NotImplementedError(
NotImplementedError: Exception encountered when calling Lambda.call().

We could not automatically infer the shape of the Lambda’s output. Please specify the output_shape argument for this Lambda layer.

Arguments received by Lambda.call():
• args=(‘<KerasTensor shape=(None, None, 4), dtype=float32, sparse=None, name=input_gt_boxes>’,)
• kwargs={‘mask’: ‘None’}

Did anyone figure it out? If yes, please reply. Thanks in advance!