From 6c7c38e70c795077ba727aadeefc670888bec089 Mon Sep 17 00:00:00 2001 From: Michalis Spyrou Date: Wed, 29 Aug 2018 16:28:11 +0100 Subject: COMPMID-1462 SSD support: Create CL PriorBox Change-Id: I5bf5d751ec7c02d96c26a769f49d03ea23a248b7 --- tests/datasets/PriorBoxLayerDataset.h | 134 +++++++++++++++++++ tests/validation/CL/PriorBoxLayer.cpp | 103 +++++++++++++++ tests/validation/fixtures/PriorBoxLayerFixture.h | 112 ++++++++++++++++ tests/validation/reference/PriorBoxLayer.cpp | 158 +++++++++++++++++++++++ tests/validation/reference/PriorBoxLayer.h | 44 +++++++ 5 files changed, 551 insertions(+) create mode 100644 tests/datasets/PriorBoxLayerDataset.h create mode 100644 tests/validation/CL/PriorBoxLayer.cpp create mode 100644 tests/validation/fixtures/PriorBoxLayerFixture.h create mode 100644 tests/validation/reference/PriorBoxLayer.cpp create mode 100644 tests/validation/reference/PriorBoxLayer.h (limited to 'tests') diff --git a/tests/datasets/PriorBoxLayerDataset.h b/tests/datasets/PriorBoxLayerDataset.h new file mode 100644 index 0000000000..22a0d97057 --- /dev/null +++ b/tests/datasets/PriorBoxLayerDataset.h @@ -0,0 +1,134 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_PRIORBOX_LAYER_DATASET +#define ARM_COMPUTE_TEST_PRIORBOX_LAYER_DATASET + +#include "utils/TypePrinter.h" + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class PriorBoxLayerDataset +{ +public: + using type = std::tuple; + + struct iterator + { + iterator(std::vector::const_iterator src_it, + std::vector::const_iterator infos_it) + : _src_it{ std::move(src_it) }, + _infos_it{ std::move(infos_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "In=" << *_src_it << ":"; + description << "Info=" << *_infos_it << ":"; + return description.str(); + } + + PriorBoxLayerDataset::type operator*() const + { + return std::make_tuple(*_src_it, *_infos_it); + } + + iterator &operator++() + { + ++_src_it; + ++_infos_it; + + return *this; + } + + private: + std::vector::const_iterator _src_it; + std::vector::const_iterator _infos_it; + }; + + iterator begin() const + { + return iterator(_src_shapes.begin(), _infos.begin()); + } + + int size() const + { + return std::min(_src_shapes.size(), _infos.size()); + } + + void add_config(TensorShape src, PriorBoxLayerInfo info) + { + _src_shapes.emplace_back(std::move(src)); + _infos.emplace_back(std::move(info)); + } + +protected: + PriorBoxLayerDataset() = default; + PriorBoxLayerDataset(PriorBoxLayerDataset &&) = default; + +private: + std::vector _src_shapes{}; + std::vector _infos{}; +}; + +class SmallPriorBoxLayerDataset final : public PriorBoxLayerDataset +{ +public: + SmallPriorBoxLayerDataset() + { + std::vector min_val = { 30.f }; + std::vector var = { 0.1, 0.1, 0.2, 0.2 }; + std::vector max_val = { 60.f }; + std::vector aspect_ratio = { 2.f }; + std::array steps = { 8.f, 8.f }; + + add_config(TensorShape(4U, 4U), PriorBoxLayerInfo(min_val, var, 0.5f, true, false, max_val, aspect_ratio, Coordinates2D{ 8, 8 }, steps)); + } +}; + +class LargePriorBoxLayerDataset final : public PriorBoxLayerDataset +{ +public: + LargePriorBoxLayerDataset() + { + std::vector min_val = { 30.f }; + std::vector var = { 0.1, 0.1, 0.2, 0.2 }; + std::vector max_val = { 60.f }; + std::vector aspect_ratio = { 2.f }; + std::array steps = { 8.f, 8.f }; + add_config(TensorShape(150U, 245U, 4U, 12U), PriorBoxLayerInfo(min_val, var, 0.5f, true, false, max_val, aspect_ratio, Coordinates2D{ 8, 8 }, steps)); + } +}; +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_PRIORBOX_LAYER_DATASET */ diff --git a/tests/validation/CL/PriorBoxLayer.cpp b/tests/validation/CL/PriorBoxLayer.cpp new file mode 100644 index 0000000000..79776b5bcd --- /dev/null +++ b/tests/validation/CL/PriorBoxLayer.cpp @@ -0,0 +1,103 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "arm_compute/core/Types.h" +#include "arm_compute/runtime/CL/CLTensor.h" +#include "arm_compute/runtime/CL/CLTensorAllocator.h" +#include "arm_compute/runtime/CL/functions/CLPriorBoxLayer.h" +#include "tests/CL/CLAccessor.h" +#include "tests/PaddingCalculator.h" +#include "tests/datasets/PriorBoxLayerDataset.h" +#include "tests/datasets/ShapeDatasets.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/PriorBoxLayerFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +constexpr AbsoluteTolerance tolerance_f32(0.00001f); /**< Tolerance value for comparing reference's output against implementation's output for DataType::F32 */ +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(PriorBoxLayer) + +template +using CLPriorBoxLayerFixture = PriorBoxLayerValidationFixture; + +// *INDENT-OFF* +// clang-format off +DATA_TEST_CASE(Validate, framework::DatasetMode::ALL, zip(zip(zip(zip( + framework::dataset::make("Input1Info", { TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), // Window shrink + }), + framework::dataset::make("Input2Info", { TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(10U, 10U, 2U), 1, DataType::F32), + })), + framework::dataset::make("OutputInfo",{ TensorInfo(TensorShape(1200U, 2U), 1, DataType::F32), + TensorInfo(TensorShape(1000U, 2U), 1, DataType::F32), + })), + framework::dataset::make("PriorBoxInfo",{ PriorBoxLayerInfo(std::vector(1), std::vector(1), 0, true, true, std::vector(1), std::vector(1), Coordinates2D{8, 8}, std::array()), + PriorBoxLayerInfo(std::vector(1), std::vector(1), 0, true, true, std::vector(1), std::vector(1), Coordinates2D{8, 8}, std::array()), + })), + framework::dataset::make("Expected", { true, false})), + input1_info, input2_info, output_info, info, expected) +{ + bool has_error = bool(CLPriorBoxLayer::validate(&input1_info.clone()->set_is_resizable(false), &input2_info.clone()->set_is_resizable(false), &output_info.clone()->set_is_resizable(false), info)); + ARM_COMPUTE_EXPECT(has_error == expected, framework::LogLevel::ERRORS); +} +// clang-format on +// *INDENT-ON* + +TEST_SUITE(Float) +TEST_SUITE(FP32) +FIXTURE_DATA_TEST_CASE(RunSmall, CLPriorBoxLayerFixture, framework::DatasetMode::PRECOMMIT, combine(combine(datasets::SmallPriorBoxLayerDataset(), + framework::dataset::make("DataType", DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32, 0); +} +FIXTURE_DATA_TEST_CASE(RunLarge, CLPriorBoxLayerFixture, framework::DatasetMode::NIGHTLY, combine(combine(datasets::LargePriorBoxLayerDataset(), + framework::dataset::make("DataType", DataType::F32)), + framework::dataset::make("DataLayout", { DataLayout::NCHW, DataLayout::NHWC }))) +{ + // Validate output + validate(CLAccessor(_target), _reference, tolerance_f32, 0); +} +TEST_SUITE_END() // Float +TEST_SUITE_END() // FP32 + +TEST_SUITE_END() // PriorBoxLayer +TEST_SUITE_END() // CL +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/PriorBoxLayerFixture.h b/tests/validation/fixtures/PriorBoxLayerFixture.h new file mode 100644 index 0000000000..dd7a49ee1f --- /dev/null +++ b/tests/validation/fixtures/PriorBoxLayerFixture.h @@ -0,0 +1,112 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef ARM_COMPUTE_TEST_PRIOR_BOX_LAYER_FIXTURE +#define ARM_COMPUTE_TEST_PRIOR_BOX_LAYER_FIXTURE + +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "arm_compute/core/utils/misc/ShapeCalculator.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/Helpers.h" +#include "tests/validation/reference/PriorBoxLayer.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template +class PriorBoxLayerValidationFixture : public framework::Fixture +{ +public: + template + void setup(TensorShape input_shape, PriorBoxLayerInfo info, DataType data_type, DataLayout data_layout) + { + TensorInfo input_info(input_shape, 1, data_type); + const TensorShape output_shape = misc::shape_calculator::compute_prior_box_shape(input_info, info); + _data_type = data_type; + + _target = compute_target(input_shape, output_shape, info, data_type, data_layout); + _reference = compute_reference(input_shape, output_shape, info, data_type); + } + +protected: + TensorType compute_target(TensorShape input_shape, TensorShape output_shape, PriorBoxLayerInfo info, DataType data_type, DataLayout data_layout) + { + if(data_layout == DataLayout::NHWC) + { + permute(input_shape, PermutationVector(2U, 0U, 1U)); + permute(output_shape, PermutationVector(2U, 0U, 1U)); + } + + // Create tensors + TensorType src1 = create_tensor(input_shape, data_type, 1, QuantizationInfo(), data_layout); + TensorType src2 = create_tensor(input_shape, data_type, 1, QuantizationInfo(), data_layout); + TensorType dst = create_tensor(output_shape, data_type, 1, QuantizationInfo(), data_layout); + + // Create and configure function + FunctionType prior_box; + prior_box.configure(&src1, &src2, &dst, info); + + ARM_COMPUTE_EXPECT(src1.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(src2.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Allocate tensors + src1.allocator()->allocate(); + src2.allocator()->allocate(); + dst.allocator()->allocate(); + + ARM_COMPUTE_EXPECT(!src1.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!src2.info()->is_resizable(), framework::LogLevel::ERRORS); + ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Compute function + prior_box.run(); + + return dst; + } + + SimpleTensor compute_reference(const TensorShape &input_shape, const TensorShape &output_shape, PriorBoxLayerInfo info, DataType data_type) + { + // Create reference + SimpleTensor input1{ input_shape, data_type, 1 }; + SimpleTensor input2{ input_shape, data_type, 1 }; + + return reference::prior_box_layer(input1, input2, info, output_shape); + } + + TensorType _target{}; + SimpleTensor _reference{}; + DataType _data_type{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_PRIOR_BOX_LAYER_FIXTURE */ diff --git a/tests/validation/reference/PriorBoxLayer.cpp b/tests/validation/reference/PriorBoxLayer.cpp new file mode 100644 index 0000000000..0fd4a8aa48 --- /dev/null +++ b/tests/validation/reference/PriorBoxLayer.cpp @@ -0,0 +1,158 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#include "PriorBoxLayer.h" + +#include "ActivationLayer.h" + +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor prior_box_layer(const SimpleTensor &src1, const SimpleTensor &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape) +{ + const auto layer_width = static_cast(src1.shape()[0]); + const auto layer_height = static_cast(src1.shape()[1]); + + int img_width = info.img_size().x; + int img_height = info.img_size().y; + if(img_width == 0 || img_height == 0) + { + img_width = static_cast(src2.shape()[0]); + img_height = static_cast(src2.shape()[1]); + } + + float step_x = info.steps()[0]; + float step_y = info.steps()[1]; + if(step_x == 0.f || step_y == 0.f) + { + step_x = static_cast(img_width) / layer_width; + step_x = static_cast(img_height) / layer_height; + } + + // Calculate number of aspect ratios + const int num_priors = info.aspect_ratios().size() * info.min_sizes().size() + info.max_sizes().size(); + const int total_elements = layer_width * layer_height * num_priors * 4; + + SimpleTensor result(output_shape, src1.data_type()); + + int idx = 0; + for(int y = 0; y < layer_height; ++y) + { + for(int x = 0; x < layer_width; ++x) + { + const float center_x = (x + info.offset()) * step_x; + const float center_y = (y + info.offset()) * step_y; + float box_width; + float box_height; + for(unsigned int i = 0; i < info.min_sizes().size(); ++i) + { + const float min_size = info.min_sizes().at(i); + box_width = min_size; + box_height = min_size; + // (xmin, ymin, xmax, ymax) + result[idx++] = (center_x - box_width / 2.f) / img_width; + result[idx++] = (center_y - box_height / 2.f) / img_height; + result[idx++] = (center_x + box_width / 2.f) / img_width; + result[idx++] = (center_y + box_height / 2.f) / img_height; + + if(!info.max_sizes().empty()) + { + const float max_size = info.max_sizes().at(i); + box_width = sqrt(min_size * max_size); + box_height = box_width; + + // (xmin, ymin, xmax, ymax) + result[idx++] = (center_x - box_width / 2.f) / img_width; + result[idx++] = (center_y - box_height / 2.f) / img_height; + result[idx++] = (center_x + box_width / 2.f) / img_width; + result[idx++] = (center_y + box_height / 2.f) / img_height; + } + + // rest of priors + for(auto ar : info.aspect_ratios()) + { + if(fabs(ar - 1.) < 1e-6) + { + continue; + } + + box_width = min_size * sqrt(ar); + box_height = min_size / sqrt(ar); + + // (xmin, ymin, xmax, ymax) + result[idx++] = (center_x - box_width / 2.f) / img_width; + result[idx++] = (center_y - box_height / 2.f) / img_height; + result[idx++] = (center_x + box_width / 2.f) / img_width; + result[idx++] = (center_y + box_height / 2.f) / img_height; + } + } + } + } + + // clip the coordinates + if(info.clip()) + { + for(int i = 0; i < total_elements; ++i) + { + result[i] = std::min(std::max(result[i], 0.f), 1.f); + } + } + + // set the variance. + if(info.variances().size() == 1) + { + std::fill_n(result.data() + idx, total_elements, info.variances().at(0)); + } + else + { + for(int h = 0; h < layer_height; ++h) + { + for(int w = 0; w < layer_width; ++w) + { + for(int i = 0; i < num_priors; ++i) + { + for(int j = 0; j < 4; ++j) + { + result[idx++] = info.variances().at(j); + } + } + } + } + } + + return result; +} +template SimpleTensor prior_box_layer(const SimpleTensor &src1, const SimpleTensor &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape); + +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/PriorBoxLayer.h b/tests/validation/reference/PriorBoxLayer.h new file mode 100644 index 0000000000..25e567f59a --- /dev/null +++ b/tests/validation/reference/PriorBoxLayer.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2018 ARM Limited. + * + * SPDX-License-Identifier: MIT + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to + * deal in the Software without restriction, including without limitation the + * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or + * sell copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in all + * copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE + * SOFTWARE. + */ +#ifndef __ARM_COMPUTE_TEST_PRIOR_BOX_LAYER_H__ +#define __ARM_COMPUTE_TEST_PRIOR_BOX_LAYER_H__ + +#include "tests/SimpleTensor.h" +#include "tests/validation/Helpers.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template +SimpleTensor prior_box_layer(const SimpleTensor &src1, const SimpleTensor &src2, const PriorBoxLayerInfo &info, const TensorShape &output_shape); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_PRIOR_BOX_LAYER_H__ */ -- cgit v1.2.1