blob: ded9e737f3750577c8061935a6befebdd8f5385f [file] [log] [blame]
Giorgio Arena50f9fd72017-06-19 17:05:30 +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
25#include "Globals.h"
Giorgio Arena50f9fd72017-06-19 17:05:30 +010026#include "NEON/NEAccessor.h"
27#include "TensorLibrary.h"
28#include "TypePrinter.h"
29#include "Utils.h"
30#include "validation/Datasets.h"
31#include "validation/Reference.h"
32#include "validation/Validation.h"
33#include "validation/ValidationUserConfiguration.h"
34
35#include "arm_compute/core/Helpers.h"
36#include "arm_compute/core/Types.h"
37#include "arm_compute/runtime/NEON/functions/NESobel3x3.h"
38#include "arm_compute/runtime/Tensor.h"
39#include "arm_compute/runtime/TensorAllocator.h"
40
41#include "PaddingCalculator.h"
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{
SiCong Li7a035752017-06-28 15:27:02 +010054constexpr unsigned int filter_size = 3; /** Size of the kernel/filter in number of elements. */
55constexpr BorderSize border_size(filter_size / 2); /** Border size of the kernel/filter around its central element. */
56
Giorgio Arena50f9fd72017-06-19 17:05:30 +010057/** Compute Neon Sobel 3x3 function.
58 *
59 * @param[in] shape Shape of the input and output tensors.
60 * @param[in] border_mode BorderMode used by the input tensor
61 * @param[in] constant_border_value Constant to use if @p border_mode == CONSTANT
62 *
63 * @return Computed output tensor.
64 */
65std::pair<Tensor, Tensor> compute_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
66{
67 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +010068 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
69 Tensor dst_x = create_tensor<Tensor>(shape, DataType::S16);
70 Tensor dst_y = create_tensor<Tensor>(shape, DataType::S16);
Giorgio Arena50f9fd72017-06-19 17:05:30 +010071
72 src.info()->set_format(Format::U8);
73 dst_x.info()->set_format(Format::S16);
74 dst_y.info()->set_format(Format::S16);
75
76 // Create sobel image configure function
77 NESobel3x3 sobel_3x3;
78 sobel_3x3.configure(&src, &dst_x, &dst_y, border_mode, constant_border_value);
79
80 // Allocate tensors
81 src.allocator()->allocate();
82 dst_x.allocator()->allocate();
83 dst_y.allocator()->allocate();
84
85 BOOST_TEST(!src.info()->is_resizable());
86 BOOST_TEST(!dst_x.info()->is_resizable());
87 BOOST_TEST(!dst_y.info()->is_resizable());
88
89 // Fill tensors
90 library->fill_tensor_uniform(NEAccessor(src), 0);
91
92 // Compute function
93 sobel_3x3.run();
94
95 return std::make_pair(std::move(dst_x), std::move(dst_y));
96}
97} // namespace
98
99#ifndef DOXYGEN_SKIP_THIS
100BOOST_AUTO_TEST_SUITE(NEON)
101BOOST_AUTO_TEST_SUITE(Sobel3x3)
102
103BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
104BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes()) * BorderModes(), shape, border_mode)
105{
106 // Create tensors
Moritz Pflanzer94450f12017-06-30 12:48:43 +0100107 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
108 Tensor dst_x = create_tensor<Tensor>(shape, DataType::S16);
109 Tensor dst_y = create_tensor<Tensor>(shape, DataType::S16);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100110
111 src.info()->set_format(Format::U8);
112 dst_x.info()->set_format(Format::S16);
113 dst_y.info()->set_format(Format::S16);
114
115 BOOST_TEST(src.info()->is_resizable());
116 BOOST_TEST(dst_x.info()->is_resizable());
117 BOOST_TEST(dst_y.info()->is_resizable());
118
119 // Create sobel 3x3 configure function
120 NESobel3x3 sobel_3x3;
121 sobel_3x3.configure(&src, &dst_x, &dst_y, border_mode);
122
123 // Validate valid region
124 const ValidRegion src_valid_region = shape_to_valid_region(shape);
SiCong Li7a035752017-06-28 15:27:02 +0100125 const ValidRegion dst_valid_region = shape_to_valid_region(shape, border_mode == BorderMode::UNDEFINED, border_size);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100126
127 validate(src.info()->valid_region(), src_valid_region);
128 validate(dst_x.info()->valid_region(), dst_valid_region);
129 validate(dst_y.info()->valid_region(), dst_valid_region);
130
131 // Validate padding
132 PaddingCalculator calculator(shape.x(), 8);
133
134 calculator.set_border_mode(border_mode);
135 calculator.set_border_size(1);
136
137 const PaddingSize dst_padding = calculator.required_padding();
138
139 calculator.set_accessed_elements(16);
140 calculator.set_access_offset(-1);
141
142 const PaddingSize src_padding = calculator.required_padding();
143
144 validate(src.info()->padding(), src_padding);
145 validate(dst_x.info()->padding(), dst_padding);
146 validate(dst_y.info()->padding(), dst_padding);
147}
148
149BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
150BOOST_DATA_TEST_CASE(RunSmall, SmallShapes() * BorderModes(), shape, border_mode)
151{
152 uint8_t constant_border_value = 0;
153
154 // Generate a random constant value if border_mode is constant
155 if(border_mode == BorderMode::CONSTANT)
156 {
157 std::mt19937 gen(user_config.seed.get());
158 std::uniform_int_distribution<uint8_t> distribution(0, 255);
159 constant_border_value = distribution(gen);
160 }
161
162 // Compute function
163 std::pair<Tensor, Tensor> dst = compute_sobel_3x3(shape, border_mode, constant_border_value);
164
165 // Compute reference
166 std::pair<RawTensor, RawTensor> ref_dst = Reference::compute_reference_sobel_3x3(shape, 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, border_size);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100170
171 // Validate output
172 validate(NEAccessor(dst.first), ref_dst.first, valid_region);
173 validate(NEAccessor(dst.second), ref_dst.second, valid_region);
174}
175
176BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
177BOOST_DATA_TEST_CASE(RunLarge, LargeShapes() * BorderModes(), shape, border_mode)
178{
179 uint8_t constant_border_value = 0;
180
181 // Generate a random constant value if border_mode is constant
182 if(border_mode == BorderMode::CONSTANT)
183 {
184 std::mt19937 gen(user_config.seed.get());
185 std::uniform_int_distribution<uint8_t> distribution(0, 255);
186 constant_border_value = distribution(gen);
187 }
188
189 // Compute function
190 std::pair<Tensor, Tensor> dst = compute_sobel_3x3(shape, border_mode, constant_border_value);
191
192 // Compute reference
193 std::pair<RawTensor, RawTensor> ref_dst = Reference::compute_reference_sobel_3x3(shape, 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, border_size);
Giorgio Arena50f9fd72017-06-19 17:05:30 +0100197
198 // Validate output
199 validate(NEAccessor(dst.first), ref_dst.first, valid_region);
200 validate(NEAccessor(dst.second), ref_dst.second, valid_region);
201}
202
203BOOST_AUTO_TEST_SUITE_END()
204BOOST_AUTO_TEST_SUITE_END()
Anthony Barbierac69aa12017-07-03 17:39:37 +0100205#endif /* DOXYGEN_SKIP_THIS */