From e8cc4c9279124cef7d2ba6985356bc76d523543a Mon Sep 17 00:00:00 2001 From: wangliu Date: Wed, 30 May 2018 19:09:05 +0800 Subject: [PATCH] add impl for executor'predict --- CMakeLists.txt | 3 ++- scripts/push2android.sh | 14 ++++++++++++++ src/common/enforce.h | 6 +++--- src/framework/operator.cpp | 14 +------------- src/framework/operator.h | 12 ++++++++++++ src/io.cpp | 34 +++++++++++++++++++++++++--------- src/io.h | 10 ++++++---- test/net/test_googlenet.cpp | 7 ++----- 8 files changed, 65 insertions(+), 35 deletions(-) create mode 100644 scripts/push2android.sh diff --git a/CMakeLists.txt b/CMakeLists.txt index a9382f9697..f60846e98a 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,6 +1,7 @@ cmake_minimum_required(VERSION 3.0) project(paddle-mobile) -add_definitions(-DPADDLE_MOBILE_DEBUG="true") +add_definitions(-DPADDLE_MOBILE_DEBUG) +add_definitions(-DENABLE_EXCEPTION) set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++11") set(CMAKE_BUILD_TYPE RelWithDebInfo) diff --git a/scripts/push2android.sh b/scripts/push2android.sh new file mode 100644 index 0000000000..44b0ee32e9 --- /dev/null +++ b/scripts/push2android.sh @@ -0,0 +1,14 @@ +#!/usr/bin/env sh + +push_fn () { +MODELS_PATH="../test/models/*" +EXE_FILE="../test/build/*" +EXE_DIR="data/local/tmp/bin" +MODELS_DIR="data/local/tmp/models" +LIB_PATH="../build/release/arm-v7a/build/*" +adb push ${EXE_FILE} ${EXE_DIR} +adb push ${LIB_PATH} ${EXE_DIR} +adb push ${MODELS_PATH} ${MODELS_DIR} +echo "test files sync completed" +} +push_fn diff --git a/src/common/enforce.h b/src/common/enforce.h index abd6217fbe..52bda2258a 100644 --- a/src/common/enforce.h +++ b/src/common/enforce.h @@ -14,7 +14,7 @@ limitations under the License. */ #pragma once -#ifdef PADDLE_MOBILE_DEBUG +#ifdef ENABLE_EXCEPTION #include #include #include @@ -25,7 +25,7 @@ limitations under the License. */ namespace paddle_mobile { -#ifdef PADDLE_MOBILE_DEBUG +#ifdef ENABLE_EXCEPTION struct PaddleMobileException : public std::exception { const std::string exception_prefix = "paddle mobile C++ Exception: \n"; std::string message; @@ -64,7 +64,7 @@ struct PaddleMobileException : public std::exception { } #else #define PADDLE_MOBILE_THROW_EXCEPTION(...) -#define PADDLE_MOBILE_ASSERT(stat, ...) +#define PADDLE_MOBILE_ENFORCE(stat, ...) #endif } // namespace paddle_mobile diff --git a/src/framework/operator.cpp b/src/framework/operator.cpp index 808002d4c8..46feb97cb8 100644 --- a/src/framework/operator.cpp +++ b/src/framework/operator.cpp @@ -28,18 +28,6 @@ vector OperatorBase::GetOutKeys() const { return it->second.second; } -template -static T *GetVarValue(const string &key, const VariableNameMap &var_map, - const Scope &scope) { - auto var_vec = var_map.at(key); - if (!var_vec.empty()) { - auto var = scope.FindVar(var_vec[0]); - return var->GetMutable(); - } else { - return nullptr; - } -} - template OperatorBase::OperatorBase(const std::string &type, const VariableNameMap &inputs, @@ -60,7 +48,7 @@ void OperatorBase::CheckAllInputOutputSet() const {} template void OperatorBase::Run() const { RunImpl(); -#ifdef PADDLE_MOBILE_DEBUG +#if (PADDLE_MOBILE_DEBUG) vector output_keys = GetOutKeys(); for (const auto key : output_keys) { Tensor *out_ = GetVarValue(key, outputs_, *scope_); diff --git a/src/framework/operator.h b/src/framework/operator.h index 6194e5dcff..2de3a95367 100644 --- a/src/framework/operator.h +++ b/src/framework/operator.h @@ -39,6 +39,18 @@ namespace framework { using std::string; using std::vector; +template +static T *GetVarValue(const string &key, const VariableNameMap &var_map, + const Scope &scope) { + auto var_vec = var_map.at(key); + if (!var_vec.empty()) { + auto var = scope.FindVar(var_vec[0]); + return var->GetMutable(); + } else { + return nullptr; + } +} + template class OperatorBase : PaddleMobileObject { public: diff --git a/src/io.cpp b/src/io.cpp index b8350a8111..ac89106e49 100644 --- a/src/io.cpp +++ b/src/io.cpp @@ -371,31 +371,47 @@ void Executor::InitMemory() { } template -void Executor::Predict(const framework::Tensor &t, int block_id) { +std::shared_ptr Executor::Predict( + const framework::Tensor &t) { framework::Variable *g_feed_value = program_.scope->Var("feed"); framework::Tensor *feed_tensor = g_feed_value->GetMutable(); feed_tensor->Resize(t.dims()); feed_tensor->ShareDataWith(t); std::shared_ptr to_predict_block = - to_predict_program_->Block(block_id); + to_predict_program_->Block(0); for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size(); ++j) { auto op = ops_of_block_[*to_predict_block.get()][j]; op->Run(); } + auto ops = ops_of_block_[*to_predict_program_->Block(0)]; + auto last_op = ops.rbegin(); + auto output_map = (*last_op)->Outputs(); + std::vector out_keys = (*last_op)->GetOutKeys(); + PADDLE_MOBILE_ENFORCE(out_keys.size() > 0, "the last op contains no output"); + framework::LoDTensor *output_tensor = + framework::GetVarValue(out_keys[0], output_map, + *(program_.scope)); + return std::shared_ptr(output_tensor); +} +template +std::shared_ptr Executor::Predict( + const framework::Tensor &t, int block_id) { + return Predict(t); } template std::vector::Ptype> Executor::Predict( const std::vector &input, const std::vector &dims) { framework::Tensor tensor(input, framework::make_ddim(dims)); - - Predict(tensor, 0); - - framework::Variable *g_feed_value = program_.scope->Var("col"); - auto feed_tensor = g_feed_value->GetMutable(); - - return {}; + std::shared_ptr output_tensor = Predict(tensor, 0); + Executor::Ptype *output_ptr = + output_tensor->data::Ptype>(); + std::vector::Ptype> result_vector; + for (int j = 0; j < output_tensor->numel(); ++j) { + result_vector.push_back(output_ptr[j]); + } + return result_vector; } template class Executor; diff --git a/src/io.h b/src/io.h index de2d359bf5..ae99197baa 100644 --- a/src/io.h +++ b/src/io.h @@ -15,6 +15,7 @@ limitations under the License. */ #pragma once #include +#include #include #include @@ -44,24 +45,25 @@ class Executor { public: typedef typename PrecisionTrait

