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
#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());
}
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 */
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)
"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 */
/**
* @brief Constructor of Preprocess FLip Layer
*/
- PreprocessFlipLayer() :
- Layer(),
- flipdirection(FlipDirection::horizontal_and_vertical) {}
+ PreprocessFlipLayer();
/**
* @brief Destructor of Preprocess FLip Layer
/**
* @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()
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