diff --git a/paddle/operators/prior_box_op.cc b/paddle/operators/prior_box_op.cc index b492c05082bf19cd829bb8bbd76d4369d2bc5d19..286afa8b4f2e9e5766ea20f6d5508c2210c32f19 100644 --- a/paddle/operators/prior_box_op.cc +++ b/paddle/operators/prior_box_op.cc @@ -23,14 +23,14 @@ class PriorBoxOp : public framework::OperatorWithKernel { void InferShape(framework::InferShapeContext* ctx) const override { PADDLE_ENFORCE(ctx->HasInput("Input"), - "Input(X) of PriorBoxOp should not be null."); + "Input(Input) of PriorBoxOp should not be null."); PADDLE_ENFORCE(ctx->HasInput("Image"), - "Input(Offset) of PriorBoxOp should not be null."); + "Input(Image) of PriorBoxOp should not be null."); auto image_dims = ctx->GetInputDim("Image"); auto input_dims = ctx->GetInputDim("Input"); - PADDLE_ENFORCE(image_dims.size() == 4, "The format of image is NCHW."); - PADDLE_ENFORCE(input_dims.size() == 4, "The format of input is NCHW."); + PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW."); + PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW."); PADDLE_ENFORCE_LT(input_dims[2], image_dims[2], "The height of input must smaller than image."); @@ -45,7 +45,7 @@ class PriorBoxOp : public framework::OperatorWithKernel { bool flip = ctx->Attrs().Get("flip"); PADDLE_ENFORCE_GT(min_sizes.size(), 0, - "Size of min_size must be at least 1."); + "Size of min_sizes must be at least 1."); for (size_t i = 0; i < min_sizes.size(); ++i) { PADDLE_ENFORCE_GT(min_sizes[i], 0, "min_sizes[%d] must be positive.", i); } @@ -56,7 +56,7 @@ class PriorBoxOp : public framework::OperatorWithKernel { int num_priors = aspect_ratios_vec.size() * min_sizes.size(); if (max_sizes.size() > 0) { PADDLE_ENFORCE_EQ(max_sizes.size(), min_sizes.size(), - "The length of min_size and max_size must be equal."); + "The number of min_size and max_size must be equal."); for (size_t i = 0; i < min_sizes.size(); ++i) { PADDLE_ENFORCE_GT(max_sizes[i], min_sizes[i], "max_size[%d] must be greater than min_size[%d].", i, @@ -65,13 +65,10 @@ class PriorBoxOp : public framework::OperatorWithKernel { } } - if (variances.size() > 1) { - PADDLE_ENFORCE_EQ(variances.size(), 4, - "Must and only provide 4 variance."); - for (size_t i = 0; i < variances.size(); ++i) { - PADDLE_ENFORCE_GT(variances[i], 0.0, - "variance[%d] must be greater than 0.", i); - } + PADDLE_ENFORCE_EQ(variances.size(), 4, "Must and only provide 4 variance."); + for (size_t i = 0; i < variances.size(); ++i) { + PADDLE_ENFORCE_GT(variances[i], 0.0, + "variance[%d] must be greater than 0.", i); } const float step_h = ctx->Attrs().Get("step_h"); @@ -95,19 +92,19 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker { : OpProtoAndCheckerMaker(proto, op_checker) { AddInput("Input", "(Tensor, default Tensor), " - "the input feature data of PriorBoxOp, The format is NCHW."); + "the input feature data of PriorBoxOp, The layout is NCHW."); AddInput("Image", "(Tensor, default Tensor), " - "the input image data of PriorBoxOp, The format is NCHW."); + "the input image data of PriorBoxOp, The layout is NCHW."); AddOutput("Boxes", "(Tensor, default Tensor), the output prior boxes of " - "PriorBoxOp. The format is [layer_height, layer_width, " + "PriorBoxOp. The layout is [layer_height, layer_width, " "num_priors, 4]. layer_height is the height of input, " "layer_width is the width of input, num_priors is the box " "count of each position."); AddOutput("Variances", "(Tensor, default Tensor), the expanded variances of " - "PriorBoxOp. The format is [layer_height, layer_width, " + "PriorBoxOp. The layout is [layer_height, layer_width, " "num_priors, 4]. layer_height is the height of input, " "layer_width is the width of input, num_priors is the box " "count of each position."); @@ -117,12 +114,10 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker { "List of max sizes of generated prior boxes."); AddAttr>( "aspect_ratios", "(vector) ", - "List of aspect ratios of generated prior boxes.") - .SetDefault({}); + "List of aspect ratios of generated prior boxes."); AddAttr>( "variances", "(vector) ", - "List of variances to be encoded in prior boxes.") - .SetDefault({0.1}); + "List of variances to be encoded in prior boxes."); AddAttr("flip", "(bool) ", "Whether to flip aspect ratios.") .SetDefault(true); AddAttr("clip", "(bool) ", "Whether to clip out-of-boundary boxes.") diff --git a/paddle/operators/prior_box_op.h b/paddle/operators/prior_box_op.h index 9dcd4d8a2fe03513a88063de7c01e6f557f21a67..5483dd7bbe9a4aad91aca86442eab4e35a28b0d5 100644 --- a/paddle/operators/prior_box_op.h +++ b/paddle/operators/prior_box_op.h @@ -70,9 +70,9 @@ class PriorBoxOpKernel : public framework::OpKernel { std::vector aspect_ratios; ExpandAspectRatios(input_aspect_ratio, flip, aspect_ratios); - auto step_w = ctx.Attr("step_w"); - auto step_h = ctx.Attr("step_h"); - auto offset = ctx.Attr("offset"); + T step_w = static_cast(ctx.Attr("step_w")); + T step_h = static_cast(ctx.Attr("step_h")); + T offset = static_cast(ctx.Attr("offset")); auto img_width = image->dims()[3]; auto img_height = image->dims()[2]; @@ -80,10 +80,10 @@ class PriorBoxOpKernel : public framework::OpKernel { auto layer_width = input->dims()[3]; auto layer_height = input->dims()[2]; - float step_width, step_height; + T step_width, step_height; if (step_w == 0 || step_h == 0) { - step_width = static_cast(img_width) / layer_width; - step_height = static_cast(img_height) / layer_height; + step_width = static_cast(img_width) / layer_width; + step_height = static_cast(img_height) / layer_height; } else { step_width = step_w; step_height = step_h; @@ -100,9 +100,9 @@ class PriorBoxOpKernel : public framework::OpKernel { auto e_boxes = framework::EigenTensor::From(*boxes); for (int h = 0; h < layer_height; ++h) { for (int w = 0; w < layer_width; ++w) { - float center_x = (w + offset) * step_width; - float center_y = (h + offset) * step_height; - float box_width, box_height; + T center_x = (w + offset) * step_width; + T center_y = (h + offset) * step_height; + T box_width, box_height; int idx = 0; for (size_t s = 0; s < min_sizes.size(); ++s) { int min_size = min_sizes[s]; diff --git a/python/paddle/v2/fluid/tests/test_prior_box_op.py b/python/paddle/v2/fluid/tests/test_prior_box_op.py index 86e4ab76b5a54f500e264cd850661a45eaefce17..ca8d2bca74ce2d4be8160c8851e393489691ae56 100644 --- a/python/paddle/v2/fluid/tests/test_prior_box_op.py +++ b/python/paddle/v2/fluid/tests/test_prior_box_op.py @@ -1,3 +1,17 @@ +# Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserve. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# 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. + import unittest import numpy as np import sys @@ -86,44 +100,26 @@ class TestPriorBoxOp(OpTest): idx = 0 for h in range(self.layer_h): for w in range(self.layer_w): - center_x = (w + self.offset) * self.step_w - center_y = (h + self.offset) * self.step_h + c_x = (w + self.offset) * self.step_w + c_y = (h + self.offset) * self.step_h idx = 0 for s in range(len(self.min_sizes)): min_size = self.min_sizes[s] - # first prior: aspect_ratio = 1, size = min_size - box_width = box_height = min_size - # xmin - out_boxes[h, w, idx, 0] = ( - center_x - box_width / 2.) / self.image_w - # ymin - out_boxes[h, w, idx, 1] = ( - center_y - box_height / 2.) / self.image_h - # xmax - out_boxes[h, w, idx, 2] = ( - center_x + box_width / 2.) / self.image_w - # ymax - out_boxes[h, w, idx, 3] = ( - center_y + box_height / 2.) / self.image_h + c_w = c_h = min_size / 2. + out_boxes[h, w, idx, :] = [ + (c_x - c_w) / self.image_w, (c_y - c_h) / self.image_h, + (c_x + c_w) / self.image_w, (c_y + c_h) / self.image_h + ] idx += 1 if len(self.max_sizes) > 0: max_size = self.max_sizes[s] # second prior: aspect_ratio = 1, - # size = sqrt(min_size * max_size) - box_width = box_height = math.sqrt(min_size * max_size) - # xmin - out_boxes[h, w, idx, 0] = ( - center_x - box_width / 2.) / self.image_w - # ymin - out_boxes[h, w, idx, 1] = ( - center_y - box_height / 2.) / self.image_h - # xmax - out_boxes[h, w, idx, 2] = ( - center_x + box_width / 2.) / self.image_w - # ymax - out_boxes[h, w, idx, 3] = ( - center_y + box_height / 2.) / self.image_h + c_w = c_h = math.sqrt(min_size * max_size) / 2 + out_boxes[h, w, idx, :] = [(c_x - c_w) / self.image_w, + (c_y - c_h) / self.image_h, + (c_x + c_w) / self.image_w, + (c_y + c_h) / self.image_h] idx += 1 # rest of priors @@ -131,20 +127,12 @@ class TestPriorBoxOp(OpTest): ar = self.real_aspect_ratios[r] if math.fabs(ar - 1.) < 1e-6: continue - box_width = min_size * math.sqrt(ar) - box_height = min_size / math.sqrt(ar) - # xmin - out_boxes[h, w, idx, 0] = ( - center_x - box_width / 2.) / self.image_w - # ymin - out_boxes[h, w, idx, 1] = ( - center_y - box_height / 2.) / self.image_h - # xmax - out_boxes[h, w, idx, 2] = ( - center_x + box_width / 2.) / self.image_w - # ymax - out_boxes[h, w, idx, 3] = ( - center_y + box_height / 2.) / self.image_h + c_w = min_size * math.sqrt(ar) / 2 + c_h = (min_size / math.sqrt(ar)) / 2 + out_boxes[h, w, idx, :] = [(c_x - c_w) / self.image_w, + (c_y - c_h) / self.image_h, + (c_x + c_w) / self.image_w, + (c_y + c_h) / self.image_h] idx += 1 # clip the prior's coordidate such that it is within[0, 1] if self.clip: