[preprocess flip layer] Maintain layer property with props
authorhyeonseok lee <hs89.lee@samsung.com>
Wed, 8 Sep 2021 03:46:07 +0000 (12:46 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 13 Sep 2021 11:59:57 +0000 (20:59 +0900)
 - All the preprocess flip layer property will be maintain with props
 - Remove default flip direction value which was horizontal_and_vertical

**Self evaluation:**
1. Build test: [X]Passed [ ]Failed [ ]Skipped
2. Run test: [X]Passed [ ]Failed [ ]Skipped

Signed-off-by: hyeonseok lee <hs89.lee@samsung.com>
nntrainer/layers/common_properties.h
nntrainer/layers/preprocess_flip_layer.cpp
nntrainer/layers/preprocess_flip_layer.h

index 574ff6b..33e8435 100644 (file)
@@ -486,6 +486,29 @@ public:
   using prop_tag = enum_class_prop_tag;
   static constexpr const char *key = "activation";
 };
+
+/**
+ * @brief     Enumeration of flip direction
+ */
+struct FlipDirectionInfo {
+  enum class Enum { horizontal, vertical, horizontal_and_vertical };
+  static constexpr std::initializer_list<Enum> EnumList = {
+    Enum::horizontal, Enum::vertical, Enum::horizontal_and_vertical};
+
+  static constexpr const char *EnumStr[] = {"horizontal", "vertical",
+                                            "horizontal_and_vertical"};
+};
+
+/**
+ * @brief FlipDirection Enumeration Information
+ *
+ */
+class FlipDirection final : public EnumProperty<FlipDirectionInfo> {
+public:
+  using prop_tag = enum_class_prop_tag;
+  static constexpr const char *key = "flip_direction";
+};
+
 } // namespace props
 } // namespace nntrainer
 
