module ‘keras.engine’ has no attribute ‘Layer’

We Are Going To Discuss About module ‘keras.engine’ has no attribute ‘Layer’. So lets Start this Python Article.

module ‘keras.engine’ has no attribute ‘Layer’

  1. How to solve module 'keras.engine' has no attribute 'Layer'

    I found this in the github issue discussion and it worked for me.
    You need to uninstall those :
    pip uninstall keras -y pip uninstall keras-nightly -y pip uninstall keras-Preprocessing -y pip uninstall keras-vis -y pip uninstall tensorflow -y pip uninstall h5py -y
    and impose those versions :
    pip install tensorflow==1.13.1 pip install keras==2.0.8 pip install h5py==2.10.0a

  2. module 'keras.engine' has no attribute 'Layer'

    I found this in the github issue discussion and it worked for me.
    You need to uninstall those :
    pip uninstall keras -y pip uninstall keras-nightly -y pip uninstall keras-Preprocessing -y pip uninstall keras-vis -y pip uninstall tensorflow -y pip uninstall h5py -y
    and impose those versions :
    pip install tensorflow==1.13.1 pip install keras==2.0.8 pip install h5py==2.10.0a

Solution 1

I found this in the github issue discussion and it worked for me.

You need to uninstall those :

pip uninstall keras -y
pip uninstall keras-nightly -y
pip uninstall keras-Preprocessing -y
pip uninstall keras-vis -y
pip uninstall tensorflow -y
pip uninstall h5py -y

and impose those versions :

pip install tensorflow==1.13.1
pip install keras==2.0.8
pip install h5py==2.10.0a

Original Author Ayo Of This Content

Solution 2

I encountered this problem when I was running the project.
https://github.com/matterport/Mask_RCNN

In the file model.py,
there was a line

import keras.engine as KE

I changed it to

import keras.engine.topology as KE

and the problem disappeared.

Original Author bfhaha Of This Content

Solution 3

This isn’t strictly a duplicate, but a similar question is found here: AttributeError: module ‘keras.engine’ has no attribute ‘input_layer’

In essence, many of the import and attribute errors from keras come from the fact that keras changes its imports depending on whether you are using a CPU or using a GPU or ASIC. Some of the engine classes don’t get imported in every case.

Instead, use from keras.layers import Layer and use that layer class in place of the one from the engine.

Original Author thshea Of This Content

Solution 4

Installing tensorflow with version as following

pip uninstall tensorflow -y
pip uninstall keras -y
pip install tensorflow==2.4.3
pip install keras==2.4.0

After above, some errors will arise. You could solve them by following steps.

@Error: [module ‘tensorflow’ has no attribute XXXXXXXX]

In the model.py or your code, resolving some api with tf.compat.v1, e.g. tf.compat.v1.Session or import tensorflow.compat.v1 as tf

@Error: [ValueError: Tried to convert ‘shape’ to a tensor and failed. Error: None values not supported.]

mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)

replace with this this if-else code block:

if s[1]==None:
    mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
else:
    mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)

@Error: [ValueError: None values not supported.]

indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)

replace with

indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)

@Error: [AttributeError: module ‘keras.engine.saving’ has no attribute ‘load_weights_from_hdf5_group_by_name’]

from keras import saving

replace with

from tensorflow.python.keras.saving import hdf5_format

and

saving.load_weights_from_hdf5_group(f, layers)
saving.load_weights_from_hdf5_group_by_name(f, layers)

replace with

hdf5_format.load_weights_from_hdf5_group(f, layers)
hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)

Reference:

Original Author Epic Chen Of This Content

Conclusion

So This is all About This Tutorial. Hope This Tutorial Helped You. Thank You.

Also Read,

ittutorial team

I am an Information Technology Engineer. I have Completed my MCA And I have 4 Year Plus Experience, I am a web developer with knowledge of multiple back-end platforms Like PHP, Node.js, Python and frontend JavaScript frameworks Like Angular, React, and Vue.

Leave a Comment