blob: 4666cdf31b48ca61b84ab4f868817afa373d979f [file] [log] [blame]
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +01001/*
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 Pflanzerfb5aabb2017-07-18 14:39:55 +010024#include "AssetsLibrary.h"
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010025#include "Globals.h"
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010026#include "NEON/NEAccessor.h"
27#include "PaddingCalculator.h"
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010028#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
47using namespace arm_compute;
48using namespace arm_compute::test;
49using namespace arm_compute::test::neon;
50using namespace arm_compute::test::validation;
51
52namespace
53{
54/** Compute NonLinearFilter function.
55 *
56 * @param[in] input Shape of the input and output tensors.
57 * @param[in] function Non linear function to perform
58 * @param[in] mask_size Mask size. Supported sizes: 3, 5
59 * @param[in] pattern Mask pattern
60 * @param[in] mask The given mask. Will be used only if pattern is specified to PATTERN_OTHER
61 * @param[in] border_mode Strategy to use for borders.
62 * @param[in] constant_border_value (Optional) Constant value to use for borders if border_mode is set to CONSTANT.
63 *
64 * @return Computed output tensor.
65 */
66Tensor compute_non_linear_filter(const TensorShape &shape, NonLinearFilterFunction function, unsigned int mask_size,
67 MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode,
68 uint8_t constant_border_value)
69{
70 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010071 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
72 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +010073
74 // Create and configure function
75 NENonLinearFilter filter;
76 filter.configure(&src, &dst, function, mask_size, pattern, mask, border_mode, constant_border_value);
77
78 // Allocate tensors
79 src.allocator()->allocate();
80 dst.allocator()->allocate();
81
82 BOOST_TEST(!src.info()->is_resizable());
83 BOOST_TEST(!dst.info()->is_resizable());
84
85 // Fill tensors
86 library->fill_tensor_uniform(NEAccessor(src), 0);
87
88 // Compute function
89 filter.run();
90
91 return dst;
92}
93} // namespace
94
95#ifndef DOXYGEN_SKIP_THIS
96BOOST_AUTO_TEST_SUITE(NEON)
97BOOST_AUTO_TEST_SUITE(NonLinearFilter)
98
99BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
100BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes())
101 * NonLinearFilterFunctions() * boost::unit_test::data::make({ 3U, 5U })
102 * MatrixPatterns() * BorderModes(),
103 shape, function, mask_size, pattern, border_mode)
104{
105 std::mt19937 generator(user_config.seed.get());
106 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
107 const uint8_t constant_border_value = distribution_u8(generator);
108
109 // Create the mask
110 uint8_t mask[mask_size * mask_size];
111 fill_mask_from_pattern(mask, mask_size, mask_size, pattern);
112 const auto half_mask_size = static_cast<int>(mask_size / 2);
113
114 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100115 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
116 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100117
118 BOOST_TEST(src.info()->is_resizable());
119 BOOST_TEST(dst.info()->is_resizable());
120
121 // Create and configure function
122 NENonLinearFilter filter;
123 filter.configure(&src, &dst, function, mask_size, pattern, mask, border_mode, constant_border_value);
124
125 // Validate valid region
126 const ValidRegion src_valid_region = shape_to_valid_region(shape);
SiCong Li7a035752017-06-28 15:27:02 +0100127 const ValidRegion dst_valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, BorderSize(half_mask_size));
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100128
129 validate(src.info()->valid_region(), src_valid_region);
130 validate(dst.info()->valid_region(), dst_valid_region);
131
132 // Validate padding
133 PaddingCalculator calculator(shape.x(), ((MatrixPattern::OTHER == pattern) ? 1 : 8));
134 calculator.set_border_mode(border_mode);
135 calculator.set_border_size(half_mask_size);
136
137 const PaddingSize write_padding = calculator.required_padding(PaddingCalculator::Option::EXCLUDE_BORDER);
138
139 calculator.set_accessed_elements(16);
140 calculator.set_access_offset(-half_mask_size);
141
142 const PaddingSize read_padding = calculator.required_padding(PaddingCalculator::Option::INCLUDE_BORDER);
143
144 validate(src.info()->padding(), read_padding);
145 validate(dst.info()->padding(), write_padding);
146}
147
148BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
149BOOST_DATA_TEST_CASE(RunSmall, SmallShapes()
150 * NonLinearFilterFunctions() * boost::unit_test::data::make({ 3U, 5U })
151 * MatrixPatterns() * BorderModes(),
152 shape, function, mask_size, pattern, border_mode)
153{
154 std::mt19937 generator(user_config.seed.get());
155 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
156 const uint8_t constant_border_value = distribution_u8(generator);
157
158 // Create the mask
159 uint8_t mask[mask_size * mask_size];
160 fill_mask_from_pattern(mask, mask_size, mask_size, pattern);
161
162 // Compute function
163 Tensor dst = compute_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value);
164
165 // Compute reference
166 RawTensor ref_dst = Reference::compute_reference_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value);
167
168 // Calculate valid region
SiCong Li7a035752017-06-28 15:27:02 +0100169 const ValidRegion valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, BorderSize(static_cast<int>(mask_size / 2)));
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100170
171 // Validate output
172 validate(NEAccessor(dst), ref_dst, valid_region);
173}
174
175BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
176BOOST_DATA_TEST_CASE(RunLarge, LargeShapes()
177 * NonLinearFilterFunctions() * boost::unit_test::data::make({ 3U, 5U })
178 * MatrixPatterns() * BorderModes(),
179 shape, function, mask_size, pattern, border_mode)
180{
181 std::mt19937 generator(user_config.seed.get());
182 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
183 const uint8_t constant_border_value = distribution_u8(generator);
184
185 // Create the mask
186 uint8_t mask[mask_size * mask_size];
187 fill_mask_from_pattern(mask, mask_size, mask_size, pattern);
188
189 // Compute function
190 Tensor dst = compute_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value);
191
192 // Compute reference
193 RawTensor ref_dst = Reference::compute_reference_non_linear_filter(shape, function, mask_size, pattern, mask, border_mode, constant_border_value);
194
195 // Calculate valid region
SiCong Li7a035752017-06-28 15:27:02 +0100196 const ValidRegion valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, BorderSize(static_cast<int>(mask_size / 2)));
Isabella Gottardi3b77e9d2017-06-22 11:05:41 +0100197
198 // Validate output
199 validate(NEAccessor(dst), ref_dst, valid_region);
200}
201
202BOOST_AUTO_TEST_SUITE_END()
203BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100204#endif /* DOXYGEN_SKIP_THIS */