From b21005dfd7036dafdfab4989f254803436c8c9de Mon Sep 17 00:00:00 2001 From: eclipsess Date: Sat, 26 May 2018 16:49:40 +0800 Subject: [PATCH] test relu with excu4test --- test/executor_for_test.h | 2 + test/operators/test_cov_op.cpp | 3 + test/operators/test_elementwise_add_op.cpp | 2 +- test/operators/test_relu_op.cpp | 112 ++++----------------- test/test_helper.h | 2 +- 5 files changed, 25 insertions(+), 96 deletions(-) diff --git a/test/executor_for_test.h b/test/executor_for_test.h index 27866e1ac5..f1ca2c7829 100644 --- a/test/executor_for_test.h +++ b/test/executor_for_test.h @@ -21,6 +21,7 @@ limitations under the License. */ #include "io.h" #include "operators/conv_op.h" #include "operators/pool_op.h" +#include "operators/relu_op.h" #include "operators/reshape_op.h" #include "operators/softmax_op.h" #include "operators/transpose_op.h" @@ -56,6 +57,7 @@ class Executor4Test : public Executor { std::vector> ops = block_desc->Ops(); for (std::shared_ptr op : ops) { if (op->Type() == op_type) { + /// test first meeting op in program std::shared_ptr op_ptr = std::make_shared( op->Type(), op->GetInputs(), op->GetOutputs(), op->GetAttrMap(), this->program_.scope); diff --git a/test/operators/test_cov_op.cpp b/test/operators/test_cov_op.cpp index 260cdfa04c..2fe7f3577b 100644 --- a/test/operators/test_cov_op.cpp +++ b/test/operators/test_cov_op.cpp @@ -29,6 +29,9 @@ int main() { paddle_mobile::framework::Tensor input; GetInput(g_test_image_1x3x224x224, &input, {1, 3, 224, 224}); + // // use SetupTensor if not has local input image . + // SetupTensor(&input, {1, 3, 224, 224}, static_cast(0), + // static_cast(1)); auto out_ddim = paddle_mobile::framework::make_ddim({1, 64, 112, 112}); auto output = executor.predict(input, "data", "conv2d_0.tmp_0", out_ddim); diff --git a/test/operators/test_elementwise_add_op.cpp b/test/operators/test_elementwise_add_op.cpp index 309f86b22d..eeb642a3f4 100644 --- a/test/operators/test_elementwise_add_op.cpp +++ b/test/operators/test_elementwise_add_op.cpp @@ -111,7 +111,7 @@ int main() { DLOG << "begin to run ElementAddOp Test"; paddle_mobile::Loader loader; auto program = - loader.Load(std::string("../../test/models/" + loader.Load(std::string("../models/" "image_classification_resnet.inference.model")); /// input x (1,3,224,224) diff --git a/test/operators/test_relu_op.cpp b/test/operators/test_relu_op.cpp index 6c2084f8c8..6fefb0368b 100644 --- a/test/operators/test_relu_op.cpp +++ b/test/operators/test_relu_op.cpp @@ -12,108 +12,32 @@ 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. */ -#pragma once +#include "../executor_for_test.h" #include "../test_include.h" -#include "operators/relu_op.h" - -namespace paddle_mobile { -namespace framework { - -template -class TestReluOp { - public: - explicit TestReluOp(const Program p) : program_(p) { - if (use_optimize_) { - to_predict_program_ = program_.optimizeProgram; - } else { - to_predict_program_ = program_.originProgram; - } - - const std::vector> blocks = - to_predict_program_->Blocks(); - // DLOG << " **block size " << blocks.size(); - for (auto block_desc : blocks) { - std::vector> ops = block_desc->Ops(); - // DLOG << " ops " << ops.size(); - for (auto op : ops) { - if (op->Type() == "relu" && - op->Input("X")[0] == "batch_norm_34.tmp_2") { - DLOG << "in"; - std::shared_ptr> test_op = - std::make_shared>( - op->Type(), op->GetInputs(), op->GetOutputs(), - op->GetAttrMap(), program_.scope); - ops_of_block_[*block_desc.get()].push_back(test_op); - } - } - } - } - - std::shared_ptr predict(const Tensor &t1) { - // feed - auto scope = program_.scope; - Variable *x1_feed_value = scope->Var("batch_norm_34.tmp_2"); - auto tensor_x1 = x1_feed_value->GetMutable(); - tensor_x1->ShareDataWith(t1); - - Variable *output = scope->Var("batch_norm_34.tmp_3"); - auto *output_tensor = output->GetMutable(); - output_tensor->mutable_data({1, 2, 3, 4}); - - // DLOG << typeid(output_tensor).name(); - // DLOG << "output_tensor dims: " << output_tensor->dims(); - - std::shared_ptr out_tensor = std::make_shared(); - out_tensor.reset(output_tensor); - - predict(t1, 0); - - return out_tensor; - // return outvars_tensor; - } - - private: - const framework::Program program_; - std::shared_ptr to_predict_program_; - std::map>>> - ops_of_block_; - bool use_optimize_ = false; - - void predict(const Tensor &t1, int block_id) { - std::shared_ptr to_predict_block = - to_predict_program_->Block(block_id); - for (int j = 0; j < ops_of_block_[*to_predict_block.get()].size(); ++j) { - auto op = ops_of_block_[*to_predict_block.get()][j]; - DLOG << "op -> run()"; - op->Run(); - } - } -}; - -template class TestReluOp; -} // namespace framework -} // namespace paddle_mobile int main() { - DLOG << "----------**********----------"; - DLOG << "begin to run Relu Test"; paddle_mobile::Loader loader; - auto program = loader.Load(std::string("../../test/models/mobilenet+ssd")); + // ../models/image_classification_resnet.inference.model + auto program = loader.Load(g_mobilenet_ssd); - /// input x (1,3,300,300) - paddle_mobile::framework::Tensor inputx1; - SetupTensor(&inputx1, {1, 2, 3, 4}, static_cast(-1), - static_cast(1)); - auto *inputx1_ptr = inputx1.data(); + PADDLE_MOBILE_ENFORCE(program.originProgram != nullptr, + "program file read fail"); - paddle_mobile::framework::TestReluOp testReluOp(program); + Executor4Test> + executor(program, "relu"); + + paddle_mobile::framework::Tensor input; + SetupTensor(&input, {1, 2, 3, 4}, static_cast(-1), + static_cast(1)); - auto output = testReluOp.predict(inputx1); - auto *output_ptr = output->data(); + auto out_ddim = paddle_mobile::framework::make_ddim({1, 2, 3, 4}); + auto output = executor.predict(input, "batch_norm_0.tmp_2", + "batch_norm_0.tmp_3", out_ddim); - for (int i = 0; i < output->numel(); i++) { - DLOG << output_ptr[i]; + auto output_ptr = output->data(); + for (int j = 0; j < output->numel(); ++j) { + DLOG << " value of output: " << output_ptr[j]; } return 0; } diff --git a/test/test_helper.h b/test/test_helper.h index 58a7507f1b..e2d6a183cb 100644 --- a/test/test_helper.h +++ b/test/test_helper.h @@ -23,7 +23,7 @@ limitations under the License. */ static const std::string g_googlenet = "../models/googlenet"; static const std::string g_mobilenet = "../models/mobilenet"; -static const std::string g_mobilenet_ssd = "../models/mobilenet"; +static const std::string g_mobilenet_ssd = "../models/mobilenet+ssd"; static const std::string g_squeezenet = "../models/squeezenet"; static const std::string g_resnet = "../models/image_classification_resnet.inference.model"; -- GitLab