xref: /aosp_15_r20/external/ComputeLibrary/tests/validation/fixtures/CropResizeFixture.h (revision c217d954acce2dbc11938adb493fc0abd69584f3)
1 /*
2  * Copyright (c) 2019-2021 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 #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"
33 #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 
39 namespace arm_compute
40 {
41 namespace test
42 {
43 namespace validation
44 {
45 template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
46 class CropResizeFixture : public framework::Fixture
47 {
48 public:
49     template <typename...>
setup(TensorShape src_shape,TensorShape boxes_shape,Coordinates2D crop_size,InterpolationPolicy method,float extrapolation_value,bool is_outside_bounds,DataType data_type)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 
57 protected:
58     template <typename U>
fill(U && tensor,int i)59     void fill(U &&tensor, int i)
60     {
61         library->fill_tensor_uniform(tensor, i);
62     }
63 
64     template <typename U, typename V>
fill(U && tensor,int i,V min,V max)65     void fill(U &&tensor, int i, V min, V max)
66     {
67         library->fill_tensor_uniform(tensor, i, min, max);
68     }
69 
compute_target(const TensorShape & src_shape,const TensorShape & boxes_shape,const Coordinates2D & crop_size,InterpolationPolicy method,float extrapolation_value,bool is_outside_bounds,DataType data_type)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 
81         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 
86         // Create and configure function
87         FunctionType crop;
88         crop.configure(&src, &boxes, &boxes_ind, &dst, crop_size, method, extrapolation_value);
89 
90         ARM_COMPUTE_ASSERT(src.info()->is_resizable());
91         ARM_COMPUTE_ASSERT(dst.info()->is_resizable());
92 
93         // Allocate tensors
94         src.allocator()->allocate();
95         dst.allocator()->allocate();
96 
97         ARM_COMPUTE_ASSERT(!src.info()->is_resizable());
98         ARM_COMPUTE_ASSERT(!dst.info()->is_resizable());
99 
100         // Fill tensors
101         fill(AccessorType(src), 0);
102 
103         // Compute function
104         crop.run();
105         return dst;
106     }
107 
compute_reference(const TensorShape & src_shape,const TensorShape & boxes_shape,const Coordinates2D & crop_size,InterpolationPolicy method,float extrapolation_value,bool is_outside_bounds,DataType data_type)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 */
136