Add Caffe model console dumper (#536)
authorDmitry Mozolev/AI Tools Lab /SRR/Engineer/삼성전자 <d.mozolev@samsung.com>
Fri, 6 Jul 2018 13:03:30 +0000 (16:03 +0300)
committerSergey Vostokov/AI Tools Lab /SRR/Staff Engineer/삼성전자 <s.vostokov@samsung.com>
Fri, 6 Jul 2018 13:03:30 +0000 (22:03 +0900)
Add Caffe model visiting mechanism

Used for traversing the model and doing something with
objects that it contains.

Signed-off-by: Dmitry Mozolev <d.mozolev@samsung.com>
contrib/nnc/libs/frontend/caffe/include/caffe_dump_visitor.h [new file with mode: 0644]
contrib/nnc/libs/frontend/caffe/src/caffe_dump_visitor.cpp [new file with mode: 0644]

diff --git a/contrib/nnc/libs/frontend/caffe/include/caffe_dump_visitor.h b/contrib/nnc/libs/frontend/caffe/include/caffe_dump_visitor.h
new file mode 100644 (file)
index 0000000..e893717
--- /dev/null
@@ -0,0 +1,29 @@
+#ifndef NNCC_CAFFE_DUMP_VISITOR_H
+#define NNCC_CAFFE_DUMP_VISITOR_H
+
+#include "caffe_visitor.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace caffe
+{
+
+class DumpVisitor : public Visitor
+{
+public:
+    void visit(const NetParameter &parameter) override;
+    void visit(const LayerParameter &parameter) override;
+    void visit(const BlobProto &proto) override;
+    void visit(const BlobShape &shape) override;
+};
+
+} // namespace caffe
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc
+
+#endif //NNCC_CAFFE_DUMP_VISITOR_H
diff --git a/contrib/nnc/libs/frontend/caffe/src/caffe_dump_visitor.cpp b/contrib/nnc/libs/frontend/caffe/src/caffe_dump_visitor.cpp
new file mode 100644 (file)
index 0000000..d56a2fa
--- /dev/null
@@ -0,0 +1,75 @@
+#include <iostream>
+
+#include "caffe_dump_visitor.h"
+
+namespace nncc
+{
+namespace contrib
+{
+namespace frontend
+{
+namespace caffe
+{
+
+using std::cout;
+using std::endl;
+
+std::ostream& operator<<(std::ostream& os, const BlobShape& bs)
+{
+    cout << "[";
+    for (int i = 0; i < bs.dim_size(); ++i)
+    {
+        if (i != 0)
+            cout << ", ";
+        cout << bs.dim(i);
+    }
+    cout << "]";
+
+    return os;
+}
+
+void DumpVisitor::visit(const NetParameter &np)
+{
+    cout << "[Network name]: " << np.name() << endl;
+    cout << "[Network layer_size]: " << np.layer_size() << endl;
+
+    if (np.layers_size() != 0)
+        cout << "[DEPRECATED, Network layers_size]: " << np.layers_size() << endl;
+    if (np.input_size() != 0)
+        cout << "[DEPRECATED, Network input_size]: " << np.input_size() << endl;
+    if (np.input_shape_size() != 0)
+        cout << "[DEPRECATED, Network input_shapes_size]: " << np.input_shape_size() << endl;
+    if (np.input_dim_size() != 0)
+        cout << "[DEPRECATED, Network input_dim_size]: " << np.input_dim_size() << endl;
+}
+
+void DumpVisitor::visit(const LayerParameter &lp)
+{
+    cout << "[" << lp.type() << "]: " << lp.name() << endl;
+
+    for (int i = 0; i < lp.bottom_size(); ++i)
+        cout << "    [Input]: " << lp.bottom(i) << endl;
+
+    for (int i = 0; i < lp.top_size(); ++i)
+        cout << "    [Output]: " << lp.top(i) << endl;;
+}
+
+void DumpVisitor::visit(const BlobProto &bp)
+{
+    if (bp.has_shape())
+    {
+        cout << "    [Blob shape]: ";
+        cout << bp.shape() << endl;
+    }
+}
+
+void DumpVisitor::visit(const BlobShape& bs)
+{
+    cout << "    [Blob shape]: ";
+    cout << bs << endl;
+}
+
+} // namespace caffe
+} // namespace frontend
+} // namespace contrib
+} // namespace nncc