[tfkit] Initial commit (#3004)
author박종현/On-Device Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 7 Feb 2019 06:28:18 +0000 (15:28 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 7 Feb 2019 06:28:18 +0000 (15:28 +0900)
This commit introduces tfkit which allows users to inspect tensorflow
graphdef files (.pb) with command-line interface.

The current implementation includes only "decode" command.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/tfkit/.FORMATCHECKED [new file with mode: 0644]
contrib/tfkit/CMakeLists.txt [new file with mode: 0644]
contrib/tfkit/src/DecodeCommand.cpp [new file with mode: 0644]
contrib/tfkit/src/DecodeCommand.hpp [new file with mode: 0644]
contrib/tfkit/src/Main.cpp [new file with mode: 0644]

diff --git a/contrib/tfkit/.FORMATCHECKED b/contrib/tfkit/.FORMATCHECKED
new file mode 100644 (file)
index 0000000..e69de29
diff --git a/contrib/tfkit/CMakeLists.txt b/contrib/tfkit/CMakeLists.txt
new file mode 100644 (file)
index 0000000..4ea9eb6
--- /dev/null
@@ -0,0 +1,43 @@
+nncc_find_package(Protobuf QUIET)
+nncc_find_package(TensorFlowSource EXACT 1.12 QUIET)
+
+if(NOT Protobuf_FOUND)
+  return()
+endif(NOT Protobuf_FOUND)
+
+if(NOT TensorFlowSource_FOUND)
+  return()
+endif(NOT TensorFlowSource_FOUND)
+
+message(STATUS "Build tfkit: TRUE")
+
+# Minimal Protocol Buffer specification for GraphDef file (.pb) encoding/decoding
+unset(PROTO_FILES)
+list(APPEND PROTO_FILES tensorflow/core/framework/versions.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/resource_handle.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/types.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/tensor.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/tensor_shape.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/attr_value.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/op_def.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/node_def.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/function.proto)
+list(APPEND PROTO_FILES tensorflow/core/framework/graph.proto)
+
+Protobuf_Generate(TENSORFLOW_GRAPHDEF_PROTO
+                  "${CMAKE_CURRENT_BINARY_DIR}/generated"
+                  "${TensorFlowSource_DIR}"
+                  ${PROTO_FILES})
+
+file(GLOB_RECURSE SOURCES "src/*.cpp")
+
+add_library(tfkitproto STATIC ${TENSORFLOW_GRAPHDEF_PROTO_SOURCES})
+set_target_properties(tfkitproto PROPERTIES POSITION_INDEPENDENT_CODE ON)
+target_include_directories(tfkitproto PUBLIC ${TENSORFLOW_GRAPHDEF_PROTO_INCLUDE_DIRS})
+target_link_libraries(tfkitproto PUBLIC libprotobuf)
+
+add_executable(tfkit ${SOURCES})
+target_link_libraries(tfkit PRIVATE stdex)
+target_link_libraries(tfkit PRIVATE cli)
+target_link_libraries(tfkit PRIVATE tfkitproto)
+target_link_libraries(tfkit PRIVATE nncc_common)
diff --git a/contrib/tfkit/src/DecodeCommand.cpp b/contrib/tfkit/src/DecodeCommand.cpp
new file mode 100644 (file)
index 0000000..878a6d8
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * 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.
+ */
+
+#include "DecodeCommand.hpp"
+
+#include <tensorflow/core/framework/graph.pb.h>
+
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/text_format.h>
+
+#include <iostream>
+
+int DecodeCommand::run(int, const char *const *) const
+{
+  tensorflow::GraphDef graph_def;
+
+  // Load binary from standard input
+  google::protobuf::io::IstreamInputStream is{&std::cin};
+  google::protobuf::io::CodedInputStream coded_is{&is};
+
+  if (!graph_def.ParseFromCodedStream(&coded_is))
+  {
+    std::cerr << "ERROR: Failed to parse tensorflow model" << std::endl;
+    return 255;
+  }
+
+  // Write text into standard output
+  google::protobuf::io::OstreamOutputStream os{&std::cout};
+  google::protobuf::TextFormat::Print(graph_def, &os);
+
+  return 0;
+}
diff --git a/contrib/tfkit/src/DecodeCommand.hpp b/contrib/tfkit/src/DecodeCommand.hpp
new file mode 100644 (file)
index 0000000..023791b
--- /dev/null
@@ -0,0 +1,27 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * 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.
+ */
+
+#ifndef __DECODE_COMMAND_H__
+#define __DECODE_COMMAND_H__
+
+#include <cli/Command.h>
+
+struct DecodeCommand final : public cli::Command
+{
+  int run(int argc, const char *const *argv) const override;
+};
+
+#endif // __DECODE_COMMAND_H__
diff --git a/contrib/tfkit/src/Main.cpp b/contrib/tfkit/src/Main.cpp
new file mode 100644 (file)
index 0000000..0e1566c
--- /dev/null
@@ -0,0 +1,29 @@
+/*
+ * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * 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.
+ */
+
+#include "DecodeCommand.hpp"
+
+#include <cli/App.h>
+#include <stdex/Memory.h>
+
+int main(int argc, char **argv)
+{
+  cli::App app{argv[0]};
+
+  app.insert("decode", stdex::make_unique<DecodeCommand>());
+
+  return app.run(argc - 1, argv + 1);
+}