Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported. #1070

Closed
ShibaPrasad opened this issue Oct 25, 2018 · 34 comments

Comments

@ShibaPrasad
Copy link

ShibaPrasad commented Oct 25, 2018

I am running the below step - on my work environment to reproduce the code (using the kaggle data set)

I spent some time and not able to figure it out. Please help me.

I am using pyhon 3.6 and keras - 2.1.6-tf version and Linux


Create model in inference modefrom - < inspect_nucleus_model.ipynb > file - I am trying to run the below code (with default setting) and getting the error (copied far below). I

with tf.device(DEVICE):
model = modellib.MaskRCNN(mode = "inference", model_dir = LOGS_DIR, config = config)


ValueError Traceback (most recent call last)
/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
509 as_ref=input_arg.is_ref,
--> 510 preferred_dtype=default_dtype)
511 except TypeError as err:

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1106 if ret is None:
-> 1107 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1108

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
959 return NotImplemented
--> 960 return _autopacking_helper(v, inferred_dtype, name or "packed")
961

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
921 elems_as_tensors.append(
--> 922 constant_op.constant(elem, dtype=dtype, name=str(i)))
923 return gen_array_ops.pack(elems_as_tensors, name=scope)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
195 tensor_util.make_tensor_proto(
--> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
423 if values is None:
--> 424 raise ValueError("None values not supported.")
425 # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
523 observed = ops.internal_convert_to_tensor(
--> 524 values, as_ref=input_arg.is_ref).dtype.name
525 except ValueError as err:

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1106 if ret is None:
-> 1107 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1108

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
959 return NotImplemented
--> 960 return _autopacking_helper(v, inferred_dtype, name or "packed")
961

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
921 elems_as_tensors.append(
--> 922 constant_op.constant(elem, dtype=dtype, name=str(i)))
923 return gen_array_ops.pack(elems_as_tensors, name=scope)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
195 tensor_util.make_tensor_proto(
--> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
423 if values is None:
--> 424 raise ValueError("None values not supported.")
425 # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
2 print (LOGS_DIR)
3 with tf.device(DEVICE):
----> 4 model = modellib.MaskRCNN(mode = "inference", model_dir = LOGS_DIR, config = config)

/project/bioinformatics/Rajaram_lab/s183574/myCopy/Nuclei-Counting-Segmentation/mrcnn/model.py in init(self, mode, config, model_dir)
1839 self.model_dir = model_dir
1840 self.set_log_dir()
-> 1841 self.keras_model = self.build(mode=mode, config=config)
1842
1843 def build(self, mode, config):

/project/bioinformatics/Rajaram_lab/s183574/myCopy/Nuclei-Counting-Segmentation/mrcnn/model.py in build(self, mode, config)
2040 config.POOL_SIZE, config.NUM_CLASSES,
2041 train_bn=config.TRAIN_BN,
-> 2042 fc_layers_size=config.FPN_CLASSIF_FC_LAYERS_SIZE)
2043
2044 # Detections

/project/bioinformatics/Rajaram_lab/s183574/myCopy/Nuclei-Counting-Segmentation/mrcnn/model.py in fpn_classifier_graph(rois, feature_maps, image_meta, pool_size, num_classes, train_bn, fc_layers_size)
954 # Reshape to [batch, num_rois, NUM_CLASSES, (dy, dx, log(dh), log(dw))]
955 s = K.int_shape(x)
--> 956 mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
957
958 return mrcnn_class_logits, mrcnn_probs, mrcnn_bbox

/miniconda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, inputs, *args, **kwargs)
686
687 if not in_deferred_mode:
--> 688 outputs = self.call(inputs, *args, **kwargs)
689 if outputs is None:
690 raise ValueError('A layer's call method should return a Tensor '

/miniconda/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in call(self, inputs)
438 def call(self, inputs):
439 return array_ops.reshape(inputs,
--> 440 (array_ops.shape(inputs)[0],) + self.target_shape)
441
442 def get_config(self):

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in reshape(tensor, shape, name)
6195 if _ctx is None or not _ctx._eager_context.is_eager:
6196 _, _, _op = _op_def_lib._apply_op_helper(
-> 6197 "Reshape", tensor=tensor, shape=shape, name=name)
6198 _result = _op.outputs[:]
6199 _inputs_flat = _op.inputs

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
526 raise ValueError(
527 "Tried to convert '%s' to a tensor and failed. Error: %s" %
--> 528 (input_name, err))
529 prefix = ("Input '%s' of '%s' Op has type %s that does not match" %
530 (input_name, op_type_name, observed))

ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.

as_ref=input_arg.is_ref,
--> 510 preferred_dtype=default_dtype)
511 except TypeError as err:

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1106 if ret is None:
-> 1107 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1108

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
959 return NotImplemented
--> 960 return _autopacking_helper(v, inferred_dtype, name or "packed")
961

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
921 elems_as_tensors.append(
--> 922 constant_op.constant(elem, dtype=dtype, name=str(i)))
923 return gen_array_ops.pack(elems_as_tensors, name=scope)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
195 tensor_util.make_tensor_proto(
--> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
423 if values is None:
--> 424 raise ValueError("None values not supported.")
425 # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
523 observed = ops.internal_convert_to_tensor(
--> 524 values, as_ref=input_arg.is_ref).dtype.name
525 except ValueError as err:

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/ops.py in internal_convert_to_tensor(value, dtype, name, as_ref, preferred_dtype, ctx)
1106 if ret is None:
-> 1107 ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
1108

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_conversion_function(v, dtype, name, as_ref)
959 return NotImplemented
--> 960 return _autopacking_helper(v, inferred_dtype, name or "packed")
961

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in _autopacking_helper(list_or_tuple, dtype, name)
921 elems_as_tensors.append(
--> 922 constant_op.constant(elem, dtype=dtype, name=str(i)))
923 return gen_array_ops.pack(elems_as_tensors, name=scope)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py in constant(value, dtype, shape, name, verify_shape)
195 tensor_util.make_tensor_proto(
--> 196 value, dtype=dtype, shape=shape, verify_shape=verify_shape))
197 dtype_value = attr_value_pb2.AttrValue(type=tensor_value.tensor.dtype)

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/tensor_util.py in make_tensor_proto(values, dtype, shape, verify_shape)
423 if values is None:
--> 424 raise ValueError("None values not supported.")
425 # if dtype is provided, forces numpy array to be the type

