matterport / Mask_RCNN

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

ValueError: Tried to convert 'shape' to a tensor and failed. After TensorFlow updating. #1820

Open kiflowb777 opened 5 years ago

kiflowb777 commented 5 years ago

After update TensorFlow 1.14 to 2.0 and use tf.keras instead of keras, when using fpn_classifier_graph I get:

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

  File "D:\project\project.py", line 55, in fpn_classifier_graph
    mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
  File "C:\python36\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 634, in __call__
    outputs = call_fn(inputs, *args, **kwargs)
  File "C:\python36\lib\site-packages\tensorflow\python\keras\layers\core.py", line 467, in call
    (array_ops.shape(inputs)[0],) + self.target_shape)
  File "C:\python36\lib\site-packages\tensorflow\python\ops\gen_array_ops.py", line 9094, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "C:\python36\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 545, in _apply_op_helper
    (input_name, err))
ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.
sainatarajan commented 5 years ago

The value is "None". Make sure that in this line: mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) s[1] is not None or maybe x.

kiflowb777 commented 5 years ago

Thanks.

With Keras 2.2.4 and TensorFlow 1.14 s is tuple where s[1] = 1000 (int) and x = Tensor("mrcnn_bbox_fc/Reshape_1:0", shape=(?, 1000, 20), dtype=float32)

With tf.keras (2.2.4-tf) and TensorFlow (both 1.14 and 2.0) s[1] = None and x = Tensor("mrcnn_bbox_fc/Reshape_1:0", shape=(?, ?, 20), dtype=float32)

sainatarajan commented 5 years ago

@kiflowb777 Great find! The problem is with the version issues then.

kiflowb777 commented 5 years ago

All inputs in both Keras and tf.keras are the same in the fpn_classifier_graph() function. But I noticed that calling the internal function: x = KL.TimeDistributed(KL.Conv2D(fc_layers_size, (pool_size, pool_size), padding="valid"),name="mrcnn_class_conv1")(x) gives a different result: (1.) x = Tensor("mrcnn_class_conv1/Reshape_1:0", shape=(?, 1000, 1, 1, 256), dtype=float32) (2.) x = Tensor("mrcnn_class_conv1/Reshape_1:0", shape=(?, ?, 1, 1, 256), dtype=float32)

(1.) is Keras, (2.) is tf.keras (both tf_version 1.14).

kiflowb777 commented 5 years ago

I noticed that in tf.keras in function fpn_classifier_graph the input rois does not have the parameter _keras_shape that contains a num_rois (which in my case has a value of 1000). In my project the rois are returned from ProposalLayer.

I found a similar problem: https://stackoverflow.com/questions/54613474/tensorflow-keras-input-layer-does-not-add-keras-shape

sainatarajan commented 5 years ago

@kiflowb777 I think you should post this on the Tensorflow issues page. This seems like some bug on their part and because they would not have adopted this recent change in their tf.keras package. So, this is working for you when you use keras and not tf.keras right?

kiflowb777 commented 5 years ago

I posted this on TensorFlow page: https://github.com/tensorflow/tensorflow/issues/33785

I also noticed that in Keras the overwritten function compute_output_shape is called, but not in the tf.keras.

For this reason, the shape is miscalculated by the ProposalLayer.

kiflowb777 commented 5 years ago

I temporarily fixed it by adding a line: rois.set_shape((None, num_rois, 4)) at the beginning of the function: fpn_classifier_graph but during training:

   File "C:\Project\mrcnn\model.py", line 2199, in compile
    self.keras_model.metrics_tensors.append(loss)
AttributeError: 'Model' object has no attribute 'metrics_tensors'
sainatarajan commented 5 years ago

This is definitely a Keras/TensorFlow version issue. self.keras_model is a Keras model object and maybe in the old/new version of Keras, they might have removed it I guess. I am not sure.

kiflowb777 commented 5 years ago

The AttributeError: 'Model' object has no attribute 'metrics_tensors' can by fixed by changing line model.metrics_tensors.append(loss) to model.metrics.append(loss)

but then I get:

in get_layer_elements_count
    input_shapes = layer.input_shape if type(layer.input_shape) is list else [layer.input_shape]
  File "C:\python\lib\site-packages\tensorflow\python\keras\engine\base_layer.py", line 1391, in input_shape
    ' has multiple inbound nodes, '
AttributeError: The layer "rpn_model has multiple inbound nodes, with different input shapes. Hence the notion of "input shape" is ill-defined for the layer. Use `get_input_shape_at(node_index)` instead.
paka0401 commented 4 years ago

I had the same error AttributeError: 'Model' object has no attribute 'metrics_tensors. I fixed it by downgrading keras to 2.1.0.

By the way I also had to downgrade numpy to 1.16.0 and tensorflow to 1.15.0 to be able to use the Mask_RCNN library. The code I am now able to run can be found here: https://machinelearningmastery.com/how-to-train-an-object-detection-model-with-keras/

