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

----> 6 from mrcnn.model import MaskRCNN

/usr/local/lib/python3.7/dist-packages/mrcnn/model.py in () 253 254 --> 255 class ProposalLayer(KE.Layer): 256 """Receives anchor scores and selects a subset to pass as proposals 257 to the second stage. Filtering is done based on anchor scores and

AttributeError: module 'keras.engine' has no attribute 'Layer'



Solution 1:[1]

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.

Solution 2:[2]

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.0

Solution 3:[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.

Solution 4:[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:

Solution 5:[5]

For lines where you are using Layers like ProposalLayer(KE.Layer)

Instead of using KE.Layer do

import keras.layers as KL

and replace all instances of KE by KL

Solution 6:[6]

You should write keras.layers

instead keras.engine at import section in model.py file

Solution 7:[7]

When running the https://github.com/matterport/Mask_RCNN repository, I faced also all aforementioned issues. After some days, I finally found a way how to run this repository which I would like to share with you:

First, I installed WSL2 + Ubuntu 20.04 GUI (https://medium.com/@japheth.yates/the-complete-wsl2-gui-setup-2582828f4577) and then created the following environment:

conda create tf1_maskrcnn python=3.6 -y
conda activate tf1_maskrcnn
pip install -r requirements.txt
python setup.py install

It should be noted that I have adjusted requirements.txt:

numpy==1.19.5
scipy==1.5.4
Pillow==8.4.0
cython==0.29.28
matplotlib==3.3.4
scikit-image==0.17.2
tensorflow==1.3.0
keras==2.0.8
opencv-python==4.5.5.64
h5py==2.10.0
imgaug==0.4.0
ipykernel
pycocotools

Even though the https://github.com/akTwelve/Mask_RCNN repository which is based upon TensorFlow 2 is available, the pre-trained weights - which are automatically downloaded or can be retrieved from https://github.com/matterport/Mask_RCNN/releases - lead to unsatisfactory results. However, if this repository is used to train the model from scratch it should definitely be preferred over the tf1 version. Nonetheless, if the intention is to see how well this model works on a different dataset, which requires proper pre-trained weights, the tf1 version is the repository to go with.

Peronal option: As most Github repositories concerning deep learning computer vision tasks are tested on Ubuntu, implementing those models on Windows often lead to a multitude of errors which can be avoided by using a virtual machine. The main advantage by using WSL + Ubuntu 20.04 GUI is that it is much faster than using virtual machines. Even though some time needs to be invested at the beginning it is worth investigating this option.

Solution 8:[8]

I struggled for a bit with both the outdated Keras and Tensorflow versions on this Mask-RCNN repo.

If you want to use the current versions, instead of replacing lines I recommend you cloning the following repo: https://github.com/akTwelve/Mask_RCNN It has been updated to run on tensorflow v2+ and keras v2+.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 bfhaha
Solution 2 sheß
Solution 3 thshea
Solution 4
Solution 5 betelgeuse
Solution 6 p?nar
Solution 7 Gabauer
Solution 8 ivarmak