title: Tensorflow:assertion failed: [`labels` out of bound]
body: <p>Getting a 'Label out of bound' error while running the testing script. Error is thrown in the confusion_matrix function when the annotation values are compared with the number of classes. In my case annotation value is an image(560x560) and number_of_classes = 2.</p>
  <pre><code>[check_ops.assert_less(labels, num_classes_int64, message='`labels` out of bound')], labels
  </code></pre>
  <p>The above condition is always going to fail, as the annotation data is bigger than number of classes.</p>
  <p>First, There is a good chance that I am misunderstanding the code, but I can not make any sense of it.</p>
  <p>Second, If this is a valid check, then how can I modify my code or data to avoid this error.</p>
  <pre class="lang-py prettyprint-override"><code>def confusion_matrix(labels, predictions, num_classes=None, dtype=dtypes.int32,
                       name=None, weights=None):
   with ops.name_scope(name, 'confusion_matrix',
                        (predictions, labels, num_classes, weights)) as name:
      labels, predictions = remove_squeezable_dimensions(
          ops.convert_to_tensor(labels, name='labels'),
          ops.convert_to_tensor(
              predictions, name='predictions'))
      predictions = math_ops.cast(predictions, dtypes.int64)
      labels = math_ops.cast(labels, dtypes.int64)
  
      # Sanity checks - underflow or overflow can cause memory corruption.
      labels = control_flow_ops.with_dependencies(
          [check_ops.assert_non_negative(
              labels, message='`labels` contains negative values')],
          labels)
      predictions = control_flow_ops.with_dependencies(
          [check_ops.assert_non_negative(
              predictions, message='`predictions` contains negative values')],
          predictions)
      print(num_classes)
      if num_classes is None:
        num_classes = math_ops.maximum(math_ops.reduce_max(predictions),
                                       math_ops.reduce_max(labels)) + 1
        #$
  
      else:
        num_classes_int64 = math_ops.cast(num_classes, dtypes.int64)
        ----&gt;&gt;labels = control_flow_ops.with_dependencies(
            [check_ops.assert_less(
                labels, num_classes_int64, message='`labels` out of bound')],
            labels)&lt;&lt;----
        predictions = control_flow_ops.with_dependencies(
            [check_ops.assert_less(
                predictions, num_classes_int64,
                message='`predictions` out of bound')],
            predictions)
  
      if weights is not None:
        predictions.get_shape().assert_is_compatible_with(weights.get_shape())
        weights = math_ops.cast(weights, dtype)
  
      shape = array_ops.stack([num_classes, num_classes])
      indices = array_ops.transpose(array_ops.stack([labels, predictions]))
      values = (array_ops.ones_like(predictions, dtype)
                if weights is None else weights)
      cm_sparse = sparse_tensor.SparseTensor(
          indices=indices, values=values, dense_shape=math_ops.to_int64(shape))
      zero_matrix = array_ops.zeros(math_ops.to_int32(shape), dtype)
  
      return sparse_ops.sparse_add(zero_matrix, cm_sparse)
  </code></pre>
  
  <pre class="lang-none prettyprint-override"><code>Traceback (most recent call last):
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1327, in _do_call
      return fn(*args)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1306, in _run_fn
      status, run_metadata)
    File &quot;C:\Program Files\Python35\lib\contextlib.py&quot;, line 66, in __exit__
      next(self.gen)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py&quot;, line 466, in raise_exception_on_not_ok_status
      pywrap_tensorflow.TF_GetCode(status))
  tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [`labels` out of bound] [Condition x &lt; y did not hold element-wise:x (mean_iou/confusion_matrix/control_dependency:0) = ] [0 0 0...] [y (mean_iou/ToInt64_2:0) = ] [21]
       [[Node: mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_INT64, DT_STRING, DT_INT64], summarize=3, _device=&quot;/job:localhost/replica:0/task:0/cpu:0&quot;](mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_0, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_1, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch_1, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_3, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch_2)]]
  
  During handling of the above exception, another exception occurred:
  
  Traceback (most recent call last):
    File &quot;C:/Users/supriya.godge/PycharmProjects/tf-image-segmentation/tf_image_segmentation/recipes/pascal_voc/DeepLab/output/resnet_v1_101_8s_test_airplan.py&quot;, line 81, in &lt;module&gt;
      image_np, annotation_np, pred_np, tmp = sess.run([image, annotation, pred, update_op])
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py&quot;, line 895, in run
      run_metadata_ptr)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1124, in _run
      feed_dict_tensor, options, run_metadata)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1321, in _do_run
      options, run_metadata)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py&quot;, line 1340, in _do_call
      raise type(e)(node_def, op, message)
  tensorflow.python.framework.errors_impl.InvalidArgumentError: assertion failed: [`labels` out of bound] [Condition x &lt; y did not hold element-wise:x (mean_iou/confusion_matrix/control_dependency:0) = ] [0 0 0...] [y (mean_iou/ToInt64_2:0) = ] [21]
       [[Node: mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_INT64, DT_STRING, DT_INT64], summarize=3, _device=&quot;/job:localhost/replica:0/task:0/cpu:0&quot;](mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_0, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_1, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch_1, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_3, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch_2)]]
  
  Caused by op 'mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert', defined at:
    File &quot;C:/Users/supriya.godge/PycharmProjects/tf-image-segmentation/tf_image_segmentation/recipes/pascal_voc/DeepLab/output/resnet_v1_101_8s_test_airplan.py&quot;, line 64, in &lt;module&gt;
      weights=weights)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\contrib\metrics\python\ops\metric_ops.py&quot;, line 2245, in streaming_mean_iou
      updates_collections=updates_collections, name=name)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\metrics_impl.py&quot;, line 917, in mean_iou
      num_classes, weights)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\metrics_impl.py&quot;, line 285, in _streaming_confusion_matrix
      labels, predictions, num_classes, weights=weights, dtype=cm_dtype)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\confusion_matrix.py&quot;, line 178, in confusion_matrix
      labels, num_classes_int64, message='`labels` out of bound')],
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\check_ops.py&quot;, line 401, in assert_less
      return control_flow_ops.Assert(condition, data, summarize=summarize)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\util\tf_should_use.py&quot;, line 175, in wrapped
      return _add_should_use_warning(fn(*args, **kwargs))
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 131, in Assert
      condition, no_op, true_assert, name=&quot;AssertGuard&quot;)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\util\deprecation.py&quot;, line 296, in new_func
      return func(*args, **kwargs)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 1828, in cond
      orig_res_f, res_f = context_f.BuildCondBranch(false_fn)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 1694, in BuildCondBranch
      original_result = fn()
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py&quot;, line 129, in true_assert
      condition, data, summarize, name=&quot;Assert&quot;)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py&quot;, line 35, in _assert
      summarize=summarize, name=name)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py&quot;, line 767, in apply_op
      op_def=op_def)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py&quot;, line 2630, in create_op
      original_op=self._default_original_op, op_def=op_def)
    File &quot;C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py&quot;, line 1204, in __init__
      self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access
  
  InvalidArgumentError (see above for traceback): assertion failed: [`labels` out of bound] [Condition x &lt; y did not hold element-wise:x (mean_iou/confusion_matrix/control_dependency:0) = ] [0 0 0...] [y (mean_iou/ToInt64_2:0) = ] [21]
       [[Node: mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert = Assert[T=[DT_STRING, DT_STRING, DT_INT64, DT_STRING, DT_INT64], summarize=3, _device=&quot;/job:localhost/replica:0/task:0/cpu:0&quot;](mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_0, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_1, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch_1, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/data_3, mean_iou/confusion_matrix/assert_less/Assert/AssertGuard/Assert/Switch_2)]]
  </code></pre>
  
  <p>I am really lost here so any help or suggestion is appreciated!</p>