diff --git a/src/framework/operator.cpp b/src/framework/operator.cpp index 7c66f932df3df9793f116c8e62fea704e346b146..dd865fb27d4345f16ddca8005463986787d681be 100644 --- a/src/framework/operator.cpp +++ b/src/framework/operator.cpp @@ -62,13 +62,24 @@ void OperatorBase::Run() const { DLOG << "-------------" << type_ << "----------------------------"; vector input_keys = GetInputKeys(); for (const auto key : input_keys) { - Tensor *input = GetVarValue(key, inputs_, *scope_); - if (input) DLOG << type_ << " input- " << key << "=" << *input; + auto var_vec_in = inputs_.at(key); + for (int i = 0; i < var_vec_in.size(); ++i) { + auto vari = scope_->FindVar(var_vec_in[i]); + if (vari->IsInitialized()) { + Tensor *tensor = vari->template GetMutable(); + if (tensor) DLOG << type_ << " input- " << key << "=" << *tensor; + } + } } - vector output_keys = GetOutKeys(); - for (const auto key : output_keys) { - Tensor *out_ = GetVarValue(key, outputs_, *scope_); - DLOG << type_ << " output- " << key << "=" << *out_; + for (const auto key : GetOutKeys()) { + auto var_vec_out = outputs_.at(key); + for (int i = 0; i < var_vec_out.size(); ++i) { + auto vari = scope_->FindVar(var_vec_out[i]); + if (vari->IsInitialized()) { + Tensor *tensor = vari->template GetMutable(); + if (tensor) DLOG << type_ << " output- " << key << "=" << *tensor; + } + } } #endif } diff --git a/src/operators/kernel/central-arm-func/prior_box_arm_func.h b/src/operators/kernel/central-arm-func/prior_box_arm_func.h index 7129996319aac7c71836d8706eb5c02300e576e6..e783c52f8184d6e09b04cd5c8210f5b89276541e 100644 --- a/src/operators/kernel/central-arm-func/prior_box_arm_func.h +++ b/src/operators/kernel/central-arm-func/prior_box_arm_func.h @@ -16,6 +16,7 @@ limitations under the License. */ #pragma once #include +#include #include namespace paddle_mobile { @@ -89,26 +90,8 @@ void PriorBoxCompute(const PriorBoxParam ¶m) { int idx = 0; for (size_t s = 0; s < min_sizes.size(); ++s) { auto min_size = min_sizes[s]; - // 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.; + if (param.MinMaxAspectRatiosOrder()) { + box_width = box_height = min_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] = @@ -118,6 +101,73 @@ void PriorBoxCompute(const PriorBoxParam ¶m) { output_boxes_dataptr[h * stride0 + w * stride1 + idx * stride2 + 3] = (center_y + box_height) / img_height; 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++; + } } } } diff --git a/src/operators/op_param.h b/src/operators/op_param.h index 1c5815c64236f1b67fb6ab7752d0c4caef7c2646..f11e6c37cd9d1aa3625240619e0e31ea6f4d5a0d 100644 --- a/src/operators/op_param.h +++ b/src/operators/op_param.h @@ -676,6 +676,11 @@ class PriorBoxParam : public OpParam { max_sizes_ = GetAttr>("max_sizes", attrs); aspect_ratios_ = GetAttr>("aspect_ratios", attrs); variances_ = GetAttr>("variances", attrs); + + if (HasAttr("min_max_aspect_ratios_order", attrs)) { + min_max_aspect_ratios_order_ = + GetAttr("min_max_aspect_ratios_order", attrs); + } flip_ = GetAttr("flip", attrs); clip_ = GetAttr("clip", attrs); step_w_ = GetAttr("step_w", attrs); @@ -708,6 +713,10 @@ class PriorBoxParam : public OpParam { const float &Offset() const { return offset_; } + const bool &MinMaxAspectRatiosOrder() const { + return min_max_aspect_ratios_order_; + } + private: RType *input_; RType *input_image_; @@ -722,6 +731,7 @@ class PriorBoxParam : public OpParam { float step_w_; float step_h_; float offset_; + bool min_max_aspect_ratios_order_; }; #endif diff --git a/test/net/test_mobilenet_025_fssd.cpp b/test/net/test_mobilenet_025_fssd.cpp index ed27435a51a4aefee627149eff802121045d7c8c..c0d037ceb05f57361f1385cb9959beed66186e4f 100644 --- a/test/net/test_mobilenet_025_fssd.cpp +++ b/test/net/test_mobilenet_025_fssd.cpp @@ -16,35 +16,46 @@ limitations under the License. */ #include "../test_helper.h" #include "../test_include.h" -int main() { - paddle_mobile::PaddleMobile paddle_mobile; - paddle_mobile.SetThreadNum(4); - // ../../../test/models/googlenet - // ../../../test/models/mobilenet - auto time1 = time(); - if (paddle_mobile.Load(std::string(g_fluid_fssd_new) + "/model", - std::string(g_fluid_fssd_new) + "/params", true)) { - auto time2 = time(); - std::cout << "load cost :" << time_diff(time1, time1) << "ms" << std::endl; +int main(int argc, char **argv) { + int times = 10; + if (argc <= 1) { + times = 10; + std::cout << "没有输入 , 使用默认10次 " << times << std::endl; + } else { + std::string arstr = argv[1]; + times = std::stoi(arstr); + std::cout << "input times: " << times << std::endl; + } + paddle_mobile::PaddleMobile 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 input; std::vector dims{1, 3, 160, 160}; - Tensor input_tensor; - SetupTensor(&input_tensor, {1, 3, 160, 160}, static_cast(0), - static_cast(1)); + GetInput(g_imgfssd_ar1, &input, dims); + std::cout << "预热10次....." << std::endl; - std::vector input(input_tensor.data(), - input_tensor.data() + input_tensor.numel()); // 预热十次 for (int i = 0; i < 10; ++i) { - paddle_mobile.Predict(input, dims); + auto output = paddle_mobile.Predict(input, dims); } - auto time3 = time(); - for (int i = 0; i < 10; ++i) { - paddle_mobile.Predict(input, dims); + std::cout << "开始....." << std::endl; + + 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 << "predict cost :" << time_diff(time3, time4) / 10 << "ms" - << std::endl; + std::cout << "平均时间:" << time_sum / times << "ms" << std::endl; } return 0; } diff --git a/test/test_helper.h b/test/test_helper.h index 7581405c3d9f14e7e997e73be91cb624ad6d9798..a6898b2cc8d55c79e5bd82d661bbe07533e2ec4f 100644 --- a/test/test_helper.h +++ b/test/test_helper.h @@ -48,6 +48,8 @@ static const char *g_test_image_1x3x224x224 = static const char *g_test_image_1x3x224x224_banana = "../images/input_3x224x224_banana"; static const char *g_hand = "../images/hand_image"; +static const char *g_imgfssd_ar = "../images/test_image_ssd_ar"; +static const char *g_imgfssd_ar1 = "../images/003_0001.txt"; static const char *g_img = "../images/img.bin"; using paddle_mobile::framework::DDim;