Introduce graphdump tool (#43)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 8 Mar 2018 00:57:23 +0000 (09:57 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 8 Mar 2018 00:57:23 +0000 (09:57 +0900)
This commit introduces graphdump tool that shows the content of saved
models(.pb) in text.

Note that decode.sh under tools/graphdef already allows us to dump
the content of saved models, but this graphdump is introduced to test
tensorflow_graphdef library.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
externals/CMakeLists.txt
tools/CMakeLists.txt
tools/graphdump/CMakeLists.txt [new file with mode: 0644]
tools/graphdump/src/graphdump.cc [new file with mode: 0644]

index b383236..cb946d9 100644 (file)
@@ -19,6 +19,10 @@ set(TENSORFLOW_LITE_BASE ${TENSORFLOW_BASE}/tensorflow/contrib/lite)
 # TensorFlow Graph Definition Accessor
 #
 if(ENABLE_TENSORFLOW_GRAPHDEF)
+  include(FindProtobuf)
+
+  find_package(Protobuf REQUIRED)
+
   list(APPEND TF_GRAPH_PROTO_TAGS tensorflow/core/framework/versions)
   list(APPEND TF_GRAPH_PROTO_TAGS tensorflow/core/framework/function)
   list(APPEND TF_GRAPH_PROTO_TAGS tensorflow/core/framework/types)
@@ -41,7 +45,8 @@ if(ENABLE_TENSORFLOW_GRAPHDEF)
   endforeach()
 
   add_library(tensorflow_graphdef ${TF_GRAPH_PROTO_SRCS})
-  target_include_directories(tensorflow_graphdef PUBLIC ${TF_GRAPH_PROTO_GENERATED})
+  target_include_directories(tensorflow_graphdef PUBLIC ${TF_GRAPH_PROTO_GENERATED} ${PROTOBUF_INCLUDE_DIRS})
+  target_link_libraries(tensorflow_graphdef PUBLIC ${PROTOBUF_LIBRARIES})
 endif(ENABLE_TENSORFLOW_GRAPHDEF)
 
 #
index 1abaede..ab88628 100644 (file)
@@ -1,2 +1,3 @@
+add_subdirectory(graphdump)
 add_subdirectory(tflite_run)
 add_subdirectory(nnapi_bindings)
diff --git a/tools/graphdump/CMakeLists.txt b/tools/graphdump/CMakeLists.txt
new file mode 100644 (file)
index 0000000..ebb6a4f
--- /dev/null
@@ -0,0 +1,7 @@
+# NOTE Graph dump uses libtensorflow_graphdef
+if(ENABLE_TENSORFLOW_GRAPHDEF)
+  list(APPEND SRCS "src/graphdump.cc")
+
+  add_executable(graphdump ${SRCS})
+  target_link_libraries(graphdump tensorflow_graphdef)
+endif(ENABLE_TENSORFLOW_GRAPHDEF)
diff --git a/tools/graphdump/src/graphdump.cc b/tools/graphdump/src/graphdump.cc
new file mode 100644 (file)
index 0000000..f96c813
--- /dev/null
@@ -0,0 +1,40 @@
+#include <google/protobuf/io/coded_stream.h>
+#include <google/protobuf/io/zero_copy_stream_impl.h>
+#include <google/protobuf/text_format.h>
+
+#include <tensorflow/core/framework/graph.pb.h>
+
+#include <iostream>
+
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <unistd.h>
+
+int main(int argc, char **argv)
+{
+  tensorflow::GraphDef graph;
+
+  bool loaded = false;
+  {
+    int fd = open(argv[1], O_RDONLY);
+    google::protobuf::io::FileInputStream raw(fd);
+    google::protobuf::io::CodedInputStream in(&raw);
+
+    loaded = graph.ParseFromCodedStream(&in);
+
+    if (!loaded)
+    {
+      std::cerr << "ERROR: ParseFromCodedStream failed" << std::endl;
+    }
+
+    close(fd);
+  }
+
+  if (loaded)
+  {
+    google::protobuf::io::FileOutputStream output(1);
+    google::protobuf::TextFormat::Print(graph, &output);
+  }
+  return 0;
+}