K-M-Ibrahim-Khalilullah commented 4 years ago

@kiflowb777 Have yo solved the problem. I have the same problem on this line: mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) My tensorflow version: 1.13.1

Is there any suggestion please? thanks

kiflowb777 commented 4 years ago

@ibrahimLearning
Something they started fixing in: https://github.com/tensorflow/tensorflow/pull/34248 You can try run your code with tf-nightly: https://pypi.org/project/tf-nightly-gpu/ Maybe in TF2.1 the shape issue will be fixed.

kiflowb777 commented 4 years ago

You can also try call tf.reshape(out_tensor, self.compute_output_shape(input_shape)) after each usage of Custom Layer. More info: https://stackoverflow.com/questions/51028861/tensorflow-compute-output-shape-not-working-for-custom-layer https://github.com/tensorflow/tensorflow/issues/33785

But in spite of this, the model summary differ in keras and tf.keras.

lanternx commented 4 years ago

@ibrahimLearning Something they started fixing in: tensorflow/tensorflow#34248 You can try run your code with tf-nightly: https://pypi.org/project/tf-nightly-gpu/ Maybe in TF2.1 the shape issue will be fixed.

I wonder how to solve this problem? My tensorflow version is 2.2.

nguyendinh1987 commented 4 years ago

To solve the problem, you can change this line: mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) by : mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)

It works for me and the output is fine.

dji-mmmjukimo commented 4 years ago

To solve the problem, you can change this line: mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) by : mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)

It works for me and the output is fine.

@nguyendinh1987 I tried your way, but the error still exist: ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported. My tf vision is 2.3.0 and keras vision is 2.4.3 Do you know any model.py file can solve the all different vision problem?

nguyendinh1987 commented 4 years ago

@dji-mmmjukimo : My tf and Keras version are 1.13.0-rc and 2.2.4-tf respectively. According to tf.keras.Reshape API for your tf version (https://www.tensorflow.org/api_docs/python/tf/keras/layers/Reshape) input -1 is accepted. I think the problem is not because of -1 or s[1].


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

In the error message, "None values not supported" might stand for one of your input values fed to Reshape function is None. Can you try to check value of variables "num_classes" and "x" when you run your program.


Do you know any model.py file can solve the all different vision problem?

I do not have any idea :). Each version has its own specification and we need to modify our code to fit with it.

lanternx commented 4 years ago

@dji-mmmjukimo : My tf and Keras version are 1.13.0-rc and 2.2.4-tf respectively. According to tf.keras.Reshape API for your tf version (https://www.tensorflow.org/api_docs/python/tf/keras/layers/Reshape) input -1 is accepted. I think the problem is not because of -1 or s[1].

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

In the error message, "None values not supported" might stand for one of your input values fed to Reshape function is None. Can you try to check value of variables "num_classes" and "x" when you run your program.

Do you know any model.py file can solve the all different vision problem?

I do not have any idea :). Each version has its own specification and we need to modify our code to fit with it.

Thank you. But seems hard for an unskilled coder like me. :-|

Al-Badri179 commented 3 years ago

KL.Reshape

The value in not none and the same problem is exist

kiflowb777 commented 3 years ago

@ML-BOSS Please try this:

if s[1] is 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)
Al-Badri179 commented 3 years ago

if s[1] is 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 tried it and the problem didn't solve. I have this error:

File "c:\users\msi-pc\appdata\local\programs\python\python37\lib\site-packages\mask_rcnn-2.1-py3.7.egg\mrcnn\model.py", line 954 mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x) ^ IndentationError: expected an indented block

kiflowb777 commented 3 years ago

@Al-Badri179 which version of the TensorFlow are you using?

rampam commented 3 years ago

if s[1] is 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 tried it and the problem didn't solve. I have this error:

File "c:\users\msi-pc\appdata\local\programs\python\python37\lib\site-packages\mask_rcnn-2.1-py3.7.egg\mrcnn\model.py", line 954 mrcnn_bbox = layers.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x) ^ IndentationError: expected an indented block

You forgot the indentation after the if and else statements.

Liuxuweilxw commented 3 years ago

Try to uninstall keras and tensorflow ,then install tensorflow==1.3.0 and keras==2.0.8. If you don't want to uninstall tensorflow2.x which has been installed on your system,try to create a new environment.

madhumnk commented 1 year ago

ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_9028\2113197888.py in 1 print("loading weights for Mask R-CNN model…") ----> 2 model = modellib.MaskRCNN(mode="inference", config=config, model_dir='./')

~\Mask_RCNN\mrcnn\model.py in init(self, mode, config, model_dir) 1842 self.model_dir = model_dir 1843 self.set_log_dir() -> 1844 self.keras_model = self.build(mode=mode, config=config) 1845 1846 def build(self, mode, config):

