From 1c1ec8f3f645ee88dec89df95ec7a6a68e2a9214 Mon Sep 17 00:00:00 2001 From: hyeonseok lee Date: Mon, 6 Sep 2021 14:20:14 +0900 Subject: [PATCH] [input layer] Maintain input layer property with props - 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 --- nntrainer/layers/common_properties.cpp | 4 ++ nntrainer/layers/common_properties.h | 32 +++++++++++++ nntrainer/layers/input_layer.cpp | 65 +++++++------------------- nntrainer/layers/input_layer.h | 18 ++----- nntrainer/layers/tflite_layer.h | 10 ---- 5 files changed, 55 insertions(+), 74 deletions(-) diff --git a/nntrainer/layers/common_properties.cpp b/nntrainer/layers/common_properties.cpp index b4ce8cc6..4b8627aa 100644 --- a/nntrainer/layers/common_properties.cpp +++ b/nntrainer/layers/common_properties.cpp @@ -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; diff --git a/nntrainer/layers/common_properties.h b/nntrainer/layers/common_properties.h index 02d67838..8ca1b1fc 100644 --- a/nntrainer/layers/common_properties.h +++ b/nntrainer/layers/common_properties.h @@ -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 { +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 { +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 * diff --git a/nntrainer/layers/input_layer.cpp b/nntrainer/layers/input_layer.cpp index fc8ca609..0b33b5f3 100644 --- a/nntrainer/layers/input_layer.cpp +++ b/nntrainer/layers/input_layer.cpp @@ -24,6 +24,7 @@ #include #include #include +#include #include #include @@ -31,63 +32,24 @@ namespace nntrainer { static constexpr size_t SINGLE_INOUT_IDX = 0; -void InputLayer::setProperty(const std::vector &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(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 &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(input_props)) hidden_.normalization_i(); - if (standardization) + if (std::get(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()); } diff --git a/nntrainer/layers/input_layer.h b/nntrainer/layers/input_layer.h index 8b7f9419..69980eb0 100644 --- a/nntrainer/layers/input_layer.h +++ b/nntrainer/layers/input_layer.h @@ -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 input_props; }; } // namespace nntrainer diff --git a/nntrainer/layers/tflite_layer.h b/nntrainer/layers/tflite_layer.h index 1a7640f1..964ae0f9 100644 --- a/nntrainer/layers/tflite_layer.h +++ b/nntrainer/layers/tflite_layer.h @@ -89,16 +89,6 @@ private: */ void setDimensions(const std::vector &tensor_idx_list, std::vector &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 -- 2.34.1