::ptype Ptype; - Executor() = default; - Executor(const framework::Program p, int batch_size = 1, bool use_optimize = true); - // std::shared_ptr Predict(framework::Tensor &t); + std::shared_ptr Predict(const framework::Tensor &t); std::vector Predict(const std::vector &input, const std::vector &dims); protected: + Executor() = default; + void InitMemory(); void LoadMemory(const framework::VarDesc var_desc, framework::LoDTensor *tensor, const std::string &file_path); framework::Program program_; int batch_size_ = 1; std::shared_ptr to_predict_program_; - void Predict(const framework::Tensor &t, int block_id); + std::shared_ptr Predict(const framework::Tensor &t, + int block_id); std::map>>> ops_of_block_; diff --git a/test/net/test_googlenet.cpp b/test/net/test_googlenet.cpp index 139579e911..0640af890c 100644 --- a/test/net/test_googlenet.cpp +++ b/test/net/test_googlenet.cpp @@ -18,20 +18,17 @@ limitations under the License. */ int main() { paddle_mobile::Loader loader; - // ../../../test/models/googlenet - // ../../../test/models/mobilenet auto time1 = time(); auto program = loader.Load(g_googlenet, false); auto time2 = time(); - DLOG << "load cost :" << time_diff(time1, time1) << "ms"; + DLOG << "load cost :" << time_diff(time1, time2) << "ms\n"; paddle_mobile::Executor executor(program, 1, false); - std::vector input; std::vector dims{1, 3, 224, 224}; GetInput(g_test_image_1x3x224x224, &input, dims); auto time3 = time(); executor.Predict(input, dims); auto time4 = time(); - DLOG << "predict cost :" << time_diff(time3, time4) << "ms"; + DLOG << "predict cost :" << time_diff(time3, time4) << "ms\n"; return 0; } -- GitLab