提交 142f6328 编写于 作者: W wanghaox

update code

上级 f020f4b5
......@@ -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<bool>("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,14 +65,11 @@ class PriorBoxOp : public framework::OperatorWithKernel {
}
}
if (variances.size() > 1) {
PADDLE_ENFORCE_EQ(variances.size(), 4,
"Must and only provide 4 variance.");
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<float>("step_h");
PADDLE_ENFORCE_GT(step_h, 0.0, "step_h should be larger than 0.");
......@@ -95,19 +92,19 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
: OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("Input",
"(Tensor, default Tensor<float>), "
"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<float>), "
"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<float>), 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<float>), 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<std::vector<float>>(
"aspect_ratios", "(vector<float>) ",
"List of aspect ratios of generated prior boxes.")
.SetDefault({});
"List of aspect ratios of generated prior boxes.");
AddAttr<std::vector<float>>(
"variances", "(vector<float>) ",
"List of variances to be encoded in prior boxes.")
.SetDefault({0.1});
"List of variances to be encoded in prior boxes.");
AddAttr<bool>("flip", "(bool) ", "Whether to flip aspect ratios.")
.SetDefault(true);
AddAttr<bool>("clip", "(bool) ", "Whether to clip out-of-boundary boxes.")
......
......@@ -70,9 +70,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
std::vector<float> aspect_ratios;
ExpandAspectRatios(input_aspect_ratio, flip, aspect_ratios);
auto step_w = ctx.Attr<float>("step_w");
auto step_h = ctx.Attr<float>("step_h");
auto offset = ctx.Attr<float>("offset");
T step_w = static_cast<T>(ctx.Attr<float>("step_w"));
T step_h = static_cast<T>(ctx.Attr<float>("step_h"));
T offset = static_cast<T>(ctx.Attr<float>("offset"));
auto img_width = image->dims()[3];
auto img_height = image->dims()[2];
......@@ -80,10 +80,10 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
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<float>(img_width) / layer_width;
step_height = static_cast<float>(img_height) / layer_height;
step_width = static_cast<T>(img_width) / layer_width;
step_height = static_cast<T>(img_height) / layer_height;
} else {
step_width = step_w;
step_height = step_h;
......@@ -100,9 +100,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
auto e_boxes = framework::EigenTensor<T, 4>::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];
......
# 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:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册