From abfc76e19289d350f841ce8a1234f1927c5ec2b1 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: Fri, 24 Aug 2018 10:12:45 +0900 Subject: [PATCH] [enco] Process Input layer (#1158) This commit revises enco Caffe frontend to support Input layer. Signed-off-by: Jonghyun Park --- contrib/enco/frontend/caffe/src/Frontend.cpp | 96 +++++++++++++++++++++++++++- 1 file changed, 93 insertions(+), 3 deletions(-) diff --git a/contrib/enco/frontend/caffe/src/Frontend.cpp b/contrib/enco/frontend/caffe/src/Frontend.cpp index cd47acb..1dbeb4d 100644 --- a/contrib/enco/frontend/caffe/src/Frontend.cpp +++ b/contrib/enco/frontend/caffe/src/Frontend.cpp @@ -1,6 +1,42 @@ #include "Frontend.h" +#include +#include + +#include +#include +#include + #include +#include + +#include + +using namespace nncc::core::ADT; + +using tensor::num_elements; +using tensor::LexicalLayout; + +namespace +{ + +tensor::Shape as_tensor_shape(const ::caffe::BlobShape &blob_shape) +{ + const uint32_t rank = blob_shape.dim_size(); + + tensor::Shape res; + + res.resize(rank); + + for (uint32_t axis = 0; axis < rank; ++axis) + { + res.dim(axis) = blob_shape.dim(axis); + } + + return res; +} + +} // namespace Frontend::Frontend() : _prototxt{new ::caffe::NetParameter}, _caffemodel{new ::caffe::NetParameter} { @@ -12,9 +48,63 @@ enco::Bundle Frontend::load(void) const auto m = coco::Module::create(); auto d = coco::Data::create(); - // TODO Remove this restriction - assert(_prototxt->layer_size() == 0); - assert(_caffemodel->layer_size() == 0); + // For inter-layer communication + std::map shape_ctx; + std::map bag_ctx; + + std::set top; + + for (const auto &layer : _prototxt->layer()) + { + assert(layer.has_name()); + assert(layer.has_type()); + + top.clear(); + top.insert(layer.top().begin(), layer.top().end()); + + if (layer.type() == "Input") + { + assert(layer.has_input_param()); + const auto ¶m = layer.input_param(); + + for (uint32_t n = 0; n < layer.top_size(); ++n) + { + const auto &name = layer.top(n); + const auto shape = as_tensor_shape(param.shape(n)); + + auto bag = m->entity()->bag()->create(num_elements(shape)); + auto input = m->entity()->input()->create(shape); + + input->bag(bag); + input->name(name); + input->reorder(); + + m->input()->insert(input); + + bag_ctx[name] = bag; + shape_ctx[name] = shape; + } + } + else + { + throw std::runtime_error{"Not supported: " + layer.type()}; + } + } + + // Finalize: Create output for each top blob + for (const auto &name : top) + { + const auto &shape = shape_ctx.at(name); + auto bag = bag_ctx.at(name); + + auto output = m->entity()->output()->create(shape); + + output->bag(bag); + output->name(name); + output->reorder(); + + m->output()->insert(output); + } enco::Bundle bundle; -- 2.7.4