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

update code

上级 f020f4b5
...@@ -23,14 +23,14 @@ class PriorBoxOp : public framework::OperatorWithKernel { ...@@ -23,14 +23,14 @@ class PriorBoxOp : public framework::OperatorWithKernel {
void InferShape(framework::InferShapeContext* ctx) const override { void InferShape(framework::InferShapeContext* ctx) const override {
PADDLE_ENFORCE(ctx->HasInput("Input"), 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"), 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 image_dims = ctx->GetInputDim("Image");
auto input_dims = ctx->GetInputDim("Input"); auto input_dims = ctx->GetInputDim("Input");
PADDLE_ENFORCE(image_dims.size() == 4, "The format of image is NCHW."); PADDLE_ENFORCE(image_dims.size() == 4, "The layout of image is NCHW.");
PADDLE_ENFORCE(input_dims.size() == 4, "The format of input is NCHW."); PADDLE_ENFORCE(input_dims.size() == 4, "The layout of input is NCHW.");
PADDLE_ENFORCE_LT(input_dims[2], image_dims[2], PADDLE_ENFORCE_LT(input_dims[2], image_dims[2],
"The height of input must smaller than image."); "The height of input must smaller than image.");
...@@ -45,7 +45,7 @@ class PriorBoxOp : public framework::OperatorWithKernel { ...@@ -45,7 +45,7 @@ class PriorBoxOp : public framework::OperatorWithKernel {
bool flip = ctx->Attrs().Get<bool>("flip"); bool flip = ctx->Attrs().Get<bool>("flip");
PADDLE_ENFORCE_GT(min_sizes.size(), 0, 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) { for (size_t i = 0; i < min_sizes.size(); ++i) {
PADDLE_ENFORCE_GT(min_sizes[i], 0, "min_sizes[%d] must be positive.", i); PADDLE_ENFORCE_GT(min_sizes[i], 0, "min_sizes[%d] must be positive.", i);
} }
...@@ -56,7 +56,7 @@ class PriorBoxOp : public framework::OperatorWithKernel { ...@@ -56,7 +56,7 @@ class PriorBoxOp : public framework::OperatorWithKernel {
int num_priors = aspect_ratios_vec.size() * min_sizes.size(); int num_priors = aspect_ratios_vec.size() * min_sizes.size();
if (max_sizes.size() > 0) { if (max_sizes.size() > 0) {
PADDLE_ENFORCE_EQ(max_sizes.size(), min_sizes.size(), 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) { for (size_t i = 0; i < min_sizes.size(); ++i) {
PADDLE_ENFORCE_GT(max_sizes[i], min_sizes[i], PADDLE_ENFORCE_GT(max_sizes[i], min_sizes[i],
"max_size[%d] must be greater than min_size[%d].", i, "max_size[%d] must be greater than min_size[%d].", i,
...@@ -65,13 +65,10 @@ class PriorBoxOp : public framework::OperatorWithKernel { ...@@ -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.");
PADDLE_ENFORCE_EQ(variances.size(), 4, for (size_t i = 0; i < variances.size(); ++i) {
"Must and only provide 4 variance."); PADDLE_ENFORCE_GT(variances[i], 0.0,
for (size_t i = 0; i < variances.size(); ++i) { "variance[%d] must be greater than 0.", 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"); const float step_h = ctx->Attrs().Get<float>("step_h");
...@@ -95,19 +92,19 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -95,19 +92,19 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
: OpProtoAndCheckerMaker(proto, op_checker) { : OpProtoAndCheckerMaker(proto, op_checker) {
AddInput("Input", AddInput("Input",
"(Tensor, default Tensor<float>), " "(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", AddInput("Image",
"(Tensor, default Tensor<float>), " "(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", AddOutput("Boxes",
"(Tensor, default Tensor<float>), the output prior boxes of " "(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, " "num_priors, 4]. layer_height is the height of input, "
"layer_width is the width of input, num_priors is the box " "layer_width is the width of input, num_priors is the box "
"count of each position."); "count of each position.");
AddOutput("Variances", AddOutput("Variances",
"(Tensor, default Tensor<float>), the expanded variances of " "(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, " "num_priors, 4]. layer_height is the height of input, "
"layer_width is the width of input, num_priors is the box " "layer_width is the width of input, num_priors is the box "
"count of each position."); "count of each position.");
...@@ -117,12 +114,10 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker { ...@@ -117,12 +114,10 @@ class PriorBoxOpMaker : public framework::OpProtoAndCheckerMaker {
"List of max sizes of generated prior boxes."); "List of max sizes of generated prior boxes.");
AddAttr<std::vector<float>>( AddAttr<std::vector<float>>(
"aspect_ratios", "(vector<float>) ", "aspect_ratios", "(vector<float>) ",
"List of aspect ratios of generated prior boxes.") "List of aspect ratios of generated prior boxes.");
.SetDefault({});
AddAttr<std::vector<float>>( AddAttr<std::vector<float>>(
"variances", "(vector<float>) ", "variances", "(vector<float>) ",
"List of variances to be encoded in prior boxes.") "List of variances to be encoded in prior boxes.");
.SetDefault({0.1});
AddAttr<bool>("flip", "(bool) ", "Whether to flip aspect ratios.") AddAttr<bool>("flip", "(bool) ", "Whether to flip aspect ratios.")
.SetDefault(true); .SetDefault(true);
AddAttr<bool>("clip", "(bool) ", "Whether to clip out-of-boundary boxes.") AddAttr<bool>("clip", "(bool) ", "Whether to clip out-of-boundary boxes.")
......
...@@ -70,9 +70,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> { ...@@ -70,9 +70,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
std::vector<float> aspect_ratios; std::vector<float> aspect_ratios;
ExpandAspectRatios(input_aspect_ratio, flip, aspect_ratios); ExpandAspectRatios(input_aspect_ratio, flip, aspect_ratios);
auto step_w = ctx.Attr<float>("step_w"); T step_w = static_cast<T>(ctx.Attr<float>("step_w"));
auto step_h = ctx.Attr<float>("step_h"); T step_h = static_cast<T>(ctx.Attr<float>("step_h"));
auto offset = ctx.Attr<float>("offset"); T offset = static_cast<T>(ctx.Attr<float>("offset"));
auto img_width = image->dims()[3]; auto img_width = image->dims()[3];
auto img_height = image->dims()[2]; auto img_height = image->dims()[2];
...@@ -80,10 +80,10 @@ class PriorBoxOpKernel : public framework::OpKernel<T> { ...@@ -80,10 +80,10 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
auto layer_width = input->dims()[3]; auto layer_width = input->dims()[3];
auto layer_height = input->dims()[2]; auto layer_height = input->dims()[2];
float step_width, step_height; T step_width, step_height;
if (step_w == 0 || step_h == 0) { if (step_w == 0 || step_h == 0) {
step_width = static_cast<float>(img_width) / layer_width; step_width = static_cast<T>(img_width) / layer_width;
step_height = static_cast<float>(img_height) / layer_height; step_height = static_cast<T>(img_height) / layer_height;
} else { } else {
step_width = step_w; step_width = step_w;
step_height = step_h; step_height = step_h;
...@@ -100,9 +100,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> { ...@@ -100,9 +100,9 @@ class PriorBoxOpKernel : public framework::OpKernel<T> {
auto e_boxes = framework::EigenTensor<T, 4>::From(*boxes); auto e_boxes = framework::EigenTensor<T, 4>::From(*boxes);
for (int h = 0; h < layer_height; ++h) { for (int h = 0; h < layer_height; ++h) {
for (int w = 0; w < layer_width; ++w) { for (int w = 0; w < layer_width; ++w) {
float center_x = (w + offset) * step_width; T center_x = (w + offset) * step_width;
float center_y = (h + offset) * step_height; T center_y = (h + offset) * step_height;
float box_width, box_height; T box_width, box_height;
int idx = 0; int idx = 0;
for (size_t s = 0; s < min_sizes.size(); ++s) { for (size_t s = 0; s < min_sizes.size(); ++s) {
int min_size = min_sizes[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 unittest
import numpy as np import numpy as np
import sys import sys
...@@ -86,44 +100,26 @@ class TestPriorBoxOp(OpTest): ...@@ -86,44 +100,26 @@ class TestPriorBoxOp(OpTest):
idx = 0 idx = 0
for h in range(self.layer_h): for h in range(self.layer_h):
for w in range(self.layer_w): for w in range(self.layer_w):
center_x = (w + self.offset) * self.step_w c_x = (w + self.offset) * self.step_w
center_y = (h + self.offset) * self.step_h c_y = (h + self.offset) * self.step_h
idx = 0 idx = 0
for s in range(len(self.min_sizes)): for s in range(len(self.min_sizes)):
min_size = self.min_sizes[s] min_size = self.min_sizes[s]
# first prior: aspect_ratio = 1, size = min_size c_w = c_h = min_size / 2.
box_width = box_height = min_size out_boxes[h, w, idx, :] = [
# xmin (c_x - c_w) / self.image_w, (c_y - c_h) / self.image_h,
out_boxes[h, w, idx, 0] = ( (c_x + c_w) / self.image_w, (c_y + c_h) / self.image_h
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
idx += 1 idx += 1
if len(self.max_sizes) > 0: if len(self.max_sizes) > 0:
max_size = self.max_sizes[s] max_size = self.max_sizes[s]
# second prior: aspect_ratio = 1, # second prior: aspect_ratio = 1,
# size = sqrt(min_size * max_size) c_w = c_h = math.sqrt(min_size * max_size) / 2
box_width = box_height = math.sqrt(min_size * max_size) out_boxes[h, w, idx, :] = [(c_x - c_w) / self.image_w,
# xmin (c_y - c_h) / self.image_h,
out_boxes[h, w, idx, 0] = ( (c_x + c_w) / self.image_w,
center_x - box_width / 2.) / self.image_w (c_y + c_h) / self.image_h]
# 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
idx += 1 idx += 1
# rest of priors # rest of priors
...@@ -131,20 +127,12 @@ class TestPriorBoxOp(OpTest): ...@@ -131,20 +127,12 @@ class TestPriorBoxOp(OpTest):
ar = self.real_aspect_ratios[r] ar = self.real_aspect_ratios[r]
if math.fabs(ar - 1.) < 1e-6: if math.fabs(ar - 1.) < 1e-6:
continue continue
box_width = min_size * math.sqrt(ar) c_w = min_size * math.sqrt(ar) / 2
box_height = min_size / math.sqrt(ar) c_h = (min_size / math.sqrt(ar)) / 2
# xmin out_boxes[h, w, idx, :] = [(c_x - c_w) / self.image_w,
out_boxes[h, w, idx, 0] = ( (c_y - c_h) / self.image_h,
center_x - box_width / 2.) / self.image_w (c_x + c_w) / self.image_w,
# ymin (c_y + c_h) / self.image_h]
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
idx += 1 idx += 1
# clip the prior's coordidate such that it is within[0, 1] # clip the prior's coordidate such that it is within[0, 1]
if self.clip: if self.clip:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册