From 88a7823c1d39a57cc723f497b1f42fecce1a1ae6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=84=B8=ED=9D=AC/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Mon, 22 Apr 2019 13:30:27 +0900 Subject: [PATCH] [moco] provide istream as input of conversion (#3334) This will add another load() to parse from istream and a test code Signed-off-by: SaeHie Park --- .../lib/frontend/tf/include/moco/tf/Frontend.h | 2 + contrib/moco/lib/frontend/tf/src/Frontend.cpp | 14 ++++ contrib/moco/lib/frontend/tf/src/Frontend.test.cpp | 84 ++++++++++++++++++++++ 3 files changed, 100 insertions(+) diff --git a/contrib/moco/lib/frontend/tf/include/moco/tf/Frontend.h b/contrib/moco/lib/frontend/tf/include/moco/tf/Frontend.h index f9495fa..8f48e9f 100644 --- a/contrib/moco/lib/frontend/tf/include/moco/tf/Frontend.h +++ b/contrib/moco/lib/frontend/tf/include/moco/tf/Frontend.h @@ -19,6 +19,7 @@ #include +#include #include #include #include @@ -56,6 +57,7 @@ public: public: std::unique_ptr load(const ModelSignature &, const char *, FileType) const; + std::unique_ptr load(const ModelSignature &, std::istream *, FileType) const; }; } // namespace tf diff --git a/contrib/moco/lib/frontend/tf/src/Frontend.cpp b/contrib/moco/lib/frontend/tf/src/Frontend.cpp index f687144..f5da991 100644 --- a/contrib/moco/lib/frontend/tf/src/Frontend.cpp +++ b/contrib/moco/lib/frontend/tf/src/Frontend.cpp @@ -212,5 +212,19 @@ std::unique_ptr Frontend::load(const ModelSignature &signature, con return std::move(graph); } +std::unique_ptr Frontend::load(const ModelSignature &signature, std::istream *stream, + FileType type) const +{ + tensorflow::GraphDef tf_graph_def; + + load_tf(stream, type, tf_graph_def); + + auto graph = loco::make_graph(); + + convert_graph(signature, tf_graph_def, graph.get()); + + return std::move(graph); +} + } // namespace tf } // namespace moco diff --git a/contrib/moco/lib/frontend/tf/src/Frontend.test.cpp b/contrib/moco/lib/frontend/tf/src/Frontend.test.cpp index 9eec957..a52d111 100644 --- a/contrib/moco/lib/frontend/tf/src/Frontend.test.cpp +++ b/contrib/moco/lib/frontend/tf/src/Frontend.test.cpp @@ -18,6 +18,10 @@ #include +#include + +#define STRING(content) #content + TEST(MocoTensotFlowFrontendTest, Dummy) { moco::tf::Frontend frontend; } TEST(TensorFlowFrontend, load_model) @@ -31,3 +35,83 @@ TEST(TensorFlowFrontend, load_model) frontend.load(signature, "../../../test/tf/Placeholder_000.pb", moco::tf::Frontend::FileType::Binary); } + +namespace +{ + +struct membuf : std::streambuf +{ + membuf(char const *base, size_t size) + { + char *p(const_cast(base)); + this->setg(p, p, p + size); + } +}; + +struct imemstream : virtual membuf, std::istream +{ + imemstream(char const *base, size_t size) + : membuf(base, size), std::istream(static_cast(this)) + { + } +}; + +// clang-format off +const char *basic_pbtxtdata = STRING( +node { + name: "Placeholder" + op: "Placeholder" + attr { + key: "dtype" + value { + type: DT_FLOAT + } + } + attr { + key: "shape" + value { + shape { + dim { + size: 1 + } + dim { + size: 2 + } + dim { + size: 1 + } + dim { + size: 2 + } + } + } + } +} +node { + name: "output/identity" + op: "Identity" + input: "Placeholder" + attr { + key: "T" + value { + type: DT_FLOAT + } + } +} +); +// clang-format on + +} // namespace + +TEST(TensorFlowFrontend, load_model_withio) +{ + moco::tf::Frontend frontend; + moco::tf::ModelSignature signature; + + imemstream mempb(basic_pbtxtdata, std::strlen(basic_pbtxtdata)); + + signature.add_input("Placeholder"); + signature.add_output("output/identity"); + + frontend.load(signature, &mempb, moco::tf::Frontend::FileType::Text); +} -- 2.7.4