ValueError: None values not supported.

During handling of the above exception, another exception occurred:

ValueError Traceback (most recent call last)
in ()
2 print (LOGS_DIR)
3 with tf.device(DEVICE):
----> 4 model = modellib.MaskRCNN(mode = "inference", model_dir = LOGS_DIR, config = config)

/project/bioinformatics/Rajaram_lab/s183574/myCopy/Nuclei-Counting-Segmentation/mrcnn/model.py in init(self, mode, config, model_dir)
1839 self.model_dir = model_dir
1840 self.set_log_dir()
-> 1841 self.keras_model = self.build(mode=mode, config=config)
1842
1843 def build(self, mode, config):

/project/bioinformatics/Rajaram_lab/s183574/myCopy/Nuclei-Counting-Segmentation/mrcnn/model.py in build(self, mode, config)
2040 config.POOL_SIZE, config.NUM_CLASSES,
2041 train_bn=config.TRAIN_BN,
-> 2042 fc_layers_size=config.FPN_CLASSIF_FC_LAYERS_SIZE)
2043
2044 # Detections

/project/bioinformatics/Rajaram_lab/s183574/myCopy/Nuclei-Counting-Segmentation/mrcnn/model.py in fpn_classifier_graph(rois, feature_maps, image_meta, pool_size, num_classes, train_bn, fc_layers_size)
954 # Reshape to [batch, num_rois, NUM_CLASSES, (dy, dx, log(dh), log(dw))]
955 s = K.int_shape(x)
--> 956 mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
957
958 return mrcnn_class_logits, mrcnn_probs, mrcnn_bbox

