blob: 4f6389155a72a4bf483d64a0568a1d8750839a59 [file] [log] [blame]
George Wort05398a92019-01-25 15:38:33 +00001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2019-2020 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"
33#include "tests/RawLutAccessor.h"
34#include "tests/framework/Asserts.h"
35#include "tests/framework/Fixture.h"
36#include "tests/validation/Helpers.h"
37#include "tests/validation/reference/CropResize.h"
38#include "tests/validation/reference/Permute.h"
39
40namespace arm_compute
41{
42namespace test
43{
44namespace validation
45{
46template <typename TensorType, typename AccessorType, typename FunctionType, typename T>
47class CropResizeFixture : public framework::Fixture
48{
49public:
50 template <typename...>
51 void setup(TensorShape src_shape, TensorShape boxes_shape, Coordinates2D crop_size, InterpolationPolicy method,
52 float extrapolation_value, bool is_outside_bounds, DataType data_type)
53 {
54 _target = compute_target(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type);
55 _reference = compute_reference(src_shape, boxes_shape, crop_size, method, extrapolation_value, is_outside_bounds, data_type);
56 }
57
58protected:
59 template <typename U>
60 void fill(U &&tensor, int i)
61 {
62 library->fill_tensor_uniform(tensor, i);
63 }
64
65 template <typename U, typename V>
66 void fill(U &&tensor, int i, V min, V max)
67 {
68 library->fill_tensor_uniform(tensor, i, min, max);
69 }
70
71 TensorType compute_target(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method,
72 float extrapolation_value, bool is_outside_bounds, DataType data_type)
73 {
74 TensorShape dst_shape(src_shape[0], crop_size.x, crop_size.y, boxes_shape[1]);
75
76 // Create tensors
77 TensorType src = create_tensor<TensorType>(src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC);
78 TensorType boxes = create_tensor<TensorType>(boxes_shape, DataType::F32);
79 TensorType boxes_ind = create_tensor<TensorType>(TensorShape(boxes_shape[1]), DataType::S32);
80 TensorType dst = create_tensor<TensorType>(dst_shape, DataType::F32, 1, QuantizationInfo(), DataLayout::NHWC);
81
Manuel Bottini2b84be52020-04-08 10:15:51 +010082 boxes.allocator()->allocate();
83 boxes_ind.allocator()->allocate();
84 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);
85 fill(AccessorType(boxes_ind), 2, 0, static_cast<int32_t>(src_shape[3] - 1));
86
George Wort05398a92019-01-25 15:38:33 +000087 // Create and configure function
88 FunctionType crop;
89 crop.configure(&src, &boxes, &boxes_ind, &dst, crop_size, method, extrapolation_value);
90
91 ARM_COMPUTE_EXPECT(src.info()->is_resizable(), framework::LogLevel::ERRORS);
George Wort05398a92019-01-25 15:38:33 +000092 ARM_COMPUTE_EXPECT(dst.info()->is_resizable(), framework::LogLevel::ERRORS);
93
94 // Allocate tensors
95 src.allocator()->allocate();
George Wort05398a92019-01-25 15:38:33 +000096 dst.allocator()->allocate();
97
98 ARM_COMPUTE_EXPECT(!src.info()->is_resizable(), framework::LogLevel::ERRORS);
George Wort05398a92019-01-25 15:38:33 +000099 ARM_COMPUTE_EXPECT(!dst.info()->is_resizable(), framework::LogLevel::ERRORS);
100
101 // Fill tensors
102 fill(AccessorType(src), 0);
George Wort05398a92019-01-25 15:38:33 +0000103
104 // Compute function
105 crop.run();
106 return dst;
107 }
108
109 SimpleTensor<float> compute_reference(const TensorShape &src_shape, const TensorShape &boxes_shape, const Coordinates2D &crop_size, InterpolationPolicy method,
110 float extrapolation_value, bool is_outside_bounds, DataType data_type)
111 {
112 // Create reference
113 SimpleTensor<T> src{ src_shape, data_type, 1, QuantizationInfo(), DataLayout::NHWC };
114 SimpleTensor<float> boxes{ boxes_shape, DataType::F32 };
115 SimpleTensor<int32_t> boxes_ind{ TensorShape(boxes_shape[1]), DataType::S32 };
116
117 // Fill reference
118 fill(src, 0);
119 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);
120 fill(boxes_ind, 2, 0, static_cast<int32_t>(src.shape()[3] - 1));
121
122 SimpleTensor<float> output = reference::crop_and_resize(src, boxes, boxes_ind, crop_size, method, extrapolation_value);
123
124 SimpleTensor<float> permuted = reference::permute(output, PermutationVector(1, 2U, 0U));
125 return permuted;
126 }
127
128 constexpr static float out_of_bounds_reach = 2.0f;
129
130 TensorType _target{};
131 SimpleTensor<float> _reference{};
132};
133} // namespace validation
134} // namespace test
135} // namespace arm_compute
136#endif /* ARM_COMPUTE_TEST_SLICE_OPERATIONS_FIXTURE */