[input layer] Maintain input layer property with props
authorhyeonseok lee <hs89.lee@samsung.com>
Mon, 6 Sep 2021 05:20:14 +0000 (14:20 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 13 Sep 2021 11:59:57 +0000 (20:59 +0900)
 - All the property of input layer will be maintain with props
 - Remove unused setProperty function of tflite_layer

**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.cpp
nntrainer/layers/common_properties.h
nntrainer/layers/input_layer.cpp
nntrainer/layers/input_layer.h
nntrainer/layers/tflite_layer.h

index b4ce8cc66ed066a5710042aa9c43389bb52ba6cd..4b8627aaf05a1913a79a6b522d7834377a4759fc 100644 (file)
@@ -43,6 +43,10 @@ bool Name::isValid(const std::string &v) const {
   return !v.empty() && std::regex_match(v, allowed);
 }
 
+Normalization::Normalization(bool value) { set(value); }
+
+Standardization::Standardization(bool value) { set(value); }
+
 bool DropOutSpec::isValid(const float &v) const {
   if (v <= 0.0)
     return false;
index 02d6783840b2e6f9c1a3370e831a3b62008ec64f..8ca1b1fc2453863fadf086af7ce7c847e3c25d67 100644 (file)
@@ -102,6 +102,38 @@ public:
   using prop_tag = bool_prop_tag;
 };
 
+/**
+ * @brief Normalization property, normalize the input to be in range [0, 1] if
+ * true
+ *
+ */
+class Normalization : public nntrainer::Property<bool> {
+public:
+  /**
+   * @brief Construct a new Normalization object
+   *
+   */
+  Normalization(bool value = false);
+  static constexpr const char *key = "normalization";
+  using prop_tag = bool_prop_tag;
+};
+
+/**
+ * @brief Standardization property, standardization standardize the input
+ * to be mean 0 and std 1 if true
+ *
+ */
+class Standardization : public nntrainer::Property<bool> {
+public:
+  /**
+   * @brief Construct a new Standardization object
+   *
+   */
+  Standardization(bool value = false);
+  static constexpr const char *key = "standardization";
+  using prop_tag = bool_prop_tag;
+};
+
 /**
  * @brief RAII class to define the connection spec
  *
index fc8ca6093b02e053917a7a373b91731a76d1379a..0b33b5f3db5e5c9cbcb4ce24db302ecb2f8970fc 100644 (file)
@@ -24,6 +24,7 @@
 #include <input_layer.h>
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
+#include <node_exporter.h>
 #include <parse_util.h>
 #include <util_func.h>
 
@@ -31,63 +32,24 @@ namespace nntrainer {
 
 static constexpr size_t SINGLE_INOUT_IDX = 0;
 
-void InputLayer::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);
-  }
-}
+InputLayer::InputLayer() :
+  Layer(),
+  input_props(props::Normalization(), props::Standardization()) {}
 
-void InputLayer::setProperty(const std::string &type_str,
-                             const std::string &value) {
-  using PropertyType = nntrainer::Layer::PropertyType;
-  int status = ML_ERROR_NONE;
-  nntrainer::Layer::PropertyType type =
-    static_cast<nntrainer::Layer::PropertyType>(parseLayerProperty(type_str));
-
-  switch (type) {
-  case PropertyType::normalization: {
-    status = setBoolean(normalization, value);
-    throw_status(status);
-  } break;
-  case PropertyType::standardization: {
-    status = setBoolean(standardization, value);
-    throw_status(status);
-  } break;
-  case PropertyType::weight_initializer: {
-    ml_logw("Deprecated property: %s", type_str.c_str());
-  } break;
-  case PropertyType::bias_initializer: {
-    ml_logw("Deprecated property: %s", type_str.c_str());
-  } break;
-  default:
-    std::string msg =
-      "[InputLayer] Unknown Layer Property Key for value " + std::string(value);
-    throw exception::not_supported(msg);
-  }
+void InputLayer::setProperty(const std::vector<std::string> &values) {
+  auto remain_props = loadProperties(values, input_props);
+  NNTR_THROW_IF(!remain_props.empty(), std::invalid_argument)
+    << "[InputLayer] Unknown Layer Properties count " +
+         std::to_string(values.size());
 }
 
 void InputLayer::forwarding(RunLayerContext &context, bool training) {
   Tensor &hidden_ = context.getOutput(SINGLE_INOUT_IDX);
   hidden_ = context.getInput(SINGLE_INOUT_IDX);
 
-  if (normalization)
+  if (std::get<props::Normalization>(input_props))
     hidden_.normalization_i();
-  if (standardization)
+  if (std::get<props::Standardization>(input_props))
     hidden_.standardization_i();
 }
 
@@ -96,6 +58,11 @@ void InputLayer::calcDerivative(RunLayerContext &context) {
     "calcDerivative for input layer is not supported");
 }
 
+void InputLayer::exportTo(Exporter &exporter,
+                          const ExportMethods &method) const {
+  exporter.saveResult(input_props, method, this);
+}
+
 void InputLayer::finalize(InitLayerContext &context) {
   context.setOutputDimensions(context.getInputDimensions());
 }
index 8b7f9419ac634077673d15fa9dacd21c2a62e7d4..69980eb0e5db2727ebaacdcd5104144a337fa073 100644 (file)
@@ -40,7 +40,7 @@ public:
   /**
    * @brief     Constructor of InputLayer
    */
-  InputLayer() : Layer(), normalization(false), standardization(false) {}
+  InputLayer();
 
   /**
    * @brief     Destructor of InputLayer
@@ -82,8 +82,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()
@@ -98,18 +97,7 @@ public:
   inline static const std::string type = "input";
 
 private:
-  bool normalization;   /**< normalize the input to be in range [0,1] */
-  bool standardization; /**< standardize the input to be mean 0 and std 1 */
-
-  /**
-   * @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);
+  std::tuple<props::Normalization, props::Standardization> input_props;
 };
 } // namespace nntrainer
 
index 1a7640f13189678ad873b0f65b0921b8e4dc34a4..964ae0f98fc51dcb5162ab15cede19e6f6a59a3a 100644 (file)
@@ -89,16 +89,6 @@ private:
    */
   void setDimensions(const std::vector<int> &tensor_idx_list,
                      std::vector<TensorDim> &dim, bool is_output);
-
-  /**
-   * @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_str, const std::string &value);
 };
 
 } // namespace nntrainer