From ec6dedfc318c4a46894c608f16ee27d3eab45acb Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EB=B0=95=EC=A2=85=ED=98=84/=EB=8F=99=EC=9E=91=EC=A0=9C?= =?utf8?q?=EC=96=B4Lab=28SR=29/Staff=20Engineer/=EC=82=BC=EC=84=B1?= =?utf8?q?=EC=A0=84=EC=9E=90?= Date: Tue, 14 Aug 2018 15:48:19 +0900 Subject: [PATCH] [nnkit] Extract Caffe backend implementation (#997) This commit extracts Caffe backend class from nnkit Caffe module. Signed-off-by: Jonghyun Park --- contrib/nnkit/backends/caffe/Module.cpp | 59 +-------------------- .../caffe/include/nnkit/support/caffe/Backend.h | 60 ++++++++++++++++++++++ 2 files changed, 62 insertions(+), 57 deletions(-) create mode 100644 contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/Backend.h diff --git a/contrib/nnkit/backends/caffe/Module.cpp b/contrib/nnkit/backends/caffe/Module.cpp index f8f9782..5c12e0f 100644 --- a/contrib/nnkit/backends/caffe/Module.cpp +++ b/contrib/nnkit/backends/caffe/Module.cpp @@ -1,59 +1,4 @@ -#include "nnkit/support/caffe/BlobContext.h" -#include "nnkit/support/caffe/InputBlobContext.h" -#include "nnkit/support/caffe/OutputBlobContext.h" -#include "nnkit/support/caffe/TensorContext.h" - -#include - -using namespace nnkit::support::caffe; - -#include - -#include - -namespace -{ - -template class CaffeBackend final : public nnkit::Backend -{ -public: - CaffeBackend(std::unique_ptr> &&net) : _net{std::move(net)} - { - // DO NOTHING - } - -public: - void prepare(const std::function &f) override; - void run(void) override; - void teardown(const std::function &f) override; - -private: - std::unique_ptr> _net; -}; - -template -void CaffeBackend::prepare(const std::function &f) -{ - InputBlobContext blobs(*_net); - TensorContext tensors(blobs); - f(tensors); -} - -template -void CaffeBackend::run(void) -{ - _net->Forward(); -} - -template -void CaffeBackend::teardown(const std::function &f) -{ - OutputBlobContext blobs(*_net); - TensorContext tensors(blobs); - f(tensors); -} - -} // namespace +#include "nnkit/support/caffe/Backend.h" #include #include @@ -69,5 +14,5 @@ extern "C" std::unique_ptr make_backend(const nnkit::CmdlineArgu net->CopyTrainedLayersFrom(args.at(1)); } - return nncc::foundation::make_unique>(std::move(net)); + return nncc::foundation::make_unique<::nnkit::support::caffe::Backend>(std::move(net)); } diff --git a/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/Backend.h b/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/Backend.h new file mode 100644 index 0000000..a84107e --- /dev/null +++ b/contrib/nnkit/libs/support/caffe/include/nnkit/support/caffe/Backend.h @@ -0,0 +1,60 @@ +#ifndef __NNKIT_SUPPORT_CAFFE_BACKEND_H__ +#define __NNKIT_SUPPORT_CAFFE_BACKEND_H__ + +#include "nnkit/support/caffe/InputBlobContext.h" +#include "nnkit/support/caffe/OutputBlobContext.h" +#include "nnkit/support/caffe/TensorContext.h" + +#include + +#include + +#include +#include + +namespace nnkit +{ +namespace support +{ +namespace caffe +{ + +template class Backend final : public nnkit::Backend +{ +public: + Backend(std::unique_ptr<::caffe::Net> &&net) : _net{std::move(net)} + { + // DO NOTHING + } + +public: + void prepare(const std::function &f) override + { + InputBlobContext blobs(*_net); + TensorContext tensors(blobs); + f(tensors); + } + +public: + void run(void) override + { + _net->Forward(); + } + +public: + void teardown(const std::function &f) override + { + OutputBlobContext blobs(*_net); + TensorContext tensors(blobs); + f(tensors); + } + +private: + std::unique_ptr<::caffe::Net> _net; +}; + +} // namespace caffe +} // namespace support +} // namespace nnkit + +#endif // __NNKIT_SUPPORT_CAFFE_BACKEND_H__ -- 2.7.4