blob: 30a3fd85690078f677e2ea552cec01b42a1bbf86 [file] [log] [blame]
George Wort05398a92019-01-25 15:38:33 +00001/*
Matthew Bentham945b8da2023-07-12 11:54:59 +00002 * Copyright (c) 2019-2021, 2023 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:
George Wort05398a92019-01-25 15:38:33 +000049 void setup(TensorShape src_shape, TensorShape boxes_shape, Coordinates2D crop_size, InterpolationPolicy method,
50 float extrapolation_value, bool is_outside_bounds, DataType data_type)
51 {
52 _target = compute_target(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type);
53 _reference = compute_reference(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type);
54 }
55
56protected:
57 template <typename U>
58 void fill(U &&tensor, int i)
59 {
60 library->fill_tensor_uniform(tensor, i);
61 }
62
63 template <typename U, typename V>
64 void fill(U &&tensor, int i, V min, V max)
65 {
66 library->fill_tensor_uniform(tensor, i, min, max);
67 }
68
69 TensorType compute_target(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method,
70 float extrapolation_value, bool is_outside_bounds, DataType data_type)
71 {
72 TensorShape dst_shape(src_shape[0], crop_size.x, crop_size.y, boxes_shape[1]);
73
74 // Create tensors
75 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC);
76 TensorType boxes = create_tensor<TensorType>(boxes_shape, DataType::F32);
77 TensorType boxes_ind = create_tensor<TensorType>(TensorShape(boxes_shape[1]), DataType::S32);
78 TensorType dst = create_tensor<TensorType>(dst_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC);
79
Manuel Bottini2b84be52020-04-08 10:15:51 +010080 boxes.allocator()->allocate();
81 boxes_ind.allocator()->allocate();
82 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);
83 fill(AccessorType(boxes_ind), 2, 0, static_cast<int32_t>(src_shape[3] - 1));
84
George Wort05398a92019-01-25 15:38:33 +000085 // Create and configure function
86 FunctionType crop;
87 crop.configure(&src, &boxes, &boxes_ind, &dst, crop_size, method, extrapolation_value);
88
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010089 ARM_COMPUTE_ASSERT(src.info()->is_resizable());
90 ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
George Wort05398a92019-01-25 15:38:33 +000091
92 // Allocate tensors
93 src.allocator()->allocate();
George Wort05398a92019-01-25 15:38:33 +000094 dst.allocator()->allocate();
95
Michele Di Giorgio4fc10b32021-04-30 18:30:41 +010096 ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
97 ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
George Wort05398a92019-01-25 15:38:33 +000098
99 // Fill tensors
100 fill(AccessorType(src), 0);
George Wort05398a92019-01-25 15:38:33 +0000101
102 // Compute function
103 crop.run();
104 return dst;
105 }
106
107 SimpleTensor<float> compute_reference(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method,
108 float extrapolation_value, bool is_outside_bounds, DataType data_type)
109 {
110 // Create reference
111 SimpleTensor<T> src{ src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
112 SimpleTensor<float> boxes{ boxes_shape, DataType::F32 };
113 SimpleTensor<int32_t> boxes_ind{ TensorShape(boxes_shape[1]), DataType::S32 };
114
115 // Fill reference
116 fill(src, 0);
117 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);
118 fill(boxes_ind, 2, 0, static_cast<int32_t>(src.shape()[3] - 1));
119
120 SimpleTensor<float> output = reference::crop_and_resize(src, boxes, boxes_ind, crop_size, method, extrapolation_value);
121
122 SimpleTensor<float> permuted = reference::permute(output, PermutationVector(1, 2U, 0U));
123 return permuted;
124 }
125
126 constexpr static float out_of_bounds_reach = 2.0f;
127
128 TensorType _target{};
129 SimpleTensor<float> _reference{};
130};
131} // namespace validation
132} // namespace test
133} // namespace arm_compute
134#endif /* ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE */