From 8b9834e18d0279ed1f259d630697cb6e0b44869b Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/On-Device=20Lab=28SR=29/Staff?= =?utf8?q?=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Thu, 7 Feb 2019 15:28:18 +0900 Subject: [PATCH] [tfkit] Initial commit (#3004) 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 --- contrib/tfkit/.FORMATCHECKED | 0 contrib/tfkit/CMakeLists.txt | 43 ++++++++++++++++++++++++++++++++++ contrib/tfkit/src/DecodeCommand.cpp | 46 +++++++++++++++++++++++++++++++++++++ contrib/tfkit/src/DecodeCommand.hpp | 27 ++++++++++++++++++++++ contrib/tfkit/src/Main.cpp | 29 +++++++++++++++++++++++ 5 files changed, 145 insertions(+) create mode 100644 contrib/tfkit/.FORMATCHECKED create mode 100644 contrib/tfkit/CMakeLists.txt create mode 100644 contrib/tfkit/src/DecodeCommand.cpp create mode 100644 contrib/tfkit/src/DecodeCommand.hpp create mode 100644 contrib/tfkit/src/Main.cpp diff --git a/contrib/tfkit/.FORMATCHECKED b/contrib/tfkit/.FORMATCHECKED new file mode 100644 index 0000000..e69de29 diff --git a/contrib/tfkit/CMakeLists.txt b/contrib/tfkit/CMakeLists.txt new file mode 100644 index 0000000..4ea9eb6 --- /dev/null +++ b/contrib/tfkit/CMakeLists.txt @@ -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 index 0000000..878a6d8 --- /dev/null +++ b/contrib/tfkit/src/DecodeCommand.cpp @@ -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 + +#include +#include +#include + +#include + +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 index 0000000..023791b --- /dev/null +++ b/contrib/tfkit/src/DecodeCommand.hpp @@ -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 + +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 index 0000000..0e1566c --- /dev/null +++ b/contrib/tfkit/src/Main.cpp @@ -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 +#include + +int main(int argc, char **argv) +{ + cli::App app{argv[0]}; + + app.insert("decode", stdex::make_unique()); + + return app.run(argc - 1, argv + 1); +} -- 2.7.4