diff options
author | 박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com> | 2018-05-28 13:10:33 +0900 |
---|---|---|
committer | 서상민/동작제어Lab(SR)/Senior Engineer/삼성전자 <sangmin7.seo@samsung.com> | 2018-05-28 13:10:33 +0900 |
commit | 407333838bfe765fd1edcbd5ffe2797bc2cc897c (patch) | |
tree | 30e221d815066d1c4b55b9e9a7c339e68442f47e /contrib | |
parent | 83c1d70cddf9edf5e7618fc1e519b49d5152766e (diff) | |
download | nnfw-407333838bfe765fd1edcbd5ffe2797bc2cc897c.tar.gz nnfw-407333838bfe765fd1edcbd5ffe2797bc2cc897c.tar.bz2 nnfw-407333838bfe765fd1edcbd5ffe2797bc2cc897c.zip |
Add tensorflow-based 'detection' example (#1368)
This commit adds 'detection' sample app which loads T/F model (which
takes 1x320x320x3 input whose name is 'input_node') and measures elapsed
time for 5 runs.
Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
Diffstat (limited to 'contrib')
-rw-r--r-- | contrib/detection/CMakeLists.txt | 29 | ||||
-rw-r--r-- | contrib/detection/detection.cpp | 57 |
2 files changed, 86 insertions, 0 deletions
diff --git a/contrib/detection/CMakeLists.txt b/contrib/detection/CMakeLists.txt new file mode 100644 index 000000000..1f8460c5e --- /dev/null +++ b/contrib/detection/CMakeLists.txt @@ -0,0 +1,29 @@ +if(NOT BUILD_DETECTION_APP) + return() +endif(NOT BUILD_DETECTION_APP) + +if(NOT DEFINED TENSORFLOW_DIR) + set(TENSORFLOW_DIR ${CMAKE_SOURCE_DIR}/externals/tensorflow) +endif(NOT DEFINED TENSORFLOW_DIR) + +if(NOT DEFINED NSYNC_ARCH) + set(NSYNC_ARCH "default") +endif(NOT DEFINED NSYNC_ARCH) + +set(TENSROFLOW_MAKEFILE_DIR "${TENSORFLOW_DIR}/tensorflow/contrib/makefile") +set(TENSORFLOW_GEN_DIR "${TENSROFLOW_MAKEFILE_DIR}/gen") +set(TENSORFLOW_DOWNLOADS_DIR "${TENSROFLOW_MAKEFILE_DIR}/downloads") + +list(APPEND SOURCES detection.cpp) + +add_executable(detection ${SOURCES}) +target_include_directories(detection PRIVATE "${TENSORFLOW_DIR}") +target_include_directories(detection PRIVATE "${TENSORFLOW_GEN_DIR}/proto") +target_include_directories(detection PRIVATE "${TENSORFLOW_GEN_DIR}/protobuf/include") +target_include_directories(detection PRIVATE "${TENSORFLOW_DOWNLOADS_DIR}/eigen") +target_include_directories(detection PRIVATE "${TENSORFLOW_DOWNLOADS_DIR}/nsync/public") +target_link_libraries(detection nnfw_util) +target_link_libraries(detection -Wl,--whole-archive "${TENSORFLOW_GEN_DIR}/lib/libtensorflow-core.a" -Wl,--no-whole-archive) +target_link_libraries(detection "${TENSORFLOW_GEN_DIR}/protobuf/lib/libprotobuf.a") +target_link_libraries(detection "${TENSORFLOW_DOWNLOADS_DIR}/nsync/builds/${NSYNC_ARCH}.linux.c++11/libnsync.a") +target_link_libraries(detection pthread dl) diff --git a/contrib/detection/detection.cpp b/contrib/detection/detection.cpp new file mode 100644 index 000000000..58beaad25 --- /dev/null +++ b/contrib/detection/detection.cpp @@ -0,0 +1,57 @@ +#include <tensorflow/core/public/session.h> + +#include <iostream> +#include <stdexcept> + +#include <cassert> +#include <cstring> + +#include "util/benchmark.h" + +#define CHECK_TF(e) { \ + if(!(e).ok()) \ + { \ + throw std::runtime_error{"'" #e "' FAILED"}; \ + } \ +} + +int main(int argc, char **argv) +{ + if (argc < 2) + { + std::cerr << "USAGE: " << argv[0] << " [T/F model path] [output 0] [output 1] ..." << std::endl; + return 255; + } + + std::vector<std::string> output_nodes; + + for (int argn = 2; argn < argc; ++argn) + { + output_nodes.emplace_back(argv[argn]); + } + + tensorflow::Session* sess; + + CHECK_TF(tensorflow::NewSession(tensorflow::SessionOptions(), &sess)); + + tensorflow::GraphDef graph_def; + + CHECK_TF(ReadBinaryProto(tensorflow::Env::Default(), argv[1], &graph_def)); + CHECK_TF(sess->Create(graph_def)); + + tensorflow::Tensor input(tensorflow::DT_FLOAT, tensorflow::TensorShape({1, 320, 320, 3})); + std::vector<tensorflow::Tensor> outputs; + + for (uint32_t n = 0; n < 5; ++n) + { + std::chrono::milliseconds elapsed(0); + + nnfw::util::benchmark::measure(elapsed) << [&] (void) { + CHECK_TF(sess->Run({{"input_node", input}}, output_nodes, {}, &outputs)); + }; + + std::cout << "Takes " << elapsed.count() << "ms" << std::endl; + } + + return 0; +} |