Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 2 | * Copyright (c) 2016-2020 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 | |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 26 | namespace arm_compute |
| 27 | { |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 28 | ValidRegion calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape, |
| 29 | InterpolationPolicy interpolate_policy, SamplingPolicy sampling_policy, bool border_undefined) |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 30 | { |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 31 | const DataLayout data_layout = src_info.data_layout(); |
| 32 | const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH); |
| 33 | const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT); |
| 34 | |
| 35 | const float scale_x = static_cast<float>(dst_shape[idx_width]) / src_info.tensor_shape()[idx_width]; |
| 36 | const float scale_y = static_cast<float>(dst_shape[idx_height]) / src_info.tensor_shape()[idx_height]; |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 37 | const float sampling_point = (sampling_policy == SamplingPolicy::CENTER) ? 0.5f : 0.0f; |
| 38 | |
| 39 | // Get input's valid region start and end points |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 40 | const int valid_start_in_x = src_info.valid_region().anchor[idx_width]; |
| 41 | const int valid_start_in_y = src_info.valid_region().anchor[idx_height]; |
| 42 | const int valid_end_in_x = src_info.valid_region().anchor[idx_width] + src_info.valid_region().shape[idx_width]; |
| 43 | const int valid_end_in_y = src_info.valid_region().anchor[idx_height] + src_info.valid_region().shape[idx_height]; |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 44 | |
| 45 | // Initialize output's valid region start and end points |
| 46 | auto valid_start_out_x = static_cast<int>(valid_start_in_x * scale_x); |
| 47 | auto valid_start_out_y = static_cast<int>(valid_start_in_y * scale_y); |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 48 | auto valid_end_out_x = std::min<int>(std::ceil(valid_end_in_x * scale_x), dst_shape[idx_width]); |
| 49 | auto valid_end_out_y = std::min<int>(std::ceil(valid_end_in_y * scale_y), dst_shape[idx_height]); |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 50 | |
| 51 | // Handle valid points in case of the bi-linear interpolation |
| 52 | if(border_undefined) |
| 53 | { |
| 54 | switch(interpolate_policy) |
| 55 | { |
| 56 | case InterpolationPolicy::NEAREST_NEIGHBOR: |
| 57 | { |
| 58 | // (start_out + sampling_point) >= (start_in * scale) |
| 59 | // start_out = ceil((start_in * scale) - sampling_point) |
| 60 | valid_start_out_x = std::ceil(valid_start_in_x * scale_x - sampling_point); |
| 61 | valid_start_out_y = std::ceil(valid_start_in_y * scale_y - sampling_point); |
| 62 | |
| 63 | // (end_out - 1 + sampling_point) < (end_in * scale) |
| 64 | // end_out = ceil((end_in * scale) - sampling_point); // <-- ceil(x - 1) strictly less |
| 65 | valid_end_out_x = std::ceil(valid_end_in_x * scale_x - sampling_point); |
| 66 | valid_end_out_y = std::ceil(valid_end_in_y * scale_y - sampling_point); |
| 67 | break; |
| 68 | } |
| 69 | case InterpolationPolicy::BILINEAR: |
| 70 | { |
| 71 | // (start_out + sampling_point) >= ((start_in + sampling_point) * scale) |
| 72 | // start_out = ceil(((start_in + sampling_point) * scale) - sampling_point) |
| 73 | valid_start_out_x = std::ceil((valid_start_in_x + sampling_point) * scale_x - sampling_point); |
| 74 | valid_start_out_y = std::ceil((valid_start_in_y + sampling_point) * scale_y - sampling_point); |
| 75 | |
| 76 | // (end_out - 1 + sampling_point) <= ((end_in - 1 + sampling_point) * scale) |
| 77 | // end_out = floor(((end_in - 1 + sampling_point) * scale) - sampling_point + 1) |
| 78 | valid_end_out_x = std::floor((valid_end_in_x - 1.f + sampling_point) * scale_x - sampling_point + 1.f); |
| 79 | valid_end_out_y = std::floor((valid_end_in_y - 1.f + sampling_point) * scale_y - sampling_point + 1.f); |
| 80 | break; |
| 81 | } |
| 82 | case InterpolationPolicy::AREA: |
| 83 | break; |
| 84 | default: |
| 85 | { |
| 86 | ARM_COMPUTE_ERROR("Invalid InterpolationPolicy"); |
| 87 | break; |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | |
| 92 | // Setup output valid region |
Georgios Pinitas | cbf4d55 | 2020-09-04 20:51:24 +0100 | [diff] [blame] | 93 | ValidRegion valid_region{ Coordinates(), dst_shape, dst_shape.num_dimensions() }; |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 94 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 95 | valid_region.anchor.set(idx_width, std::max(0, valid_start_out_x)); |
| 96 | valid_region.anchor.set(idx_height, std::max(0, valid_start_out_y)); |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 97 | |
Georgios Pinitas | 393fa4c | 2018-05-08 15:54:53 +0100 | [diff] [blame] | 98 | valid_region.shape.set(idx_width, std::min<size_t>(valid_end_out_x - valid_start_out_x, dst_shape[idx_width])); |
| 99 | valid_region.shape.set(idx_height, std::min<size_t>(valid_end_out_y - valid_start_out_y, dst_shape[idx_height])); |
Diego Lopez Recas | 0085429 | 2018-02-22 13:08:01 +0000 | [diff] [blame] | 100 | |
| 101 | return valid_region; |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 102 | } |
SiCong Li | 96209c7 | 2020-08-21 12:28:30 +0100 | [diff] [blame] | 103 | } // namespace arm_compute |