Unverified Commit b0648401 authored by Robert Kimball's avatar Robert Kimball Committed by GitHub

Add simple method to add default values for missing deserialized fields.…

Add simple method to add default values for missing deserialized fields. Provided to ease backwards compatibility. (#547)
parent 8c246fe5
......@@ -372,7 +372,7 @@ static shared_ptr<ngraph::Function>
auto padding_below = node_js.at("padding_below").get<vector<size_t>>();
auto padding_above = node_js.at("padding_above").get<vector<size_t>>();
auto include_padding_in_avg_computation =
node_js.at("include_padding_in_avg_computation").get<bool>();
get_or_default<bool>(node_js, "include_padding_in_avg_computation", false);
node = make_shared<op::AvgPoolBackprop>(forward_arg_shape,
args[0],
window_shape,
......
......@@ -28,4 +28,19 @@ namespace ngraph
std::string serialize(std::shared_ptr<ngraph::Function>, size_t indent = 0);
std::shared_ptr<ngraph::Function> deserialize(std::istream&);
std::shared_ptr<ngraph::Function> deserialize(const std::string&);
template <typename T>
T get_or_default(nlohmann::json& j, const std::string& key, const T& default_value)
{
T rc;
try
{
rc = j.at(key).get<T>();
}
catch (...)
{
rc = default_value;
}
return rc;
}
}
......@@ -106,6 +106,18 @@ TEST(serialize, existing_models)
}
}
TEST(serialize, default_value)
{
json j = {{"test1", 1}, {"test2", 2}};
int x1 = j.at("test1").get<int>();
EXPECT_EQ(x1, 1);
int x2 = get_or_default<int>(j, "test2", 0);
EXPECT_EQ(x2, 2);
int x3 = get_or_default<int>(j, "test3", 3);
EXPECT_EQ(x3, 3);
}
TEST(benchmark, serialize)
{
stopwatch timer;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment