From a05d25cf41fd080a41067b80395baf49526772da Mon Sep 17 00:00:00 2001 From: wanghaox Date: Tue, 23 Jan 2018 14:02:23 +0800 Subject: [PATCH] update code and doc, change input x to LoDTensor --- paddle/operators/iou_similarity_op.cc | 30 +++++++++++++------ paddle/operators/iou_similarity_op.cu | 1 - paddle/operators/iou_similarity_op.h | 6 ++-- .../v2/fluid/tests/test_iou_similarity_op.py | 13 ++++++++ 4 files changed, 38 insertions(+), 12 deletions(-) diff --git a/paddle/operators/iou_similarity_op.cc b/paddle/operators/iou_similarity_op.cc index 49e54a1a53c..6c1b99b46f2 100755 --- a/paddle/operators/iou_similarity_op.cc +++ b/paddle/operators/iou_similarity_op.cc @@ -44,11 +44,14 @@ class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker { IOUSimilarityOpMaker(OpProto *proto, OpAttrChecker *op_checker) : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("X", - "(Tensor, default Tensor) " - "Box list X holds N boxes, each box is " - "represented as [xmin, ymin, xmax, ymax], the shape of X is [N, " - "4]. [xmin, ymin] is the lower left coordinate of the box, and " - "[xmax, ymax] is the right upper coordinate of the box."); + "(LoDTensor, default LoDTensor) " + "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 lower left " + "coordinate of the box, and [xmax, ymax] is the right upper " + "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."); AddInput("Y", "(Tensor, default Tensor) " "Box list Y holds M boxes, each box is " @@ -56,14 +59,23 @@ class IOUSimilarityOpMaker : public framework::OpProtoAndCheckerMaker { "4]. [xmin, ymin] is the lower left coordinate of the box, and " "[xmax, ymax] is the right upper coordinate of the box."); - AddOutput( - "Out", - "(Tensor) The output of iou_similarity op, a tensor with shape [N, M] " - "representing pairwise iou scores."); + AddOutput("Out", + "(LoDTensor or Tensor, the lod is same as input X) The output of " + "iou_similarity op, a tensor with shape [N, M] " + "representing pairwise iou scores."); AddComment(R"DOC( 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 input images. + Given two box 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)} +$$ + )DOC"); } }; diff --git a/paddle/operators/iou_similarity_op.cu b/paddle/operators/iou_similarity_op.cu index 526edaefeea..fa505262461 100755 --- a/paddle/operators/iou_similarity_op.cu +++ b/paddle/operators/iou_similarity_op.cu @@ -12,7 +12,6 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ -#define EIGEN_USE_GPU #include "paddle/operators/iou_similarity_op.h" namespace ops = paddle::operators; diff --git a/paddle/operators/iou_similarity_op.h b/paddle/operators/iou_similarity_op.h index fa1b60ae34e..f42e05b078b 100644 --- a/paddle/operators/iou_similarity_op.h +++ b/paddle/operators/iou_similarity_op.h @@ -71,9 +71,9 @@ template class IOUSimilarityKernel : public framework::OpKernel { public: void Compute(const framework::ExecutionContext& ctx) const override { - const framework::Tensor* in_x = ctx.Input("X"); + const framework::LoDTensor* in_x = ctx.Input("X"); const framework::Tensor* in_y = ctx.Input("Y"); - framework::Tensor* out = ctx.Output("Out"); + framework::LoDTensor* out = ctx.Output("Out"); int x_n = in_x->dims()[0]; int y_n = in_y->dims()[0]; @@ -83,6 +83,8 @@ class IOUSimilarityKernel : public framework::OpKernel { platform::ForRange for_range( static_cast(ctx.device_context()), x_n); for_range(functor); + + out->set_lod(in_x->lod()); } }; // namespace operators diff --git a/python/paddle/v2/fluid/tests/test_iou_similarity_op.py b/python/paddle/v2/fluid/tests/test_iou_similarity_op.py index 694da618628..128f2e49771 100755 --- a/python/paddle/v2/fluid/tests/test_iou_similarity_op.py +++ b/python/paddle/v2/fluid/tests/test_iou_similarity_op.py @@ -38,5 +38,18 @@ class TestIOUSimilarityOp(OpTest): self.outputs = {'Out': self.output} +class TestIOUSimilarityOpWithLoD(TestIOUSimilarityOp): + def test_check_output(self): + self.check_output() + + def setUp(self): + super(TestIOUSimilarityOpWithLoD, self).setUp() + self.boxes1_lod = [[0, 1, 2]] + self.output_lod = [[0, 1, 2]] + + self.inputs = {'X': (self.boxes1, self.boxes1_lod), 'Y': self.boxes2} + self.outputs = {'Out': (self.output, self.output_lod)} + + if __name__ == '__main__': unittest.main() -- GitLab