/miniconda/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in call(self, inputs, *args, **kwargs)
686
687 if not in_deferred_mode:
--> 688 outputs = self.call(inputs, *args, **kwargs)
689 if outputs is None:
690 raise ValueError('A layer's call method should return a Tensor '

/miniconda/lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py in call(self, inputs)
438 def call(self, inputs):
439 return array_ops.reshape(inputs,
--> 440 (array_ops.shape(inputs)[0],) + self.target_shape)
441
442 def get_config(self):

/miniconda/lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py in reshape(tensor, shape, name)
6195 if _ctx is None or not _ctx._eager_context.is_eager:
6196 _, _, _op = _op_def_lib._apply_op_helper(
-> 6197 "Reshape", tensor=tensor, shape=shape, name=name)
6198 _result = _op.outputs[:]
6199 _inputs_flat = _op.inputs

/miniconda/lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py in _apply_op_helper(self, op_type_name, name, **keywords)
526 raise ValueError(
527 "Tried to convert '%s' to a tensor and failed. Error: %s" %
--> 528 (input_name, err))
529 prefix = ("Input '%s' of '%s' Op has type %s that does not match" %
530 (input_name, op_type_name, observed))

ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.

@ShibaPrasad
Copy link
Author

Has anybody got this problem before ????

@ShibaPrasad ShibaPrasad changed the title Error - model = modellib.MaskRCNN(mode = "inference", model_dir = LOGS_DIR, config = config) ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported. Oct 25, 2018
@GianKiMoon
Copy link

Hi @ShibaPrasad! I'm running into a similar problem, did you managed to find the problem? :)

@ShibaPrasad
Copy link
Author

Hi GianKiMoon,
Yes, I am able to resolve - by installing a few more software (Linus) in your environment. I used all the keras packages and it worked smoothly.

@m29110938
Copy link

@ShibaPrasad
how to fix this problem?

@Nakeuh
Copy link

Nakeuh commented Jun 11, 2019

I had the same problem using the Keras package embbedded in Tensorflow.
Importing Tensorflow and Keras separately solved the problem.

@deluongo
Copy link

deluongo commented Sep 23, 2019

If you want to use tf.keras or from tensorflow import keras, all you have to do to fix this error is replace the first value (s[1] == None) in the tf.keras.layers.Reshape function with -1:

Replace with the following code:

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

@AI-ML-Enthusiast
Copy link

If you want to use tf.keras or from tensorflow import keras, all you have to do to fix this error is replace the first value (s[1] == None) in the tf.keras.layers.Reshape function with -1:

Replace with the following code:

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

@deluongo
I have the same problem, thanks for your comments.

However, you mean, I have to write like this:

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

Am I right?

But the result will be same or any bad effect there on the result after changing?

Thanks

@hansong999
Copy link

I have the same problem.

\Anaconda3\lib\site-packages\tensorflow_core\python\framework\tensor_util.py:437 make_tensor_proto
raise ValueError("None values not supported.")

ValueError: None values not supported.

Source=mrcnn\model.py
StackTrace:
File "mrcnn\model.py", line 2042, in build
[rpn_rois, mrcnn_class, mrcnn_bbox, input_image_meta])
File "mrcnn\model.py", line 1835, in init
self.keras_model = self.build(mode=mode, config=config)
File "Mask_R_CNN_demo.py", line 140, in
rcnn = MaskRCNN(mode='inference', model_dir='./', config=TestConfig())

@omdxp
Copy link

omdxp commented Mar 19, 2020

If you want to use tf.keras or from tensorflow import keras, all you have to do to fix this error is replace the first value (s[1] == None) in the tf.keras.layers.Reshape function with -1:

Replace with the following code:

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

I did as you wrote, but I got another error :
ValueError: None values not supported.

@Trotts
Copy link

Trotts commented Apr 24, 2020

@deluongo @IbrahimLearning Hi, I am also in the process of converting model.py from Keras to tf.keras. I've attempted the fix proposed by @deluongo above, but I am running into a further error when I implement:

ValueError                                Traceback (most recent call last)
<ipython-input-5-2ba0790b0ffe> in <module>
      2 model = modellib.MaskRCNN(mode="inference", 
      3                           config=inference_config,
----> 4                           model_dir=model_path)
      5 
      6 print("Loading weights from ", model_path)

~/dolphin-recognition/Mask_RCNN-master/mrcnn/model.py in __init__(self, mode, config, model_dir)
   1856         self.model_dir = model_dir
   1857         self.set_log_dir()
-> 1858         self.keras_model = self.build(mode=mode, config=config)
   1859 
   1860     def build(self, mode, config):

~/dolphin-recognition/Mask_RCNN-master/mrcnn/model.py in build(self, mode, config)
   2064             # normalized coordinates
   2065             detections = DetectionLayer(config, name="mrcnn_detection")(
-> 2066                 [rpn_rois, mrcnn_class, mrcnn_bbox, input_image_meta])
   2067 
   2068             # Create masks for detections

