[tflchef] Support Reshape (#2268)
author박세희/동작제어Lab(SR)/Principal Engineer/삼성전자 <saehie.park@samsung.com>
Wed, 14 Nov 2018 08:11:02 +0000 (17:11 +0900)
committer박종현/동작제어Lab(SR)/Staff Engineer/삼성전자 <jh1302.park@samsung.com>
Wed, 14 Nov 2018 08:11:02 +0000 (17:11 +0900)
This will extend to support Reshape operator

Signed-off-by: SaeHie Park <saehie.park@samsung.com>
contrib/tflchef/core/src/Op/Reshape.cpp [new file with mode: 0644]
contrib/tflchef/core/src/Op/Reshape.h [new file with mode: 0644]
contrib/tflchef/core/src/OpChef.def
contrib/tflchef/core/src/OpChefs.h
contrib/tflchef/proto/tflchef.proto
contrib/tflchef/tests/reshape/test.recipe [new file with mode: 0644]

diff --git a/contrib/tflchef/core/src/Op/Reshape.cpp b/contrib/tflchef/core/src/Op/Reshape.cpp
new file mode 100644 (file)
index 0000000..99555e8
--- /dev/null
@@ -0,0 +1,62 @@
+/*
+ * Copyright (c) 2018 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 "Reshape.h"
+#include "Convert.h"
+
+#include <cassert>
+#include <vector>
+
+namespace
+{
+
+std::vector<int32_t> vector_new_shape(const tflchef::ReshapeOptions &options)
+{
+  std::vector<int32_t> shapes;
+
+  for (int i = 0; i < options.new_shape_size(); ++i)
+  {
+    shapes.push_back(options.new_shape(i));
+  }
+
+  return shapes;
+}
+
+} // namespace
+
+flatbuffers::Offset<void> ReshapeChef::value(flatbuffers::FlatBufferBuilder &fbb) const
+{
+  auto &operation = (*_operation);
+
+  assert(operation.has_reshape_options());
+
+  auto options = operation.reshape_options();
+  auto shapes = vector_new_shape(options);
+  // Note: 'CreateVector' should be placed before 'options_builder'
+  //       Read flatbuffers.h 'void NotNested()' for more information
+  auto fb_new_shape = fbb.CreateVector(shapes);
+
+  tflite::ReshapeOptionsBuilder options_builder{fbb};
+
+  options_builder.add_new_shape(fb_new_shape);
+
+  return options_builder.Finish().Union();
+}
+
+std::unique_ptr<OpChef> ReshapeChefFactory::create(const tflchef::Operation *operation) const
+{
+  return std::unique_ptr<OpChef>{new ReshapeChef{operation}};
+}
diff --git a/contrib/tflchef/core/src/Op/Reshape.h b/contrib/tflchef/core/src/Op/Reshape.h
new file mode 100644 (file)
index 0000000..78e91da
--- /dev/null
@@ -0,0 +1,46 @@
+/*
+ * Copyright (c) 2018 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_RESHAPE_H__
+#define __OP_RESHAPE_H__
+
+#include "OpChef.h"
+
+class ReshapeChef final : public OpChef
+{
+public:
+  explicit ReshapeChef(const tflchef::Operation *operation) : _operation{operation}
+  {
+    // DO NOTHING
+  }
+
+public:
+  tflite::BuiltinOperator code(void) const override { return tflite::BuiltinOperator_RESHAPE; }
+
+  tflite::BuiltinOptions type(void) const override { return tflite::BuiltinOptions_ReshapeOptions; }
+
+  flatbuffers::Offset<void> value(flatbuffers::FlatBufferBuilder &fbb) const override;
+
+private:
+  const tflchef::Operation *_operation;
+};
+
+struct ReshapeChefFactory final : public OpChefFactory
+{
+  std::unique_ptr<OpChef> create(const tflchef::Operation *operation) const override;
+};
+
+#endif // __OP_RESHAPE_H__
index 02c56a2..2c371d4 100644 (file)
@@ -9,3 +9,4 @@ OP_CHEF(Concatenation, ConcatenationChefFactory)
 OP_CHEF(Conv2D, Conv2DChefFactory)
 OP_CHEF(MaxPool2D, MaxPool2DChefFactory)
 OP_CHEF(ReLU6, ReLU6ChefFactory)
+OP_CHEF(Reshape, ReshapeChefFactory)
index 0b55e94..82cccc3 100644 (file)
@@ -22,5 +22,6 @@
 #include "Op/Conv2D.h"
 #include "Op/MaxPool2D.h"
 #include "Op/ReLU6.h"
+#include "Op/Reshape.h"
 
 #endif // __OP_CHEFS_H__
index ac0113e..e60d2e7 100644 (file)
@@ -58,6 +58,10 @@ message ConcatenationOptions {
   optional Activation activation = 2 [default = NONE];
 }
 
+message ReshapeOptions {
+  repeated int32 new_shape = 1;
+}
+
 message Operation {
   optional string type = 1;
   repeated string input = 2;
@@ -67,6 +71,7 @@ message Operation {
   optional Pool2DOptions averagepool2d_options = 101;
   optional ConcatenationOptions concatenation_options = 102;
   optional Pool2DOptions maxpool2d_options = 103;
+  optional ReshapeOptions reshape_options = 104;
 }
 
 message ModelRecipe {
diff --git a/contrib/tflchef/tests/reshape/test.recipe b/contrib/tflchef/tests/reshape/test.recipe
new file mode 100644 (file)
index 0000000..cdca589
--- /dev/null
@@ -0,0 +1,20 @@
+operand {
+  name: "ifm"
+  type: FLOAT32
+  shape { dim: 1 dim: 1 dim: 1 dim: 10 }
+}
+operand {
+  name: "ofm"
+  type: FLOAT32
+  shape { dim: 10 }
+}
+operation {
+  type: "Reshape"
+  reshape_options {
+    new_shape: 10
+  }
+  input: "ifm"
+  output: "ofm"
+}
+input: "ifm"
+output: "ofm"