diff options
author | John Richardson <john.richardson@arm.com> | 2018-02-05 15:12:22 +0000 |
---|---|---|
committer | Anthony Barbier <anthony.barbier@arm.com> | 2018-11-02 16:51:37 +0000 |
commit | 7f4a8191a0fff69ec6c819e8d785a2c780388feb (patch) | |
tree | e027b6d011055f79d7de15b9b145aa621bf90411 /tests | |
parent | c13021e335b3e395c9d1a3a9935baedb42aebf08 (diff) | |
download | ComputeLibrary-7f4a8191a0fff69ec6c819e8d785a2c780388feb.tar.gz |
COMPMID-597: Port HOGMultiDetection to new framework
Change-Id: I4b31b7f052a06bea4154d04c9926a0e076e28d73
Reviewed-on: https://eu-gerrit-1.euhpc.arm.com/126555
Tested-by: Jenkins <bsgcomp@arm.com>
Reviewed-by: John Richardson <john.richardson@arm.com>
Reviewed-by: Gian Marco Iodice <gianmarco.iodice@arm.com>
Reviewed-by: Anthony Barbier <anthony.barbier@arm.com>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/datasets/HOGMultiDetectionDataset.h | 170 | ||||
-rw-r--r-- | tests/validation/CL/HOGMultiDetection.cpp | 97 | ||||
-rw-r--r-- | tests/validation/NEON/HOGMultiDetection.cpp | 96 | ||||
-rw-r--r-- | tests/validation/fixtures/HOGMultiDetectionFixture.h | 193 | ||||
-rw-r--r-- | tests/validation/reference/HOGMultiDetection.cpp | 279 | ||||
-rw-r--r-- | tests/validation/reference/HOGMultiDetection.h | 48 |
6 files changed, 883 insertions, 0 deletions
diff --git a/tests/datasets/HOGMultiDetectionDataset.h b/tests/datasets/HOGMultiDetectionDataset.h new file mode 100644 index 0000000000..eb493d0792 --- /dev/null +++ b/tests/datasets/HOGMultiDetectionDataset.h @@ -0,0 +1,170 @@ +/* + * 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_HOG_MULTI_DETECTION_DATASET +#define ARM_COMPUTE_HOG_MULTI_DETECTION_DATASET + +#include "arm_compute/core/HOGInfo.h" +#include "tests/framework/datasets/Datasets.h" + +namespace arm_compute +{ +namespace test +{ +namespace datasets +{ +class HOGMultiDetectionDataset +{ +public: + using type = std::tuple<std::string, std::vector<HOGInfo>>; + + struct iterator + { + iterator(std::vector<std::string>::const_iterator image_it, + std::vector<std::string>::const_iterator hog_infos_name_it, + std::vector<std::vector<HOGInfo>>::const_iterator hog_infos_it) + : _image_it{ std::move(image_it) }, + _hog_infos_name_it{ std::move(hog_infos_name_it) }, + _hog_infos_it{ std::move(hog_infos_it) } + { + } + + std::string description() const + { + std::stringstream description; + description << "Image=" << *_image_it << ":"; + description << "HOGInfoSet=" << *_hog_infos_name_it; + + return description.str(); + } + + HOGMultiDetectionDataset::type operator*() const + { + return std::make_tuple(*_image_it, *_hog_infos_it); + } + + iterator &operator++() + { + ++_image_it; + ++_hog_infos_name_it; + ++_hog_infos_it; + + return *this; + } + + private: + std::vector<std::string>::const_iterator _image_it; + std::vector<std::string>::const_iterator _hog_infos_name_it; + std::vector<std::vector<HOGInfo>>::const_iterator _hog_infos_it; + }; + + iterator begin() const + { + return iterator(_image.begin(), _hog_infos_name.begin(), _hog_infos.begin()); + } + + int size() const + { + return std::min(_image.size(), _hog_infos.size()); + } + + void add_config(std::string image, + std::string hog_infos_name, + std::vector<HOGInfo> hog_info_vec) + { + _image.emplace_back(std::move(image)); + _hog_infos_name.emplace_back(std::move(hog_infos_name)); + _hog_infos.emplace_back(hog_info_vec); + } + +protected: + HOGMultiDetectionDataset() = default; + HOGMultiDetectionDataset(HOGMultiDetectionDataset &&) = default; + +private: + std::vector<std::string> _image{}; + std::vector<std::string> _hog_infos_name{}; + std::vector<std::vector<HOGInfo>> _hog_infos{}; +}; + +using MultiHOGDataset = std::vector<HOGInfo>; + +// *INDENT-OFF* +// clang-format off +static const MultiHOGDataset mixed +{ + // cell_size block_size detection_size block_stride bin normalization_type thresh phase_type + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(64U, 128U), Size2D(8U, 8U), 3U, HOGNormType::L1_NORM, 0.2f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(128U, 256U), Size2D(8U, 8U), 5U, HOGNormType::L1_NORM, 0.3f, PhaseType::SIGNED), + HOGInfo(Size2D(16U, 16U), Size2D(32U, 32U), Size2D(64U, 128U), Size2D(32U, 32U), 7U, HOGNormType::L1_NORM, 0.4f, PhaseType::SIGNED), + HOGInfo(Size2D(16U, 16U), Size2D(32U, 32U), Size2D(128U, 256U), Size2D(32U, 32U), 9U, HOGNormType::L1_NORM, 0.5f, PhaseType::SIGNED), +}; + +// cell_size and bin_size fixed +static const MultiHOGDataset skip_binning +{ + // cell_size block_size detection_size block_stride bin normalization_type thresh phase_type + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(64U, 128U), Size2D(8U, 8U), 9U, HOGNormType::L2HYS_NORM, 0.2f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(128U, 256U), Size2D(8U, 8U), 9U, HOGNormType::L2HYS_NORM, 0.2f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(32U, 32U), Size2D(64U, 128U), Size2D(16U, 16U), 9U, HOGNormType::L2HYS_NORM, 0.2f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(32U, 32U), Size2D(128U, 256U), Size2D(16U, 16U), 9U, HOGNormType::L2HYS_NORM, 0.2f, PhaseType::SIGNED), +}; + +// cell_size and bin_size and block_size and block_stride fixed +static const MultiHOGDataset skip_normalization +{ + // cell_size block_size detection_size block_stride bin normalization_type thresh phase_type + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(64U, 128U), Size2D(8U, 8U), 9U, HOGNormType::L2_NORM, 0.2f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(128U, 256U), Size2D(8U, 8U), 9U, HOGNormType::L2_NORM, 0.3f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(64U, 128U), Size2D(8U, 8U), 9U, HOGNormType::L2_NORM, 0.4f, PhaseType::SIGNED), + HOGInfo(Size2D(8U, 8U), Size2D(16U, 16U), Size2D(128U, 256U), Size2D(8U, 8U), 9U, HOGNormType::L2_NORM, 0.5f, PhaseType::SIGNED), +}; +// clang-format on +// *INDENT-ON* + +class SmallHOGMultiDetectionDataset final : public HOGMultiDetectionDataset +{ +public: + SmallHOGMultiDetectionDataset() + { + add_config("800x600.ppm", "MIXED", mixed); + add_config("800x600.ppm", "SKIP_BINNING", skip_binning); + add_config("800x600.ppm", "SKIP_NORMALIZATION", skip_normalization); + } +}; + +class LargeHOGMultiDetectionDataset final : public HOGMultiDetectionDataset +{ +public: + LargeHOGMultiDetectionDataset() + { + add_config("1920x1080.ppm", "MIXED", mixed); + add_config("1920x1080.ppm", "SKIP_BINNING", skip_binning); + add_config("1920x1080.ppm", "SKIP_NORMALIZATION", skip_normalization); + } +}; + +} // namespace datasets +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_HOG_MULTI_DETECTION_DATASET */ diff --git a/tests/validation/CL/HOGMultiDetection.cpp b/tests/validation/CL/HOGMultiDetection.cpp new file mode 100644 index 0000000000..634af416e2 --- /dev/null +++ b/tests/validation/CL/HOGMultiDetection.cpp @@ -0,0 +1,97 @@ +/* + * 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/runtime/CL/CLMultiHOG.h" +#include "arm_compute/runtime/CL/functions/CLHOGDescriptor.h" +#include "arm_compute/runtime/CL/functions/CLHOGMultiDetection.h" +#include "arm_compute/runtime/Tensor.h" +#include "tests/CL/CLAccessor.h" +#include "tests/CL/CLArrayAccessor.h" +#include "tests/CL/CLHOGAccessor.h" +#include "tests/datasets/HOGMultiDetectionDataset.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/HOGMultiDetectionFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/* Set the tolerance (percentage) used when validating the strength of detection window. */ +RelativeTolerance<float> tolerance(0.1f); +} // namespace + +TEST_SUITE(CL) +TEST_SUITE(HOGMultiDetection) + +// *INDENT-OFF* +// clang-format off +using CLHOGMultiDetectionFixture = HOGMultiDetectionValidationFixture<CLTensor, + CLHOG, + CLMultiHOG, + CLDetectionWindowArray, + CLSize2DArray, + CLAccessor, + CLArrayAccessor<Size2D>, + CLArrayAccessor<DetectionWindow>, + CLHOGAccessor, + CLHOGMultiDetection, + uint8_t, + float>; + + +FIXTURE_DATA_TEST_CASE(RunSmall, CLHOGMultiDetectionFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine( + datasets::SmallHOGMultiDetectionDataset(), + framework::dataset::make("Format", Format::U8)), + framework::dataset::make("BorderMode", {BorderMode::CONSTANT, BorderMode::REPLICATE})), + framework::dataset::make("NonMaximaSuppression", {false, true}))) +{ + // Validate output + validate_detection_windows(_target.begin(), _target.end(), _reference.begin(), _reference.end(), tolerance); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, CLHOGMultiDetectionFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine( + datasets::LargeHOGMultiDetectionDataset(), + framework::dataset::make("Format", Format::U8)), + framework::dataset::make("BorderMode", {BorderMode::CONSTANT, BorderMode::REPLICATE})), + framework::dataset::make("NonMaximaSuppression", {false, true}))) +{ + // Validate output + validate_detection_windows(_target.begin(), _target.end(), _reference.begin(), _reference.end(), tolerance); +} + +// clang-format on +// *INDENT-ON* + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/NEON/HOGMultiDetection.cpp b/tests/validation/NEON/HOGMultiDetection.cpp new file mode 100644 index 0000000000..d6017e000c --- /dev/null +++ b/tests/validation/NEON/HOGMultiDetection.cpp @@ -0,0 +1,96 @@ +/* + * 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/runtime/MultiHOG.h" +#include "arm_compute/runtime/NEON/functions/NEHOGDescriptor.h" +#include "arm_compute/runtime/NEON/functions/NEHOGMultiDetection.h" +#include "arm_compute/runtime/Tensor.h" +#include "tests/NEON/Accessor.h" +#include "tests/NEON/ArrayAccessor.h" +#include "tests/NEON/HOGAccessor.h" +#include "tests/datasets/HOGMultiDetectionDataset.h" +#include "tests/framework/Macros.h" +#include "tests/framework/datasets/Datasets.h" +#include "tests/validation/Validation.h" +#include "tests/validation/fixtures/HOGMultiDetectionFixture.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace +{ +/* Set the tolerance (percentage) used when validating the strength of detection window. */ +RelativeTolerance<float> tolerance(1.0f); +} // namespace + +TEST_SUITE(NEON) +TEST_SUITE(HOGMultiDetection) + +// *INDENT-OFF* +// clang-format off +using NEHOGMultiDetectionFixture = HOGMultiDetectionValidationFixture<Tensor, + HOG, + MultiHOG, + DetectionWindowArray, + Size2DArray, + Accessor, + ArrayAccessor<Size2D>, + ArrayAccessor<DetectionWindow>, + HOGAccessor, + NEHOGMultiDetection, + uint8_t, + float>; + +FIXTURE_DATA_TEST_CASE(RunSmall, NEHOGMultiDetectionFixture, framework::DatasetMode::PRECOMMIT, + combine(combine(combine( + datasets::SmallHOGMultiDetectionDataset(), + framework::dataset::make("Format", Format::U8)), + framework::dataset::make("BorderMode", {BorderMode::CONSTANT, BorderMode::REPLICATE})), + framework::dataset::make("NonMaximaSuppression", {false, true}))) +{ + // Validate output + validate_detection_windows(_target.begin(), _target.end(), _reference.begin(), _reference.end(), tolerance); +} + +FIXTURE_DATA_TEST_CASE(RunLarge, NEHOGMultiDetectionFixture, framework::DatasetMode::NIGHTLY, + combine(combine(combine( + datasets::LargeHOGMultiDetectionDataset(), + framework::dataset::make("Format", Format::U8)), + framework::dataset::make("BorderMode", {BorderMode::CONSTANT, BorderMode::REPLICATE})), + framework::dataset::make("NonMaximaSuppression", {false, true}))) +{ + // Validate output + validate_detection_windows(_target.begin(), _target.end(), _reference.begin(), _reference.end(), tolerance); +} + +// clang-format on +// *INDENT-ON* + +TEST_SUITE_END() +TEST_SUITE_END() +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/fixtures/HOGMultiDetectionFixture.h b/tests/validation/fixtures/HOGMultiDetectionFixture.h new file mode 100644 index 0000000000..039f3f4b74 --- /dev/null +++ b/tests/validation/fixtures/HOGMultiDetectionFixture.h @@ -0,0 +1,193 @@ +/* + * 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_HOG_MULTI_DETECTION_FIXTURE +#define ARM_COMPUTE_TEST_HOG_MULTI_DETECTION_FIXTURE + +#include "arm_compute/core/HOGInfo.h" +#include "arm_compute/core/TensorShape.h" +#include "arm_compute/core/Types.h" +#include "tests/AssetsLibrary.h" +#include "tests/Globals.h" +#include "tests/IAccessor.h" +#include "tests/IHOGAccessor.h" +#include "tests/framework/Asserts.h" +#include "tests/framework/Fixture.h" +#include "tests/validation/reference/HOGMultiDetection.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +template <typename TensorType, + typename HOGType, + typename MultiHOGType, + typename DetectionWindowArrayType, + typename DetectionWindowStrideType, + typename AccessorType, + typename Size2DArrayAccessorType, + typename DetectionWindowArrayAccessorType, + typename HOGAccessorType, + typename FunctionType, + typename T, + typename U> +class HOGMultiDetectionValidationFixture : public framework::Fixture +{ +public: + template <typename...> + void setup(std::string image, std::vector<HOGInfo> models, Format format, BorderMode border_mode, bool non_maxima_suppression) + { + // Only defined borders supported + ARM_COMPUTE_ERROR_ON(border_mode == BorderMode::UNDEFINED); + + // Generate a random constant value + std::mt19937 gen(library->seed()); + std::uniform_int_distribution<T> int_dist(0, 255); + const T constant_border_value = int_dist(gen); + + // Initialize descriptors vector + std::vector<std::vector<U>> descriptors(models.size()); + + // Use default values for threshold and min_distance + const float threshold = 0.f; + const float min_distance = 1.f; + + // Maximum number of detection windows per batch + const unsigned int max_num_detection_windows = 100000; + + _target = compute_target(image, format, border_mode, constant_border_value, models, descriptors, max_num_detection_windows, threshold, non_maxima_suppression, min_distance); + _reference = compute_reference(image, format, border_mode, constant_border_value, models, descriptors, max_num_detection_windows, threshold, non_maxima_suppression, min_distance); + } + +protected: + template <typename V> + void fill(V &&tensor, const std::string image, Format format) + { + library->fill(tensor, image, format); + } + + void initialize_batch(const std::vector<HOGInfo> &models, MultiHOGType &multi_hog, + std::vector<std::vector<U>> &descriptors, DetectionWindowStrideType &detection_window_strides) + { + for(unsigned i = 0; i < models.size(); ++i) + { + auto hog_model = reinterpret_cast<HOGType *>(multi_hog.model(i)); + hog_model->init(models[i]); + + // Initialise descriptor (linear SVM coefficients). + std::random_device::result_type seed = 0; + descriptors.at(i) = generate_random_real(models[i].descriptor_size(), -0.505f, 0.495f, seed); + + // Copy HOG descriptor values to HOG memory + { + HOGAccessorType hog_accessor(*hog_model); + std::memcpy(hog_accessor.descriptor(), descriptors.at(i).data(), descriptors.at(i).size() * sizeof(U)); + } + + // Initialize detection window stride + Size2DArrayAccessorType accessor(detection_window_strides); + accessor.at(i) = models[i].block_stride(); + } + } + + std::vector<DetectionWindow> compute_target(const std::string image, Format &format, BorderMode &border_mode, T constant_border_value, + const std::vector<HOGInfo> &models, std::vector<std::vector<U>> &descriptors, unsigned int max_num_detection_windows, + float threshold, bool non_max_suppression, float min_distance) + { + MultiHOGType multi_hog(models.size()); + DetectionWindowArrayType detection_windows(max_num_detection_windows); + DetectionWindowStrideType detection_window_strides(models.size()); + + // Resize detection window_strides for index access + detection_window_strides.resize(models.size()); + + // Initialiize MultiHOG and detection windows + initialize_batch(models, multi_hog, descriptors, detection_window_strides); + + // Get image shape for src tensor + TensorShape shape = library->get_image_shape(image); + + // Create tensors + TensorType src = create_tensor<TensorType>(shape, data_type_from_format(format)); + ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Create and configure function + FunctionType hog_multi_detection; + hog_multi_detection.configure(&src, &multi_hog, &detection_windows, &detection_window_strides, border_mode, constant_border_value, threshold, non_max_suppression, min_distance); + + // Reset detection windows + detection_windows.clear(); + + // Allocate tensors + src.allocator()->allocate(); + ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS); + + // Fill tensors + fill(AccessorType(src), image, format); + + // Compute function + hog_multi_detection.run(); + + // Copy detection windows + std::vector<DetectionWindow> windows; + DetectionWindowArrayAccessorType accessor(detection_windows); + + for(size_t i = 0; i < accessor.num_values(); i++) + { + DetectionWindow win; + win.x = accessor.at(i).x; + win.y = accessor.at(i).y; + win.width = accessor.at(i).width; + win.height = accessor.at(i).height; + win.idx_class = accessor.at(i).idx_class; + win.score = accessor.at(i).score; + + windows.push_back(win); + } + + return windows; + } + + std::vector<DetectionWindow> compute_reference(const std::string image, Format format, BorderMode border_mode, T constant_border_value, + const std::vector<HOGInfo> &models, const std::vector<std::vector<U>> &descriptors, unsigned int max_num_detection_windows, + float threshold, bool non_max_suppression, float min_distance) + { + // Create reference + SimpleTensor<T> src{ library->get_image_shape(image), data_type_from_format(format) }; + + // Fill reference + fill(src, image, format); + + // NOTE: Detection window stride fixed to block stride + return reference::hog_multi_detection(src, border_mode, constant_border_value, models, descriptors, max_num_detection_windows, threshold, non_max_suppression, min_distance); + } + + std::vector<DetectionWindow> _target{}; + std::vector<DetectionWindow> _reference{}; +}; +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* ARM_COMPUTE_TEST_HOG_MULTI_DETECTION_FIXTURE */ diff --git a/tests/validation/reference/HOGMultiDetection.cpp b/tests/validation/reference/HOGMultiDetection.cpp new file mode 100644 index 0000000000..2f5e4395a0 --- /dev/null +++ b/tests/validation/reference/HOGMultiDetection.cpp @@ -0,0 +1,279 @@ +/* + * Copyright (c) 2017-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 "HOGMultiDetection.h" + +#include "Derivative.h" +#include "HOGDescriptor.h" +#include "HOGDetector.h" +#include "Magnitude.h" +#include "Phase.h" + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +namespace +{ +void validate_models(const std::vector<HOGInfo> &models) +{ + ARM_COMPUTE_ERROR_ON(0 == models.size()); + + for(size_t i = 1; i < models.size(); ++i) + { + ARM_COMPUTE_ERROR_ON_MSG(models[0].phase_type() != models[i].phase_type(), + "All HOG parameters must have the same phase type"); + + ARM_COMPUTE_ERROR_ON_MSG(models[0].normalization_type() != models[i].normalization_type(), + "All HOG parameters must have the same normalization_type"); + + ARM_COMPUTE_ERROR_ON_MSG((models[0].l2_hyst_threshold() != models[i].l2_hyst_threshold()) && (models[0].normalization_type() == arm_compute::HOGNormType::L2HYS_NORM), + "All HOG parameters must have the same l2 hysteresis threshold if you use L2 hysteresis normalization type"); + } +} +} // namespace + +void detection_windows_non_maxima_suppression(std::vector<DetectionWindow> &multi_windows, float min_distance) +{ + const size_t num_candidates = multi_windows.size(); + size_t num_detections = 0; + + // Sort by idx_class first and by score second + std::sort(multi_windows.begin(), multi_windows.end(), [](const DetectionWindow & lhs, const DetectionWindow & rhs) + { + if(lhs.idx_class < rhs.idx_class) + { + return true; + } + if(rhs.idx_class < lhs.idx_class) + { + return false; + } + + // idx_classes are equal so compare by score + if(lhs.score > rhs.score) + { + return true; + } + if(rhs.score > lhs.score) + { + return false; + } + + return false; + }); + + const float min_distance_pow2 = min_distance * min_distance; + + // Euclidean distance + for(size_t i = 0; i < num_candidates; ++i) + { + if(0.0f != multi_windows.at(i).score) + { + DetectionWindow cur; + cur.x = multi_windows.at(i).x; + cur.y = multi_windows.at(i).y; + cur.width = multi_windows.at(i).width; + cur.height = multi_windows.at(i).height; + cur.idx_class = multi_windows.at(i).idx_class; + cur.score = multi_windows.at(i).score; + + // Store window + multi_windows.at(num_detections) = cur; + ++num_detections; + + const float xc = cur.x + cur.width * 0.5f; + const float yc = cur.y + cur.height * 0.5f; + + for(size_t k = i + 1; k < (num_candidates) && (cur.idx_class == multi_windows.at(k).idx_class); ++k) + { + const float xn = multi_windows.at(k).x + multi_windows.at(k).width * 0.5f; + const float yn = multi_windows.at(k).y + multi_windows.at(k).height * 0.5f; + + const float dx = std::fabs(xn - xc); + const float dy = std::fabs(yn - yc); + + if(dx < min_distance && dy < min_distance) + { + const float d = dx * dx + dy * dy; + + if(d < min_distance_pow2) + { + // Invalidate detection window + multi_windows.at(k).score = 0.0f; + } + } + } + } + } + + multi_windows.resize(num_detections); +} + +template <typename T> +std::vector<DetectionWindow> hog_multi_detection(const SimpleTensor<T> &src, BorderMode border_mode, T constant_border_value, + const std::vector<HOGInfo> &models, std::vector<std::vector<float>> descriptors, + unsigned int max_num_detection_windows, float threshold, bool non_maxima_suppression, float min_distance) +{ + ARM_COMPUTE_ERROR_ON(descriptors.size() != models.size()); + validate_models(models); + + const size_t width = src.shape().x(); + const size_t height = src.shape().y(); + const size_t num_models = models.size(); + + // Initialize previous values + size_t prev_num_bins = models[0].num_bins(); + Size2D prev_cell_size = models[0].cell_size(); + Size2D prev_block_size = models[0].block_size(); + Size2D prev_block_stride = models[0].block_stride(); + + std::vector<size_t> input_orient_bin; + std::vector<size_t> input_hog_detect; + std::vector<std::pair<size_t, size_t>> input_block_norm; + + input_orient_bin.push_back(0); + input_hog_detect.push_back(0); + input_block_norm.emplace_back(0, 0); + + // Iterate through the number of models and check if orientation binning + // and block normalization steps can be skipped + for(size_t i = 1; i < num_models; ++i) + { + size_t cur_num_bins = models[i].num_bins(); + Size2D cur_cell_size = models[i].cell_size(); + Size2D cur_block_size = models[i].block_size(); + Size2D cur_block_stride = models[i].block_stride(); + + // Check if binning and normalization steps are required + if((cur_num_bins != prev_num_bins) || (cur_cell_size.width != prev_cell_size.width) || (cur_cell_size.height != prev_cell_size.height)) + { + prev_num_bins = cur_num_bins; + prev_cell_size = cur_cell_size; + prev_block_size = cur_block_size; + prev_block_stride = cur_block_stride; + + // Compute orientation binning and block normalization. Update input to process + input_orient_bin.push_back(i); + input_block_norm.emplace_back(i, input_orient_bin.size() - 1); + } + else if((cur_block_size.width != prev_block_size.width) || (cur_block_size.height != prev_block_size.height) || (cur_block_stride.width != prev_block_stride.width) + || (cur_block_stride.height != prev_block_stride.height)) + { + prev_block_size = cur_block_size; + prev_block_stride = cur_block_stride; + + // Compute block normalization. Update input to process + input_block_norm.emplace_back(i, input_orient_bin.size() - 1); + } + + // Update input to process for hog detector + input_hog_detect.push_back(input_block_norm.size() - 1); + } + + size_t num_orient_bin = input_orient_bin.size(); + size_t num_block_norm = input_block_norm.size(); + size_t num_hog_detect = input_hog_detect.size(); + + std::vector<SimpleTensor<float>> hog_spaces(num_orient_bin); + std::vector<SimpleTensor<float>> hog_norm_spaces(num_block_norm); + + // Calculate derivative + SimpleTensor<int16_t> grad_x; + SimpleTensor<int16_t> grad_y; + std::tie(grad_x, grad_y) = derivative<int16_t>(src, border_mode, constant_border_value, GradientDimension::GRAD_XY); + + // Calculate magnitude and phase + SimpleTensor<int16_t> _mag = magnitude(grad_x, grad_y, MagnitudeType::L2NORM); + SimpleTensor<uint8_t> _phase = phase(grad_x, grad_y, models[0].phase_type()); + + // Calculate Tensors for the HOG space and orientation binning + for(size_t i = 0; i < num_orient_bin; ++i) + { + const size_t idx_multi_hog = input_orient_bin[i]; + + const size_t num_bins = models[idx_multi_hog].num_bins(); + const size_t num_cells_x = width / models[idx_multi_hog].cell_size().width; + const size_t num_cells_y = height / models[idx_multi_hog].cell_size().height; + + // TensorShape of hog space + TensorShape hog_space_shape(num_cells_x, num_cells_y); + + // Initialise HOG space + TensorInfo info_hog_space(hog_space_shape, num_bins, DataType::F32); + hog_spaces.at(i) = SimpleTensor<float>(info_hog_space.tensor_shape(), DataType::F32, info_hog_space.num_channels()); + + // For each cell create histogram based on magnitude and phase + hog_orientation_binning(_mag, _phase, hog_spaces[i], models[idx_multi_hog]); + } + + // Calculate Tensors for the normalized HOG space and block normalization + for(size_t i = 0; i < num_block_norm; ++i) + { + const size_t idx_multi_hog = input_block_norm[i].first; + const size_t idx_orient_bin = input_block_norm[i].second; + + // Create tensor info for HOG descriptor + TensorInfo tensor_info(models[idx_multi_hog], src.shape().x(), src.shape().y()); + hog_norm_spaces.at(i) = SimpleTensor<float>(tensor_info.tensor_shape(), DataType::F32, tensor_info.num_channels()); + + // Normalize histograms based on block size + hog_block_normalization(hog_norm_spaces[i], hog_spaces[idx_orient_bin], models[idx_multi_hog]); + } + + std::vector<DetectionWindow> multi_windows; + + // Calculate Detection Windows for HOG detector + for(size_t i = 0; i < num_hog_detect; ++i) + { + const size_t idx_block_norm = input_hog_detect[i]; + + // NOTE: Detection window stride fixed to block stride + const Size2D detection_window_stride = models[i].block_stride(); + + std::vector<DetectionWindow> windows = hog_detector(hog_norm_spaces[idx_block_norm], descriptors[i], + max_num_detection_windows, models[i], detection_window_stride, threshold, i); + + multi_windows.insert(multi_windows.end(), windows.begin(), windows.end()); + } + + // Suppress Non-maxima detection windows + if(non_maxima_suppression) + { + detection_windows_non_maxima_suppression(multi_windows, min_distance); + } + + return multi_windows; +} + +template std::vector<DetectionWindow> hog_multi_detection(const SimpleTensor<uint8_t> &src, BorderMode border_mode, uint8_t constant_border_value, + const std::vector<HOGInfo> &models, std::vector<std::vector<float>> descriptors, + unsigned int max_num_detection_windows, float threshold, bool non_maxima_suppression, float min_distance); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute diff --git a/tests/validation/reference/HOGMultiDetection.h b/tests/validation/reference/HOGMultiDetection.h new file mode 100644 index 0000000000..6d75bf4a8e --- /dev/null +++ b/tests/validation/reference/HOGMultiDetection.h @@ -0,0 +1,48 @@ +/* + * 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_HOG_MULTI_DETECTION_H__ +#define __ARM_COMPUTE_TEST_HOG_MULTI_DETECTION_H__ + +#include "arm_compute/core/Types.h" +#include "tests/SimpleTensor.h" + +#include <vector> + +namespace arm_compute +{ +namespace test +{ +namespace validation +{ +namespace reference +{ +template <typename T> +std::vector<DetectionWindow> hog_multi_detection(const SimpleTensor<T> &src, BorderMode border_mode, T constant_border_value, + const std::vector<HOGInfo> &models, std::vector<std::vector<float>> descriptors, + unsigned int max_num_detection_windows, float threshold = 0.0f, bool non_maxima_suppression = false, float min_distance = 1.0f); +} // namespace reference +} // namespace validation +} // namespace test +} // namespace arm_compute +#endif /* __ARM_COMPUTE_TEST_HOG_MULTI_DETECTION_H__ */ |