From 9c30462c4c5ed67e61ad41b4a0ae5cc66650511f 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 11:01:50 +0900 Subject: [PATCH] [nnkit] Construct CaffeBackend from caffe::Net (#967) * [nnkit] Construct CaffeBackend from caffe::Net Currently, the constructor of CaffeBackend takes arguments for caffe::Net, and forwards it to internally allocated caffe::Net instance. This commit revises CaffeBackend constructor to take an exteranlly allocated caffe::Net instance as its argument. This change allows us to construct a caffe backend from in-memory caffe model (instead of serialized one). Signed-off-by: Jonghyun Park * Remove unnecessary comment --- contrib/nnkit/backends/caffe/Module.cpp | 29 ++++++++++++++--------------- 1 file changed, 14 insertions(+), 15 deletions(-) diff --git a/contrib/nnkit/backends/caffe/Module.cpp b/contrib/nnkit/backends/caffe/Module.cpp index c8b9218..7ce5002 100644 --- a/contrib/nnkit/backends/caffe/Module.cpp +++ b/contrib/nnkit/backends/caffe/Module.cpp @@ -103,47 +103,43 @@ private: #include +#include + namespace { class FloatCaffeBackend final : public nnkit::Backend { public: - FloatCaffeBackend(const std::string &prototxt) : _net{prototxt, caffe::TEST} + FloatCaffeBackend(std::unique_ptr> &&net) : _net{std::move(net)} { // DO NOTHING } public: - FloatCaffeBackend(const std::string &prototxt, const std::string &caffemodel) : _net{prototxt, caffe::TEST} - { - _net.CopyTrainedLayersFrom(caffemodel); - } - -public: void prepare(const std::function &f) override; void run(void) override; void teardown(const std::function &f) override; private: - caffe::Net _net; + std::unique_ptr> _net; }; void FloatCaffeBackend::prepare(const std::function &f) { - InputBlobContext blobs(_net); + InputBlobContext blobs(*_net); CaffeTensorContext tensors(blobs); f(tensors); } void FloatCaffeBackend::run(void) { - _net.Forward(); + _net->Forward(); } void FloatCaffeBackend::teardown(const std::function &f) { - OutputBlobContext blobs(_net); + OutputBlobContext blobs(*_net); CaffeTensorContext tensors(blobs); f(tensors); } @@ -155,11 +151,14 @@ void FloatCaffeBackend::teardown(const std::function make_backend(const nnkit::CmdlineArguments &args) { - if (args.size() == 1) + using nncc::foundation::make_unique; + + auto net = make_unique<::caffe::Net>(args.at(0), caffe::TEST); + + if (args.size() > 1) { - // TODO Select Float/Double based on command-line arguments - return nncc::foundation::make_unique(args.at(0)); + net->CopyTrainedLayersFrom(args.at(1)); } - return nncc::foundation::make_unique(args.at(0), args.at(1)); + return nncc::foundation::make_unique(std::move(net)); } -- 2.7.4