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