~/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py in __call__(self, inputs, *args, **kwargs)
    632                     outputs = base_layer_utils.mark_as_return(outputs, acd)
    633                 else:
--> 634                   outputs = call_fn(inputs, *args, **kwargs)
    635 
    636             except TypeError as e:

~/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/autograph/impl/api.py in wrapper(*args, **kwargs)
    147       except Exception as e:  # pylint:disable=broad-except
    148         if hasattr(e, 'ag_error_metadata'):
--> 149           raise e.ag_error_metadata.to_exception(type(e))
    150         else:
    151           raise

ValueError: in converted code:

    /home/b3020111/dolphin-recognition/Mask_RCNN-master/mrcnn/model.py:826 call *
        detections_batch = utils.batch_slice(
    /home/b3020111/dolphin-recognition/Mask_RCNN-master/mrcnn/utils.py:821 batch_slice *
        output_slice = graph_fn(*inputs_slice)
    /tmp/tmppziso32p.py:15 None *
        detections_batch = ag__.converted_call('batch_slice', utils, ag__.ConversionOptions(recursive=True, force_conversion=False, optional_features=(), internal_convert_user_code=True), ([rois, mrcnn_class, mrcnn_bbox, window], lambda x, y, w, z: ag__.converted_call(refine_detections_graph, None, ag__.ConversionOptions(recursive=True, force_conversion=False, optional_features=(), internal_convert_user_code=True), (x, y, w, z, self.config), None), self.config.IMAGES_PER_GPU), None)
    /home/b3020111/dolphin-recognition/Mask_RCNN-master/mrcnn/model.py:712 refine_detections_graph *
        indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
    /home/b3020111/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/ops/math_ops.py:1305 range
        limit = ops.convert_to_tensor(limit, dtype=dtype, name="limit")
    /home/b3020111/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/framework/ops.py:1087 convert_to_tensor
        return convert_to_tensor_v2(value, dtype, preferred_dtype, name)
    /home/b3020111/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/framework/ops.py:1145 convert_to_tensor_v2
        as_ref=False)
    /home/b3020111/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/framework/ops.py:1224 internal_convert_to_tensor
        ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
    /home/b3020111/.virtualenvs/scorecam/lib/python3.6/site-packages/tensorflow/python/framework/constant_op.py:358 _dimension_tensor_conversion_function
        raise ValueError("Cannot convert an unknown Dimension to a Tensor: %s" % d)

    ValueError: Cannot convert an unknown Dimension to a Tensor: ?

Is this something you have ran into and know how to overcome? My model.py file can be viewed here for reference. My tensofrlow version is 1.14, tf.keras version 2.2.4-tf.

Thank you in advance for any help or advice you can give :)

EDIT: After a bit of playing around, I seem to have fixed the issue. This can be achieved by changing line 714 of model.py from:

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

to

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

@thandava
Copy link

I have the same problem.

\Anaconda3\lib\site-packages\tensorflow_core\python\framework\tensor_util.py:437 make_tensor_proto
raise ValueError("None values not supported.")

ValueError: None values not supported.

Source=mrcnn\model.py
StackTrace:
File "mrcnn\model.py", line 2042, in build
[rpn_rois, mrcnn_class, mrcnn_bbox, input_image_meta])
File "mrcnn\model.py", line 1835, in init
self.keras_model = self.build(mode=mode, config=config)
File "Mask_R_CNN_demo.py", line 140, in
rcnn = MaskRCNN(mode='inference', model_dir='./', config=TestConfig())

If you want to use tf.keras or from tensorflow import keras, all you have to do to fix this error is replace the first value (s[1] == None) in the tf.keras.layers.Reshape function with -1:
Replace with the following code:
mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)

I did as you wrote, but I got another error :
ValueError: None values not supported.

Hi,
i have the same issue please tell how to resolve it.

@thandava
Copy link

If you want to use tf.keras or from tensorflow import keras, all you have to do to fix this error is replace the first value (s[1] == None) in the tf.keras.layers.Reshape function with -1:
Replace with the following code:
mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)

@deluongo
I have the same problem, thanks for your comments.

However, you mean, I have to write like this:

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

Am I right?

But the result will be same or any bad effect there on the result after changing?

Thanks

i have the same issue & i followed as you said but getting error Value Error: None values not supported. . please tell me how to resolve this issue

@damien-ls
Copy link

damien-ls commented Jul 8, 2020

Given all the open issues due to using deprecated version and that this repo is not maintained it should be marked as deprecated in the readme.md

@Krupal09
Copy link

Hi GianKiMoon,
Yes, I am able to resolve - by installing a few more software (Linus) in your environment. I used all the keras packages and it worked smoothly.

Hello @ShibaPrasad,
I am new to this. May I know which more libraries from keras packages to install ? A little bit confused because we are already importing entire keras package using import keras. I am also using linux system.

Thanking you in advance,
Krupal

@nielsuit227
Copy link

The two edits given in the above comments worked for me (TF 2.2.0)

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

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

So thanks!

@salimkhazem
Copy link

Hello,
i had the same problem using Colab few days ago, i tried a lot of possibilities
I managed to solve it using the correct version of tensorflow, so if you're using Colab, just make sure to introduce this line :
%tensorflow_version 1.x

It worked for me

@ghost
Copy link

ghost commented Sep 13, 2020

Hello,
i had the same problem using Colab few days ago, i tried a lot of possibilities
I managed to solve it using the correct version of tensorflow, so if you're using Colab, just make sure to introduce this line :
%tensorflow_version 1.x

It worked for me

It worked! Just to clarify, you have to add the line before initializing tensorflow. If you are already in a runtime, you have to restart the runtime after adding the line.

@liketheflower
Copy link

liketheflower commented Oct 14, 2020

There is no problem with TF1.14, however, for TF2.3, I got the problem.
It seems for the TF2.X, the Keras will not call the compute_output_shape.
I tried the most voted solution and still got an error. The following way solved my problem.
At the end of the call function for ProposalLayer class, add the following two lines to change the shape.
Then we will get rid of the None value as we change the
proposals shape from [None, None, 4] to [None, proposal_count, 4].

 proposals = tf.reshape(proposals, [-1, self.proposal_count, 4])
 scores_after_nms = tf.reshape(scores_after_nms, [-1, self.proposal_count])

Hope it helps.

@ankit705078
Copy link

Hello,
i had the same problem using Colab few days ago, i tried a lot of possibilities
I managed to solve it using the correct version of tensorflow, so if you're using Colab, just make sure to introduce this line :
%tensorflow_version 1.x
It worked for me

It worked! Just to clarify, you have to add the line before initializing tensorflow. If you are already in a runtime, you have to restart the runtime after adding the line.

Yes it works great !! Tried everything mentioned on this page above but none worked actually I wanted to use Tensorflow 2.X but even after editting a lot the model.py file but none.Will try to edit some more files so that it works for T 2.X versions if successful will update ans here.
https://docs.github.com/articles/github-community-guidelines

@mayurmahurkar
Copy link

mayurmahurkar commented Dec 8, 2020

The following steps worked for me:

1. Upgrade the scripts by using the following line on the root folder:

tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt

This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt

2. Replace the following line:

mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
with this this if-else code block:

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

3. Change the following line:

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

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

4. Now, you need to replace:

from keras import saving
with:

from tensorflow.python.keras import saving
then you will also want to replace the lines in both if and else block:

saving.load_weights_from_hdf5_group(f, layers)
and so on with the follwoing lines, inside if and else block respectively:

saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)

saving.hdf5_format.load_weights_from_hdf5_group(f, layers)

Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

@BobbyZ04
Copy link

Hello,
i had the same problem using Colab few days ago, i tried a lot of possibilities
I managed to solve it using the correct version of tensorflow, so if you're using Colab, just make sure to introduce this line :
%tensorflow_version 1.x

It worked for me

This is the best solution for my pile of errors haha

@GP-Kulam
Copy link

GP-Kulam commented Jan 1, 2021

There is no problem with TF1.14, however, for TF2.3, I got the problem.
It seems for the TF2.X, the Keras will not call the compute_output_shape.
I tried the most voted solution and still got an error. The following way solved my problem.
At the end of the call function for ProposalLayer class, add the following two lines to change the shape.
Then we will get rid of the None value as we change the
proposals shape from [None, None, 4] to [None, proposal_count, 4].

 proposals = tf.reshape(proposals, [-1, self.proposal_count, 4])
 scores_after_nms = tf.reshape(scores_after_nms, [-1, self.proposal_count])

