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