首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Tensorflow:断言失败:[`labels`越界]

Tensorflow:断言失败:[`labels`越界]
EN

Stack Overflow用户
提问于 2017-09-12 05:09:54
回答 5查看 5.9K关注 0票数 2

运行测试脚本时出现“Label out out bound”错误。将注释值与类的数量进行比较时,会在confusion_matrix函数中抛出错误。在我的例子中,注释值是一幅图像(560x560),number_of_classes = 2。

代码语言:javascript
复制
[check_ops.assert_less(labels, num_classes_int64, message='`labels` out of bound')], labels

上面的条件总是会失败,因为注释数据大于类的数量。

首先,我很有可能误解了代码,但我不能理解它。

其次,如果这是一个有效的检查,那么我如何修改我的代码或数据来避免这个错误。

代码语言:javascript
复制
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)
      ---->>labels = control_flow_ops.with_dependencies(
          [check_ops.assert_less(
              labels, num_classes_int64, message='`labels` out of bound')],
          labels)<<----
      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)
代码语言:javascript
复制
Traceback (most recent call last):
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1327, in _do_call
    return fn(*args)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1306, in _run_fn
    status, run_metadata)
  File "C:\Program Files\Python35\lib\contextlib.py", line 66, in __exit__
    next(self.gen)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\errors_impl.py", 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 < 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="/job:localhost/replica:0/task:0/cpu:0"](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 "C:/Users/supriya.godge/PycharmProjects/tf-image-segmentation/tf_image_segmentation/recipes/pascal_voc/DeepLab/output/resnet_v1_101_8s_test_airplan.py", line 81, in <module>
    image_np, annotation_np, pred_np, tmp = sess.run([image, annotation, pred, update_op])
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 895, in run
    run_metadata_ptr)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1124, in _run
    feed_dict_tensor, options, run_metadata)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", line 1321, in _do_run
    options, run_metadata)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\client\session.py", 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 < 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="/job:localhost/replica:0/task:0/cpu:0"](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 "C:/Users/supriya.godge/PycharmProjects/tf-image-segmentation/tf_image_segmentation/recipes/pascal_voc/DeepLab/output/resnet_v1_101_8s_test_airplan.py", line 64, in <module>
    weights=weights)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\contrib\metrics\python\ops\metric_ops.py", line 2245, in streaming_mean_iou
    updates_collections=updates_collections, name=name)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\metrics_impl.py", line 917, in mean_iou
    num_classes, weights)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\metrics_impl.py", line 285, in _streaming_confusion_matrix
    labels, predictions, num_classes, weights=weights, dtype=cm_dtype)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\confusion_matrix.py", line 178, in confusion_matrix
    labels, num_classes_int64, message='`labels` out of bound')],
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\check_ops.py", line 401, in assert_less
    return control_flow_ops.Assert(condition, data, summarize=summarize)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\util\tf_should_use.py", line 175, in wrapped
    return _add_should_use_warning(fn(*args, **kwargs))
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 131, in Assert
    condition, no_op, true_assert, name="AssertGuard")
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\util\deprecation.py", line 296, in new_func
    return func(*args, **kwargs)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1828, in cond
    orig_res_f, res_f = context_f.BuildCondBranch(false_fn)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 1694, in BuildCondBranch
    original_result = fn()
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\control_flow_ops.py", line 129, in true_assert
    condition, data, summarize, name="Assert")
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\ops\gen_logging_ops.py", line 35, in _assert
    summarize=summarize, name=name)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\op_def_library.py", line 767, in apply_op
    op_def=op_def)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", line 2630, in create_op
    original_op=self._default_original_op, op_def=op_def)
  File "C:\Program Files\Python35\lib\site-packages\tensorflow\python\framework\ops.py", 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 < 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="/job:localhost/replica:0/task:0/cpu:0"](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)]]

我真的迷失在这里,所以任何帮助或建议是感激的!

EN

回答 5

Stack Overflow用户

发布于 2017-09-13 21:51:54

在注释文件中,标签是0、1、2、255。标签的范围是3。所以当在上面的注释文件中检测到255时,就会抛出错误。在我删除了所有255个值之后,代码运行正常,没有任何错误。

票数 3
EN

Stack Overflow用户

发布于 2018-07-24 07:25:18

这里也有同样的问题。

“labels”数组Y_true的值为0, 255。我使用:

代码语言:javascript
复制
Y_true = Y_true/255

Y_true压缩到0, 1

这样就消除了错误。

票数 3
EN

Stack Overflow用户

发布于 2020-03-26 13:32:21

在tensorflow 1.15.2下,tensorflow/models/research/deeplab基本上是可以的。

错误消息如下:

无效参数:断言失败:labels越界x (mean_iou/confusion_matrix/control_dependency:0) =

很可能是因为没有将背景视为1类。例如deeplab/datasets/data_generator.py

代码语言:javascript
复制
# Number of semantic classes, including the
# background class (if exists). For example, there
# are 20 foreground classes + 1 background class in
# the PASCAL VOC 2012 dataset. Thus, we set
# num_classes=21.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46164419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档