簡體   English   中英

如何閱讀tensorflow非最大抑制方法源代碼?

[英]How to read tensorflow non-maximum-suppression method source code?

我正在嘗試在此行中閱讀Tensorflow非最大抑制方法的源代碼。 它是從gen_image_ops文件導入的,但是我在tensorflow源代碼的任何地方都找不到該文件。

有什么資料可以聯系到此方法的代碼嗎?

干得好

每當他們在python op定義中看到“ import gen_ *”行時,他們就會導入一個自動生成的python模塊,該模塊具有對op的c ++實現的綁定。 如果從源代碼構建,則將在那時進行生成。 如果要下載pip模塊或其他預構建的版本,則說明生成已經完成,您只需要引用已編譯的庫即可。

我也嘗試過挖掘他們的倉庫,但是沒有運氣,所以我最終只是從編輯器獲取代碼。

我使用了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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM