summaryrefslogtreecommitdiff
path: root/runtimes/neurun/frontend/tflite/loader.h
diff options
context:
space:
mode:
authorChunseok Lee <chunseok.lee@samsung.com>2020-03-05 15:10:09 +0900
committerChunseok Lee <chunseok.lee@samsung.com>2020-03-05 15:22:53 +0900
commitd91a039e0eda6fd70dcd22672b8ce1817c1ca50e (patch)
tree62668ec548cf31fadbbf4e99522999ad13434a25 /runtimes/neurun/frontend/tflite/loader.h
parentbd11b24234d7d43dfe05a81c520aa01ffad06e42 (diff)
downloadnnfw-d91a039e0eda6fd70dcd22672b8ce1817c1ca50e.tar.gz
nnfw-d91a039e0eda6fd70dcd22672b8ce1817c1ca50e.tar.bz2
nnfw-d91a039e0eda6fd70dcd22672b8ce1817c1ca50e.zip
catch up to tizen_5.5 and remove unness dir
- update to tizen_5.5 - remove dirs
Diffstat (limited to 'runtimes/neurun/frontend/tflite/loader.h')
-rw-r--r--runtimes/neurun/frontend/tflite/loader.h115
1 files changed, 115 insertions, 0 deletions
diff --git a/runtimes/neurun/frontend/tflite/loader.h b/runtimes/neurun/frontend/tflite/loader.h
new file mode 100644
index 000000000..c398cbc00
--- /dev/null
+++ b/runtimes/neurun/frontend/tflite/loader.h
@@ -0,0 +1,115 @@
+/*
+ * 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 __TFLITE_LOADER_LOADER_H__
+#define __TFLITE_LOADER_LOADER_H__
+
+#include "schema_generated.h"
+
+#include "graph/Graph.h"
+
+#include <fstream>
+#include <map>
+
+namespace tflite_loader
+{
+/**
+ * @brief TFLite model loader
+ */
+class Loader
+{
+public:
+ /**
+ * @brief Construct a new Loader object
+ *
+ * @param graph reference on graph
+ */
+ explicit Loader(neurun::graph::Graph &graph) : _graph(graph){};
+ ~Loader() = default;
+ /**
+ * @brief Load a model from file
+ *
+ * @param file_path
+ */
+ void loadFromFile(const char *file_path);
+ /**
+ * @brief Load a model from buffer
+ *
+ * @param ref Pointers on begin and end of buffer
+ */
+ void loadFromBuffer(std::pair<const char *, const char *> ref);
+
+private:
+ void loadModel();
+ // Helper functions
+ // Load subgraphs
+ void loadSubgraph(const tflite::SubGraph *subgraph);
+ // Load data from buffer to tensor on index
+ void loadConstantTensor(const tflite::Buffer *buffer, const uint32_t &index);
+ // Create operands form tflite::Tensor
+ void loadOperand(const tflite::Tensor *tensor);
+ // Create operations from tflite::Operator
+ void loadOperation(const tflite::Operator *op);
+ // Load Strides and Paddings from options to param
+ template <typename Param, typename OptionsType>
+ void loadStridesAndPaddings(Param &param, const OptionsType *options);
+ // Load Pool2D param
+ template <typename Param> void loadPool2D(Param &param, const tflite::Pool2DOptions *options);
+ // Create new Operand from Shape and TypeInfo
+ template <typename T>
+ neurun::model::OperandIndex createOperand(const uint8_t *ptr, const neurun::model::Shape &shape,
+ const neurun::model::TypeInfo &type_info);
+
+ // Operations
+ void loadConv2D(const tflite::Operator *op);
+ void loadDepthwiseConv2D(const tflite::Operator *op);
+ void loadTransposeConv(const tflite::Operator *op);
+ void loadAvgPool2D(const tflite::Operator *op);
+ void loadReshape(const tflite::Operator *op);
+ void loadSoftmax(const tflite::Operator *op);
+ void loadMaxPool2D(const tflite::Operator *op);
+ void loadConcatenation(const tflite::Operator *op);
+ void loadFC(const tflite::Operator *op);
+ void loadAdd(const tflite::Operator *op);
+ void loadSub(const tflite::Operator *op);
+ void loadMul(const tflite::Operator *op);
+ void loadDiv(const tflite::Operator *op);
+ void loadRelu(const tflite::Operator *op);
+ void loadRelu6(const tflite::Operator *op);
+ void loadRsqrt(const tflite::Operator *op);
+ void loadSqrt(const tflite::Operator *op);
+ void loadSquaredDifference(const tflite::Operator *op);
+ void loadTanh(const tflite::Operator *op);
+ void loadTranspose(const tflite::Operator *op);
+ void loadMean(const tflite::Operator *op);
+
+ void loadCustom(const tflite::Operator *op);
+
+private:
+ // Buffer for loading (if needed)
+ std::vector<char> _buffer;
+ // Reference on loadable Graph
+ neurun::graph::Graph &_graph;
+ // Mapping from tflite tensor index to Graph OperandIndex
+ std::map<uint32_t, neurun::model::OperandIndex> _tensor_to_operand;
+ // Mapping from operator code to BuiltinOperator
+ std::vector<tflite::BuiltinOperator> _op_code_to_builtin_op;
+ std::unordered_map<uint32_t, std::string> _opcode_index_to_custom_opcode;
+};
+
+} // namespace tflite_loader
+
+#endif //__TFLITE_LOADER_LOADER_H__