~\Mask_RCNN\mrcnn\model.py in build(self, mode, config) 2036 # Network Heads 2037 # Proposal classifier and BBox regressor heads -> 2038 mrcnn_class_logits, mrcnn_class, mrcnn_bbox = fpn_classifier_graph(rpn_rois, mrcnn_feature_maps, input_image_meta, 2039 config.POOL_SIZE, config.NUM_CLASSES, 2040 train_bn=config.TRAIN_BN,

~\Mask_RCNN\mrcnn\model.py in fpn_classifier_graph(rois, feature_maps, image_meta, pool_size, num_classes, train_bn, fc_layers_size) 964 # Reshape to [batch, num_rois, NUM_CLASSES, (dy, dx, log(dh), log(dw))] 965 s = K.int_shape(x) --> 966 mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) 967 968 return mrcnn_class_logits, mrcnn_probs, mrcnn_bbox

~\anaconda3\lib\site-packages\keras\engine\base_layer_v1.py in call(self, *args, *kwargs) 836 self._compute_dtype_object 837 ): --> 838 outputs = call_fn(cast_inputs, args, **kwargs) 839 840 except tf.errors.OperatorNotAllowedInGraphError as e:

~\anaconda3\lib\site-packages\keras\layers\reshaping\reshape.py in call(self, inputs) 135 136 def call(self, inputs): --> 137 result = tf.reshape(inputs, (tf.shape(inputs)[0],) + self.target_shape) 138 if not tf.executing_eagerly(): 139 # Set the static shape for the result since it might lost during

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, **kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.traceback) --> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\op_def_library.py in _ExtractInputsAndAttrs(op_type_name, op_def, allowed_list_attr_map, keywords, default_type_attr_map, attrs, inputs, input_types) 569 values, as_ref=input_arg.is_ref).dtype.name 570 except ValueError as err: --> 571 raise ValueError( 572 f"Tried to convert '{input_name}' to a tensor and failed. " 573 f"Error: {err}")

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

madhumnk commented 1 year ago

ValueError Traceback (most recent call last) ~\AppData\Local\Temp\ipykernel_9028\2113197888.py in 1 print("loading weights for Mask R-CNN model…") ----> 2 model = modellib.MaskRCNN(mode="inference", config=config, model_dir='./')

~\Mask_RCNN\mrcnn\model.py in init(self, mode, config, model_dir) 1842 self.model_dir = model_dir 1843 self.set_log_dir() -> 1844 self.keras_model = self.build(mode=mode, config=config) 1845 1846 def build(self, mode, config):

~\Mask_RCNN\mrcnn\model.py in build(self, mode, config) 2036 # Network Heads 2037 # Proposal classifier and BBox regressor heads -> 2038 mrcnn_class_logits, mrcnn_class, mrcnn_bbox = fpn_classifier_graph(rpn_rois, mrcnn_feature_maps, input_image_meta, 2039 config.POOL_SIZE, config.NUM_CLASSES, 2040 train_bn=config.TRAIN_BN,

~\Mask_RCNN\mrcnn\model.py in fpn_classifier_graph(rois, feature_maps, image_meta, pool_size, num_classes, train_bn, fc_layers_size) 964 # Reshape to [batch, num_rois, NUM_CLASSES, (dy, dx, log(dh), log(dw))] 965 s = K.int_shape(x) --> 966 mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x) 967 968 return mrcnn_class_logits, mrcnn_probs, mrcnn_bbox

~\anaconda3\lib\site-packages\keras\engine\base_layer_v1.py in call(self, *args, *kwargs) 836 self._compute_dtype_object 837 ): --> 838 outputs = call_fn(cast_inputs, args, **kwargs) 839 840 except tf.errors.OperatorNotAllowedInGraphError as e:

~\anaconda3\lib\site-packages\keras\layers\reshaping\reshape.py in call(self, inputs) 135 136 def call(self, inputs): --> 137 result = tf.reshape(inputs, (tf.shape(inputs)[0],) + self.target_shape) 138 if not tf.executing_eagerly(): 139 # Set the static shape for the result since it might lost during

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\util\traceback_utils.py in error_handler(*args, kwargs) 151 except Exception as e: 152 filtered_tb = _process_traceback_frames(e.traceback**) --> 153 raise e.with_traceback(filtered_tb) from None 154 finally: 155 del filtered_tb

~\AppData\Roaming\Python\Python39\site-packages\tensorflow\python\framework\op_def_library.py in _ExtractInputsAndAttrs(op_type_name, op_def, allowed_list_attr_map, keywords, default_type_attr_map, attrs, inputs, input_types) 569 values, as_ref=input_arg.is_ref).dtype.name 570 except ValueError as err: --> 571 raise ValueError( 572 f"Tried to convert '{input_name}' to a tensor and failed. " 573 f"Error: {err}")

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

Getting error after trying all sort of solutions provided here, Please help me with it