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

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'

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

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

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.

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:
Tried to convert 'shape' to a tensor and failed. Error: None values not supported
module 'keras.engine.saving' has no attribute 'load_weights_from_hdf5_group_by_name'

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.

You should write keras.layers
instead keras.engine at import section in model.py file

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.

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.

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

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

Related

Setting up keras-rl2 on my M1 Macbook Pro

I am working on a project on Reinforcement Learning - and completely new at this. I installed keras-rl as
pip install keras-rl, however it caused an error as many has mentioned:
TypeError: Keras symbolic inputs/outputs do not implement `__len__`. You may be trying to pass Keras symbolic inputs/outputs to a TF API that does not register dispatching, preventing Keras from automatically converting the API call to a lambda layer in the Functional Model. This error will also get raised if you try asserting a symbolic input/output directly.
And the solution for this is to used keras-rl2. Howver I cannot install keras-rl2 on my Mac M1 as the output is following:
ERROR: Cannot install keras-rl2==1.0.0, keras-rl2==1.0.1, keras-rl2==1.0.2, keras-rl2==1.0.3, keras-rl2==1.0.4 and keras-rl2==1.0.5 because these package versions have conflicting dependencies.
When I tried a specific version, i.e
pip install keras-rl2==1.0.5, the output is:
ERROR: Could not find a version that satisfies the requirement tensorflow (from keras-rl2) (from versions: none)
ERROR: No matching distribution found for tensorflow
I have tensorflow 2.6.0 installed by Apple Developer guide. Any idea how I can install keras-rl2?
1. To install Tensorflow on M1 Macs
https://developer.apple.com/metal/tensorflow-plugin/
2. To Install Keras-rl2
Open a terminal window and run these commands from:
https://github.com/keras-rl/keras-rl (last line changed)
git clone https://github.com/wau/keras-rl2.git
cd keras-rl
python3 setup.py install
The setup.py script will still tell you that it can't find a TensorFlow version but you will still be able to import it using:
import rl

Python package (tensorflow) displays mismatching versions between pip list and .__version__ output

I suddenly encounter this situation today, all below output is under the same environment.
pip show tensorflow, the output gives:
Name: tensorflow
Version: 1.14.0rc1
But, if I enter python
python
>>> import tensorflow as tf
>>> print(tf.__version__) . It gives:
'1.13.1'
I am sure I am using the same python under the environment I need with pure terminal or in IDE. This is weird, cuz I used to not having this issue. I just installed few other packages these two days, but I believe they have nothing to do with tensorflow. And you can also verify this through pip show tensorflow or pip list output to see the version is 1.14.0rc1. So why when I actually use python, the tensorflow is not loaded properly?
Solved it by the following:
(under the same environment)
pip uninstall tensorflow (in order to reinstall tensorflow the right version)
pip install tensorflow==1.14.0rc1
Note during installing, there is a piece of info from the terminal:
Installing collected packages: tensorflow
Attempting uninstall: tensorflow
Found existing installation: tensorflow 1.13.1
Uninstalling tensorflow-1.13.1:
Successfully uninstalled tensorflow-1.13.1
So it seems like the tensorflow 1.13.1 has been accidentally installed previously, and whenever in a python program import tensorflow, during searching stage, it hits the tensorflow==1.13.1 before finding the 1.14.0rc1 version. Altho pip list only displays tensorflow==1.14.0rc1 but not tensorflow==1.13.1 might also be due to its internal search or duplicated package resolve mechanism.

Tensorflow issues when downloading and importing

I'm trying to use tensorflow more accurately keras but it used tensorflow backend. I have had to convert my python to 3.6.2 in order to download tensorflow and even though it installs when I go to import it doesnt work saying :
ModuleNotFoundError: No module named 'tensorflow'
So i have tried installing Anaconda and performing this input:
conda create -n tensorflow_cpu pip python=3.6 #also tried with just tensorflow
activate tensorflow_cpu
pip install --ignore-installed --upgrade tensorflow
Then I get this error:
ERROR: Could not find a version that satisfies the requirement tensorflow_cpu (from versions: none)
ERROR: No matching distribution found for tensorflow_cpu
So now I am completely stuck. Why is it so hard to just import a module. Why hasn't tensorflow made the effort to make it work with newer versions of python. I am trying to perform machine learning projects but this issue is demotivating me massively and is turning me away from it all together.
If anyone has a solution or reason could you please let me know it simple matters so i can follow thoroughly. Thanks for your time.
If you are on Windows install Python 3.6.x and then Tensorflow as of 8-13-2018 and if you are on Linux just follow this link's instruction https://tensorflow-object-detection-api-tutorial.readthedocs.io/en/latest/install.html .

How to install keras on Win10?

I know there are a lot of tutorials of installing keras on win. But I am faced with a weird problem and I cannot find a solution on Google.
The version of python I used is 2.7(anaconda 64bit).
When I typed 'pip install keras' in cmd, an error occurred.
It is said that no matching distribution found for tensorflow(from keras).
I googled it and one saying goes that this problem occurred when python is 32bit on windows. However, my python is 64-bit, so this saying does not hold.
I want to know how to handle this, given that I want to use theano as keras backend instead of tensorflow.
I think the problem is that TensorFlow on Windows only support Python 3.5.X.
You should try installing anaconda3.
Source: https://www.tensorflow.org/install/install_windows
using commad pip install -v -v -v keras may offer you more information about this
and I find this commad may download Keras tar file in site https://pypi.python.org/simple/keras/
and after download file, it will check the requires.txt in Keras.egg-info
then i find in Keras 2.0.0 it require tensorflow instead of theano in this version, otherwise you may fail to install
so the solution is, download the other keras in site which requires theano not tensorflow, and unpack it, then cd its directory, and use python setup.py install to install this version of keras

Theano AttributeError: 'module' object has no attribute 'relu'

I want to use theano.tensor.nnet.relu, but I keep getting this error:
AttributeError: 'module' object has no attribute 'relu'
I have already updated theano via sudo pip install --upgrade theano command as described in theano's documentation, also i've tried sudo pip install --upgrade --no-deps theano. Neither worked, I still get the same error.
I was trying to do theano -v to confirm that I have installed the latest version but then I get the following error: -bash: theano: command not found
So my two questions here are:
How can I see theano's version?
Am I doing something wrong when updating theano? How can I solve the error first mentioned?
relu is available for theano >= 0.7.1. My guess is that pip linked to theano==0.7.
You can check theano version with pip freeze:
pip freeze | grep Theano
So you have to install latest theano with pointing pip to theano git repo :
pip install --upgrade --no-deps git+git://github.com/Theano/Theano.git
Also note that relu is function and not module, so to access it you have to use one of imports below:
from theano.tensor.nnet import relu # access `relu` as is ..
import theano.tensor.nnet as theano_nnet #access `relu` as `theano_nnet.relu`
The theano.tensor.nnet module only supports relu in the most recent versions. In order to use it, you need to install the bleeding edge version from github or wait until the next release.
Alternatively, you can implement it like this:
def relu(x):
return T.maximum(x, 0.)
This may not be inplace but it gives you the result and the gradient.
to see theano version you can run the following code:
import theano
print theano.__version__
You should follow the instructions from here to get the bleeding edge version
Actually relu function is simple to code, you can create your own relu function, like eickenberg's answer, or like theano.tensor.nnet style:
def relu(x):
return 0.5 * (x + abs(x))

Categories