[parse] Parse unknown properties
authorParichay Kapoor <pk.kapoor@samsung.com>
Thu, 18 Jun 2020 06:10:22 +0000 (15:10 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 18 Jun 2020 08:10:27 +0000 (17:10 +0900)
Properties exposed to the users and internal are different (losslayer, etc)
Hence using `*_string.size() - 1` for unknown cases will cause bugs in parse_util
Replaced with its own individual unknown value

V2:
Combined all individual layer properties into common properties in layer.h

Signed-off-by: Parichay Kapoor <pk.kapoor@samsung.com>
12 files changed:
nntrainer/include/bn_layer.h
nntrainer/include/conv2d_layer.h
nntrainer/include/fc_layer.h
nntrainer/include/input_layer.h
nntrainer/include/layer.h
nntrainer/include/neuralnet.h
nntrainer/include/optimizer.h
nntrainer/include/pooling2d_layer.h
nntrainer/src/conv2d_layer.cpp
nntrainer/src/fc_layer.cpp
nntrainer/src/neuralnet.cpp
nntrainer/src/parse_util.cpp

index 71687995c12c91d6705c60712a8ea78010143d88..dcb4d6845bd1c57381c9e1f529b317cc8c66b5dc 100644 (file)
@@ -118,18 +118,6 @@ public:
    */
   int setProperty(std::vector<std::string> values);
 
-  /**
-   * @brief     Property Enumeration
-   *            0. input shape : string
-   *            1. bias zero : bool
-   *            5. epsilon : float
-   */
-  enum class PropertyType {
-    input_shape = 0,
-    bias_zero = 1,
-    epsilon = 5,
-  };
-
 private:
   Tensor weight;
   Tensor bias;
index a896ee6ea870ea23a56ec943f12f83f38789e5c5..615e7d813130baf5929abe1c386fbf7015911844 100644 (file)
@@ -33,38 +33,6 @@ namespace nntrainer {
  */
 class Conv2DLayer : public Layer {
 public:
-  /**
-   * @brief     Property Enumeration
-   *            0. input shape : string
-   *            1. bias zero : bool
-   *            2. normalization : bool
-   *            3. standardization : bool
-   *            4. activation : string (type)
-   *            6. weight_decay : string (type)
-   *            7. weight_decay_lambda : float
-   *            8. weight_ini : string (type)
-   *            9. filter_size : int
-   *            10. kernel_size : ( n , m )
-   *            11. stride : ( n, m )
-   *            12, padding : valid | same
-   *
-   */
-
-  enum class PropertyType {
-    input_shape = 0,
-    bias_zero = 1,
-    normalization = 2,
-    standardization = 3,
-    activation = 4,
-    weight_decay = 6,
-    weight_decay_lambda = 7,
-    weight_ini = 9,
-    filter = 10,
-    kernel_size = 11,
-    stride = 12,
-    padding = 13,
-  };
-
   /**
    * @brief     Constructor of Conv 2D Layer
    */
index 0cd173915e5fe1b99bb58bb1b99c9091bb7c2038..f1d480c7e3bd877eeaf59f0c0a2e4fec95597f50 100644 (file)
@@ -101,24 +101,6 @@ public:
    */
   int setOptimizer(Optimizer &opt);
 
-  /**
-   * @brief     Property Enumeration
-   *            1. bias zero : bool
-   *            4. activation : bool
-   *            6. weight_decay : string (type)
-   *            7. weight_decay_lambda : float
-   *            8. unit : int
-   *            9. weight_init,
-   */
-  enum class PropertyType {
-    bias_zero = 1,
-    activation = 4,
-    weight_decay = 6,
-    weight_decay_lambda = 7,
-    unit = 8,
-    weight_init = 9,
-  };
-
 private:
   unsigned int unit;
   Tensor weight;
index 9d3278710b91ab111b5a99b06eb6a783e330c976..f9c588ca904e313667cd2cfbaef21e2b49a2db97 100644 (file)
@@ -131,20 +131,6 @@ public:
    */
   int setProperty(std::vector<std::string> values);
 
-  /**
-   * @brief     Property Enumeration
-   *            0. input shape : string
-   *            1. bias zero : bool
-   *            2. normalization : bool
-   *            3. normalization : bool
-   */
-  enum class PropertyType {
-    input_shape = 0,
-    bias_zero = 1,
-    normalization = 2,
-    standardization = 3
-  };
-
 private:
   bool normalization;
   bool standardization;
index 6feb28afd72a103bb29c63a0ebf40fd99e9ea25e..73dc5932b70a160eb3adf75dcf52f222e1421788 100644 (file)
@@ -38,7 +38,11 @@ namespace nntrainer {
  *            1. ENTROPY ( Cross Entropy )
  *            2. Unknown
  */
-typedef enum { COST_MSR, COST_ENTROPY, COST_UNKNOWN } CostType;
+typedef enum {
+  COST_MSR,
+  COST_ENTROPY,
+  COST_UNKNOWN }
+CostType;
 
 /**
  * @brief     Enumeration of activation function type
@@ -277,6 +281,45 @@ public:
    */
   int setCost(CostType c);
 
+  /**
+   * @brief     Property Enumeration
+   *            0. input shape : string
+   *            1. bias zero : bool
+   *            2. normalization : bool
+   *            3. standardization : bool
+   *            4. activation : string (type)
+   *            5. epsilon : float
+   *            6. weight_decay : string (type)
+   *            7. weight_decay_lambda : float
+   *            8. unit : int
+   *            9. weight_ini : string (type)
+   *            10. filter_size : int
+   *            11. kernel_size : ( n , m )
+   *            12. stride : ( n, m )
+   *            13. padding : ( n, m )
+   *            14, pooling_size : ( n,m )
+   *            15, pooling : max, average, global_max, global_average
+   */
+  enum class PropertyType {
+    input_shape = 0,
+    bias_zero = 1,
+    normalization = 2,
+    standardization = 3,
+    activation = 4,
+    epsilon = 5,
+    weight_decay = 6,
+    weight_decay_lambda = 7,
+    unit = 8,
+    weight_ini = 9,
+    filter = 10,
+    kernel_size = 11,
+    stride = 12,
+    padding = 13,
+    pooling_size = 14,
+    pooling = 15,
+    unknown = 16
+  };
+
 protected:
   /**
    * @brief     Input Tensor
index 3403467f427098b4d097b49db82f0f606667acf7..da1a180b9bce95362cdd3ff85463c23094eb56e2 100644 (file)
@@ -260,6 +260,7 @@ public:
     epochs = 8,
     model_file = 9,
     continue_train = 10,
+    unknown = 11,
   };
 
 private:
index 8a7671be874c3590eb318b20b0a512fe6c9d6730..d3e048ce9aee3cecbbeffac0a2a8d8a71c8673d8 100644 (file)
@@ -177,6 +177,7 @@ public:
     beta2 = 4,
     epsilon = 5,
     continue_train = 6,
+    unknown = 7,
   };
 
   /**
index 47ec23ddfb54d4e9d30770f294adec7436133549..798fce6351469eeee7577776aebfe4482df51df3 100644 (file)
@@ -32,21 +32,6 @@ namespace nntrainer {
  */
 class Pooling2DLayer : public Layer {
 public:
-  /**
-   * @brief     Property Enumeration
-   *            12. stride : ( n, m )
-   *            13, padding : ( n, m )
-   *            14, pooling_size : ( n,m )
-   *            15, pooling : max, average, global_max, global_average
-   */
-
-  enum class PropertyType {
-    stride = 12,
-    padding = 13,
-    pooling_size = 14,
-    pooling = 15
-  };
-
   enum class PoolingType {
     max = 0,
     average = 1,
@@ -143,12 +128,12 @@ public:
   Tensor pooling2d(Tensor in, int &status);
 
   /* TO DO : support keras type of padding */
-  /* enum class PaddingType { */
-  /*   full = 0, */
-  /*   same = 1, */
-  /*   valid = 2, */
-  /*   unknown = 3, */
-  /* }; */
+  enum class PaddingType {
+    full = 0,
+    same = 1,
+    valid = 2,
+    unknown = 3,
+  };
 
 private:
   unsigned int pooling_size[POOLING2D_DIM];
index 091a00dd8d81fe10a6e96f6f69bced87736e7f1b..37c6ffd533909ad538c9ffd0fb03b59e6d2031ad 100644 (file)
@@ -133,7 +133,7 @@ void Conv2DLayer::copy(std::shared_ptr<Layer> l) {
   this->last_layer = from->last_layer;
 }
 
-int Conv2DLayer::setSize(int *size, nntrainer::Conv2DLayer::PropertyType type) {
+int Conv2DLayer::setSize(int *size, PropertyType type) {
   int status = ML_ERROR_NONE;
   switch (type) {
   case PropertyType::kernel_size:
index 254ff913563569334fecd54b80f5081fce7a200e..e0376eddbcd10a4df9f6b19b7236b84c628559ab 100644 (file)
@@ -98,7 +98,7 @@ int FullyConnectedLayer::setProperty(std::vector<std::string> values) {
       status = setFloat(weight_decay.lambda, value);
       NN_RETURN_STATUS();
       break;
-    case PropertyType::weight_init:
+    case PropertyType::weight_ini:
       weight_ini_type = (WeightIniType)parseType(value, TOKEN_WEIGHTINI);
       break;
     default:
index c135a914d74cfd4e1a6fd13ca68947e2633af480..f9976568e692f4392e74bd58faf1d4def7e09430 100644 (file)
@@ -293,7 +293,7 @@ int NeuralNetwork::init() {
                   (int *)size);
       NN_INI_RETURN_STATUS();
       status = conv2d_layer->setSize(
-        size, nntrainer::Conv2DLayer::PropertyType::kernel_size);
+        size, Layer::PropertyType::kernel_size);
       NN_INI_RETURN_STATUS();
 
       status = getValues(
@@ -302,7 +302,7 @@ int NeuralNetwork::init() {
         (int *)size);
       NN_INI_RETURN_STATUS();
       status = conv2d_layer->setSize(
-        size, nntrainer::Conv2DLayer::PropertyType::stride);
+        size, Layer::PropertyType::stride);
       NN_INI_RETURN_STATUS();
 
       status = getValues(CONV2D_DIM,
@@ -311,7 +311,7 @@ int NeuralNetwork::init() {
                          (int *)size);
       NN_INI_RETURN_STATUS();
       status = conv2d_layer->setSize(
-        size, nntrainer::Conv2DLayer::PropertyType::padding);
+        size, Layer::PropertyType::padding);
       NN_INI_RETURN_STATUS();
 
       status = conv2d_layer->setFilter(
index c2fee0422941cfc5a3fdd58668978862fd37fbff..7b683a9f5c954edb2a9a79526e1e496bc146a690 100644 (file)
 
 #include <array>
 #include <assert.h>
+#include <layer.h>
+#include <neuralnet.h>
 #include <nntrainer_error.h>
 #include <nntrainer_log.h>
+#include <optimizer.h>
 #include <parse_util.h>
+#include <pooling2d_layer.h>
 #include <regex>
 #include <string.h>
 
@@ -55,21 +59,21 @@ int getKeyValue(std::string input_str, std::string &key, std::string &value) {
 }
 
 unsigned int parseType(std::string ll, InputType t) {
-  int ret;
+  unsigned int ret;
   unsigned int i;
   /**
    * @brief     Optimizer String from configure file
    *            "sgd"  : Stochestic Gradient Descent
    *            "adam" : Adaptive Moment Estimation
    */
-  std::array<std::string, 3> optimizer_string = {"sgd", "adam", "unknown"};
+  std::array<std::string, 2> optimizer_string = {"sgd", "adam"};
 
   /**
    * @brief     Cost Function String from configure file
    *            "msr"  : Mean Squared Roots
    *            "caterogical" : Categorical Cross Entropy
    */
-  std::array<std::string, 3> cost_string = {"msr", "cross", "unknown"};
+  std::array<std::string, 2> cost_string = {"msr", "cross"};
 
   /**
    * @brief     Network Type String from configure file
@@ -77,8 +81,8 @@ unsigned int parseType(std::string ll, InputType t) {
    *            "regression" : Logistic Regression
    *            "neuralnet" : Neural Network
    */
-  std::array<std::string, 4> network_type_string = {"knn", "regression",
-                                                    "neuralnet", "unknown"};
+  std::array<std::string, 3> network_type_string = {"knn", "regression",
+                                                    "neuralnet"};
 
   /**
    * @brief     Activation Type String from configure file
@@ -87,8 +91,8 @@ unsigned int parseType(std::string ll, InputType t) {
    *            "relu" : relu
    *            "softmax" : softmax
    */
-  std::array<std::string, 5> activation_string = {"tanh", "sigmoid", "relu",
-                                                  "softmax", "unknown"};
+  std::array<std::string, 4> activation_string = {"tanh", "sigmoid", "relu",
+                                                  "softmax"};
 
   /**
    * @brief     Layer Type String from configure file
@@ -100,9 +104,9 @@ unsigned int parseType(std::string ll, InputType t) {
    *            "flatten" : Flatten Layer Object
    *            "unknown" :
    */
-  std::array<std::string, 7> layer_string = {
-    "input",   "fully_connected", "batch_normalization", "conv2d", "pooling2d",
-    "flatten", "unknown"};
+  std::array<std::string, 6> layer_string = {
+    "input", "fully_connected", "batch_normalization", "conv2d", "pooling2d",
+    "flatten"};
 
   /**
    * @brief     Weight Initialization Type String from configure file
@@ -113,25 +117,23 @@ unsigned int parseType(std::string ll, InputType t) {
    *            "he_normal"  : He Normal Initialization
    *            "he_uniform"  : He Uniform Initialization
    */
-  std::array<std::string, 7> weight_ini_string = {
+  std::array<std::string, 6> weight_ini_string = {
     "lecun_normal", "lecun_uniform", "xavier_normal", "xavier_uniform",
-    "he_normal",    "he_uniform",    "unknown"};
+    "he_normal", "he_uniform"};
 
   /**
    * @brief     Weight Decay String from configure file
    *            "L2Norm"  : squared norm regularization
    *            "Regression" : Regression
    */
-  std::array<std::string, 3> weight_decay_string = {"l2norm", "regression",
-                                                    "unknown"};
+  std::array<std::string, 2> weight_decay_string = {"l2norm", "regression"};
 
   /**
    * @brief     Weight Decay String from configure file
    *            "L2Norm"  : squared norm regularization
    *            "Regression" : Regression
    */
-  std::array<std::string, 4> padding_string = {"full", "same", "valid",
-                                               "unknown"};
+  std::array<std::string, 3> padding_string = {"full", "same", "valid"};
 
   /**
    * @brief     Pooling String from configure file
@@ -140,8 +142,8 @@ unsigned int parseType(std::string ll, InputType t) {
    *            "global_max" : Global Max Pooling
    *            "global_average" : Global Average Pooling
    */
-  std::array<std::string, 5> pooling_string = {"max", "average", "global_max",
-                                               "global_average", "unknown"};
+  std::array<std::string, 4> pooling_string = {"max", "average", "global_max",
+                                               "global_average"};
 
   switch (t) {
   case TOKEN_OPT:
@@ -151,7 +153,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) OptType::unknown;
     break;
   case TOKEN_COST:
     for (i = 0; i < cost_string.size(); i++) {
@@ -160,7 +162,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) CostType::COST_UNKNOWN;
     break;
   case TOKEN_NET:
     for (i = 0; i < network_type_string.size(); i++) {
@@ -169,7 +171,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = NetType::NET_UNKNOWN;
     break;
   case TOKEN_ACTI:
     for (i = 0; i < activation_string.size(); i++) {
@@ -179,7 +181,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) ActiType::ACT_UNKNOWN;
     break;
   case TOKEN_LAYER:
     for (i = 0; i < layer_string.size(); i++) {
@@ -188,7 +190,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) LayerType::LAYER_UNKNOWN;
     break;
   case TOKEN_WEIGHTINI:
     for (i = 0; i < weight_ini_string.size(); i++) {
@@ -197,7 +199,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) WeightIniType::WEIGHT_UNKNOWN;
     break;
   case TOKEN_WEIGHT_DECAY:
     for (i = 0; i < weight_decay_string.size(); i++) {
@@ -206,7 +208,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) WeightDecayType::unknown;
     break;
   case TOKEN_PADDING:
     for (i = 0; i < padding_string.size(); i++) {
@@ -215,7 +217,7 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) Pooling2DLayer::PaddingType::unknown;
     break;
   case TOKEN_POOLING:
     for (i = 0; i < pooling_string.size(); i++) {
@@ -224,18 +226,18 @@ unsigned int parseType(std::string ll, InputType t) {
         return (i);
       }
     }
-    ret = i - 1;
+    ret = (unsigned int) Pooling2DLayer::PoolingType::unknown;
     break;
   case TOKEN_UNKNOWN:
   default:
-    ret = 3;
+    ml_loge("Error: unknown token cannot be parsed.");
+    ret = 0;
     break;
   }
   return ret;
 }
 
 unsigned int parseLayerProperty(std::string property) {
-  int ret;
   unsigned int i;
 
   /**
@@ -279,13 +281,11 @@ unsigned int parseLayerProperty(std::string property) {
       return (i);
     }
   }
-  ret = i - 1;
 
-  return ret;
+  return (unsigned int) Layer::PropertyType::unknown;
 }
 
 unsigned int parseOptProperty(std::string property) {
-  int ret;
   unsigned int i;
 
   /**
@@ -298,9 +298,9 @@ unsigned int parseOptProperty(std::string property) {
    * epsilon = 5,
    * continue_train = 6,
    */
-  std::array<std::string, 8> property_string = {
+  std::array<std::string, 7> property_string = {
     "learning_rate", "decay_rate", "decay_steps", "beta1", "beta2", "epsilon",
-    "continue_train", "unknown"};
+    "continue_train"};
 
   for (i = 0; i < property_string.size(); i++) {
     unsigned int size = (property_string[i].size() > property.size())
@@ -311,13 +311,11 @@ unsigned int parseOptProperty(std::string property) {
       return (i);
     }
   }
-  ret = i - 1;
 
-  return ret;
+  return (unsigned int) Optimizer::PropertyType::unknown;
 }
 
 unsigned int parseNetProperty(std::string property) {
-  int ret;
   unsigned int i;
 
   /**
@@ -333,10 +331,10 @@ unsigned int parseNetProperty(std::string property) {
    * epochs = 8,
    * model_file = 9
    */
-  std::array<std::string, 11> property_string = {
+  std::array<std::string, 10> property_string = {
     "loss",      "cost",       "train_data",  "val_data",
     "test_data", "label_data", "buffer_size", "batch_size",
-    "epochs",    "model_file", "unknown"};
+    "epochs",    "model_file"};
 
   for (i = 0; i < property_string.size(); i++) {
     unsigned int size = (property_string[i].size() > property.size())
@@ -347,9 +345,8 @@ unsigned int parseNetProperty(std::string property) {
       return (i);
     }
   }
-  ret = i - 1;
 
-  return ret;
+  return (unsigned int) NeuralNetwork::PropertyType::unknown;
 }
 
 int setInt(int &val, std::string str) {