blob: 7a9bfeeaaba01e357dd9f52079c24df084011ced [file] [log] [blame]
Giorgio Arena93a690e2017-08-01 16:09:33 +01001/*
Manuel Bottini8481d832019-12-10 15:28:40 +00002 * Copyright (c) 2017-2020 ARM Limited.
Giorgio Arena93a690e2017-08-01 16:09:33 +01003 *
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
Manuel Bottini8481d832019-12-10 15:28:40 +000064template int8_t bilinear_policy(const SimpleTensor<int8_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, int8_t constant_border_value);
Isabella Gottardi1fab09f2017-07-13 15:55:57 +010065template 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 +010066template int16_t bilinear_policy(const SimpleTensor<int16_t> &in, Coordinates id, float xn, float yn, BorderMode border_mode, int16_t constant_border_value);
67template half bilinear_policy(const SimpleTensor<half> &in, Coordinates id, float xn, float yn, BorderMode border_mode, half constant_border_value);
68template 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 +010069
Moritz Pflanzercde1e8a2017-09-08 09:53:14 +010070RawTensor transpose(const RawTensor &src, int chunk_width)
71{
72 // Create reference
73 TensorShape dst_shape(src.shape());
74 dst_shape.set(0, src.shape().y() * chunk_width);
75 dst_shape.set(1, std::ceil(src.shape().x() / static_cast<float>(chunk_width)));
76
77 RawTensor dst{ dst_shape, src.data_type() };
78
79 // Compute reference
80 uint8_t *out_ptr = dst.data();
81
82 for(int i = 0; i < dst.num_elements(); i += chunk_width)
83 {
84 Coordinates coord = index2coord(dst.shape(), i);
85 size_t coord_x = coord.x();
86 coord.set(0, coord.y() * chunk_width);
87 coord.set(1, coord_x / chunk_width);
88
89 const int num_elements = std::min<int>(chunk_width, src.shape().x() - coord.x());
90
91 std::copy_n(static_cast<const uint8_t *>(src(coord)), num_elements * src.element_size(), out_ptr);
92
93 out_ptr += chunk_width * dst.element_size();
94 }
95
96 return dst;
97}
Abe Mbise31bbce12017-09-19 16:19:13 +010098
99bool valid_bilinear_policy(float xn, float yn, int width, int height, BorderMode border_mode)
100{
101 if(border_mode != BorderMode::UNDEFINED)
102 {
103 return true;
104 }
105 if((0 <= yn + 1) && (yn + 1 < height) && (0 <= xn + 1) && (xn + 1 < width))
106 {
107 return true;
108 }
109 return false;
110}
Giorgio Arena93a690e2017-08-01 16:09:33 +0100111} // namespace validation
112} // namespace test
113} // namespace arm_compute