blob: 2f54879818835c5b694977794cf90e5dde1997f9 [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +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 */
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010024#include "Utils.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010025
Moritz Pflanzera09de0c2017-09-01 20:41:12 +010026#include "tests/validation/Helpers.h"
Giorgio Arena93a690e2017-08-01 16:09:33 +010027
28namespace arm_compute
29{
30namespace test
31{
32namespace validation
33{
Giorgio Arena93a690e2017-08-01 16:09:33 +010034// Return a tensor element at a specified coordinate with different border modes
35template <typename T>
36T tensor_elem_at(const SimpleTensor<T> &in, Coordinates coord, BorderMode border_mode, T constant_border_value)
37{
38 const int x = coord.x();
39 const int y = coord.y();
40 const auto width = static_cast<int>(in.shape().x());
41 const auto height = static_cast<int>(in.shape().y());
42
43 // If coordinates beyond range of tensor's width or height
44 if(x < 0 || y < 0 || x >= width || y >= height)
45 {
46 if(border_mode == BorderMode::REPLICATE)
47 {
48 coord.set(0, std::max(0, std::min(x, width - 1)));
49 coord.set(1, std::max(0, std::min(y, height - 1)));
Giorgio Arena93a690e2017-08-01 16:09:33 +010050 }
51 else
52 {
Georgios Pinitas583137c2017-08-31 18:12:42 +010053 return static_cast<T>(constant_border_value);
Giorgio Arena93a690e2017-08-01 16:09:33 +010054 }
55 }
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010056 return in[coord2index(in.shape(), coord)];
Giorgio Arena93a690e2017-08-01 16:09:33 +010057}
Georgios Pinitas583137c2017-08-31 18:12:42 +010058
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010059template uint8_t tensor_elem_at(const SimpleTensor<uint8_t> &in, Coordinates coord, BorderMode border_mode, uint8_t constant_border_value);
Georgios Pinitas583137c2017-08-31 18:12:42 +010060template int16_t tensor_elem_at(const SimpleTensor<int16_t> &in, Coordinates coord, BorderMode border_mode, int16_t constant_border_value);
61template half tensor_elem_at(const SimpleTensor<half> &in, Coordinates coord, BorderMode border_mode, half constant_border_value);
62template float tensor_elem_at(const SimpleTensor<float> &in, Coordinates coord, BorderMode border_mode, float constant_border_value);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010063
64// Return the bilinear value at a specified coordinate with different border modes
65template <typename T>
Georgios Pinitas583137c2017-08-31 18:12:42 +010066T bilinear_policy(const SimpleTensor<T> &in, Coordinates id, float xn, float yn, BorderMode border_mode, T constant_border_value)
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010067{
68 int idx = std::floor(xn);
69 int idy = std::floor(yn);
70
71 const float dx = xn - idx;
72 const float dy = yn - idy;
73 const float dx_1 = 1.0f - dx;
74 const float dy_1 = 1.0f - dy;
75
Georgios Pinitas583137c2017-08-31 18:12:42 +010076 const T border_value = constant_border_value;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010077
Georgios Pinitas583137c2017-08-31 18:12:42 +010078 id.set(0, idx);
79 id.set(1, idy);
80 const float tl = tensor_elem_at(in, id, border_mode, border_value);
81 id.set(0, idx + 1);
82 id.set(1, idy);
83 const float tr = tensor_elem_at(in, id, border_mode, border_value);
84 id.set(0, idx);
85 id.set(1, idy + 1);
86 const float bl = tensor_elem_at(in, id, border_mode, border_value);
87 id.set(0, idx + 1);
88 id.set(1, idy + 1);
89 const float br = tensor_elem_at(in, id, border_mode, border_value);
90
91 return static_cast<T>(tl * (dx_1 * dy_1) + tr * (dx * dy_1) + bl * (dx_1 * dy) + br * (dx * dy));
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010092}
Georgios Pinitas583137c2017-08-31 18:12:42 +010093
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010094template uint8_t bilinear_policy(const SimpleTensor<uint8_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, uint8_t constant_border_value);
Georgios Pinitas583137c2017-08-31 18:12:42 +010095template int16_t bilinear_policy(const SimpleTensor<int16_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, int16_t constant_border_value);
96template half bilinear_policy(const SimpleTensor<half> &in, Coordinates id, float xn, float yn, BorderMode border_mode, half constant_border_value);
97template float bilinear_policy(const SimpleTensor<float> &in, Coordinates id, float xn, float yn, BorderMode border_mode, float constant_border_value);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010098
Abe Mbise3f0ab6e2017-09-01 16:43:47 +010099/* Apply 2D spatial filter on a single element of @p in at coordinates @p coord
100 *
101 * - filter sizes have to be odd number
102 * - Row major order of filter assumed
103 * - TO_ZERO rounding policy assumed
104 * - SATURATE convert policy assumed
105 *
106 */
107template <typename T1, typename T2, typename T3>
108void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<T1> &in, SimpleTensor<T3> &out, const TensorShape &filter_shape, const T2 *filter_itr, float scale, BorderMode border_mode,
109 T1 constant_border_value)
110{
111 double val = 0;
112 const int x = coord.x();
113 const int y = coord.y();
114 for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j)
115 {
116 for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i)
117 {
118 coord.set(0, i);
119 coord.set(1, j);
120 val += static_cast<double>(*filter_itr) * tensor_elem_at(in, coord, border_mode, constant_border_value);
121 ++filter_itr;
122 }
123 }
124 coord.set(0, x);
125 coord.set(1, y);
126 const double rounded_val = support::cpp11::trunc(val * static_cast<double>(scale));
127 out[coord2index(in.shape(), coord)] = saturate_cast<T3>(rounded_val);
128}
129template void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<float> &in, SimpleTensor<float> &out, const TensorShape &filter_shape, const float *filter_itr, float scale,
130 BorderMode border_mode,
131 float constant_border_value);
132template void apply_2d_spatial_filter(Coordinates coord, const SimpleTensor<uint8_t> &in, SimpleTensor<uint8_t> &out, const TensorShape &filter_shape, const uint8_t *filter_itr, float scale,
133 BorderMode border_mode,
134 uint8_t constant_border_value);
135
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +0100136RawTensor transpose(const RawTensor &src, int chunk_width)
137{
138 // Create reference
139 TensorShape dst_shape(src.shape());
140 dst_shape.set(0, src.shape().y() * chunk_width);
141 dst_shape.set(1, std::ceil(src.shape().x() / static_cast<float>(chunk_width)));
142
143 RawTensor dst{ dst_shape, src.data_type() };
144
145 // Compute reference
146 uint8_t *out_ptr = dst.data();
147
148 for(int i = 0; i < dst.num_elements(); i += chunk_width)
149 {
150 Coordinates coord = index2coord(dst.shape(), i);
151 size_t coord_x = coord.x();
152 coord.set(0, coord.y() * chunk_width);
153 coord.set(1, coord_x / chunk_width);
154
155 const int num_elements = std::min<int>(chunk_width, src.shape().x() - coord.x());
156
157 std::copy_n(static_cast<const uint8_t *>(src(coord)), num_elements * src.element_size(), out_ptr);
158
159 out_ptr += chunk_width * dst.element_size();
160 }
161
162 return dst;
163}
Giorgio Arena93a690e2017-08-01 16:09:33 +0100164} // namespace validation
165} // namespace test
166} // namespace arm_compute