blob: 2c102ea37e6fae0478bbe85d26e7ccd83a45d168 [file] [log] [blame]
Isabella Gottardi62031532017-07-04 11:21:28 +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 "AssetsLibrary.h"
25#include "Globals.h"
26#include "NEON/Accessor.h"
27#include "PaddingCalculator.h"
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/NEWarpPerspective.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::validation;
50
51namespace
52{
53/** Compute Warp Perspective function.
54 *
55 * @param[in] input Shape of the input and output tensors.
56 * @param[in] matrix The perspective matrix. Must be 3x3 of type float.
57 * @param[in] policy The interpolation type.
58 * @param[in] border_mode Strategy to use for borders.
59 * @param[in] constant_border_value Constant value to use for borders if border_mode is set to CONSTANT.
60 *
61 * @return Computed output tensor.
62 */
63Tensor compute_warp_perspective(const TensorShape &shape, const float *matrix, InterpolationPolicy policy,
64 BorderMode border_mode, uint8_t constant_border_value)
65{
66 // Create tensors
67 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
68 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
69
70 // Create and configure function
71 NEWarpPerspective warp_perspective;
72 warp_perspective.configure(&src, &dst, matrix, policy, border_mode, constant_border_value);
73
74 // Allocate tensors
75 src.allocator()->allocate();
76 dst.allocator()->allocate();
77
78 BOOST_TEST(!src.info()->is_resizable());
79 BOOST_TEST(!dst.info()->is_resizable());
80
81 // Fill tensors
82 library->fill_tensor_uniform(Accessor(src), 0);
83
84 // Compute function
85 warp_perspective.run();
86
87 return dst;
88}
89} // namespace
90
91#ifndef DOXYGEN_SKIP_THIS
92BOOST_AUTO_TEST_SUITE(NEON)
93BOOST_AUTO_TEST_SUITE(WarpPerspective)
94
95BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit") * boost::unit_test::label("nightly"))
96BOOST_DATA_TEST_CASE(Configuration, (SmallShapes() + LargeShapes())
97 * boost::unit_test::data::make({ InterpolationPolicy::BILINEAR, InterpolationPolicy::NEAREST_NEIGHBOR }) * BorderModes(),
98 shape, policy, border_mode)
99{
100 uint8_t constant_border_value = 0;
101
102 // Generate a random constant value if border_mode is constant
103 if(border_mode == BorderMode::CONSTANT)
104 {
105 std::mt19937 gen(user_config.seed.get());
106 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
107 constant_border_value = distribution_u8(gen);
108 }
109
110 // Create the matrix
111 std::array<float, 9> matrix;
112 fill_warp_matrix<9>(matrix, 3, 3);
113
114 // Create tensors
115 Tensor src = create_tensor<Tensor>(shape, DataType::U8);
116 Tensor dst = create_tensor<Tensor>(shape, DataType::U8);
117
118 BOOST_TEST(src.info()->is_resizable());
119 BOOST_TEST(dst.info()->is_resizable());
120
121 // Create and configure function
122 NEWarpPerspective warp_perspective;
123 warp_perspective.configure(&src, &dst, matrix.data(), policy, border_mode, constant_border_value);
124
125 // Validate valid region
126 const ValidRegion valid_region = shape_to_valid_region(shape);
127
128 validate(src.info()->valid_region(), valid_region);
129 validate(dst.info()->valid_region(), valid_region);
130
131 // Validate padding
132 PaddingCalculator calculator(shape.x(), 1);
133 calculator.set_border_mode(border_mode);
134 calculator.set_border_size(1);
135
136 const PaddingSize read_padding(1);
137 const PaddingSize write_padding = calculator.required_padding();
138
139 validate(src.info()->padding(), read_padding);
140 validate(dst.info()->padding(), write_padding);
141}
142
143BOOST_TEST_DECORATOR(*boost::unit_test::label("precommit"))
144BOOST_DATA_TEST_CASE(RunSmall, SmallShapes()
145 * boost::unit_test::data::make({ InterpolationPolicy::BILINEAR, InterpolationPolicy::NEAREST_NEIGHBOR })
146 * BorderModes(),
147 shape, policy, border_mode)
148{
149 uint8_t constant_border_value = 0;
150
151 // Generate a random constant value if border_mode is constant
152 if(border_mode == BorderMode::CONSTANT)
153 {
154 std::mt19937 gen(user_config.seed.get());
155 std::uniform_int_distribution<uint8_t> distribution_u8(0, 255);
156 constant_border_value = distribution_u8(gen);
157 }
158
159 // Create the valid mask Tensor
160 RawTensor valid_mask(shape, DataType::U8);
161
162 // Create the matrix
163 std::array<float, 9> matrix;
164 fill_warp_matrix<9>(matrix, 3, 3);
165
166 // Compute function
167 Tensor dst = compute_warp_perspective(shape, matrix.data(), policy, border_mode, constant_border_value);
168
169 // Compute reference
170 RawTensor ref_dst = Reference::compute_reference_warp_perspective(shape, valid_mask, matrix.data(), policy, border_mode, constant_border_value);
171
172 // Validate output
173 validate(Accessor(dst), ref_dst, valid_mask, 1, 0.2f);
174}
175BOOST_TEST_DECORATOR(*boost::unit_test::label("nightly"))
176BOOST_DATA_TEST_CASE(RunLarge, LargeShapes()
177 * boost::unit_test::data::make({ InterpolationPolicy::NEAREST_NEIGHBOR, InterpolationPolicy::BILINEAR }) * BorderModes(),
178 shape, policy, 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_u8(0, 255);
187 constant_border_value = distribution_u8(gen);
188 }
189
190 // Create the valid mask Tensor
191 RawTensor valid_mask(shape, DataType::U8);
192
193 // Create the matrix
194 std::array<float, 9> matrix;
195 fill_warp_matrix<9>(matrix, 3, 3);
196
197 // Compute function
198 Tensor dst = compute_warp_perspective(shape, matrix.data(), policy, border_mode, constant_border_value);
199
200 // Compute reference
201 RawTensor ref_dst = Reference::compute_reference_warp_perspective(shape, valid_mask, matrix.data(), policy, border_mode, constant_border_value);
202
203 // Validate output
204 validate(Accessor(dst), ref_dst, valid_mask, 1, 0.2f);
205}
206
207BOOST_AUTO_TEST_SUITE_END()
208BOOST_AUTO_TEST_SUITE_END()
209#endif /* DOXYGEN_SKIP_THIS */