tensorflow / models

Models and examples built with TensorFlow
Other
77.05k stars 45.77k forks source link

reframe_image_corners_relative_to_boxes() breaks run_inference_for_single_image() #10330

Closed Paperone80 closed 3 years ago

Paperone80 commented 3 years ago

Prerequisites

Please answer the following questions for yourself before submitting an issue.

1. The entire URL of the file you are using

https://github.com/tensorflow/models/tree/master/research/...

2. Describe the bug

@vighneshbirodkar, the rewrite of reframe_image_corners_relative_to_boxes() in research/object_detection/utils/ops.py with commit #58644b9653601e2cc8ba2c335b7ecb76887ff8b7 breaks run_inference_for_single_image().

PATH_TO_FROZEN_GRAPH = "mask_rcnn_inception_v2_coco_2018_01_28/frozen_inference_graph.pb"

Resolved after reverting back to previous version: git checkout 41aafda97ff4b7bc6125c07bbfe3658707414a81 -- "research/object_detection/utils/ops.py"

Last part of the traceback:

in run_inference_for_single_image(image, graph) 44 detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) 45 detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( ---> 46 detection_masks, detection_boxes, image.shape[0], image.shape[1]) 47 detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8) 48 ## Follow the convention by adding back the batch dimension /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/object_detection/utils/ops.py in reframe_box_masks_to_image_masks(box_masks, boxes, image_height, image_width, resize_method) 871 tf.shape(box_masks)[0] > 0, 872 reframe_box_masks_to_image_masks_default, --> 873 lambda: tf.zeros([0, image_height, image_width, 1], box_masks.dtype)) 874 return tf.squeeze(image_masks, axis=3) 875 /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/tensorflow_core/python/util/deprecation.py in new_func(*args, **kwargs) 505 'in a future version' if date is None else ('after %s' % date), 506 instructions) --> 507 return func(*args, **kwargs) 508 509 doc = _add_deprecated_arg_notice_to_docstring( /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py in cond(pred, true_fn, false_fn, strict, name, fn1, fn2) 1222 try: 1223 context_t.Enter() -> 1224 orig_res_t, res_t = context_t.BuildCondBranch(true_fn) 1225 if orig_res_t is None: 1226 raise ValueError("true_fn must have a return value.") /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/tensorflow_core/python/ops/control_flow_ops.py in BuildCondBranch(self, fn) 1059 """Add the subgraph defined by fn() to the graph.""" 1060 pre_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION) # pylint: disable=protected-access -> 1061 original_result = fn() 1062 post_summaries = ops.get_collection(ops.GraphKeys._SUMMARY_COLLECTION) # pylint: disable=protected-access 1063 if len(post_summaries) > len(pre_summaries): /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/object_detection/utils/ops.py in reframe_box_masks_to_image_masks_default() 861 resized_crops = tf.image.crop_and_resize( 862 image=box_masks_expanded, --> 863 boxes=reframe_image_corners_relative_to_boxes(boxes), 864 box_ind=tf.range(num_boxes), 865 crop_size=[image_height, image_width], /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/object_detection/utils/ops.py in reframe_image_corners_relative_to_boxes(boxes) 812 reframed_boxes: Reframes boxes with same shape as input. 813 """ --> 814 ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=1) 815 816 height = tf.maximum(ymax - ymin, 1e-4) /opt/actions-runner/_work/_tool/Python/3.6.12/x64/lib/python3.6/site-packages/tensorflow_core/python/ops/array_ops.py in unstack(value, num, axis, name) 1320 num = value_shape.dims[axis].value 1321 if num is None: -> 1322 raise ValueError("Cannot infer num from shape %s" % value_shape) 1323 return gen_array_ops.unpack(value, num=num, axis=axis, name=name) 1324 ValueError: Cannot infer num from shape (?, ?) ValueError: Cannot infer num from shape (?, ?) ## 6. System information - OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Ubuntu 16.04 - Mobile device name if the issue happens on a mobile device: - TensorFlow installed from (source or binary): binary - TensorFlow version (use command below): 1.15.5 - Python version: 3.6.12 - Bazel version (if compiling from source): na - GCC/Compiler version (if compiling from source): na - CUDA/cuDNN version: 10.0.130 - GPU model and memory: 2080 RTX Ti 11GB
kumariko commented 3 years ago

@Paperone80 As you are using an older version of tensorflow(1.x) which is officially considered as end of life. could you please upgrade to 2.6 which is latest stable version of TF and let us know if the issue still persists in newer versions. Thanks!

Paperone80 commented 3 years ago

@kumariko, @vighneshbirodkar, yep, still the same issue with TF 2.6.0 and latest clone from models master:

tensorflow 2.6.0 tensorflow-addons 0.11.2 tensorflow-datasets 4.1.0 tensorflow-estimator 2.6.0 tensorflow-gpu 2.6.0 tensorflow-hub 0.10.0 tensorflow-metadata 0.26.0 tensorflow-model-optimization 0.5.0 tensorflow-serving-api 2.3.0 tensorflow-text 2.6.0


ValueError Traceback (most recent call last)

in 19 with tf.compat.v1.Session(graph=detection_graph) as sess: 20 output_dict, tensor_dict = run_inference_for_single_image( ---> 21 image_np.astype('uint8'), graph=detection_graph) #detection_graph 22 23 print(len(output_dict)) in run_inference_for_single_image(image, graph) 44 detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1]) 45 detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks( ---> 46 detection_masks, detection_boxes, image.shape[0], image.shape[1]) 47 detection_masks_reframed = tf.cast(tf.greater(detection_masks_reframed, 0.5), tf.uint8) 48 ## Follow the convention by adding back the batch dimension /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/object_detection/utils/ops.py in reframe_box_masks_to_image_masks(box_masks, boxes, image_height, image_width, resize_method) 871 tf.shape(box_masks)[0] > 0, 872 reframe_box_masks_to_image_masks_default, --> 873 lambda: tf.zeros([0, image_height, image_width, 1], box_masks.dtype)) 874 return tf.squeeze(image_masks, axis=3) 875 /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs) 204 """Call target, and fall back on dispatchers if there is a TypeError.""" 205 try: --> 206 return target(*args, **kwargs) 207 except (TypeError, ValueError): 208 # Note: convert_to_eager_tensor currently raises a ValueError, not a /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/util/deprecation.py in new_func(*args, **kwargs) 547 'in a future version' if date is None else ('after %s' % date), 548 instructions) --> 549 return func(*args, **kwargs) 550 551 doc = _add_deprecated_arg_notice_to_docstring( /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/ops/control_flow_ops.py in cond(pred, true_fn, false_fn, strict, name, fn1, fn2) 1252 # Always enable control flow v2 if building a function, regardless of toggle. 1253 if util.EnableControlFlowV2(ops.get_default_graph()): -> 1254 return cond_v2.cond_v2(pred, true_fn, false_fn, name) 1255 1256 with ops.name_scope(name, "cond", [pred]): /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/ops/cond_v2.py in cond_v2(pred, true_fn, false_fn, name) 86 true_name, collections=ops.get_default_graph()._collections), # pylint: disable=protected-access 87 add_control_dependencies=add_control_dependencies, ---> 88 op_return_value=pred) 89 false_graph = func_graph_module.func_graph_from_py_func( 90 false_name, /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/framework/func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes, acd_record_initial_resource_uses) 1005 _, original_func = tf_decorator.unwrap(python_func) 1006 -> 1007 func_outputs = python_func(*func_args, **func_kwargs) 1008 1009 # invariant: `func_outputs` contains only Tensors, CompositeTensors, /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/object_detection/utils/ops.py in reframe_box_masks_to_image_masks_default() 861 resized_crops = tf.image.crop_and_resize( 862 image=box_masks_expanded, --> 863 boxes=reframe_image_corners_relative_to_boxes(boxes), 864 box_ind=tf.range(num_boxes), 865 crop_size=[image_height, image_width], /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/object_detection/utils/ops.py in reframe_image_corners_relative_to_boxes(boxes) 812 reframed_boxes: Reframes boxes with same shape as input. 813 """ --> 814 ymin, xmin, ymax, xmax = tf.unstack(boxes, axis=1) 815 816 height = tf.maximum(ymax - ymin, 1e-4) /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/util/dispatch.py in wrapper(*args, **kwargs) 204 """Call target, and fall back on dispatchers if there is a TypeError.""" 205 try: --> 206 return target(*args, **kwargs) 207 except (TypeError, ValueError): 208 # Note: convert_to_eager_tensor currently raises a ValueError, not a /opt/intelpython3/envs/python3.6_env_tf2_od/lib/python3.6/site-packages/tensorflow/python/ops/array_ops.py in unstack(value, num, axis, name) 1671 num = value_shape.dims[axis].value 1672 if num is None: -> 1673 raise ValueError("Cannot infer num from shape %s" % value_shape) 1674 return gen_array_ops.unpack(value, num=num, axis=axis, name=name) 1675 ValueError: Cannot infer num from shape (None, None)
vighneshbirodkar commented 3 years ago

I am taking a look now.

vighneshbirodkar commented 3 years ago

@kumariko Can you try now ?

Paperone80 commented 3 years ago

@vighneshbirodkar, pulled your latest changes and ran our tests again. No error and the return values are as expected again. Seems resolved now. Thanks.

google-ml-butler[bot] commented 3 years ago

Are you satisfied with the resolution of your issue? Yes No