diff --git a/src/framework/executor.cpp b/src/framework/executor.cpp index 45b469a3d9ba08e8b07126dd66d722d85a414fee..b36b81b294f543eac297857eb7172453d178864e 100644 --- a/src/framework/executor.cpp +++ b/src/framework/executor.cpp @@ -505,18 +505,23 @@ void Executor::GetResults(std::vector *v) { template void Executor::GetTensorResults( std::vector *v) { - auto output_size = v->size(); - PADDLE_MOBILE_ENFORCE(output_size > 0, "Empty output"); auto vars = program_.scope->VarContain("fetch"); - PADDLE_MOBILE_ENFORCE(output_size == vars.size(), - "output data number not correct"); + auto output_size = vars.size(); + for (int i = 0; i < output_size; i++) { auto var = program_.scope->Var("fetch", i); auto fetch_tensor = var->template GetMutable(); - (*v)[i] = fetch_tensor; + v->push_back(fetch_tensor); } } +template +framework::Tensor *Executor::GetTensorByName( + const std::string &name) { + auto var = program_.scope->Var(name); + return var->template GetMutable(); +}; + template std::shared_ptr Executor::FetchResult(int id) { auto &ops = ops_of_block_[0]; diff --git a/src/framework/executor.h b/src/framework/executor.h index 8c3013e2de2ac9e436256b4675827646337bfd4e..65484b11465089eb90a99ac21caa9e1c386d4d10 100644 --- a/src/framework/executor.h +++ b/src/framework/executor.h @@ -57,6 +57,7 @@ class Executor { void GetResults(std::vector *v); void GetTensorResults(std::vector *v); + framework::Tensor *GetTensorByName(const std::string &name); std::shared_ptr FetchResult(int id = -1); void Predict_From_To(int start = 0, int end = -1); diff --git a/src/io/api_paddle_mobile.cc b/src/io/api_paddle_mobile.cc index 1baa9044e695ae5eceb777f104a2cebf87f740dd..d644dc4804b6daa1447aa68084283d345b2dc6f8 100644 --- a/src/io/api_paddle_mobile.cc +++ b/src/io/api_paddle_mobile.cc @@ -151,15 +151,26 @@ void PaddleMobilePredictor::FeedPaddleTensors( template void PaddleMobilePredictor::FetchPaddleTensors( std::vector *outputs) { - auto num = outputs->size(); - PADDLE_MOBILE_ENFORCE(num > 0, "0 output pointers is not permitted"); - std::vector tensors(num, nullptr); + // auto num = outputs->size(); + // PADDLE_MOBILE_ENFORCE(num > 0, "0 output pointers is not permitted"); + // std::vector tensors(num, nullptr); + outputs->clear(); + std::vector tensors; paddle_mobile_->GetTensorResults(&tensors); + auto num = tensors.size(); + outputs->resize(num, PaddleTensor()); for (int i = 0; i < num; i++) { ConvertTensors(*tensors[i], &(*outputs)[i]); } } +template +void PaddleMobilePredictor::GetPaddleTensor(const std::string &name, + PaddleTensor *output) { + framework::Tensor *t = paddle_mobile_->GetTensorByName(name); + ConvertTensors(*t, output); +}; + template void PaddleMobilePredictor::FeedData( const std::vector &inputs) { diff --git a/src/io/api_paddle_mobile.h b/src/io/api_paddle_mobile.h index 4ea83123355ac4dfff0479045eef7f1c0a4734d2..2e3d8dc69b60138eb5f82c1b1faa9c182c51442b 100644 --- a/src/io/api_paddle_mobile.h +++ b/src/io/api_paddle_mobile.h @@ -37,6 +37,8 @@ class PaddleMobilePredictor : public PaddlePredictor { void Predict_From_To(int start, int end) override; void FeedPaddleTensors(const std::vector& inputs) override; void FetchPaddleTensors(std::vector* outputs) override; + void GetPaddleTensor(const std::string& name, PaddleTensor* output) override; + #endif ~PaddleMobilePredictor() override; diff --git a/src/io/paddle_inference_api.h b/src/io/paddle_inference_api.h index f7e66740f0b5f732e7517db527ad60dd660d6807..81779da1eafa892bd800641e133014988dab13e8 100644 --- a/src/io/paddle_inference_api.h +++ b/src/io/paddle_inference_api.h @@ -27,8 +27,6 @@ limitations under the License. */ #include #include -// #define PADDLE_MOBILE_FPGA - namespace paddle_mobile { #ifdef PADDLE_MOBILE_FPGA @@ -133,6 +131,8 @@ class PaddlePredictor { 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; + virtual void GetPaddleTensor(const std::string& name, + PaddleTensor* output) = 0; #endif protected: diff --git a/src/io/paddle_mobile.cpp b/src/io/paddle_mobile.cpp index 21f0f58b904c0c926323d3b8196568d7ae49c374..6027f2d3c7396bfa33fd7674abc379390f784586 100644 --- a/src/io/paddle_mobile.cpp +++ b/src/io/paddle_mobile.cpp @@ -249,6 +249,12 @@ void PaddleMobile::GetTensorResults( executor_->GetTensorResults(v); } +template +framework::Tensor *PaddleMobile::GetTensorByName( + const std::string &name) { + return executor_->GetTensorByName(name); +}; + template std::shared_ptr PaddleMobile::FetchResult( int id) { diff --git a/src/io/paddle_mobile.h b/src/io/paddle_mobile.h index d0330d9356136494c537c2b7166ac682ee598e93..d835eed2daad2a78e3c0e430cb73a8fbc078b667 100644 --- a/src/io/paddle_mobile.h +++ b/src/io/paddle_mobile.h @@ -95,6 +95,7 @@ class PaddleMobile { void GetResults(std::vector *v); void GetTensorResults(std::vector *v); + framework::Tensor *GetTensorByName(const std::string &name); std::shared_ptr FetchResult(int id = -1); void Predict_From_To(int start = 0, int end = -1); diff --git a/test/fpga/test_rfcn.cpp b/test/fpga/test_rfcn.cpp index a45666365b876abe18d5e24a79525160f3cd8e93..50f8aa863d45c3c118f60367bf7b9921e0667891 100644 --- a/test/fpga/test_rfcn.cpp +++ b/test/fpga/test_rfcn.cpp @@ -133,39 +133,16 @@ int main() { readStream(g_image_src_float, reinterpret_cast(img)); std::vector v(3, nullptr); - paddle_mobile.FeedData({img_info, img}); + paddle_mobile.FeedData(std::vector({img_info, img})); paddle_mobile.Predict_To(-1); - for (int i = 55; i < 69; i++) { + for (int i = 65; i < 69; i++) { auto tensor_ptr = paddle_mobile.FetchResult(i); std::string saveName = "rfcn_" + std::to_string(i); - // if(i != 58) paddle_mobile::fpga::fpga_invalidate((*tensor_ptr).get_data(), tensor_ptr->numel() * sizeof(float)); - // tensor_ptr->numel() * sizeof(float)); - if ((i == 48) || (i == 47)) { - dump_stride(saveName, (*tensor_ptr), 20, - false); // 20);//tensor_ptr->numel()); - } else if (i == 55) { - dump_stride(saveName, (*tensor_ptr), tensor_ptr->numel(), - true); // 20);//tensor_ptr->numel()); - } else { - dump_stride(saveName, (*tensor_ptr), tensor_ptr->numel(), - true); // 20);//tensor_ptr->numel()); - } - /* float result = 0; - std::string str = "softmax_input_data"; - float* data = - static_cast(fpga::fpga_malloc(tensor_ptr->numel() * - sizeof(float))); str = "softmax_output_data"; auto output_ptr = - static_cast((*tensor_ptr).get_data()); for (int idx = 0; idx < - tensor_ptr->numel(); ++idx) - { - data[idx] = fpga::fp16_2_fp32(output_ptr[idx]); - } - fpga::savefile(str,data, tensor_ptr->numel(), result ); */ + dump_stride(saveName, (*tensor_ptr), tensor_ptr->numel(), true); } - // paddle_mobile.GetResults(&v); DLOG << "Computation done"; fpga::fpga_free(img); diff --git a/test/fpga/test_rfcn_api.cpp b/test/fpga/test_rfcn_api.cpp index 2f6f23d34d3d305db71ddd27b932a0cd6e229794..74e99ff5ffe553101a07c407cd05c177bd6287b2 100644 --- a/test/fpga/test_rfcn_api.cpp +++ b/test/fpga/test_rfcn_api.cpp @@ -12,6 +12,9 @@ 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. */ +#ifndef PADDLE_MOBILE_FPGA +#define PADDLE_MOBILE_FPGA +#endif #include #include #include "../../src/io/paddle_inference_api.h" @@ -59,14 +62,14 @@ int main() { CreatePaddlePredictor(config); - std::cout << "after loading model" << std::endl; + std::cout << "Finishing loading model" << std::endl; float img_info[3] = {768, 1536, 768.0f / 960.0f}; int img_length = 768 * 1536 * 3; auto img = reinterpret_cast(fpga_malloc(img_length * sizeof(float))); readStream(g_image, reinterpret_cast(img)); - std::cout << "after initializing data" << std::endl; + std::cout << "Finishing initializing data" << std::endl; /* predictor->FeedData({img_info, img}); predictor->Predict_From_To(0, -1); @@ -110,8 +113,10 @@ int main() { predictor->Predict_From_To(0, -1); std::cout << "Finishing predicting " << std::endl; - std::vector v(3, PaddleTensor()); - predictor->FetchPaddleTensors(&v); + 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; + auto post_nms = v[0].data.length() / sizeof(float) / 8; for (int num = 0; num < post_nms; num++) { for (int i = 0; i < 8; i++) { @@ -131,5 +136,14 @@ int main() { std::cout << p[num * 4 + i] << std::endl; } } + std::cout << "Finish getting vector values" << std::endl; + + PaddleTensor tensor; + predictor->GetPaddleTensor("fetch2", &tensor); + for (int i = 0; i < post_nms; i++) { + auto p = reinterpret_cast(tensor.data.data()); + std::cout << p[+i] << std::endl; + } + return 0; }