From 8a4376c975b0a2f28bf97f8c6bb394f4d23fc048 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Dmitry=20Mozolev/AI=20Tools=20Lab=20/SRR/Engineer/=EC=82=BC?= =?utf8?q?=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Fri, 6 Jul 2018 14:28:25 +0300 Subject: [PATCH] Add Caffe model visiting mechanism (#535) Add Caffe model visiting mechanism Used for traversing the model and doing something with objects that it contains. Signed-off-by: Dmitry Mozolev --- .../libs/frontend/caffe/include/caffe_visitor.h | 33 ++++++++++++++++++ .../nnc/libs/frontend/caffe/include/caffe_walker.h | 36 +++++++++++++++++++ .../nnc/libs/frontend/caffe/src/caffe_walker.cpp | 40 ++++++++++++++++++++++ 3 files changed, 109 insertions(+) create mode 100644 contrib/nnc/libs/frontend/caffe/include/caffe_visitor.h create mode 100644 contrib/nnc/libs/frontend/caffe/include/caffe_walker.h create mode 100644 contrib/nnc/libs/frontend/caffe/src/caffe_walker.cpp diff --git a/contrib/nnc/libs/frontend/caffe/include/caffe_visitor.h b/contrib/nnc/libs/frontend/caffe/include/caffe_visitor.h new file mode 100644 index 0000000..c02c2a8 --- /dev/null +++ b/contrib/nnc/libs/frontend/caffe/include/caffe_visitor.h @@ -0,0 +1,33 @@ +#ifndef NNCC_CAFFE_VISITOR_H +#define NNCC_CAFFE_VISITOR_H + +#include "caffe/proto/caffe.pb.h" + +namespace nncc +{ +namespace contrib +{ +namespace frontend +{ +namespace caffe +{ + +using namespace ::caffe; + +class Visitor +{ +public: + virtual ~Visitor() = default; + + virtual void visit(const NetParameter&) = 0; + virtual void visit(const LayerParameter&) = 0; + virtual void visit(const BlobProto&) = 0; + virtual void visit(const BlobShape&) = 0; +}; + +} // namespace caffe +} // namespace frontend +} // namespace contrib +} // namespace nncc + +#endif //NNCC_CAFFE_VISITOR_H diff --git a/contrib/nnc/libs/frontend/caffe/include/caffe_walker.h b/contrib/nnc/libs/frontend/caffe/include/caffe_walker.h new file mode 100644 index 0000000..4574a20 --- /dev/null +++ b/contrib/nnc/libs/frontend/caffe/include/caffe_walker.h @@ -0,0 +1,36 @@ +#ifndef NNCC_CAFFE_WALKER_H +#define NNCC_CAFFE_WALKER_H + +#include "caffe/proto/caffe.pb.h" + +#include "caffe_visitor.h" + +namespace nncc +{ +namespace contrib +{ +namespace frontend +{ +namespace caffe +{ + +using namespace ::caffe; + +class ModelWalker +{ +public: + ModelWalker(Visitor* visitor) : visitor(visitor) {}; + + void walkNetParameter(const NetParameter&); + void walkLayerParameter(const LayerParameter&); + +private: + Visitor* visitor; +}; + +} // namespace caffe +} // namespace frontend +} // namespace contrib +} // namespace nncc + +#endif // NNCC_CAFFE_WALKER_H diff --git a/contrib/nnc/libs/frontend/caffe/src/caffe_walker.cpp b/contrib/nnc/libs/frontend/caffe/src/caffe_walker.cpp new file mode 100644 index 0000000..9b89dff --- /dev/null +++ b/contrib/nnc/libs/frontend/caffe/src/caffe_walker.cpp @@ -0,0 +1,40 @@ +#include "caffe_walker.h" + +namespace nncc +{ +namespace contrib +{ +namespace frontend +{ +namespace caffe +{ + +void ModelWalker::walkNetParameter(const NetParameter& np) +{ + visitor->visit(np); + + for (int i = 0; i < np.layer_size(); ++i) + { + walkLayerParameter(np.layer(i)); + } + + for (int i = 0; i < np.input_shape_size(); ++i) + { + visitor->visit(np.input_shape(i)); + } +} + +void ModelWalker::walkLayerParameter(const LayerParameter& lp) +{ + visitor->visit(lp); + + for (int i = 0; i < lp.blobs_size(); ++i) + { + visitor->visit(lp.blobs(i)); + } +} + +} // namespace caffe +} // namespace frontend +} // namespace contrib +} // namespace nncc -- 2.7.4