Hope it helps.

Please mention exact locaion where to do this change

@alexbarnadas
Copy link

The following steps worked for me:

1. Upgrade the scripts by using the following line on the root folder:

tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt

This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt

2. Replace the following line:

mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
with this this if-else code block:

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

3. Change the following line:

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

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

4. Now, you need to replace:

from keras import saving
with:

from tensorflow.python.keras import saving
then you will also want to replace the lines in both if and else block:

saving.load_weights_from_hdf5_group(f, layers)
and so on with the follwoing lines, inside if and else block respectively:

saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)

saving.hdf5_format.load_weights_from_hdf5_group(f, layers)

Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

Hello I'm having the same issue! I don't have these line in my model.py, instead I have a similar one, which is the one that seems to be causing trouble:
mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)

I'm a bit of a noob and I am a bit lost, please HELP.

@nehaingavale
Copy link

Hello,
i had the same problem using Colab few days ago, i tried a lot of possibilities
I managed to solve it using the correct version of tensorflow, so if you're using Colab, just make sure to introduce this line :
%tensorflow_version 1.x
It worked for me

It worked! Just to clarify, you have to add the line before initializing tensorflow. If you are already in a runtime, you have to restart the runtime after adding the line.

it is not working for me.

@wenjieji86
Copy link

The following steps worked for me:
1. Upgrade the scripts by using the following line on the root folder:
tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt
This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt
2. Replace the following line:
mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
with this this if-else code block:

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

3. Change the following line:
indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
with this line:
indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
4. Now, you need to replace:
from keras import saving
with:
from tensorflow.python.keras import saving
then you will also want to replace the lines in both if and else block:
saving.load_weights_from_hdf5_group(f, layers)
and so on with the follwoing lines, inside if and else block respectively:
saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
saving.hdf5_format.load_weights_from_hdf5_group(f, layers)
Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

Hello I'm having the same issue! I don't have these line in my model.py, instead I have a similar one, which is the one that seems to be causing trouble:
mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)

I'm a bit of a noob and I am a bit lost, please HELP.

It's in Feature Pyramid Network Heads section, line 951 if you are using the original scripts from matterport. @mayurmahurkar 's work around worked for me.

okovko added a commit to okovko/Mask_RCNN that referenced this issue Feb 14, 2021
matterport#1070 and matterport#1070 (comment)
tensorflow/tensorflow#43982 and tensorflow/tensorflow#43982 (comment)
matterport#2252 and matterport#2252 (comment)

See the diff to see where everything was updated for 2021 using python 3 and latest versions of tensorflow and keras.
As of this commit, the demo.ipynb runs and displays images as expected.
gzlin7 added a commit to gzlin7/Mask_RCNN that referenced this issue May 7, 2021
@vjohannesb
Copy link

vjohannesb commented Jun 16, 2021

The following steps worked for me:
1. Upgrade the scripts by using the following line on the root folder:
tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt
This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt
2. Replace the following line:
mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
with this this if-else code block:

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

3. Change the following line:
indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
with this line:
indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
4. Now, you need to replace:
from keras import saving
with:
from tensorflow.python.keras import saving
then you will also want to replace the lines in both if and else block:
saving.load_weights_from_hdf5_group(f, layers)
and so on with the follwoing lines, inside if and else block respectively:
saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
saving.hdf5_format.load_weights_from_hdf5_group(f, layers)
Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

Hello I'm having the same issue! I don't have these line in my model.py, instead I have a similar one, which is the one that seems to be causing trouble:
mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)

I'm a bit of a noob and I am a bit lost, please HELP.

A bit late but if anyone else stumbles in here and have the same problem; if your layers are named "KL" - just replace layers. in his solution with KL. So:

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)

Leejung8763 added a commit to Leejung8763/Mask_RCNN that referenced this issue Jun 22, 2021
code change 
1. import keras.engine as KE -> import keras.engine.topology as KE
2. from keras import saving -> from tensorflow.python.keras.saving import hdf5_format
3. saving.load_weights_from_hdf5_group(f, layers) -> hdf5_format.load_weights_from_hdf5_group(f, layers)
4. import tensorflow as tf -> import tensorflow.compat.v1 as tf
5. indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1) -> indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
6. mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x) -> 
if s[1]==None:
  mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
 else:
  mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
7. indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1) -> indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
8. from keras import saving -> from tensorflow.python.keras import saving

https://stackoverflow.com/questions/67905185/module-keras-engine-has-no-attribute-layer
matterport#2252 (comment)
matterport#1797 (comment)
matterport#1070 (comment)
matterport#1070 (comment)
SaiDunoyer added a commit to SaiDunoyer/Mask_RCNN that referenced this issue Jul 6, 2021
@Amitind800
Copy link

The following steps worked for me:

  1. Upgrade the scripts by using the following line on the root folder:
    tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt
    This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt
  2. Replace the following line:
    mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
    with this this if-else code block:

if s[1]==None:
mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
else:
mrcnn_bbox = layers.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
3. Change the following line:
indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1)
with this line:
indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
4. Now, you need to replace:
from keras import saving
with:
from tensorflow.python.keras import saving
then you will also want to replace the lines in both if and else block:
saving.load_weights_from_hdf5_group(f, layers)
and so on with the follwoing lines, inside if and else block respectively:
saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
saving.hdf5_format.load_weights_from_hdf5_group(f, layers)
Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

Hello I'm having the same issue! I don't have these line in my model.py, instead I have a similar one, which is the one that seems to be causing trouble:
mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)

I'm a bit of a noob and I am a bit lost, please HELP.

A bit late but if anyone else stumbles in here and have the same problem; if your layers are named "KL" - just replace layers. in his solution with KL. So:

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)

i did all the changes but still getting an error as nameerror s is not defined plzz help

@esmasert
Copy link

esmasert commented Nov 8, 2021

Thank you everyone! Here, How it worked for me in all steps:

The following steps worked for me:

1. Upgrade the scripts by using the following line on the root folder:

tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt

This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt

2. Replace the following line:

mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x) with this this if-else code block:

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

3. Change the following line:

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

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

4. Now, you need to replace:

from keras import saving with:

from tensorflow.python.keras import saving then you will also want to replace the lines in both if and else block:

saving.load_weights_from_hdf5_group(f, layers) and so on with the follwoing lines, inside if and else block respectively:

saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)

saving.hdf5_format.load_weights_from_hdf5_group(f, layers)

Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

I did almost everything same, but there were some differences:

In 4th one, I replaced the lines:

replace the lines
saving.load_weights_from_hdf5_group(f, layers)
and so on with
hdf5_format.load_weights_from_hdf5_group(f, layers)

from: saving.load_weights_from_hdf5_group_by_name(f, layers)
to: hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)

Thank youu!!

@BhavyaKohli
Copy link

BhavyaKohli commented Nov 28, 2021

The following steps worked for me:

1. Upgrade the scripts by using the following line on the root folder:

tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt

This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt

2. Replace the following line:

mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x) with this this if-else code block:

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

3. Change the following line:

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

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

4. Now, you need to replace:

from keras import saving with:

from tensorflow.python.keras import saving then you will also want to replace the lines in both if and else block:

saving.load_weights_from_hdf5_group(f, layers) and so on with the follwoing lines, inside if and else block respectively:

saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)

saving.hdf5_format.load_weights_from_hdf5_group(f, layers)

Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

Thanks a lot!! I was finally able to run demo.ipynb after grueling hours or looking at red text, which is big on its own 😵.

I saw a few minor changes, probably because of different versions of the code:
The version I have has an import keras.engine as KE and this is used as KE.Layer (4 times in model.py). As a replacement you have to replace all those KE.Layer's with Layer after adding this to the top from tensorflow.keras.layers import Layer.

Reg point 2, instead of layers.reshape... there was KL.reshape (where KL was keras.layers), I replaced the import keras.layers as KL with import tensorflow.keras.layers as KL (since KL was used throughout the file for all layers), followed by the equivalent edits as you have described in point 2.

Apart from these, I faced an issue with importing coco so I installed all missing modules one-by-one (there will be a better way I'm sure). They were docopt, clint and tablib.

I personally did all these in a virtual environment created in the Mask_RCNN directory, simply running pip install -r requirements.txt works fine if you follow the above steps and make those edits

@yashsandansing
Copy link

The following steps worked for me:
1. Upgrade the scripts by using the following line on the root folder:
tf_upgrade_v2 --intree Mask_RCNN --inplace --reportfile report.txt
This will automatically update the existing code to TF2. You will also get a list of changes made in report.txt
2. Replace the following line:
mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x) with this this if-else code block:

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

