Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Diego Lopez Recas | ff860cf | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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 | #include "arm_compute/core/Helpers.h" |
| 25 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 26 | using namespace arm_compute; |
| 27 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 28 | Window arm_compute::calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 29 | { |
| 30 | if(!skip_border) |
| 31 | { |
| 32 | border_size = BorderSize(0); |
| 33 | } |
| 34 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 35 | const Coordinates &anchor = valid_region.anchor; |
| 36 | const TensorShape &shape = valid_region.shape; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 37 | |
| 38 | Window window; |
| 39 | |
| 40 | window.set(0, Window::Dimension( |
| 41 | // Skip the border left of the image |
| 42 | anchor[0] + border_size.left, |
| 43 | // Skip the border right of the image |
| 44 | // Make sure the window width is a multiple of the step size |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 45 | anchor[0] + border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | steps[0])); |
| 47 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 48 | size_t n = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 50 | if(anchor.num_dimensions() > 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | { |
| 52 | window.set(1, Window::Dimension( |
| 53 | // Skip the border above the image |
| 54 | anchor[1] + border_size.top, |
| 55 | // Skip the border below the image |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 56 | anchor[1] + border_size.top + ceil_to_multiple(std::max(0, static_cast<int>(shape[1]) - static_cast<int>(border_size.top) - static_cast<int>(border_size.bottom)), steps[1]), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 57 | steps[1])); |
| 58 | |
| 59 | ++n; |
| 60 | } |
| 61 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 62 | for(; n < anchor.num_dimensions(); ++n) |
| 63 | { |
| 64 | window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n]))); |
| 65 | } |
| 66 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 67 | for(; n < Coordinates::num_max_dimensions; ++n) |
| 68 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 69 | window.set(n, Window::Dimension(0, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | return window; |
| 73 | } |
| 74 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 75 | Window arm_compute::calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 76 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 77 | const Coordinates &anchor = valid_region.anchor; |
| 78 | const TensorShape &shape = valid_region.shape; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 79 | |
| 80 | Window window; |
| 81 | |
| 82 | window.set(0, Window::Dimension( |
| 83 | // move the anchor to the start from the border |
| 84 | anchor[0] - border_size.left, |
| 85 | // move the anchor to include the right end border |
| 86 | // Make sure the window width is a multiple of the step size |
| 87 | anchor[0] - border_size.left + ceil_to_multiple(shape[0] + border_size.left + border_size.right, steps[0]), |
| 88 | steps[0])); |
| 89 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 90 | size_t n = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 91 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 92 | if(anchor.num_dimensions() > 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 93 | { |
| 94 | window.set(1, Window::Dimension( |
| 95 | // Include the border above the image |
| 96 | anchor[1] - border_size.top, |
| 97 | // Include the border below the image |
| 98 | anchor[1] - border_size.top + ceil_to_multiple(shape[1] + border_size.top + border_size.bottom, steps[1]), |
| 99 | steps[1])); |
| 100 | |
| 101 | ++n; |
| 102 | } |
| 103 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 104 | if(anchor.num_dimensions() > 2) |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 105 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 106 | window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[n]), steps[2])); |
Anthony Barbier | 7068f99 | 2017-10-26 15:23:08 +0100 | [diff] [blame] | 107 | |
| 108 | ++n; |
| 109 | } |
| 110 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 111 | for(; n < anchor.num_dimensions(); ++n) |
| 112 | { |
| 113 | window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n]))); |
| 114 | } |
| 115 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 116 | for(; n < Coordinates::num_max_dimensions; ++n) |
| 117 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 118 | window.set(n, Window::Dimension(0, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | return window; |
| 122 | } |
| 123 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 124 | Window arm_compute::calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 125 | { |
| 126 | if(skip_border) |
| 127 | { |
| 128 | border_size.top = 0; |
| 129 | border_size.bottom = 0; |
| 130 | } |
| 131 | else |
| 132 | { |
| 133 | border_size.left = 0; |
| 134 | border_size.right = 0; |
| 135 | } |
| 136 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 137 | const Coordinates &anchor = valid_region.anchor; |
| 138 | const TensorShape &shape = valid_region.shape; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 139 | |
| 140 | Window window; |
| 141 | |
| 142 | window.set(0, Window::Dimension( |
| 143 | // Skip the border left of the image |
| 144 | anchor[0] + border_size.left, |
| 145 | // Skip the border right of the image |
| 146 | // Make sure the window width is a multiple of the step size |
Moritz Pflanzer | a184836 | 2017-08-25 12:30:03 +0100 | [diff] [blame] | 147 | anchor[0] + border_size.left + ceil_to_multiple(std::max(0, static_cast<int>(shape[0]) - static_cast<int>(border_size.left) - static_cast<int>(border_size.right)), steps[0]), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 148 | steps[0])); |
| 149 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 150 | size_t n = 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 151 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 152 | if(anchor.num_dimensions() > 1) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 153 | { |
| 154 | window.set(1, Window::Dimension( |
| 155 | // Skip the border above the image |
| 156 | anchor[1] - border_size.top, |
| 157 | // Skip the border below the image |
| 158 | anchor[1] + shape[1] + border_size.bottom, |
| 159 | 1)); |
| 160 | |
| 161 | ++n; |
| 162 | } |
| 163 | |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 164 | for(; n < anchor.num_dimensions(); ++n) |
| 165 | { |
| 166 | window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n]))); |
| 167 | } |
| 168 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 169 | for(; n < Coordinates::num_max_dimensions; ++n) |
| 170 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 171 | window.set(n, Window::Dimension(0, 1)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | return window; |
| 175 | } |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 176 | |
| 177 | ValidRegion arm_compute::calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape, |
| 178 | InterpolationPolicy interpolate_policy, SamplingPolicy sampling_policy, bool border_undefined) |
| 179 | { |
| 180 | const float scale_x = static_cast<float>(dst_shape[0]) / src_info.tensor_shape()[0]; |
| 181 | const float scale_y = static_cast<float>(dst_shape[1]) / src_info.tensor_shape()[1]; |
| 182 | const float sampling_point = (sampling_policy == SamplingPolicy::CENTER) ? 0.5f : 0.0f; |
| 183 | |
| 184 | // Get input's valid region start and end points |
| 185 | const int valid_start_in_x = src_info.valid_region().anchor[0]; |
| 186 | const int valid_start_in_y = src_info.valid_region().anchor[1]; |
| 187 | const int valid_end_in_x = src_info.valid_region().anchor[0] + src_info.valid_region().shape[0]; |
| 188 | const int valid_end_in_y = src_info.valid_region().anchor[1] + src_info.valid_region().shape[1]; |
| 189 | |
| 190 | // Initialize output's valid region start and end points |
| 191 | auto valid_start_out_x = static_cast<int>(valid_start_in_x * scale_x); |
| 192 | auto valid_start_out_y = static_cast<int>(valid_start_in_y * scale_y); |
| 193 | auto valid_end_out_x = std::min<int>(std::ceil(valid_end_in_x * scale_x), dst_shape[0]); |
| 194 | auto valid_end_out_y = std::min<int>(std::ceil(valid_end_in_y * scale_y), dst_shape[1]); |
| 195 | |
| 196 | // Handle valid points in case of the bi-linear interpolation |
| 197 | if(border_undefined) |
| 198 | { |
| 199 | switch(interpolate_policy) |
| 200 | { |
| 201 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 202 | { |
| 203 | // (start_out + sampling_point) >= (start_in * scale) |
| 204 | // start_out = ceil((start_in * scale) - sampling_point) |
| 205 | valid_start_out_x = std::ceil(valid_start_in_x * scale_x - sampling_point); |
| 206 | valid_start_out_y = std::ceil(valid_start_in_y * scale_y - sampling_point); |
| 207 | |
| 208 | // (end_out - 1 + sampling_point) < (end_in * scale) |
| 209 | // end_out = ceil((end_in * scale) - sampling_point); // <-- ceil(x - 1) strictly less |
| 210 | valid_end_out_x = std::ceil(valid_end_in_x * scale_x - sampling_point); |
| 211 | valid_end_out_y = std::ceil(valid_end_in_y * scale_y - sampling_point); |
| 212 | break; |
| 213 | } |
| 214 | case InterpolationPolicy::BILINEAR: |
| 215 | { |
| 216 | // (start_out + sampling_point) >= ((start_in + sampling_point) * scale) |
| 217 | // start_out = ceil(((start_in + sampling_point) * scale) - sampling_point) |
| 218 | valid_start_out_x = std::ceil((valid_start_in_x + sampling_point) * scale_x - sampling_point); |
| 219 | valid_start_out_y = std::ceil((valid_start_in_y + sampling_point) * scale_y - sampling_point); |
| 220 | |
| 221 | // (end_out - 1 + sampling_point) <= ((end_in - 1 + sampling_point) * scale) |
| 222 | // end_out = floor(((end_in - 1 + sampling_point) * scale) - sampling_point + 1) |
| 223 | valid_end_out_x = std::floor((valid_end_in_x - 1.f + sampling_point) * scale_x - sampling_point + 1.f); |
| 224 | valid_end_out_y = std::floor((valid_end_in_y - 1.f + sampling_point) * scale_y - sampling_point + 1.f); |
| 225 | break; |
| 226 | } |
| 227 | case InterpolationPolicy::AREA: |
| 228 | break; |
| 229 | default: |
| 230 | { |
| 231 | ARM_COMPUTE_ERROR("Invalid InterpolationPolicy"); |
| 232 | break; |
| 233 | } |
| 234 | } |
| 235 | } |
| 236 | |
| 237 | // Setup output valid region |
| 238 | ValidRegion valid_region{ Coordinates(), dst_shape, src_info.tensor_shape().num_dimensions() }; |
| 239 | |
| 240 | valid_region.anchor.set(0, std::max(0, valid_start_out_x)); |
| 241 | valid_region.anchor.set(1, std::max(0, valid_start_out_y)); |
| 242 | |
| 243 | valid_region.shape.set(0, std::min<size_t>(valid_end_out_x - valid_start_out_x, dst_shape[0])); |
| 244 | valid_region.shape.set(1, std::min<size_t>(valid_end_out_y - valid_start_out_y, dst_shape[1])); |
| 245 | |
| 246 | return valid_region; |
| 247 | } |