blob: c0af3bb379d4787d86807598d5a1f9b196a1bf03 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Diego Lopez Recasff860cf2018-02-22 13:08:01 +00002 * Copyright (c) 2016-2018 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +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 */
24#include "arm_compute/core/Helpers.h"
25
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026using namespace arm_compute;
27
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000028Window arm_compute::calculate_max_window(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010029{
30 if(!skip_border)
31 {
32 border_size = BorderSize(0);
33 }
34
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000035 const Coordinates &anchor = valid_region.anchor;
36 const TensorShape &shape = valid_region.shape;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037
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 Pflanzera1848362017-08-25 12:30:03 +010045 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 Barbier6ff3b192017-09-04 18:44:23 +010046 steps[0]));
47
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000048 size_t n = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010049
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000050 if(anchor.num_dimensions() > 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051 {
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 Pflanzera1848362017-08-25 12:30:03 +010056 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 Barbier6ff3b192017-09-04 18:44:23 +010057 steps[1]));
58
59 ++n;
60 }
61
Giorgio Arenadcb5b282018-04-25 12:07:29 +010062 if(anchor.num_dimensions() > 2)
63 {
64 window.set(2, Window::Dimension(anchor[2], std::max<size_t>(1, shape[2]), steps[2]));
65
66 ++n;
67 }
68
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000069 for(; n < anchor.num_dimensions(); ++n)
70 {
71 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
72 }
73
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074 for(; n < Coordinates::num_max_dimensions; ++n)
75 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000076 window.set(n, Window::Dimension(0, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +010077 }
78
79 return window;
80}
81
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000082Window arm_compute::calculate_max_enlarged_window(const ValidRegion &valid_region, const Steps &steps, BorderSize border_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +010083{
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000084 const Coordinates &anchor = valid_region.anchor;
85 const TensorShape &shape = valid_region.shape;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010086
87 Window window;
88
89 window.set(0, Window::Dimension(
90 // move the anchor to the start from the border
91 anchor[0] - border_size.left,
92 // move the anchor to include the right end border
93 // Make sure the window width is a multiple of the step size
94 anchor[0] - border_size.left + ceil_to_multiple(shape[0] + border_size.left + border_size.right, steps[0]),
95 steps[0]));
96
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000097 size_t n = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +010098
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +000099 if(anchor.num_dimensions() > 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100100 {
101 window.set(1, Window::Dimension(
102 // Include the border above the image
103 anchor[1] - border_size.top,
104 // Include the border below the image
105 anchor[1] - border_size.top + ceil_to_multiple(shape[1] + border_size.top + border_size.bottom, steps[1]),
106 steps[1]));
107
108 ++n;
109 }
110
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000111 if(anchor.num_dimensions() > 2)
Anthony Barbier7068f992017-10-26 15:23:08 +0100112 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000113 window.set(2, Window::Dimension(0, std::max<size_t>(1, shape[n]), steps[2]));
Anthony Barbier7068f992017-10-26 15:23:08 +0100114
115 ++n;
116 }
117
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000118 for(; n < anchor.num_dimensions(); ++n)
119 {
120 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
121 }
122
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100123 for(; n < Coordinates::num_max_dimensions; ++n)
124 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000125 window.set(n, Window::Dimension(0, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100126 }
127
128 return window;
129}
130
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000131Window arm_compute::calculate_max_window_horizontal(const ValidRegion &valid_region, const Steps &steps, bool skip_border, BorderSize border_size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100132{
133 if(skip_border)
134 {
135 border_size.top = 0;
136 border_size.bottom = 0;
137 }
138 else
139 {
140 border_size.left = 0;
141 border_size.right = 0;
142 }
143
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000144 const Coordinates &anchor = valid_region.anchor;
145 const TensorShape &shape = valid_region.shape;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100146
147 Window window;
148
149 window.set(0, Window::Dimension(
150 // Skip the border left of the image
151 anchor[0] + border_size.left,
152 // Skip the border right of the image
153 // Make sure the window width is a multiple of the step size
Moritz Pflanzera1848362017-08-25 12:30:03 +0100154 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 Barbier6ff3b192017-09-04 18:44:23 +0100155 steps[0]));
156
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000157 size_t n = 1;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100158
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000159 if(anchor.num_dimensions() > 1)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100160 {
161 window.set(1, Window::Dimension(
162 // Skip the border above the image
163 anchor[1] - border_size.top,
164 // Skip the border below the image
165 anchor[1] + shape[1] + border_size.bottom,
166 1));
167
168 ++n;
169 }
170
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000171 for(; n < anchor.num_dimensions(); ++n)
172 {
173 window.set(n, Window::Dimension(anchor[n], std::max<size_t>(1, shape[n])));
174 }
175
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100176 for(; n < Coordinates::num_max_dimensions; ++n)
177 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000178 window.set(n, Window::Dimension(0, 1));
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100179 }
180
181 return window;
182}
Diego Lopez Recas00854292018-02-22 13:08:01 +0000183
184ValidRegion arm_compute::calculate_valid_region_scale(const ITensorInfo &src_info, const TensorShape &dst_shape,
185 InterpolationPolicy interpolate_policy, SamplingPolicy sampling_policy, bool border_undefined)
186{
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100187 const DataLayout data_layout = src_info.data_layout();
188 const int idx_width = get_data_layout_dimension_index(data_layout, DataLayoutDimension::WIDTH);
189 const int idx_height = get_data_layout_dimension_index(data_layout, DataLayoutDimension::HEIGHT);
190
191 const float scale_x = static_cast<float>(dst_shape[idx_width]) / src_info.tensor_shape()[idx_width];
192 const float scale_y = static_cast<float>(dst_shape[idx_height]) / src_info.tensor_shape()[idx_height];
Diego Lopez Recas00854292018-02-22 13:08:01 +0000193 const float sampling_point = (sampling_policy == SamplingPolicy::CENTER) ? 0.5f : 0.0f;
194
195 // Get input's valid region start and end points
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100196 const int valid_start_in_x = src_info.valid_region().anchor[idx_width];
197 const int valid_start_in_y = src_info.valid_region().anchor[idx_height];
198 const int valid_end_in_x = src_info.valid_region().anchor[idx_width] + src_info.valid_region().shape[idx_width];
199 const int valid_end_in_y = src_info.valid_region().anchor[idx_height] + src_info.valid_region().shape[idx_height];
Diego Lopez Recas00854292018-02-22 13:08:01 +0000200
201 // Initialize output's valid region start and end points
202 auto valid_start_out_x = static_cast<int>(valid_start_in_x * scale_x);
203 auto valid_start_out_y = static_cast<int>(valid_start_in_y * scale_y);
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100204 auto valid_end_out_x = std::min<int>(std::ceil(valid_end_in_x * scale_x), dst_shape[idx_width]);
205 auto valid_end_out_y = std::min<int>(std::ceil(valid_end_in_y * scale_y), dst_shape[idx_height]);
Diego Lopez Recas00854292018-02-22 13:08:01 +0000206
207 // Handle valid points in case of the bi-linear interpolation
208 if(border_undefined)
209 {
210 switch(interpolate_policy)
211 {
212 case InterpolationPolicy::NEAREST_NEIGHBOR:
213 {
214 // (start_out + sampling_point) >= (start_in * scale)
215 // start_out = ceil((start_in * scale) - sampling_point)
216 valid_start_out_x = std::ceil(valid_start_in_x * scale_x - sampling_point);
217 valid_start_out_y = std::ceil(valid_start_in_y * scale_y - sampling_point);
218
219 // (end_out - 1 + sampling_point) < (end_in * scale)
220 // end_out = ceil((end_in * scale) - sampling_point); // <-- ceil(x - 1) strictly less
221 valid_end_out_x = std::ceil(valid_end_in_x * scale_x - sampling_point);
222 valid_end_out_y = std::ceil(valid_end_in_y * scale_y - sampling_point);
223 break;
224 }
225 case InterpolationPolicy::BILINEAR:
226 {
227 // (start_out + sampling_point) >= ((start_in + sampling_point) * scale)
228 // start_out = ceil(((start_in + sampling_point) * scale) - sampling_point)
229 valid_start_out_x = std::ceil((valid_start_in_x + sampling_point) * scale_x - sampling_point);
230 valid_start_out_y = std::ceil((valid_start_in_y + sampling_point) * scale_y - sampling_point);
231
232 // (end_out - 1 + sampling_point) <= ((end_in - 1 + sampling_point) * scale)
233 // end_out = floor(((end_in - 1 + sampling_point) * scale) - sampling_point + 1)
234 valid_end_out_x = std::floor((valid_end_in_x - 1.f + sampling_point) * scale_x - sampling_point + 1.f);
235 valid_end_out_y = std::floor((valid_end_in_y - 1.f + sampling_point) * scale_y - sampling_point + 1.f);
236 break;
237 }
238 case InterpolationPolicy::AREA:
239 break;
240 default:
241 {
242 ARM_COMPUTE_ERROR("Invalid InterpolationPolicy");
243 break;
244 }
245 }
246 }
247
248 // Setup output valid region
249 ValidRegion valid_region{ Coordinates(), dst_shape, src_info.tensor_shape().num_dimensions() };
250
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100251 valid_region.anchor.set(idx_width, std::max(0, valid_start_out_x));
252 valid_region.anchor.set(idx_height, std::max(0, valid_start_out_y));
Diego Lopez Recas00854292018-02-22 13:08:01 +0000253
Georgios Pinitas393fa4c2018-05-08 15:54:53 +0100254 valid_region.shape.set(idx_width, std::min<size_t>(valid_end_out_x - valid_start_out_x, dst_shape[idx_width]));
255 valid_region.shape.set(idx_height, std::min<size_t>(valid_end_out_y - valid_start_out_y, dst_shape[idx_height]));
Diego Lopez Recas00854292018-02-22 13:08:01 +0000256
257 return valid_region;
258}