提交 9c1ddcf6 编写于 作者: xiebaiyuan's avatar xiebaiyuan

add new attr in pribox to suit fssd change fssd unit test

上级 de056fe0
...@@ -62,13 +62,20 @@ void OperatorBase<Dtype>::Run() const { ...@@ -62,13 +62,20 @@ void OperatorBase<Dtype>::Run() const {
DLOG << "-------------" << type_ << "----------------------------"; DLOG << "-------------" << type_ << "----------------------------";
vector<string> input_keys = GetInputKeys(); vector<string> input_keys = GetInputKeys();
for (const auto key : input_keys) { for (const auto key : input_keys) {
Tensor *input = GetVarValue<framework::LoDTensor>(key, inputs_, *scope_); auto var_vec_in = inputs_.at(key);
if (input) DLOG << type_ << " input- " << key << "=" << *input; for (int i = 0; i < var_vec_in.size(); ++i) {
auto vari = scope_->FindVar(var_vec_in[i]);
Tensor *tensor = vari->template GetMutable<framework::LoDTensor>();
if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor;
}
} }
vector<string> output_keys = GetOutKeys(); for (const auto key : GetOutKeys()) {
for (const auto key : output_keys) { auto var_vec_out = outputs_.at(key);
Tensor *out_ = GetVarValue<framework::LoDTensor>(key, outputs_, *scope_); for (int i = 0; i < var_vec_out.size(); ++i) {
DLOG << type_ << " output- " << key << "=" << *out_; auto vari = scope_->FindVar(var_vec_out[i]);
Tensor *tensor = vari->template GetMutable<framework::LoDTensor>();
if (tensor) DLOG << type_ << " output- " << key << "=" << *tensor;
}
} }
#endif #endif
} }
......
...@@ -16,6 +16,7 @@ limitations under the License. */ ...@@ -16,6 +16,7 @@ limitations under the License. */
#pragma once #pragma once
#include <algorithm> #include <algorithm>
#include <cmath>
#include <vector> #include <vector>
namespace paddle_mobile { namespace paddle_mobile {
...@@ -89,26 +90,8 @@ void PriorBoxCompute(const PriorBoxParam<CPU> &param) { ...@@ -89,26 +90,8 @@ void PriorBoxCompute(const PriorBoxParam<CPU> &param) {
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) {
auto min_size = min_sizes[s]; auto min_size = min_sizes[s];
// priors with different aspect ratios if (param.MinMaxAspectRatiosOrder()) {
for (float ar : aspect_ratios) { box_width = box_height = min_size / 2.;
box_width = min_size * sqrt(ar) / 2.;
box_height = min_size / sqrt(ar) / 2.;
/// box_width/2 , / img_width 为了得到feature map 相对于
/// 原图的归一化位置的比例。
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 0] =
(center_x - box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 1] =
(center_y - box_height) / img_height;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 2] =
(center_x + box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 3] =
(center_y + box_height) / img_height;
idx++;
}
if (!max_sizes.empty()) {
auto max_size = max_sizes[s];
// square prior with size sqrt(minSize * maxSize)
box_width = box_height = sqrt(min_size * max_size) / 2.;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 0] = output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 0] =
(center_x - box_width) / img_width; (center_x - box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 1] = output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 1] =
...@@ -118,6 +101,73 @@ void PriorBoxCompute(const PriorBoxParam<CPU> &param) { ...@@ -118,6 +101,73 @@ void PriorBoxCompute(const PriorBoxParam<CPU> &param) {
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 3] = output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 3] =
(center_y + box_height) / img_height; (center_y + box_height) / img_height;
idx++; idx++;
if (max_sizes.size() > 0) {
auto max_size = max_sizes[s];
// square prior with size sqrt(minSize * maxSize)
box_width = box_height = sqrt(min_size * max_size) / 2.;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
0] = (center_x - box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
1] = (center_y - box_height) / img_height;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
2] = (center_x + box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
3] = (center_y + box_height) / img_height;
idx++;
}
// priors with different aspect ratios
for (float ar : aspect_ratios) {
if (fabs(ar - 1.) < 1e-6) {
continue;
}
box_width = min_size * sqrt(ar) / 2.;
box_height = min_size / sqrt(ar) / 2.;
/// box_width/2 , / img_width 为了得到feature map 相对于
/// 原图的归一化位置的比例。
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
0] = (center_x - box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
1] = (center_y - box_height) / img_height;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
2] = (center_x + box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
3] = (center_y + box_height) / img_height;
idx++;
}
} else {
// priors with different aspect ratios
for (float ar : aspect_ratios) {
box_width = min_size * sqrt(ar) / 2.;
box_height = min_size / sqrt(ar) / 2.;
/// box_width/2 , / img_width 为了得到feature map 相对于
/// 原图的归一化位置的比例。
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
0] = (center_x - box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
1] = (center_y - box_height) / img_height;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
2] = (center_x + box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
3] = (center_y + box_height) / img_height;
idx++;
}
if (!max_sizes.empty()) {
auto max_size = max_sizes[s];
// square prior with size sqrt(minSize * maxSize)
box_width = box_height = sqrt(min_size * max_size) / 2.;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
0] = (center_x - box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
1] = (center_y - box_height) / img_height;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
2] = (center_x + box_width) / img_width;
output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 +
3] = (center_y + box_height) / img_height;
idx++;
}
} }
} }
} }
......
...@@ -676,6 +676,11 @@ class PriorBoxParam : public OpParam { ...@@ -676,6 +676,11 @@ class PriorBoxParam : public OpParam {
max_sizes_ = GetAttr<vector<float>>("max_sizes", attrs); max_sizes_ = GetAttr<vector<float>>("max_sizes", attrs);
aspect_ratios_ = GetAttr<vector<float>>("aspect_ratios", attrs); aspect_ratios_ = GetAttr<vector<float>>("aspect_ratios", attrs);
variances_ = GetAttr<vector<float>>("variances", attrs); variances_ = GetAttr<vector<float>>("variances", attrs);
if (HasAttr("min_max_aspect_ratios_order", attrs)){
min_max_aspect_ratios_order_ =
GetAttr<bool>("min_max_aspect_ratios_order", attrs);
}
flip_ = GetAttr<bool>("flip", attrs); flip_ = GetAttr<bool>("flip", attrs);
clip_ = GetAttr<bool>("clip", attrs); clip_ = GetAttr<bool>("clip", attrs);
step_w_ = GetAttr<float>("step_w", attrs); step_w_ = GetAttr<float>("step_w", attrs);
...@@ -708,6 +713,10 @@ class PriorBoxParam : public OpParam { ...@@ -708,6 +713,10 @@ class PriorBoxParam : public OpParam {
const float &Offset() const { return offset_; } const float &Offset() const { return offset_; }
const bool &MinMaxAspectRatiosOrder() const {
return min_max_aspect_ratios_order_;
}
private: private:
RType *input_; RType *input_;
RType *input_image_; RType *input_image_;
...@@ -722,6 +731,7 @@ class PriorBoxParam : public OpParam { ...@@ -722,6 +731,7 @@ class PriorBoxParam : public OpParam {
float step_w_; float step_w_;
float step_h_; float step_h_;
float offset_; float offset_;
bool min_max_aspect_ratios_order_;
}; };
#endif #endif
......
...@@ -16,35 +16,46 @@ limitations under the License. */ ...@@ -16,35 +16,46 @@ limitations under the License. */
#include "../test_helper.h" #include "../test_helper.h"
#include "../test_include.h" #include "../test_include.h"
int main() { int main(int argc, char **argv) {
paddle_mobile::PaddleMobile<paddle_mobile::CPU> paddle_mobile; int times = 10;
paddle_mobile.SetThreadNum(4); if (argc <= 1) {
// ../../../test/models/googlenet times = 10;
// ../../../test/models/mobilenet std::cout << "没有输入 , 使用默认10次 " << times << std::endl;
auto time1 = time(); } else {
if (paddle_mobile.Load(std::string(g_fluid_fssd_new) + "/model", std::string arstr = argv[1];
std::string(g_fluid_fssd_new) + "/params", true)) { times = std::stoi(arstr);
auto time2 = time(); std::cout << "input times: " << times << std::endl;
std::cout << "load cost :" << time_diff(time1, time1) << "ms" << std::endl; }
paddle_mobile::PaddleMobile<paddle_mobile::CPU> paddle_mobile;
paddle_mobile.SetThreadNum(1);
auto isok =
paddle_mobile.Load(std::string(g_fluid_fssd_new) + "/model",
std::string(g_fluid_fssd_new) + "/params", true);
if (isok) {
std::vector<float> input;
std::vector<int64_t> dims{1, 3, 160, 160}; std::vector<int64_t> dims{1, 3, 160, 160};
Tensor input_tensor; GetInput<float>(g_imgfssd_ar1, &input, dims);
SetupTensor<float>(&input_tensor, {1, 3, 160, 160}, static_cast<float>(0), std::cout << "预热10次....." << std::endl;
static_cast<float>(1));
std::vector<float> input(input_tensor.data<float>(),
input_tensor.data<float>() + input_tensor.numel());
// 预热十次 // 预热十次
for (int i = 0; i < 10; ++i) { for (int i = 0; i < 10; ++i) {
paddle_mobile.Predict(input, dims); auto output = paddle_mobile.Predict(input, dims);
} }
auto time3 = time(); std::cout << "开始....." << std::endl;
for (int i = 0; i < 10; ++i) {
paddle_mobile.Predict(input, dims); double time_sum = 0;
for (int i = 0; i < times; ++i) {
auto time3 = time();
auto output = paddle_mobile.Predict(input, dims);
auto time4 = time();
double timeDiff = time_diff(time3, time4);
time_sum += timeDiff;
std::cout << "第" << i << "次"
<< "predict cost :" << timeDiff << "ms" << std::endl;
} }
auto time4 = time(); std::cout << "平均时间:" << time_sum / times << "ms" << std::endl;
std::cout << "predict cost :" << time_diff(time3, time4) / 10 << "ms"
<< std::endl;
} }
return 0; return 0;
} }
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册