module 'keras.engine' has no attribute 'Layer'
Asked Answered
K

12

26

I tried to run matterport/MaskRCNN code but faced the following error

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

/usr/local/lib/python3.7/dist-packages/mrcnn/model.py in <module>()
    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'
Kenlee answered 9/6, 2021 at 13:32 Comment(0)
K
13

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

Keeleykeelhaul answered 25/2, 2022 at 2:11 Comment(0)
A
11

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
Angers answered 19/7, 2021 at 17:51 Comment(2)
Another (better?) option is to use Tensorflow upgrade tool (to v2) and the instructions in this post: github.com/matterport/Mask_RCNN/issues/… that will allow you to run matterport's MaskRCNN in newer versions of TF and Keras.Sherasherar
This didn't resolve the issue for me. Still getting ImportError: cannot import name 'Layer' from 'keras.engine'Halftimbered
C
10

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.

Chui answered 14/6, 2021 at 7:3 Comment(3)
keras=2.6.0 doesn't have topology moduleMikkimiko
ModuleNotFoundError: No module named 'keras.engine.topology'Gamb
for this answer fix, I'm getting ImportError: cannot import name 'engine' from 'keras' (/usr/local/lib/python3.10/dist-packages/keras/__init__.py). My keras version is 2.15.0Lottie
S
7

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:

Satellite answered 2/11, 2021 at 7:22 Comment(0)
V
5

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.

Vince answered 9/6, 2021 at 13:46 Comment(0)
F
4

You should write keras.layers

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

Fuchsia answered 11/5, 2022 at 15:2 Comment(1)
This doesn't seem to be the issue. Could you add a reference or additional explanation?Westmoreland
F
3

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.

Frick answered 11/4, 2022 at 10:36 Comment(0)
T
1

Tried and Tested on 23-10-2022

GoTo matterpot's Mask_RCNN/mrcnn/model.py file

Search for KE. and replace with KL.

the code runs fine and able to download coco weights.

Torrlow answered 23/10, 2022 at 3:38 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ahders
W
0

I managed to resolve this by replacing all (4) instances of KE.Layer with keras.layers.Layer in the model.py file of the mrcnn package.

Wrens answered 7/3, 2023 at 19:15 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Ahders
C
0

I found this site to be helpfull link
so just replace :

import keras.engine as KE

with:

import keras.engine.topology as KE
Crux answered 16/3, 2023 at 15:25 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Ahders
my bad. i have fixed the errorCrux
C
-1

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

Chink answered 28/1, 2022 at 12:57 Comment(0)
P
-1

Where there is KE put KL in the mrcnn/model.py file it worked for me

Paulino answered 15/12, 2022 at 15:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Maegan

© 2022 - 2024 — McMap. All rights reserved.