index 6408df5..35b08c9 100644 (file)
 
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
+#include <node_exporter.h>
 #include <parse_util.h>
 #include <preprocess_flip_layer.h>
 #include <util_func.h>
 
 namespace nntrainer {
 
-const std::string PreprocessFlipLayer::flip_horizontal = "horizontal";
-const std::string PreprocessFlipLayer::flip_vertical = "vertical";
-const std::string PreprocessFlipLayer::flip_horizontal_vertical =
-  "horizontal_and_vertical";
+PreprocessFlipLayer::PreprocessFlipLayer() :
+  Layer(),
+  preprocess_flip_props(props::FlipDirection()) {}
 
 void PreprocessFlipLayer::finalize(InitLayerContext &context) {
   context.setOutputDimensions(context.getInputDimensions());
@@ -34,53 +34,16 @@ void PreprocessFlipLayer::finalize(InitLayerContext &context) {
 }
 
 void PreprocessFlipLayer::setProperty(const std::vector<std::string> &values) {
-  /// @todo: deprecate this in favor of loadProperties
-  for (unsigned int i = 0; i < values.size(); ++i) {
-    std::string key;
-    std::string value;
-    std::stringstream ss;
-
-    if (getKeyValue(values[i], key, value) != ML_ERROR_NONE) {
-      throw std::invalid_argument("Error parsing the property: " + values[i]);
-    }
-
-    if (value.empty()) {
-      ss << "value is empty: key: " << key << ", value: " << value;
-      throw std::invalid_argument(ss.str());
-    }
-
-    /// @note this calls derived setProperty if available
-    setProperty(key, value);
-  }
-}
-
-void PreprocessFlipLayer::setProperty(const std::string &type_str,
-                                      const std::string &value) {
-  using PropertyType = nntrainer::Layer::PropertyType;
-  nntrainer::Layer::PropertyType type =
-    static_cast<nntrainer::Layer::PropertyType>(parseLayerProperty(type_str));
-
-  switch (type) {
-  case PropertyType::flip_direction: {
-    if (istrequal(value, flip_horizontal)) {
-      flipdirection = FlipDirection::horizontal;
-    } else if (istrequal(value, flip_vertical)) {
-      flipdirection = FlipDirection::vertical;
-    } else if (istrequal(value, flip_horizontal_vertical)) {
-      flipdirection = FlipDirection::horizontal_and_vertical;
-    } else {
-      throw std::invalid_argument("Argument flip direction is invalid");
-    }
-  } break;
-  default:
-    std::string msg =
-      "[PreprocessFlipLayer] Unknown Layer Property Key for value " +
-      std::string(value);
-    throw exception::not_supported(msg);
-  }
+  auto remain_props = loadProperties(values, preprocess_flip_props);
+  NNTR_THROW_IF(!remain_props.empty(), std::invalid_argument)
+    << "[PreprocessFilpLayer] Unknown Layer Properties count " +
+         std::to_string(values.size());
 }
 
 void PreprocessFlipLayer::forwarding(RunLayerContext &context, bool training) {
+  props::FlipDirection flipdirection =
+    std::get<props::FlipDirection>(preprocess_flip_props);
+
   if (!training) {
     for (unsigned int idx = 0; idx < context.getNumInputs(); idx++) {
       /** TODO: tell the graph to not include this when not training */
@@ -102,10 +65,12 @@ void PreprocessFlipLayer::forwarding(RunLayerContext &context, bool training) {
 
     for (unsigned int b = 0; b < input_dim.batch(); b++) {
       fliph = flipw = false;
-      if (flip_dist(rng) < 0.5 && flipdirection != FlipDirection::vertical)
+      if (flip_dist(rng) < 0.5 &&
+          flipdirection != props::FlipDirectionInfo::Enum::vertical)
         flipw = true;
 
-      if (flip_dist(rng) < 0.5 && flipdirection != FlipDirection::horizontal)
+      if (flip_dist(rng) < 0.5 &&
+          flipdirection != props::FlipDirectionInfo::Enum::horizontal)
         fliph = true;
 
       if (!flipw && !fliph)
@@ -136,4 +101,9 @@ void PreprocessFlipLayer::calcDerivative(RunLayerContext &context) {
     "calcDerivative for preprocess layer is not supported");
 }
 
+void PreprocessFlipLayer::exportTo(Exporter &exporter,
+                                   const ExportMethods &method) const {
+  exporter.saveResult(preprocess_flip_props, method, this);
+}
+
 } /* namespace nntrainer */
index ed97d00..aef292f 100644 (file)
@@ -30,9 +30,7 @@ public:
   /**
    * @brief     Constructor of Preprocess FLip Layer
    */
-  PreprocessFlipLayer() :
-    Layer(),
-    flipdirection(FlipDirection::horizontal_and_vertical) {}
+  PreprocessFlipLayer();
 
   /**
    * @brief     Destructor of Preprocess FLip Layer
@@ -74,8 +72,7 @@ public:
   /**
    * @copydoc Layer::exportTo(Exporter &exporter, ExportMethods method)
    */
-  void exportTo(Exporter &exporter,
-                const ExportMethods &method) const override {}
+  void exportTo(Exporter &exporter, const ExportMethods &method) const override;
 
   /**
    * @copydoc Layer::getType()
@@ -92,30 +89,10 @@ public:
   inline static const std::string type = "preprocess_flip";
 
 private:
-  /** String names for the flip direction */
-  static const std::string flip_horizontal;
-  static const std::string flip_vertical;
-  static const std::string flip_horizontal_vertical;
-
-  /**
-   * @brief Direction of the flip for data
-   */
-  enum class FlipDirection { horizontal, vertical, horizontal_and_vertical };
-
   std::mt19937 rng; /**< random number generator */
   std::uniform_real_distribution<float>
-    flip_dist;                 /**< uniform random distribution */
-  FlipDirection flipdirection; /**< direction of flip */
-
-  /**
-   * @brief setProperty by type and value separated
-   * @param[in] type property type to be passed
-   * @param[in] value value to be passed
-   * @exception exception::not_supported     when property type is not valid for
-   * the particular layer
-   * @exception std::invalid_argument invalid argument
-   */
-  void setProperty(const std::string &type, const std::string &value);
+    flip_dist; /**< uniform random distribution */
+  std::tuple<props::FlipDirection> preprocess_flip_props;
 };
 
 } // namespace nntrainer