如何阅读tensorflow非极大值抑制法源码?
How to read tensorflow non-maximum-suppression method source code?
我正在尝试阅读此line中Tensorflow非最大抑制方法的源代码。它是从 gen_image_ops
文件导入的,但我在 tensorflow 源代码中的任何地方都找不到该文件。
是否有任何来源可以获取此方法的代码?
我也尝试过挖掘他们的回购协议,但没有成功,所以我最终只是从我的编辑那里得到了代码。
我用过 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
只要您在他们的 python 操作定义中看到 "import gen_*" 行,他们就会导入一个自动生成的 python 模块,该模块绑定到操作的 C++ 实现。如果您从源代码构建,生成将在那时发生。如果您正在下载 pip 模块或其他一些预构建版本,则生成已经完成,您只是在引用已编译的库。
我正在尝试阅读此line中Tensorflow非最大抑制方法的源代码。它是从 gen_image_ops
文件导入的,但我在 tensorflow 源代码中的任何地方都找不到该文件。
是否有任何来源可以获取此方法的代码?
我也尝试过挖掘他们的回购协议,但没有成功,所以我最终只是从我的编辑那里得到了代码。
我用过 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
只要您在他们的 python 操作定义中看到 "import gen_*" 行,他们就会导入一个自动生成的 python 模块,该模块绑定到操作的 C++ 实现。如果您从源代码构建,生成将在那时发生。如果您正在下载 pip 模块或其他一些预构建版本,则生成已经完成,您只是在引用已编译的库。