iou_similarity

paddle.fluid.layers.iou_similarity(x, y, name=None)[source]

IOU Similarity Operator

Computes intersection-over-union (IOU) between two box lists. Box list ‘X’ should be a LoDTensor and ‘Y’ is a common Tensor, boxes in ‘Y’ are shared by all instance of the batched inputs of X. Given two boxes A and B, the calculation of IOU is as follows:

$$ IOU(A, B) = \frac{area(A\cap B)}{area(A)+area(B)-area(A\cap B)} $$

Parameters
  • x (Variable) – (LoDTensor, default LoDTensor<float>) Box list X is a 2-D LoDTensor with shape [N, 4] holds N boxes, each box is represented as [xmin, ymin, xmax, ymax], the shape of X is [N, 4]. [xmin, ymin] is the left top coordinate of the box if the input is image feature map, they are close to the origin of the coordinate system. [xmax, ymax] is the right bottom coordinate of the box. This tensor can contain LoD information to represent a batch of inputs. One instance of this batch can contain different numbers of entities.The data type is float32 or float64.

  • y (Variable) – (Tensor, default Tensor<float>) Box list Y holds M boxes, each box is represented as [xmin, ymin, xmax, ymax], the shape of X is [N, 4]. [xmin, ymin] is the left top coordinate of the box if the input is image feature map, and [xmax, ymax] is the right bottom coordinate of the box.The data type is float32 or float64.

Returns

(LoDTensor, the lod is same as input X) The output of iou_similarity op, a tensor with shape [N, M] representing pairwise iou scores.The data type is same with x.

Return type

Variable

Examples

import numpy as np
import paddle.fluid as fluid

use_gpu = False
place = fluid.CUDAPlace(0) if use_gpu else fluid.CPUPlace()
exe = fluid.Executor(place)

x = fluid.data(name='x', shape=[None, 4], dtype='float32')
y = fluid.data(name='y', shape=[None, 4], dtype='float32')
iou = fluid.layers.iou_similarity(x=x, y=y)

exe.run(fluid.default_startup_program())
test_program = fluid.default_main_program().clone(for_test=True)

[out_iou] = exe.run(test_program,
        fetch_list=iou,
        feed={'x': np.array([[0.5, 0.5, 2.0, 2.0],
                             [0., 0., 1.0, 1.0]]).astype('float32'),
              'y': np.array([[1.0, 1.0, 2.5, 2.5]]).astype('float32')})
# out_iou is [[0.2857143],
#             [0.       ]] with shape: [2, 1]