【问题标题】:How to read tensorflow non-maximum-suppression method source code?如何阅读 tensorflow 非极大值抑制方法源码?
【发布时间】:2018-02-21 15:56:51
【问题描述】:

我正在尝试阅读这个line中的Tensorflow非最大抑制方法的源代码。它是从gen_image_ops文件导入的,但是我在tensorflow源代码的任何地方都找不到该文件。

是否有任何来源可以获取此方法的代码?

【问题讨论】:

标签: tensorflow object-detection object-detection-api non-maximum-suppression


【解决方案1】:

Here you go

当您在他们的 python 操作定义中看到“import gen_*”行时,他们正在导入一个自动生成的 python 模块,该模块绑定到操作的 c++ 实现。如果您从源代码构建,则生成将在那时发生。如果您正在下载 pip 模块或其他一些预构建版本,则生成已经完成,您只是在引用已编译的库。

【讨论】:

    【解决方案2】:

    我也尝试过挖掘他们的 repo,但没有成功,所以我最终只是从我的编辑器中获取了代码。

    我使用了 PyCharm,所以我只是做了from tensorflow.python.ops.gen_image_ops 然后点击它来获取代码。

    我已经添加了它的两个版本,所以你去吧。

    第一版

    def _non_max_suppression(boxes, scores, max_output_size, iou_threshold=0.5, name=None):
      r"""Greedily selects a subset of bounding boxes in descending order of score,
    
      pruning away boxes that have high intersection-over-union (IOU) overlap
      with previously selected boxes.  Bounding boxes are supplied as
      [y1, x1, y2, x2], where (y1, x1) and (y2, x2) are the coordinates of any
      diagonal pair of box corners and the coordinates can be provided as normalized
      (i.e., lying in the interval [0, 1]) or absolute.  Note that this algorithm
      is agnostic to where the origin is in the coordinate system.  Note that this
      algorithm is invariant to orthogonal transformations and translations
      of the coordinate system; thus translating or reflections of the coordinate
      system result in the same boxes being selected by the algorithm.
      The output of this operation is a set of integers indexing into the input
      collection of bounding boxes representing the selected boxes.  The bounding
      box coordinates corresponding to the selected indices can then be obtained
      using the `tf.gather operation`.  For example:
        selected_indices = tf.image.non_max_suppression(
            boxes, scores, max_output_size, iou_threshold)
        selected_boxes = tf.gather(boxes, selected_indices)
    
      Args:
        boxes: A `Tensor` of type `float32`.
          A 2-D float tensor of shape `[num_boxes, 4]`.
        scores: A `Tensor` of type `float32`.
          A 1-D float tensor of shape `[num_boxes]` representing a single
          score corresponding to each box (each row of boxes).
        max_output_size: A `Tensor` of type `int32`.
          A scalar integer tensor representing the maximum number of
          boxes to be selected by non max suppression.
        iou_threshold: An optional `float`. Defaults to `0.5`.
          A float representing the threshold for deciding whether boxes
          overlap too much with respect to IOU.
        name: A name for the operation (optional).
    
      Returns:
        A `Tensor` of type `int32`.
        A 1-D integer tensor of shape `[M]` representing the selected
        indices from the boxes tensor, where `M <= max_output_size`.
      """
      if iou_threshold is None:
        iou_threshold = 0.5
      iou_threshold = _execute.make_float(iou_threshold, "iou_threshold")
      _ctx = _context.context()
      if _ctx.in_graph_mode():
        _, _, _op = _op_def_lib._apply_op_helper(
            "NonMaxSuppression", boxes=boxes, scores=scores,
            max_output_size=max_output_size, iou_threshold=iou_threshold,
            name=name)
        _result = _op.outputs[:]
        _inputs_flat = _op.inputs
        _attrs = ("iou_threshold", _op.get_attr("iou_threshold"))
      else:
        boxes = _ops.convert_to_tensor(boxes, _dtypes.float32)
        scores = _ops.convert_to_tensor(scores, _dtypes.float32)
        max_output_size = _ops.convert_to_tensor(max_output_size, _dtypes.int32)
        _inputs_flat = [boxes, scores, max_output_size]
        _attrs = ("iou_threshold", iou_threshold)
        _result = _execute.execute(b"NonMaxSuppression", 1, inputs=_inputs_flat,
                                   attrs=_attrs, ctx=_ctx, name=name)
      _execute.record_gradient(
          "NonMaxSuppression", _inputs_flat, _attrs, _result, name)
      _result, = _result
      return _result
    

    第二版

    def _non_max_suppression_v2(boxes, scores, max_output_size, iou_threshold, name=None):
      r"""Greedily selects a subset of bounding boxes in descending order of score,
    
      pruning away boxes that have high intersection-over-union (IOU) overlap
      with previously selected boxes.  Bounding boxes are supplied as
      [y1, x1, y2, x2], where (y1, x1) and (y2, x2) are the coordinates of any
      diagonal pair of box corners and the coordinates can be provided as normalized
      (i.e., lying in the interval [0, 1]) or absolute.  Note that this algorithm
      is agnostic to where the origin is in the coordinate system.  Note that this
      algorithm is invariant to orthogonal transformations and translations
      of the coordinate system; thus translating or reflections of the coordinate
      system result in the same boxes being selected by the algorithm.
    
      The output of this operation is a set of integers indexing into the input
      collection of bounding boxes representing the selected boxes.  The bounding
      box coordinates corresponding to the selected indices can then be obtained
      using the `tf.gather operation`.  For example:
    
        selected_indices = tf.image.non_max_suppression_v2(
            boxes, scores, max_output_size, iou_threshold)
        selected_boxes = tf.gather(boxes, selected_indices)
    
      Args:
        boxes: A `Tensor` of type `float32`.
          A 2-D float tensor of shape `[num_boxes, 4]`.
        scores: A `Tensor` of type `float32`.
          A 1-D float tensor of shape `[num_boxes]` representing a single
          score corresponding to each box (each row of boxes).
        max_output_size: A `Tensor` of type `int32`.
          A scalar integer tensor representing the maximum number of
          boxes to be selected by non max suppression.
        iou_threshold: A `Tensor` of type `float32`.
          A 0-D float tensor representing the threshold for deciding whether
          boxes overlap too much with respect to IOU.
        name: A name for the operation (optional).
    
      Returns:
        A `Tensor` of type `int32`.
        A 1-D integer tensor of shape `[M]` representing the selected
        indices from the boxes tensor, where `M <= max_output_size`.
      """
      _ctx = _context.context()
      if _ctx.in_graph_mode():
        _, _, _op = _op_def_lib._apply_op_helper(
            "NonMaxSuppressionV2", boxes=boxes, scores=scores,
            max_output_size=max_output_size, iou_threshold=iou_threshold,
            name=name)
        _result = _op.outputs[:]
        _inputs_flat = _op.inputs
        _attrs = None
      else:
        boxes = _ops.convert_to_tensor(boxes, _dtypes.float32)
        scores = _ops.convert_to_tensor(scores, _dtypes.float32)
        max_output_size = _ops.convert_to_tensor(max_output_size, _dtypes.int32)
        iou_threshold = _ops.convert_to_tensor(iou_threshold, _dtypes.float32)
        _inputs_flat = [boxes, scores, max_output_size, iou_threshold]
        _attrs = None
        _result = _execute.execute(b"NonMaxSuppressionV2", 1, inputs=_inputs_flat,
                                   attrs=_attrs, ctx=_ctx, name=name)
      _execute.record_gradient(
          "NonMaxSuppressionV2", _inputs_flat, _attrs, _result, name)
      _result, = _result
      return _result
    

    【讨论】:

    • 谢谢,这真的很有帮助。他们似乎调用execute_apply_op_helper 来运行nms。关于如何达到核心算法的任何想法?
    • 做和我一样的事情。从您的 IDE 中打开它
    猜你喜欢
    • 2017-08-10
    • 2015-11-06
    • 2017-12-09
    • 2014-12-28
    • 1970-01-01
    • 2014-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多