[caffegen] Add 'decode' command (#148)
author박종현/동작제어Lab(SR)/Senior Engineer/삼성전자 <jh1302.park@samsung.com>
Thu, 26 Apr 2018 00:41:48 +0000 (09:41 +0900)
committerGitHub Enterprise <noreply-CODE@samsung.com>
Thu, 26 Apr 2018 00:41:48 +0000 (09:41 +0900)
This commit supports 'decode' command in caffegen tool which takes
binary caffemodel via standard input, and shows its content in text
format via standard output.

Signed-off-by: Jonghyun Park <jh1302.park@samsung.com>
contrib/caffegen/src/DecodeCommand.cpp [new file with mode: 0644]
contrib/caffegen/src/DecodeCommand.h [new file with mode: 0644]
contrib/caffegen/src/Driver.cpp

diff --git a/contrib/caffegen/src/DecodeCommand.cpp b/contrib/caffegen/src/DecodeCommand.cpp
new file mode 100644 (file)
index 0000000..abd62c3
--- /dev/null
@@ -0,0 +1,30 @@
+#include "DecodeCommand.h"
+
+#include <caffe.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, char **) const
+{
+  caffe::NetParameter param;
+
+  // Load binary from standard input
+  google::protobuf::io::FileInputStream is{0};
+  google::protobuf::io::CodedInputStream coded_is{&is};
+
+  if (!param.ParseFromCodedStream(&coded_is))
+  {
+    std::cerr << "ERROR: Failed to parse caffemodel" << std::endl;
+    return 255;
+  }
+
+  // Write text into standard input
+  google::protobuf::io::FileOutputStream os{1};
+  google::protobuf::TextFormat::Print(param, &os);
+
+  return 0;
+}
diff --git a/contrib/caffegen/src/DecodeCommand.h b/contrib/caffegen/src/DecodeCommand.h
new file mode 100644 (file)
index 0000000..1fea0c4
--- /dev/null
@@ -0,0 +1,11 @@
+#ifndef __DECODE_COMMAND_H__
+#define __DECODE_COMMAND_H__
+
+#include "Command.h"
+
+struct DecodeCommand final : public Command
+{
+  int run(int argc, char **argv) const override;
+};
+
+#endif // __DECODE_COMMAND_H__
index 76f7344..5581f1a 100644 (file)
@@ -1,5 +1,6 @@
 #include "FillCommand.h"
 #include "EncodeCommand.h"
+#include "DecodeCommand.h"
 
 #include <nncc/foundation/Memory.h>
 
@@ -12,6 +13,7 @@ int main(int argc, char **argv)
 
   commands["fill"] = nncc::foundation::make_unique<FillCommand>();
   commands["encode"] = nncc::foundation::make_unique<EncodeCommand>();
+  commands["decode"] = nncc::foundation::make_unique<DecodeCommand>();
 
   return commands.at(argv[1])->run(argc - 2, argv + 2);
 }