matterport / Mask_RCNN

Mask R-CNN for object detection and instance segmentation on Keras and TensorFlow
Other
24.6k stars 11.69k forks source link

Variables were created within a Lambda layer (anchors) #1930

Open hulkwork opened 4 years ago

hulkwork commented 4 years ago

Hello, I try to use Mask_RCNN with tf 2.0 and i have this error. Someone can help me ?

lib/python3.6/site-packages/tensorflow_core/python/ops/resource_variable_ops.py:1636: calling BaseResourceVariable.__init__ (from tensorflow.python.ops.resource_variable_ops) with constraint is deprecated and will be removed in a future version. Instructions for updating: If using Keras pass *_constraint arguments to layers. Traceback (most recent call last): File "train.py", line 315, in <module> model_dir=args.logs) File "/home/hulkbulk/Documents/mrcnn/Mask_RCNN/mrcnn/model.py", line 1869, in __init__ self.keras_model = self.build(mode=mode, config=config) File "/home/hulkbulk/Documents/mrcnn/Mask_RCNN/mrcnn/model.py", line 1967, in build anchors = KL.Lambda(lambda x: tf.Variable(anchors) , name="anchors")(input_image) File "/home/hulkbulk/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/engine/base_layer.py", line 785, in __call__ outputs = call_fn(cast_inputs, *args, **kwargs) File "/home/hulkbulk/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py", line 846, in call self._check_variables(created_variables, tape.watched_variables()) File "/home/hulkbulk/.local/lib/python3.6/site-packages/tensorflow_core/python/keras/layers/core.py", line 872, in _check_variables raise ValueError(error_str) ValueError: The following Variables were created within a Lambda layer (anchors) but are not tracked by said layer: <tf.Variable 'anchors/Variable:0' shape=(1, 261888, 4) dtype=float32> The layer cannot safely ensure proper Variable reuse across multiple calls, and consquently this behavior is disallowed for safety. Lambda layers are not well suited to stateful computation; instead, writing a subclassed Layer is the recommend way to define layers with Variables.

Granite-phd commented 4 years ago

Hello, I'm having the same issue but xhb7636553's solution did not work for me. I tried downgrading TF but my python version (3.8.2) does not let me install any TF version lower than 2.2.0. Is there any solutions that have been found other than downgrading everything?

anushkayadav commented 4 years ago

if you are having such errors while running on Google colab. Try the following: !pip install keras==2.1.5 %tensorflow_version 1.x also replace keepdims with keep_dims in model.py

Granite-phd commented 4 years ago

if you are having such errors while running on Google colab. Try the following: !pip install keras==2.1.5 %tensorflow_version 1.x also replace keepdims with keep_dims in model.py

I, unfortunatly, am not. And downgrading everything did not even work for me because the cuda version (which I cannot change) on my lab's servers is too new to make the training work on the GPU, so it's running on the CPU. If anybody has any other idea on how to fix this, I'm taking any suggestions.

solarfresh commented 3 years ago

@elo31000 Here is a workaround on TF==2.3.1 with keras==2.4.3 about issuecomment-668556535

The sub-class must be modified as

class AnchorsLayer(KL.Layer):
    def __init__(self, anchors, name="anchors", **kwargs):
        super(AnchorsLayer, self).__init__(name=name, **kwargs)
        self.anchors = tf.Variable(anchors)

    def call(self, dummy):
        return self.anchors

    def get_config(self):
        config = super(AnchorsLayer, self).get_config()
        return config

and the input is switched to

anchors = AnchorsLayer(anchors, name="anchors")(input_image)
raditharch commented 3 years ago

it's work fine for me with rcnn master code without adding subclass. !pip install tensorflow==1.9.0 !pip install keras==2.2.0 if you using file requirements.txt to install it, better remove tensorflow and keras and add it outside (install manualy one by one). That's the solution work for me, and I'm using google colab.

TheAlpha419 commented 3 years ago

Hello, sorry for the noob question but how do you add the subclass? Thank you

TheAlpha419 commented 3 years ago

@elo31000 Here is a workaround on TF==2.3.1 with keras==2.4.3 about issuecomment-668556535

The sub-class must be modified as

class AnchorsLayer(KL.Layer):
    def __init__(self, anchors, name="anchors", **kwargs):
        super(AnchorsLayer, self).__init__(name=name, **kwargs)
        self.anchors = tf.Variable(anchors)

    def call(self, dummy):
        return self.anchors

    def get_config(self):
        config = super(AnchorsLayer, self).get_config()
        return config

