diff --git a/src/framework/executor.cpp b/src/framework/executor.cpp index 0716ee7f2e5d6c6bc7065c4ce8087ce95db4573b..e4ffdaf05d5dad129138b2a7745619c86e8ca805 100644 --- a/src/framework/executor.cpp +++ b/src/framework/executor.cpp @@ -520,11 +520,12 @@ void Executor::FeedData(const Tensor &t) { template void Executor::FeedData(const std::vector &v) { auto input_size = v.size(); - auto vars = program_.scope->VarContain("feed"); + int index = 0; + auto vars = program_.scope->VarContain("feed", &index); PADDLE_MOBILE_ENFORCE(input_size == vars.size(), "input data number not correct"); for (int i = 0; i < input_size; i++) { - auto var = vars[i]; + auto var = program_.scope->Var("feed", i + index); auto feed_tensor = var->template GetMutable(); feed_tensor->external_data = v[i]; } @@ -533,11 +534,12 @@ void Executor::FeedData(const std::vector &v) { template void Executor::FeedTensorData(const vector &v) { auto input_size = v.size(); - auto vars = program_.scope->VarContain("feed"); + int index = 0; + auto vars = program_.scope->VarContain("feed", &index); PADDLE_MOBILE_ENFORCE(input_size == vars.size(), "input data number not correct"); for (int i = 0; i < input_size; i++) { - auto var = vars[i]; + auto var = program_.scope->Var("feed", i + index); auto feed_tensor = var->template GetMutable(); feed_tensor->ShareDataWith(v[i]); } @@ -547,12 +549,13 @@ template void Executor::GetResults(std::vector *v) { auto output_size = v->size(); PADDLE_MOBILE_ENFORCE(output_size > 0, "Empty output"); - auto vars = program_.scope->VarContain("fetch"); + int index = 0; + auto vars = program_.scope->VarContain("fetch", &index); PADDLE_MOBILE_ENFORCE(output_size == vars.size(), "output data number not correct"); for (int i = 0; i < output_size; i++) { - auto var = vars[i]; + auto var = program_.scope->Var("fetch", i + index); auto fetch_tensor = var->template GetMutable(); (*v)[i] = fetch_tensor->template data(); } @@ -561,11 +564,11 @@ void Executor::GetResults(std::vector *v) { template void Executor::GetTensorResults( std::vector *v) { - auto vars = program_.scope->VarContain("fetch"); + int index = 0; + auto vars = program_.scope->VarContain("fetch", &index); auto output_size = vars.size(); - for (int i = 0; i < output_size; i++) { - auto var = vars[i]; + auto var = program_.scope->Var("fetch", i + index); auto fetch_tensor = var->template GetMutable(); v->push_back(fetch_tensor); } diff --git a/src/framework/scope.cpp b/src/framework/scope.cpp index db263081446f9804e5352588063a23f72a8bf163..026b1e6b72dcca081554152ea356204ba311b359 100644 --- a/src/framework/scope.cpp +++ b/src/framework/scope.cpp @@ -116,18 +116,26 @@ Variable *Scope::Var(const std::string &name, const int id) { return Var(name + std::to_string(id)); } -std::vector Scope::VarContain(const std::string substring) { +std::vector Scope::VarContain(const std::string substring, + int *min) { std::vector v; + + int temp = 9999; + auto len0 = substring.length(); for (auto pair : vars_) { if (pair.first.find(substring) == 0) { v.push_back(pair.second); + auto len1 = pair.first.length(); + int index = std::stoi(pair.first.substr(len0, len1)); + if (index < temp) { + temp = index; + } } } + *min = temp; return v; } -void Scope::InsertVar(const std::string str, Variable *var) {} - void Scope::print_vars() { DLOG << "====================start to print variables================="; for (auto pair : vars_) { diff --git a/src/framework/scope.h b/src/framework/scope.h index c52917499639c9ed03c8807c726cdf1dcdaece9e..4193db30e4bb487a323a188a95e4e8bf156549d9 100644 --- a/src/framework/scope.h +++ b/src/framework/scope.h @@ -77,8 +77,7 @@ class Scope { #ifdef PADDLE_MOBILE_FPGA Variable *Var(const std::string &name, const int id); - std::vector VarContain(const std::string substring); - void InsertVar(const std::string str, Variable *var); + std::vector VarContain(const std::string substring, int *min); void print_vars(); #endif diff --git a/src/io/api_paddle_mobile.cc b/src/io/api_paddle_mobile.cc index 638a369b4ef7153772dbe5ca1770722c5212edaa..1f4769b282385207a5b53d6d678364393d7da6cc 100644 --- a/src/io/api_paddle_mobile.cc +++ b/src/io/api_paddle_mobile.cc @@ -172,18 +172,6 @@ void PaddleMobilePredictor::GetPaddleTensor(const std::string &name, ConvertTensors(*t, output); } -template -void PaddleMobilePredictor::FeedData( - const std::vector &inputs) { - paddle_mobile_->FeedData(inputs); -} - -template -void PaddleMobilePredictor::GetResults( - std::vector *outputs) { - paddle_mobile_->GetResults(outputs); -} - template void PaddleMobilePredictor::Predict_From_To(int start, int end) { paddle_mobile_->Predict_From_To(start, end); diff --git a/src/io/api_paddle_mobile.h b/src/io/api_paddle_mobile.h index 15cb4a6d0bd82997f24ff5bbf0d15921c376b3f9..38af541a9262ea1f4c9ea0f8e4229316c54a4a18 100644 --- a/src/io/api_paddle_mobile.h +++ b/src/io/api_paddle_mobile.h @@ -33,8 +33,6 @@ class PaddleMobilePredictor : public PaddlePredictor { std::vector* output_data, int batch_size = -1) override; #ifdef PADDLE_MOBILE_FPGA - void FeedData(const std::vector& inputs) override; - void GetResults(std::vector* outputs) override; void Predict_From_To(int start, int end) override; void FeedPaddleTensors(const std::vector& inputs) override; void FetchPaddleTensors(std::vector* outputs) override; diff --git a/src/io/paddle_inference_api.h b/src/io/paddle_inference_api.h index 81779da1eafa892bd800641e133014988dab13e8..36435e07e6bad84527b58c0216cc12df557a8ab7 100644 --- a/src/io/paddle_inference_api.h +++ b/src/io/paddle_inference_api.h @@ -113,6 +113,7 @@ class PaddlePredictor { // `inputs`. `inputs` should be available until Run returns. Caller should be // responsible for the output tensor's buffer, either allocated or passed from // outside. + virtual bool Run(const std::vector& inputs, std::vector* output_data, int batch_size = -1) = 0; @@ -126,8 +127,6 @@ class PaddlePredictor { std::string param_file; }; #ifdef PADDLE_MOBILE_FPGA - virtual void FeedData(const std::vector& inputs) = 0; - virtual void GetResults(std::vector* outputs) = 0; virtual void Predict_From_To(int start, int end) = 0; virtual void FeedPaddleTensors(const std::vector& inputs) = 0; virtual void FetchPaddleTensors(std::vector* outputs) = 0; diff --git a/test/fpga/test_rfcn_api.cpp b/test/fpga/test_rfcn_api.cpp index d9e488962597d5df20d2fc25877dde5052a1fb9f..724ef7d14d1189bb68cb5db1a583850dd1e72816 100644 --- a/test/fpga/test_rfcn_api.cpp +++ b/test/fpga/test_rfcn_api.cpp @@ -52,8 +52,21 @@ PaddleMobileConfig GetConfig() { return config; } +PaddleMobileConfig GetConfig1() { + PaddleMobileConfig config; + config.precision = PaddleMobileConfig::FP32; + config.device = PaddleMobileConfig::kFPGA; + config.model_dir = "../models/resnet50"; + config.thread_num = 1; + config.batch_size = 1; + config.optimize = true; + config.quantification = false; + return config; +} + int main() { open_device(); + PaddleMobileConfig config = GetConfig(); auto predictor = CreatePaddlePredictor(fpga_malloc(img_length * sizeof(float))); readStream(g_image, reinterpret_cast(img)); std::cout << "Finishing initializing data" << std::endl; - /* - predictor->FeedData({img_info, img}); - predictor->Predict_From_To(0, -1); - std::cout << " Finishing predicting " << std::endl; - std::vector v(3, nullptr); - predictor->GetResults(&v); - int post_nms = 300; - for (int num = 0; num < post_nms; num ++){ - for (int i = 0; i < 8; i ++){ - std:: cout << ((float*)(v[0]))[num * 8 + i] << std::endl; - } - } - for (int num = 0; num < post_nms; num ++){ - for (int i = 0; i < 8; i ++){ - std:: cout << ((float*)(v[1]))[num * 8 + i] << std::endl; - } - } - for (int num = 0; num < post_nms; num ++){ - for (int i = 0; i < 4; i ++){ - std:: cout << ((float*)(v[2]))[num * 4 + i] << std::endl; - } - } - */ - struct PaddleTensor t_img_info, t_img; - t_img_info.dtype = FLOAT32; + t_img.dtypeid = typeid(float); t_img_info.layout = LAYOUT_HWC; t_img_info.shape = std::vector({1, 3}); t_img_info.name = "Image information"; t_img_info.data.Reset(img_info, 3 * sizeof(float)); - t_img.dtype = FLOAT32; + t_img.dtypeid = typeid(float); t_img.layout = LAYOUT_HWC; - t_img.shape = std::vector({1, 768, 1536, 3}); + t_img.shape = std::vector({1, 432, 1280, 3}); t_img.name = "Image information"; t_img.data.Reset(img, img_length * sizeof(float)); predictor->FeedPaddleTensors({t_img_info, t_img}); @@ -113,6 +102,9 @@ int main() { std::vector v; // No need to initialize v predictor->FetchPaddleTensors(&v); // Old data in v will be cleared std::cout << "Output number is " << v.size() << std::endl; + std::cout << "out[0] length " << v[0].data.length() << std::endl; + std::cout << "out[1] length " << v[1].data.length() << std::endl; + std::cout << "out[2] length " << v[2].data.length() << std::endl; auto post_nms = v[0].data.length() / sizeof(float) / 8; for (int num = 0; num < post_nms; num++) { @@ -135,6 +127,8 @@ int main() { } std::cout << "Finish getting vector values" << std::endl; + //////////////////////////////////////////////////// + PaddleTensor tensor; predictor->GetPaddleTensor("fetch2", &tensor); for (int i = 0; i < post_nms; i++) { @@ -142,5 +136,36 @@ int main() { std::cout << p[+i] << std::endl; } + ////////////////////////////////////////////////////// + + PaddleMobileConfig config1 = GetConfig1(); + auto predictor1 = + CreatePaddlePredictor(config1); + + std::cout << "Finishing loading model" << std::endl; + + int img_length1 = 224 * 224 * 3; + auto img1 = + reinterpret_cast(fpga_malloc(img_length1 * sizeof(float))); + + std::cout << "Finishing initializing data" << std::endl; + + struct PaddleTensor t_img1; + + t_img1.dtypeid = typeid(float); + t_img1.layout = LAYOUT_HWC; + t_img1.shape = std::vector({1, 224, 224, 3}); + t_img1.name = "Image information"; + t_img1.data.Reset(img1, img_length1 * sizeof(float)); + predictor1->FeedPaddleTensors({t_img1}); + predictor1->Predict_From_To(0, -1); + std::cout << "Finishing predicting " << std::endl; + + std::vector v1; // No need to initialize v + predictor1->FetchPaddleTensors(&v1); // Old data in v will be cleared + std::cout << "Output number is " << v1.size() << std::endl; + std::cout << "out[0] length " << v1[0].data.length() << std::endl; + return 0; }