From 661367abc555198ced86b0f9761807f37c6c43d8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=EC=9C=A4=ED=98=84=EC=8B=9D/On-Device=20Lab=28SR=29/Princip?= =?utf8?q?al=20Engineer/=EC=82=BC=EC=84=B1=EC=A0=84=EC=9E=90?= Date: Tue, 15 Jan 2019 15:15:05 +0900 Subject: [PATCH] [tflchef] tlchef for FullyConnected (#2852) * [tflchef] tlchef for FullyConnected This commit adds code to convert recipe for FullyConnected to tflite file. Signed-off-by: Hyun Sik Yoon * alphabetical order * remove comment about alphabetical order --- contrib/tflchef/core/src/Op/FullyConnected.cpp | 39 ++++++++++++++++++ contrib/tflchef/core/src/Op/FullyConnected.h | 52 ++++++++++++++++++++++++ contrib/tflchef/core/src/OpChef.def | 1 + contrib/tflchef/core/src/OpChefs.h | 1 + contrib/tflchef/proto/tflchef.proto | 5 +++ contrib/tflchef/tests/fullyconnected/test.recipe | 34 ++++++++++++++++ 6 files changed, 132 insertions(+) create mode 100644 contrib/tflchef/core/src/Op/FullyConnected.cpp create mode 100644 contrib/tflchef/core/src/Op/FullyConnected.h create mode 100644 contrib/tflchef/tests/fullyconnected/test.recipe diff --git a/contrib/tflchef/core/src/Op/FullyConnected.cpp b/contrib/tflchef/core/src/Op/FullyConnected.cpp new file mode 100644 index 0000000..4526991 --- /dev/null +++ b/contrib/tflchef/core/src/Op/FullyConnected.cpp @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "FullyConnected.h" +#include "Convert.h" + +#include + +flatbuffers::Offset FullyConnectedChef::value(flatbuffers::FlatBufferBuilder &fbb) const +{ + auto &operation = (*_operation); + + assert(operation.has_fullyconnected_options()); + + auto tflite_activation = as_tflite_activation(operation.fullyconnected_options().activation()); + + tflite::FullyConnectedOptionsBuilder fc_options_builder{fbb}; + fc_options_builder.add_fused_activation_function(tflite_activation); + + return fc_options_builder.Finish().Union(); +} + +std::unique_ptr FullyConnectedChefFactory::create(const tflchef::Operation *operation) const +{ + return std::unique_ptr{new FullyConnectedChef{operation}}; +} diff --git a/contrib/tflchef/core/src/Op/FullyConnected.h b/contrib/tflchef/core/src/Op/FullyConnected.h new file mode 100644 index 0000000..ea71012 --- /dev/null +++ b/contrib/tflchef/core/src/Op/FullyConnected.h @@ -0,0 +1,52 @@ +/* + * Copyright (c) 2019 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __OP_FULLYCONNECTED_H__ +#define __OP_FULLYCONNECTED_H__ + +#include "OpChef.h" + +class FullyConnectedChef final : public OpChef +{ +public: + explicit FullyConnectedChef(const tflchef::Operation *operation) : _operation{operation} + { + // DO NOTHING + } + +public: + tflite::BuiltinOperator code(void) const override + { + return tflite::BuiltinOperator_FULLY_CONNECTED; + } + + tflite::BuiltinOptions type(void) const override + { + return tflite::BuiltinOptions_FullyConnectedOptions; + } + + flatbuffers::Offset value(flatbuffers::FlatBufferBuilder &fbb) const override; + +private: + const tflchef::Operation *_operation; +}; + +struct FullyConnectedChefFactory final : public OpChefFactory +{ + std::unique_ptr create(const tflchef::Operation *operation) const override; +}; + +#endif // __OP_FULLYCONNECTED_H__ diff --git a/contrib/tflchef/core/src/OpChef.def b/contrib/tflchef/core/src/OpChef.def index 3310e45..0b9ea73 100644 --- a/contrib/tflchef/core/src/OpChef.def +++ b/contrib/tflchef/core/src/OpChef.def @@ -9,6 +9,7 @@ OP_CHEF(Concatenation, ConcatenationChefFactory) OP_CHEF(Conv2D, Conv2DChefFactory) OP_CHEF(DepthwiseConv2D, DepthwiseConv2DChefFactory) OP_CHEF(Div, DivChefFactory) +OP_CHEF(FullyConnected, FullyConnectedChefFactory) OP_CHEF(MaxPool2D, MaxPool2DChefFactory) OP_CHEF(ReLU, ReLUChefFactory) OP_CHEF(ReLU6, ReLU6ChefFactory) diff --git a/contrib/tflchef/core/src/OpChefs.h b/contrib/tflchef/core/src/OpChefs.h index 4e42163..933697a 100644 --- a/contrib/tflchef/core/src/OpChefs.h +++ b/contrib/tflchef/core/src/OpChefs.h @@ -22,6 +22,7 @@ #include "Op/Conv2D.h" #include "Op/DepthwiseConv2D.h" #include "Op/Div.h" +#include "Op/FullyConnected.h" #include "Op/MaxPool2D.h" #include "Op/ReLU.h" #include "Op/ReLU6.h" diff --git a/contrib/tflchef/proto/tflchef.proto b/contrib/tflchef/proto/tflchef.proto index 512a350..dc63f0c 100644 --- a/contrib/tflchef/proto/tflchef.proto +++ b/contrib/tflchef/proto/tflchef.proto @@ -88,6 +88,10 @@ message DivOptions { optional Activation activation = 1 [default = NONE]; } +message FullyConnectedOptions { + optional Activation activation = 1 [default = NONE]; +} + message Operation { optional string type = 1; repeated string input = 2; @@ -101,6 +105,7 @@ message Operation { optional DepthwiseConv2DOptions depthwiseconv2d_options = 105; optional SubOptions sub_options = 106; optional DivOptions div_options = 107; + optional FullyConnectedOptions fullyconnected_options = 108; } message ModelRecipe { diff --git a/contrib/tflchef/tests/fullyconnected/test.recipe b/contrib/tflchef/tests/fullyconnected/test.recipe new file mode 100644 index 0000000..dca4c09 --- /dev/null +++ b/contrib/tflchef/tests/fullyconnected/test.recipe @@ -0,0 +1,34 @@ +operand { + name: "in" + type: FLOAT32 + shape { dim: 1 dim: 64 } +} +operand { + name: "weight" + type: FLOAT32 + shape { dim: 8 dim: 64 } +} +operand { + name: "bias" + type: FLOAT32 + shape { dim: 8 } +} +operand { + name: "out" + type: FLOAT32 + shape { dim: 1 dim: 8 } +} +operation { + type: "FullyConnected" + fullyconnected_options { + activation: NONE + } + input: "in" + input: "weight" + input: "bias" + output: "out" +} +input: "in" +input: "weight" +input: "bias" +output: "out" -- 2.7.4