ONE - On-device Neural Engine
Loading...
Searching...
No Matches
ConvBackend.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2018 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "ConvBackend.h"
18
19#include <caffe/proto/caffe.pb.h>
20
22
25
26#include <memory>
27
28using std::make_unique;
29
30std::unique_ptr<nnkit::Backend> ConvBackend::create(const nnsuite::conv::Model &model)
31{
32 ::caffe::NetParameter param;
33
34 param.set_name("conv");
35
36 // Create 'Input' layer
37 {
38 auto input = param.add_layer();
39 input->set_name("input");
40 input->set_type("Input");
41 input->add_top(model.ifm_name());
42
43 auto input_param = new ::caffe::InputParameter{};
44 auto input_shape = input_param->add_shape();
45 input_shape->add_dim(1);
46 input_shape->add_dim(model.ifm_shape().depth());
47 input_shape->add_dim(model.ifm_shape().height());
48 input_shape->add_dim(model.ifm_shape().width());
49 input->set_allocated_input_param(input_param);
50 }
51
52 // Create 'Convolution' layer
53 {
54 auto conv = param.add_layer();
55 conv->set_name("conv");
56 conv->set_type("Convolution");
57 conv->add_bottom(model.ifm_name());
58 conv->add_top(model.ofm_name());
59
60 const auto &ker_shape = model.ker_shape();
61
62 auto ker_blob_shape = new ::caffe::BlobShape{};
63
64 ker_blob_shape->add_dim(ker_shape.count());
65 ker_blob_shape->add_dim(ker_shape.depth());
66 ker_blob_shape->add_dim(ker_shape.height());
67 ker_blob_shape->add_dim(ker_shape.width());
68
69 auto ker_blob = conv->add_blobs();
70
71 for (uint32_t n = 0; n < ker_shape.count(); ++n)
72 {
73 for (uint32_t ch = 0; ch < ker_shape.depth(); ++ch)
74 {
75 for (uint32_t row = 0; row < ker_shape.height(); ++row)
76 {
77 for (uint32_t col = 0; col < ker_shape.width(); ++col)
78 {
79 ker_blob->add_data(model.ker_data().at(n, ch, row, col));
80 }
81 }
82 }
83 }
84
85 ker_blob->set_allocated_shape(ker_blob_shape);
86
87 auto conv_param = new ::caffe::ConvolutionParameter{};
88 conv_param->set_num_output(model.ker_shape().count());
89 conv_param->set_bias_term(false);
90 conv_param->add_kernel_size(model.ker_shape().height());
91 conv_param->add_kernel_size(model.ker_shape().width());
92 conv->set_allocated_convolution_param(conv_param);
93 }
94
95 auto net = make_unique<::caffe::Net<float>>(param);
96 return make_unique<nnkit::support::caffe::Backend<float>>(std::move(net));
97}
static std::unique_ptr< nnkit::Backend > create(const nnsuite::conv::Model &)