blob: fc5484606e921dcb50c85fb299aa0c2bf8a8875c [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 "Reference.h"
25
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026#include "Helpers.h"
27#include "ReferenceCPP.h"
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010028#include "tests/AssetsLibrary.h"
29#include "tests/Globals.h"
30#include "tests/validation_old/Helpers.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031
32#include <random>
Georgios Pinitas7b7858d2017-06-21 16:44:24 +010033#include <vector>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
35using namespace arm_compute::test;
36
Isabella Gottardib797fa22017-06-23 15:02:11 +010037#ifndef DOXYGEN_SKIP_THIS
Anthony Barbier6ff3b192017-09-04 18:44:23 +010038namespace arm_compute
39{
40namespace test
41{
42namespace validation
43{
Giorgio Arena50f9fd72017-06-19 17:05:30 +010044std::pair<RawTensor, RawTensor> Reference::compute_reference_sobel_3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
45{
46 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010047 RawTensor ref_src(shape, Format::U8);
48 RawTensor ref_dst_x(shape, Format::S16);
49 RawTensor ref_dst_y(shape, Format::S16);
Giorgio Arena50f9fd72017-06-19 17:05:30 +010050
51 // Fill reference
52 library->fill_tensor_uniform(ref_src, 0);
53
54 // Compute reference
55 ReferenceCPP::sobel_3x3(ref_src, ref_dst_x, ref_dst_y, border_mode, constant_border_value);
56
57 return std::make_pair(ref_dst_x, ref_dst_y);
58}
Giorgio Arenafc2817d2017-06-27 17:26:37 +010059
Giorgio Arena50f9fd72017-06-19 17:05:30 +010060std::pair<RawTensor, RawTensor> Reference::compute_reference_sobel_5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
61{
62 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010063 RawTensor ref_src(shape, Format::U8);
64 RawTensor ref_dst_x(shape, Format::S16);
65 RawTensor ref_dst_y(shape, Format::S16);
Giorgio Arena50f9fd72017-06-19 17:05:30 +010066
67 // Fill reference
68 library->fill_tensor_uniform(ref_src, 0);
69
70 // Compute reference
71 ReferenceCPP::sobel_5x5(ref_src, ref_dst_x, ref_dst_y, border_mode, constant_border_value);
72
73 return std::make_pair(ref_dst_x, ref_dst_y);
74}
Michele Di Giorgioef4b4ae2017-07-04 17:19:43 +010075void Reference::compute_reference_min_max_location(const TensorShape &shape, DataType dt_in, void *min, void *max, IArray<Coordinates2D> &min_loc, IArray<Coordinates2D> &max_loc,
Giorgio Arena2ca209e2017-06-13 15:49:37 +010076 uint32_t &min_count, uint32_t &max_count)
77{
78 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +010079 RawTensor ref_src(shape, dt_in);
Giorgio Arena2ca209e2017-06-13 15:49:37 +010080
81 // Fill reference
82 library->fill_tensor_uniform(ref_src, 0);
83
84 // Compute reference
85 ReferenceCPP::min_max_location(ref_src, min, max, min_loc, max_loc, min_count, max_count);
86}
Giorgio Arenafc2817d2017-06-27 17:26:37 +010087
88KeyPointArray Reference::compute_reference_harris_corners(const TensorShape &shape, float threshold, float min_dist, float sensitivity,
89 int32_t gradient_size, int32_t block_size, BorderMode border_mode, uint8_t constant_border_value)
90{
91 // Create reference
92 RawTensor ref_src(shape, Format::U8);
93 RawTensor raw_Gx(shape, (gradient_size == 7) ? Format::S32 : Format::S16);
94 RawTensor raw_Gy(shape, (gradient_size == 7) ? Format::S32 : Format::S16);
95 RawTensor raw_candidates(shape, Format::F32);
96 RawTensor raw_non_maxima(shape, Format::F32);
97
98 KeyPointArray corners(shape.total_size());
99
100 // Fill reference
101 library->fill_tensor_uniform(ref_src, 0);
102
103 // Compute reference
104 ReferenceCPP::harris_corners(ref_src, raw_Gx, raw_Gy, raw_candidates, raw_non_maxima, threshold, min_dist, sensitivity, gradient_size, block_size, corners, border_mode, constant_border_value);
105
106 return corners;
107}
108
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100109RawTensor Reference::compute_reference_integral_image(const TensorShape &shape)
110{
111 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100112 RawTensor ref_src(shape, DataType::U8);
113 RawTensor ref_dst(shape, DataType::U32);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100114
115 // Fill reference
116 library->fill_tensor_uniform(ref_src, 0);
117
118 // Compute reference
119 ReferenceCPP::integral_image(ref_src, ref_dst);
120
121 return ref_dst;
122}
Giorgio Arenafc2817d2017-06-27 17:26:37 +0100123
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100124RawTensor Reference::compute_reference_absolute_difference(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out)
125{
126 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100127 RawTensor ref_src1(shape, dt_in0);
128 RawTensor ref_src2(shape, dt_in1);
129 RawTensor ref_dst(shape, dt_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100130
131 // Fill reference
132 library->fill_tensor_uniform(ref_src1, 0);
133 library->fill_tensor_uniform(ref_src2, 1);
134
135 // Compute reference
136 ReferenceCPP::absolute_difference(ref_src1, ref_src2, ref_dst);
137
138 return ref_dst;
139}
140
141RawTensor Reference::compute_reference_accumulate(const TensorShape &shape)
142{
143 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100144 RawTensor ref_src(shape, DataType::U8);
145 RawTensor ref_dst(shape, DataType::S16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147 // Fill reference
148 library->fill_tensor_uniform(ref_src, 0);
149 library->fill_tensor_uniform(ref_dst, 1);
150
151 // Compute reference
152 ReferenceCPP::accumulate(ref_src, ref_dst);
153
154 return ref_dst;
155}
156
157RawTensor Reference::compute_reference_accumulate_squared(const TensorShape &shape, uint32_t shift)
158{
159 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100160 RawTensor ref_src(shape, DataType::U8);
161 RawTensor ref_dst(shape, DataType::S16);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100162
163 // Fill reference
164 // ref_dst tensor filled with non-negative values
165 library->fill_tensor_uniform(ref_src, 0);
166 library->fill_tensor_uniform(ref_dst, 1, static_cast<int16_t>(0), std::numeric_limits<int16_t>::max());
167
168 // Compute reference
169 ReferenceCPP::accumulate_squared(ref_src, ref_dst, shift);
170
171 return ref_dst;
172}
173
174RawTensor Reference::compute_reference_accumulate_weighted(const TensorShape &shape, float alpha)
175{
176 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100177 RawTensor ref_src(shape, DataType::U8);
178 RawTensor ref_dst(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179
180 // Fill reference
181 library->fill_tensor_uniform(ref_src, 0);
182 library->fill_tensor_uniform(ref_dst, 1);
183
184 // Compute reference
185 ReferenceCPP::accumulate_weighted(ref_src, ref_dst, alpha);
186
187 return ref_dst;
188}
189
SiCong Li5a536642017-06-19 14:47:05 +0100190RawTensor Reference::compute_reference_gaussian3x3(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
191{
192 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100193 RawTensor ref_src(shape, DataType::U8);
194 RawTensor ref_dst(shape, DataType::U8);
SiCong Li5a536642017-06-19 14:47:05 +0100195
196 // Fill reference
197 library->fill_tensor_uniform(ref_src, 0);
198
199 // Compute reference
200 ReferenceCPP::gaussian3x3(ref_src, ref_dst, border_mode, constant_border_value);
201
202 return ref_dst;
203}
204
SiCong Li3eb263e2017-06-19 15:31:43 +0100205RawTensor Reference::compute_reference_gaussian5x5(const TensorShape &shape, BorderMode border_mode, uint8_t constant_border_value)
206{
207 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100208 RawTensor ref_src(shape, DataType::U8);
209 RawTensor ref_dst(shape, DataType::U8);
SiCong Li3eb263e2017-06-19 15:31:43 +0100210
211 // Fill reference
212 library->fill_tensor_uniform(ref_src, 0);
213
214 // Compute reference
215 ReferenceCPP::gaussian5x5(ref_src, ref_dst, border_mode, constant_border_value);
216
217 return ref_dst;
218}
219
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100220RawTensor Reference::compute_reference_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, ConvertPolicy convert_policy,
221 RoundingPolicy rounding_policy)
222{
223 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100224 RawTensor ref_src1(shape, dt_in0);
225 RawTensor ref_src2(shape, dt_in1);
226 RawTensor ref_dst(shape, dt_out);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100227
228 // Fill reference
229 library->fill_tensor_uniform(ref_src1, 0);
230 library->fill_tensor_uniform(ref_src2, 1);
231
232 // Compute reference
233 ReferenceCPP::pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy);
234
235 return ref_dst;
236}
237
238RawTensor Reference::compute_reference_fixed_point_pixel_wise_multiplication(const TensorShape &shape, DataType dt_in0, DataType dt_in1, DataType dt_out, float scale, int fixed_point_position,
239 ConvertPolicy convert_policy, RoundingPolicy rounding_policy)
240{
241 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100242 RawTensor ref_src1(shape, dt_in0, 1, fixed_point_position);
243 RawTensor ref_src2(shape, dt_in1, 1, fixed_point_position);
244 RawTensor ref_dst(shape, dt_out, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100245
246 // Fill reference
247 library->fill_tensor_uniform(ref_src1, 0);
248 library->fill_tensor_uniform(ref_src2, 1);
249
250 // Compute reference
251 ReferenceCPP::fixed_point_pixel_wise_multiplication(ref_src1, ref_src2, ref_dst, scale, convert_policy, rounding_policy);
252
253 return ref_dst;
254}
255
256RawTensor Reference::compute_reference_threshold(const TensorShape &shape, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper)
257{
258 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100259 RawTensor ref_src(shape, DataType::U8);
260 RawTensor ref_dst(shape, DataType::U8);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261
262 // Fill reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100263 library->fill_tensor_uniform(ref_src, 0);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100264
265 // Compute reference
Isabella Gottardib797fa22017-06-23 15:02:11 +0100266 ReferenceCPP::threshold(ref_src, ref_dst, threshold, false_value, true_value, type, upper);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100267
268 return ref_dst;
269}
270
Isabella Gottardi62031532017-07-04 11:21:28 +0100271RawTensor Reference::compute_reference_warp_perspective(const TensorShape &shape, RawTensor &valid_mask, const float *matrix, InterpolationPolicy policy, BorderMode border_mode,
272 uint8_t constant_border_value)
273{
274 // Create reference
275 RawTensor ref_src(shape, DataType::U8);
276 RawTensor ref_dst(shape, DataType::U8);
277
278 // Fill reference
279 library->fill_tensor_uniform(ref_src, 0);
280
281 // Compute reference
282 ReferenceCPP::warp_perspective(ref_src, ref_dst, valid_mask, matrix, policy, border_mode, constant_border_value);
283
284 return ref_dst;
285}
286
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100287RawTensor Reference::compute_reference_roi_pooling_layer(const TensorShape &shape, DataType dt, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info)
288{
289 TensorShape shape_dst;
290 shape_dst.set(0, pool_info.pooled_width());
291 shape_dst.set(1, pool_info.pooled_height());
292 shape_dst.set(2, shape.z());
293 shape_dst.set(3, rois.size());
294
295 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100296 RawTensor ref_src(shape, dt);
297 RawTensor ref_dst(shape_dst, dt);
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100298
299 // Fill reference
300 std::uniform_real_distribution<> distribution(-1, 1);
301 library->fill(ref_src, distribution, 0.0);
302
303 // Compute reference
304 ReferenceCPP::roi_pooling_layer(ref_src, ref_dst, rois, pool_info);
305
306 return ref_dst;
307}
308
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100309RawTensor Reference::compute_reference_fixed_point_operation(const TensorShape &shape, DataType dt_in, DataType dt_out, FixedPointOp op, int fixed_point_position)
310{
311 // Create reference
Moritz Pflanzerfb5aabb2017-07-18 14:39:55 +0100312 RawTensor ref_src(shape, dt_in, 1, fixed_point_position);
313 RawTensor ref_dst(shape, dt_out, 1, fixed_point_position);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100314
315 // Fill reference
316 int min = 0;
317 int max = 0;
318 switch(op)
319 {
320 case(FixedPointOp::INV_SQRT):
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100321 min = 1;
322 max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323 break;
324 case(FixedPointOp::LOG):
325 min = (1 << (fixed_point_position - 1));
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100326 max = (dt_in == DataType::QS8) ? 0x3F : 0x3FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100327 break;
328 case(FixedPointOp::EXP):
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100329 min = -(1 << (fixed_point_position - 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100330 max = (1 << (fixed_point_position - 1));
331 break;
332 case(FixedPointOp::RECIPROCAL):
333 min = 15;
Michalis Spyrou0a8334c2017-06-14 18:00:05 +0100334 max = (dt_in == DataType::QS8) ? 0x7F : 0x7FFF;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100335 break;
336 default:
337 ARM_COMPUTE_ERROR("Fixed point operation not supported");
338 }
339 std::uniform_int_distribution<> distribution(min, max);
340 library->fill(ref_src, distribution, 0);
341
342 // Compute reference
343 ReferenceCPP::fixed_point_operation(ref_src, ref_dst, op);
344
345 return ref_dst;
346}
347
348} // namespace validation
349} // namespace test
350} // namespace arm_compute
Isabella Gottardib797fa22017-06-23 15:02:11 +0100351#endif /* DOXYGEN_SKIP_THIS */