diff --git a/CMakeLists.txt b/CMakeLists.txt index a9382f9697e5c39c7e7195b94fc5332d39dc18a9..f60846e98aa9ca36bd6bd68cccdda6e3d2ff616a 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 0000000000000000000000000000000000000000..44b0ee32e99ccddf5cc6060882dc37158c149693 --- /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 abd6217fbeb62a09a7d4a9fe9b6d85d0ab1cf2d8..52bda2258a00c7444762fe8297380c1c7752dd42 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 808002d4c8f3193744ef68c1db881a787d19b133..46feb97cb879bb092e1a8f6955f7f50712f9ea3b 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 6194e5dcfff2c0320318d3e30e5c8204cd71a749..2de3a9536702ee17daabae969d81e979fc66b0c8 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 b8350a81118d8187552854f8ed7cab3a13dab8f6..ac89106e4988b35d54a59991009a98739e50d024 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 de2d359bf58d1ad328defd2f51e87e2d6bfe6295..ae99197baa97c07d2a883f8721d533b85ab7873a 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 139579e9116651c15764997d962b7d2622532146..0640af890cf9857e1dda7eeaaec593b7c613fe8a 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; }