blob: d163e8436fe6e46a07713a5f5244884223739a5f [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{
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010034// Return the bilinear value at a specified coordinate with different border modes
35template <typename T>
Georgios Pinitas583137c2017-08-31 18:12:42 +010036T 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 +010037{
Abe Mbise31bbce12017-09-19 16:19:13 +010038 const int idx = std::floor(xn);
39 const int idy = std::floor(yn);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010040
41 const float dx = xn - idx;
42 const float dy = yn - idy;
43 const float dx_1 = 1.0f - dx;
44 const float dy_1 = 1.0f - dy;
45
Georgios Pinitas583137c2017-08-31 18:12:42 +010046 const T border_value = constant_border_value;
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010047
Georgios Pinitas583137c2017-08-31 18:12:42 +010048 id.set(0, idx);
49 id.set(1, idy);
50 const float tl = tensor_elem_at(in, id, border_mode, border_value);
51 id.set(0, idx + 1);
52 id.set(1, idy);
53 const float tr = tensor_elem_at(in, id, border_mode, border_value);
54 id.set(0, idx);
55 id.set(1, idy + 1);
56 const float bl = tensor_elem_at(in, id, border_mode, border_value);
57 id.set(0, idx + 1);
58 id.set(1, idy + 1);
59 const float br = tensor_elem_at(in, id, border_mode, border_value);
60
61 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 +010062}
Georgios Pinitas583137c2017-08-31 18:12:42 +010063
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010064template 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 +010065template int16_t bilinear_policy(const SimpleTensor<int16_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, int16_t constant_border_value);
66template half bilinear_policy(const SimpleTensor<half> &in, Coordinates id, float xn, float yn, BorderMode border_mode, half constant_border_value);
67template 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 +010068
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010069RawTensor transpose(const RawTensor &src, int chunk_width)
70{
71 // Create reference
72 TensorShape dst_shape(src.shape());
73 dst_shape.set(0, src.shape().y() * chunk_width);
74 dst_shape.set(1, std::ceil(src.shape().x() / static_cast<float>(chunk_width)));
75
76 RawTensor dst{ dst_shape, src.data_type() };
77
78 // Compute reference
79 uint8_t *out_ptr = dst.data();
80
81 for(int i = 0; i < dst.num_elements(); i += chunk_width)
82 {
83 Coordinates coord = index2coord(dst.shape(), i);
84 size_t coord_x = coord.x();
85 coord.set(0, coord.y() * chunk_width);
86 coord.set(1, coord_x / chunk_width);
87
88 const int num_elements = std::min<int>(chunk_width, src.shape().x() - coord.x());
89
90 std::copy_n(static_cast<const uint8_t *>(src(coord)), num_elements * src.element_size(), out_ptr);
91
92 out_ptr += chunk_width * dst.element_size();
93 }
94
95 return dst;
96}
Abe Mbise31bbce12017-09-19 16:19:13 +010097
98bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode)
99{
100 if(border_mode != BorderMode::UNDEFINED)
101 {
102 return true;
103 }
104 if((0 <= yn + 1) && (yn + 1 < height) && (0 <= xn + 1) && (xn + 1 < width))
105 {
106 return true;
107 }
108 return false;
109}
Giorgio Arena93a690e2017-08-01 16:09:33 +0100110} // namespace validation
111} // namespace test
112} // namespace arm_compute