blob: 5aa63c4ef8a1dee30f45c7dcee2fb770fb1a343d [file] [log] [blame]
George Wort05398a92019-01-25 15:38:33 +00001/*
Georgios Pinitasc6f95102021-03-30 10:03:01 +01002 * Copyright (c) 2019-2021 Arm Limited.
George Wort05398a92019-01-25 15:38:33 +00003 *
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#ifndef ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE
25#define ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE
26
27#include "arm_compute/core/TensorShape.h"
28#include "arm_compute/core/Types.h"
29
30#include "tests/AssetsLibrary.h"
31#include "tests/Globals.h"
32#include "tests/IAccessor.h"
George Wort05398a92019-01-25 15:38:33 +000033#include "tests/framework/Asserts.h"
34#include "tests/framework/Fixture.h"
35#include "tests/validation/Helpers.h"
36#include "tests/validation/reference/CropResize.h"
37#include "tests/validation/reference/Permute.h"
38
39namespace arm_compute
40{
41namespace test
42{
43namespace validation
44{
45template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46class CropResizeFixture : public framework::Fixture
47{
48public:
49 template <typename...>
50 void setup(TensorShape src_shape, TensorShape boxes_shape, Coordinates2D crop_size, InterpolationPolicy method,
51 float extrapolation_value, bool is_outside_bounds, DataType data_type)
52 {
53 _target = compute_target(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type);
54 _reference = compute_reference(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type);
55 }
56
57protected:
58 template <typename U>
59 void fill(U &&tensor, int i)
60 {
61 library->fill_tensor_uniform(tensor, i);
62 }
63
64 template <typename U, typename V>
65 void fill(U &&tensor, int i, V min, V max)
66 {
67 library->fill_tensor_uniform(tensor, i, min, max);
68 }
69
70 TensorType compute_target(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method,
71 float extrapolation_value, bool is_outside_bounds, DataType data_type)
72 {
73 TensorShape dst_shape(src_shape[0], crop_size.x, crop_size.y, boxes_shape[1]);
74
75 // Create tensors
76 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC);
77 TensorType boxes = create_tensor<TensorType>(boxes_shape, DataType::F32);
78 TensorType boxes_ind = create_tensor<TensorType>(TensorShape(boxes_shape[1]), DataType::S32);
79 TensorType dst = create_tensor<TensorType>(dst_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC);
80
Manuel Bottini2b84be52020-04-08 10:15:51 +010081 boxes.allocator()->allocate();
82 boxes_ind.allocator()->allocate();
83 fill(AccessorType(boxes), 1, is_outside_bounds ? 0.0f - out_of_bounds_reach : 0.0f, is_outside_bounds ? 1.0f + out_of_bounds_reach : 1.0f);
84 fill(AccessorType(boxes_ind), 2, 0, static_cast<int32_t>(src_shape[3] - 1));
85
George Wort05398a92019-01-25 15:38:33 +000086 // Create and configure function
87 FunctionType crop;
88 crop.configure(&src, &boxes, &boxes_ind, &dst, crop_size, method, extrapolation_value);
89
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010090 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
91 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
George Wort05398a92019-01-25 15:38:33 +000092
93 // Allocate tensors
94 src.allocator()->allocate();
George Wort05398a92019-01-25 15:38:33 +000095 dst.allocator()->allocate();
96
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010097 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
98 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
George Wort05398a92019-01-25 15:38:33 +000099
100 // Fill tensors
101 fill(AccessorType(src), 0);
George Wort05398a92019-01-25 15:38:33 +0000102
103 // Compute function
104 crop.run();
105 return dst;
106 }
107
108 SimpleTensor<float> compute_reference(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method,
109 float extrapolation_value, bool is_outside_bounds, DataType data_type)
110 {
111 // Create reference
112 SimpleTensor<T> src{ src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
113 SimpleTensor<float> boxes{ boxes_shape, DataType::F32 };
114 SimpleTensor<int32_t> boxes_ind{ TensorShape(boxes_shape[1]), DataType::S32 };
115
116 // Fill reference
117 fill(src, 0);
118 fill(boxes, 1, is_outside_bounds ? 0.0f - out_of_bounds_reach : 0.0f, is_outside_bounds ? 1.0f + out_of_bounds_reach : 1.0f);
119 fill(boxes_ind, 2, 0, static_cast<int32_t>(src.shape()[3] - 1));
120
121 SimpleTensor<float> output = reference::crop_and_resize(src, boxes, boxes_ind, crop_size, method, extrapolation_value);
122
123 SimpleTensor<float> permuted = reference::permute(output, PermutationVector(1, 2U, 0U));
124 return permuted;
125 }
126
127 constexpr static float out_of_bounds_reach = 2.0f;
128
129 TensorType _target{};
130 SimpleTensor<float> _reference{};
131};
132} // namespace validation
133} // namespace test
134} // namespace arm_compute
135#endif /* ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE */