From f106ab745a12a5c773a9c315dcddef0c8bf11225 Mon Sep 17 00:00:00 2001 From: Narumol Prangnawarat Date: Wed, 15 Sep 2021 17:30:37 +0100 Subject: Add support for Clip with attribute on ONNX parser Signed-off-by: Narumol Prangnawarat Change-Id: I1bae42dade7eabf3da09252066e912e803a8ea32 --- src/armnnOnnxParser/OnnxParser.cpp | 12 +++++-- src/armnnOnnxParser/test/Clip.cpp | 73 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 83 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/armnnOnnxParser/OnnxParser.cpp b/src/armnnOnnxParser/OnnxParser.cpp index a7e6902fdd..49f0271aeb 100644 --- a/src/armnnOnnxParser/OnnxParser.cpp +++ b/src/armnnOnnxParser/OnnxParser.cpp @@ -1189,8 +1189,16 @@ void OnnxParserImpl::ParseActivation(const onnx::NodeProto& node, const armnn::A if (func == ActivationFunction::BoundedReLu) { - desc.m_A = node.input(2).empty() ? std::numeric_limits::max() : std::stof(node.input(2)); - desc.m_B = node.input(1).empty() ? std::numeric_limits::lowest() : std::stof(node.input(1)); + if (node.input_size() == 1 && node.attribute_size() > 0) + { + desc.m_A = ReadOptionalNodeFloatAttribute(node, "max", std::numeric_limits::max()); + desc.m_B = ReadOptionalNodeFloatAttribute(node, "min", std::numeric_limits::lowest()); + } + else + { + desc.m_A = node.input(2).empty() ? std::numeric_limits::max() : std::stof(node.input(2)); + desc.m_B = node.input(1).empty() ? std::numeric_limits::lowest() : std::stof(node.input(1)); + } } IConnectableLayer* const layer = m_Network->AddActivationLayer(desc, node.name().c_str()); diff --git a/src/armnnOnnxParser/test/Clip.cpp b/src/armnnOnnxParser/test/Clip.cpp index b0447bcad5..2b43574d6c 100644 --- a/src/armnnOnnxParser/test/Clip.cpp +++ b/src/armnnOnnxParser/test/Clip.cpp @@ -62,6 +62,68 @@ struct ClipMainFixture : public armnnUtils::ParserPrototxtFixture +{ + ClipAttributeFixture(std::string min, std::string max) + { + m_Prototext = R"( + ir_version: 3 + producer_name: "CNTK" + producer_version: "2.5.1" + domain: "ai.cntk" + model_version: 1 + graph { + name: "CNTKGraph" + input { + name: "Input" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 5 + } + } + } + } + } + node { + input: "Input" + output: "Output" + name: "ActivationLayer" + op_type: "Clip" + attribute { + name: "min" + f: )" + min + R"( + type: FLOAT + } + attribute { + name: "max" + f: )" + max + R"( + type: FLOAT + } + } + output { + name: "Output" + type { + tensor_type { + elem_type: 1 + shape { + dim { + dim_value: 5 + } + } + } + } + } + } + opset_import { + version: 7 + })"; + Setup(); + } +}; + struct ClipFixture : ClipMainFixture { ClipFixture() : ClipMainFixture("2", "3.5") {} @@ -108,4 +170,15 @@ TEST_CASE_FIXTURE(ClipNoInputFixture, "ValidNoInputClipTest") std::numeric_limits::max()}}}); } +struct ClipMinMaxAttributeFixture : ClipAttributeFixture +{ + ClipMinMaxAttributeFixture() : ClipAttributeFixture("2", "3.5") {} +}; + +TEST_CASE_FIXTURE(ClipMinMaxAttributeFixture, "ValidClipAttributeTest") +{ + RunTest<1>({{ "Input", { -1.5f, 1.25f, 3.5f, 8.0, 2.5}}}, + {{ "Output", { 2.0f, 2.0f, 3.5f, 3.5, 2.5}}}); +} + } -- cgit v1.2.1