[preprocess translate layer] Maintain layer property with props
authorhyeonseok lee <hs89.lee@samsung.com>
Tue, 7 Sep 2021 03:43:08 +0000 (12:43 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Mon, 13 Sep 2021 11:59:57 +0000 (20:59 +0900)
 - All the preprocess translate layer property will be maintain with props

**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/preprocess_translate_layer.cpp
nntrainer/layers/preprocess_translate_layer.h

index 2390c7f..7475e9e 100644 (file)
@@ -54,6 +54,10 @@ bool DropOutSpec::isValid(const float &v) const {
     return true;
 }
 
+void RandomTranslate::set(const float &value) {
+  Property<float>::set(std::abs(value));
+}
+
 bool FilePath::isValid(const std::string &v) const {
   std::ifstream file(v, std::ios::binary | std::ios::ate);
   return file.good();
index 027e9b7..80589c1 100644 (file)
@@ -345,6 +345,26 @@ public:
 };
 
 /**
+ * @brief TranslationFactor property, this defines how far the image is
+ * translated
+ *
+ */
+class RandomTranslate : public nntrainer::Property<float> {
+
+public:
+  static constexpr const char *key =
+    "random_translate";            /**< unique key to access */
+  using prop_tag = float_prop_tag; /**< property type */
+
+  /**
+   * @brief setter
+   *
+   * @param value value to set
+   */
+  void set(const float &value) override;
+};
+
+/**
  * @brief Props containing file path value
  *
  */
index 7a35350..99044fa 100644 (file)
@@ -16,6 +16,7 @@
 
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
+#include <node_exporter.h>
 #include <parse_util.h>
 #include <preprocess_translate_layer.h>
 #include <util_func.h>
 #endif
 
 namespace nntrainer {
+PreprocessTranslateLayer::PreprocessTranslateLayer() :
+  Layer(),
+  epsilon(1e-5),
+  preprocess_translate_props(props::RandomTranslate()) {}
 
 void PreprocessTranslateLayer::finalize(InitLayerContext &context) {
   context.setOutputDimensions(context.getInputDimensions());
   const TensorDim input_dim_0 = context.getInputDimensions()[0];
+  float random_translate =
+    std::get<props::RandomTranslate>(preprocess_translate_props);
 
   rng.seed(getSeed());
 
   // Made for 3 channel input
-  if (translation_factor > epsilon) {
+  if (random_translate > epsilon) {
     if (input_dim_0.channel() > 3)
       throw exception::not_supported(
         "Preprocess translate layer not supported for over 3 channels");
-    translate_dist = std::uniform_real_distribution<float>(-translation_factor,
-                                                           translation_factor);
+    translate_dist = std::uniform_real_distribution<float>(-random_translate,
+                                                           random_translate);
 
 #if defined(ENABLE_DATA_AUGMENTATION_OPENCV)
     affine_transform_mat = cv::Mat::zeros(2, 3, CV_32FC1);
@@ -59,45 +66,10 @@ void PreprocessTranslateLayer::finalize(InitLayerContext &context) {
 
 void PreprocessTranslateLayer::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 PreprocessTranslateLayer::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::random_translate: {
-    status = setFloat(translation_factor, value);
-    translation_factor = std::abs(translation_factor);
-    throw_status(status);
-  } break;
-  default:
-    std::string msg =
-      "[PreprocessTranslateLayer] Unknown Layer Property Key for value " +
-      std::string(value);
-    throw exception::not_supported(msg);
-  }
+  auto remain_props = loadProperties(values, preprocess_translate_props);
+  NNTR_THROW_IF(!remain_props.empty(), std::invalid_argument)
+    << "[PreprocessTranslateLayer] Unknown Layer Properties count " +
+         std::to_string(values.size());
 }
 
 void PreprocessTranslateLayer::forwarding(RunLayerContext &context,
@@ -115,8 +87,10 @@ void PreprocessTranslateLayer::forwarding(RunLayerContext &context,
     Tensor &hidden_ = context.getOutput(idx);
     Tensor &input_ = context.getInput(idx);
     const TensorDim input_dim = input_.getDim();
+    float random_translate =
+      std::get<props::RandomTranslate>(preprocess_translate_props);
 
-    if (translation_factor < epsilon) {
+    if (random_translate < epsilon) {
       hidden_ = input_;
       continue;
     }
@@ -158,4 +132,9 @@ void PreprocessTranslateLayer::calcDerivative(RunLayerContext &context) {
     "calcDerivative for preprocess layer is not supported");
 }
 
+void PreprocessTranslateLayer::exportTo(Exporter &exporter,
+                                        const ExportMethods &method) const {
+  exporter.saveResult(preprocess_translate_props, method, this);
+}
+
 } /* namespace nntrainer */
index b447a45..da69f2a 100644 (file)
@@ -34,10 +34,7 @@ public:
   /**
    * @brief     Constructor of Preprocess Translate Layer
    */
-  PreprocessTranslateLayer() :
-    Layer(),
-    translation_factor(0.0),
-    epsilon(1e-5) {}
+  PreprocessTranslateLayer();
 
   /**
    * @brief     Destructor of Preprocess Translate Layer
@@ -79,8 +76,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()
@@ -97,27 +93,17 @@ public:
   inline static const std::string type = "preprocess_translate";
 
 private:
-  float translation_factor;
   float epsilon;
 
   std::mt19937 rng; /**< random number generator */
   std::uniform_real_distribution<float>
     translate_dist; /**< uniform random distribution */
+  std::tuple<props::RandomTranslate> preprocess_translate_props;
 
 #if defined(ENABLE_DATA_AUGMENTATION_OPENCV)
   cv::Mat affine_transform_mat;
   cv::Mat input_mat, output_mat;
 #endif
-
-  /**
-   * @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);
 };
 
 } // namespace nntrainer