Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2017 ARM Limited. |
| 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
Moritz Pflanzer | fb5aabb | 2017-07-18 14:39:55 +0100 | [diff] [blame] | 24 | #include "AssetsLibrary.h" |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 25 | #include "Globals.h" |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 26 | #include "NEON/Accessor.h" |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 27 | #include "PaddingCalculator.h" |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 28 | #include "TypePrinter.h" |
| 29 | #include "Utils.h" |
| 30 | #include "validation/Datasets.h" |
| 31 | #include "validation/Helpers.h" |
| 32 | #include "validation/Reference.h" |
| 33 | #include "validation/Validation.h" |
| 34 | #include "validation/ValidationUserConfiguration.h" |
| 35 | |
| 36 | #include "arm_compute/core/Helpers.h" |
| 37 | #include "arm_compute/core/Types.h" |
| 38 | #include "arm_compute/runtime/NEON/functions/NENonLinearFilter.h" |
| 39 | #include "arm_compute/runtime/Tensor.h" |
| 40 | #include "arm_compute/runtime/TensorAllocator.h" |
| 41 | |
| 42 | #include "boost_wrapper.h" |
| 43 | |
| 44 | #include <random> |
| 45 | #include <string> |
| 46 | |
| 47 | using namespace arm_compute; |
| 48 | using namespace arm_compute::test; |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 49 | using namespace arm_compute::test::validation; |
| 50 | |
| 51 | namespace |
| 52 | { |
| 53 | /** Compute NonLinearFilter function. |
| 54 | * |
| 55 | * @param[in] input Shape of the input and output tensors. |
| 56 | * @param[in] function Non linear function to perform |
| 57 | * @param[in] mask_size Mask size. Supported sizes: 3, 5 |
| 58 | * @param[in] pattern Mask pattern |
| 59 | * @param[in] mask The given mask. Will be used only if pattern is specified to PATTERN_OTHER |
| 60 | * @param[in] border_mode Strategy to use for borders. |
| 61 | * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT. |
| 62 | * |
| 63 | * @return Computed output tensor. |
| 64 | */ |
| 65 | Tensor compute_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size, |
| 66 | MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, |
| 67 | uint8_t constant_border_value) |
| 68 | { |
| 69 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 70 | Tensor src = create_tensor<Tensor>(shape, DataType::U8); |
| 71 | Tensor dst = create_tensor<Tensor>(shape, DataType::U8); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 72 | |
| 73 | // Create and configure function |
| 74 | NENonLinearFilter filter; |
| 75 | filter.configure(&src, &dst, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 76 | |
| 77 | // Allocate tensors |
| 78 | src.allocator()->allocate(); |
| 79 | dst.allocator()->allocate(); |
| 80 | |
| 81 | BOOST_TEST(!src.info()->is_resizable()); |
| 82 | BOOST_TEST(!dst.info()->is_resizable()); |
| 83 | |
| 84 | // Fill tensors |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 85 | library->fill_tensor_uniform(Accessor(src), 0); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 86 | |
| 87 | // Compute function |
| 88 | filter.run(); |
| 89 | |
| 90 | return dst; |
| 91 | } |
| 92 | } // namespace |
| 93 | |
| 94 | #ifndef DOXYGEN_SKIP_THIS |
| 95 | BOOST_AUTO_TEST_SUITE(NEON) |
| 96 | BOOST_AUTO_TEST_SUITE(NonLinearFilter) |
| 97 | |
| 98 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly")) |
| 99 | BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) |
| 100 | * NonLinearFilterFunctions() * boost::unit_test::data::make({ 3U, 5U }) |
| 101 | * MatrixPatterns() * BorderModes(), |
| 102 | shape, function, mask_size, pattern, border_mode) |
| 103 | { |
| 104 | std::mt19937 generator(user_config.seed.get()); |
| 105 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 106 | const uint8_t constant_border_value = distribution_u8(generator); |
| 107 | |
| 108 | // Create the mask |
| 109 | uint8_t mask[mask_size * mask_size]; |
| 110 | fill_mask_from_pattern(mask, mask_size, mask_size, pattern); |
| 111 | const auto half_mask_size = static_cast<int>(mask_size / 2); |
| 112 | |
| 113 | // Create tensors |
Moritz Pflanzer | 94450f1 | 2017-06-30 12:48:43 +0100 | [diff] [blame] | 114 | Tensor src = create_tensor<Tensor>(shape, DataType::U8); |
| 115 | Tensor dst = create_tensor<Tensor>(shape, DataType::U8); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 116 | |
| 117 | BOOST_TEST(src.info()->is_resizable()); |
| 118 | BOOST_TEST(dst.info()->is_resizable()); |
| 119 | |
| 120 | // Create and configure function |
| 121 | NENonLinearFilter filter; |
| 122 | filter.configure(&src, &dst, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 123 | |
| 124 | // Validate valid region |
| 125 | const ValidRegion src_valid_region = shape_to_valid_region(shape); |
SiCong Li | 7a03575 | 2017-06-28 15:27:02 +0100 | [diff] [blame] | 126 | const ValidRegion dst_valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, BorderSize(half_mask_size)); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 127 | |
| 128 | validate(src.info()->valid_region(), src_valid_region); |
| 129 | validate(dst.info()->valid_region(), dst_valid_region); |
| 130 | |
| 131 | // Validate padding |
| 132 | PaddingCalculator calculator(shape.x(), ((MatrixPattern::OTHER == pattern) ? 1 : 8)); |
| 133 | calculator.set_border_mode(border_mode); |
| 134 | calculator.set_border_size(half_mask_size); |
| 135 | |
| 136 | const PaddingSize write_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER); |
| 137 | |
| 138 | calculator.set_accessed_elements(16); |
| 139 | calculator.set_access_offset(-half_mask_size); |
| 140 | |
| 141 | const PaddingSize read_padding = calculator.required_padding(PaddingCalculator::Option::INCLUDE_BORDER); |
| 142 | |
| 143 | validate(src.info()->padding(), read_padding); |
| 144 | validate(dst.info()->padding(), write_padding); |
| 145 | } |
| 146 | |
| 147 | BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit")) |
| 148 | BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() |
| 149 | * NonLinearFilterFunctions() * boost::unit_test::data::make({ 3U, 5U }) |
| 150 | * MatrixPatterns() * BorderModes(), |
| 151 | shape, function, mask_size, pattern, border_mode) |
| 152 | { |
| 153 | std::mt19937 generator(user_config.seed.get()); |
| 154 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 155 | const uint8_t constant_border_value = distribution_u8(generator); |
| 156 | |
| 157 | // Create the mask |
| 158 | uint8_t mask[mask_size * mask_size]; |
| 159 | fill_mask_from_pattern(mask, mask_size, mask_size, pattern); |
| 160 | |
| 161 | // Compute function |
| 162 | Tensor dst = compute_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 163 | |
| 164 | // Compute reference |
| 165 | RawTensor ref_dst = Reference::compute_reference_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 166 | |
| 167 | // Calculate valid region |
SiCong Li | 7a03575 | 2017-06-28 15:27:02 +0100 | [diff] [blame] | 168 | const ValidRegion valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, BorderSize(static_cast<int>(mask_size / 2))); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 169 | |
| 170 | // Validate output |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 171 | validate(Accessor(dst), ref_dst, valid_region); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly")) |
| 175 | BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() |
| 176 | * NonLinearFilterFunctions() * boost::unit_test::data::make({ 3U, 5U }) |
| 177 | * MatrixPatterns() * BorderModes(), |
| 178 | shape, function, mask_size, pattern, border_mode) |
| 179 | { |
| 180 | std::mt19937 generator(user_config.seed.get()); |
| 181 | std::uniform_int_distribution<uint8_t> distribution_u8(0, 255); |
| 182 | const uint8_t constant_border_value = distribution_u8(generator); |
| 183 | |
| 184 | // Create the mask |
| 185 | uint8_t mask[mask_size * mask_size]; |
| 186 | fill_mask_from_pattern(mask, mask_size, mask_size, pattern); |
| 187 | |
| 188 | // Compute function |
| 189 | Tensor dst = compute_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 190 | |
| 191 | // Compute reference |
| 192 | RawTensor ref_dst = Reference::compute_reference_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value); |
| 193 | |
| 194 | // Calculate valid region |
SiCong Li | 7a03575 | 2017-06-28 15:27:02 +0100 | [diff] [blame] | 195 | const ValidRegion valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, BorderSize(static_cast<int>(mask_size / 2))); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 196 | |
| 197 | // Validate output |
Moritz Pflanzer | d58cec0 | 2017-07-18 15:44:21 +0100 | [diff] [blame] | 198 | validate(Accessor(dst), ref_dst, valid_region); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | BOOST_AUTO_TEST_SUITE_END() |
| 202 | BOOST_AUTO_TEST_SUITE_END() |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 203 | #endif /* DOXYGEN_SKIP_THIS */ |