and the input is switched to

anchors = AnchorsLayer(anchors, name="anchors")(input_image)

Hello, sorry for the noob question but how do you add the subclass? Thank you

kimile599 commented 3 years ago

@elo31000 Here is a workaround on TF==2.3.1 with keras==2.4.3 about issuecomment-668556535 The sub-class must be modified as

class AnchorsLayer(KL.Layer):
    def __init__(self, anchors, name="anchors", **kwargs):
        super(AnchorsLayer, self).__init__(name=name, **kwargs)
        self.anchors = tf.Variable(anchors)

    def call(self, dummy):
        return self.anchors

    def get_config(self):
        config = super(AnchorsLayer, self).get_config()
        return config

and the input is switched to

anchors = AnchorsLayer(anchors, name="anchors")(input_image)

Hello, sorry for the noob question but how do you add the subclass? Thank you

I got this new error for this : TypeError: init() got multiple values for argument 'name'

kimile599 commented 3 years ago

@elo31000 Here is a workaround on TF==2.3.1 with keras==2.4.3 about issuecomment-668556535 The sub-class must be modified as

class AnchorsLayer(KL.Layer):
    def __init__(self, anchors, name="anchors", **kwargs):
        super(AnchorsLayer, self).__init__(name=name, **kwargs)
        self.anchors = tf.Variable(anchors)

    def call(self, dummy):
        return self.anchors

    def get_config(self):
        config = super(AnchorsLayer, self).get_config()
        return config

and the input is switched to

anchors = AnchorsLayer(anchors, name="anchors")(input_image)

Hello, sorry for the noob question but how do you add the subclass? Thank you

Hello, do you figure it out ? I am not quite sure either.

dexter1608 commented 1 year ago

Matterport implementation of MaskRCNN is not compatible with TF 2.0 Try lower version. I use Tf 1.14 Tf 1.14 is deprecated. How do I install it then?

KV619fF5 commented 1 year ago

tensorflow==1.4.1 keras==2.0.8

i can't downgrade the above version it just shows there is no other verision like this choose others and it shows tensorflow version 2.x is there any soln and i am stuck with the same lambda problem

DeepLearningMileStones commented 1 year ago

Matterport implementation of MaskRCNN is not compatible with TF 2.0 Try lower version. I use Tf 1.14

it did work after editing a file in mrcnn library named model.py. Now facing problem in tensorflow 2.8

DeepLearningMileStones commented 1 year ago

tensorflow==1.4.1 keras==2.0.8

i can't downgrade the above version it just shows there is no other verision like this choose others and it shows tensorflow version 2.x is there any soln and i am stuck with the same lambda problem

See if you can create environment with lower version of python....then try installing lower versions of tf

DeepLearningMileStones commented 1 year ago

@elo31000 I've the same error on TF 2.3.0. I am training now on TF 2.2.0. Presumably in next few days I will try to update code to TF 2.3.0 and maybe I could help then.

Could you please help me on TF 2.8. I am working on google colab. i am getting error:

The following Variables were created within a Lambda layer (anchors) but are not tracked by said layer: <tf.Variable 'anchors/Variable:0' shape=(1, 261888, 4) dtype=float32> The layer cannot safely ensure proper Variable reuse across multiple calls, and consequently this behavior is disallowed for safety. Lambda layers are not well suited to stateful computation; instead, writing a subclassed Layer is the recommend way to define layers with Variables.

I have also tried to add a class in model.py as mentioned by many here,

class AnchorsLayer(KL.Layer):
    def __init__(self, anchors, name="anchors", **kwargs):
        super(AnchorsLayer, self).__init__(name=name, **kwargs)
        self.anchors = tf.Variable(anchors)

    def call(self, dummy):
        return self.anchors

    def get_config(self):
        config = super(AnchorsLayer, self).get_config()
        return config

and

anchors = AnchorsLayer(anchors, name="anchors")(input_image)

but still no luck.

DeepLearningMileStones commented 1 year ago

anchLayer = AnchorsLayer(anchors) anchors = KL.Lambda(lambda x: anchLayer, name="anchors")(input_image)

anchors = KL.Lambda(lambda x: tf.Variable(anchors), name="anchors")(input_image)

Using TF 2.8. No luck trying what you tried....lambda layer error still persists.

DeepLearningMileStones commented 1 year ago

Matterport implementation of MaskRCNN is not compatible with TF 2.0 Try lower version. I use Tf 1.14

Can you suggest any other repository for higher TF version or like TF 2.8