3. Change the following line:
indices = tf.stack([tf.range(probs.shape[0]), class_ids], axis=1) with this line:
indices = tf.stack([tf.range(tf.shape(probs)[0]), class_ids], axis = 1)
4. Now, you need to replace:
from keras import saving with:
from tensorflow.python.keras import saving then you will also want to replace the lines in both if and else block:
saving.load_weights_from_hdf5_group(f, layers) and so on with the follwoing lines, inside if and else block respectively:
saving.hdf5_format.load_weights_from_hdf5_group_by_name(f, layers)
saving.hdf5_format.load_weights_from_hdf5_group(f, layers)
Thanks to: @deluongo, @Trotts, @nielsuit227, @IbrahimLearning

Thanks a lot!! I was finally able to run demo.ipynb after grueling hours or looking at red text, which is big on its own dizzy_face. I saw a few minor changes, probably because of different versions of the code: The version I have has an import keras.engine as KE and this is used as KE.Layer (4 times in model.py). As a replacement you have to replace all those KE.Layer's with Layer after adding this to the top from tensorflow.keras.layers import Layer. Reg point 2, instead of layers.reshape... there was KL.reshape (where KL was keras.layers), I replaced the import keras.layers as KL with import tensorflow.keras.layers as KL (since KL was used throughout the file for all layers), followed by the equivalent edits as you have described in point 2. Apart from these, I faced an issue with importing coco so I installed all missing modules one-by-one (there will be a better way I'm sure). They were docopt, clint and tablib.

I personally did all these in a virtual environment created in the Mask_RCNN directory, simply running pip install -r requirements.txt works fine if you follow the above steps and make those edits

I read a few months back that Keras had been rolled back and is a part of TensorFlow now. Tried these changes and it worked amazingly. Thank you.

Cuda-Chen added a commit to Cuda-Chen/Mask_RCNN that referenced this issue Dec 17, 2021
Upgrade TensorFlow to 2.4.0 and Keras to 2.4.3.
For further information about upgrading TensorFlow to 2.x you can
check the following links:
1. matterport#1070 (comment)
2. https://stackoverflow.com/a/69806683
@hiraksarkar
Copy link

Just wondering, as there are clear fixes, why don't we just have another branch with tf version 2 support?

@ivarmak
Copy link

ivarmak commented Jan 28, 2022

There is another repo you can clone, with files updated to run for tf v2:
https://github.com/akTwelve/Mask_RCNN

@daniil-sharou
Copy link

@yashsandansing @ivarmak @BhavyaKohli Hi folks.
Were you able to get decent results running the demo on default sample images? I'm getting some gibberish on all of them with labels like "banana" & "toaster" flying all over the place and segmentation nowhere close to what's on the image. I used the repo posted above (https://github.com/akTwelve/Mask_RCNN)

Looks like you all were able to run Mask_RCNN on TF2, and inference worked fine for you so Im wondering what I'm missing. Are coco weights supposed to work out of the box in demo.ipynb or I have to train the model first?

@ivarmak
Copy link

ivarmak commented Jan 17, 2023

@yashsandansing @ivarmak @BhavyaKohli Hi folks. Were you able to get decent results running the demo on default sample images? I'm getting some gibberish on all of them with labels like "banana" & "toaster" flying all over the place and segmentation nowhere close to what's on the image. I used the repo posted above (https://github.com/akTwelve/Mask_RCNN)

Looks like you all were able to run Mask_RCNN on TF2, and inference worked fine for you so Im wondering what I'm missing. Are coco weights supposed to work out of the box in demo.ipynb or I have to train the model first?

It has been a while since I have worked on this, but here is what I remember:

  • Make sure to download and install (makeFile) the pycocotools and cython described in step 5 of the installation process from https://github.com/akTwelve/Mask_RCNN.

  • Check the versions and compatibility of tensorflow (most important!, try different versions of 2.x this might make the difference), keras, h5py, and scikit-image in order to get correct results.

The coco weights are supposed to work without the need for training the model, unless you want inference on a custom dataset.

I hope this helps, good luck!

crimefightingllama added a commit to crimefightingllama/Mask_RCNN that referenced this issue Apr 2, 2023
darkenergy814 added a commit to darkenergy814/PPP that referenced this issue Jul 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests