Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 24 | #ifndef __ARM_COMPUTE_TEST_TENSOR_OPERATIONS_H__ |
| 25 | #define __ARM_COMPUTE_TEST_TENSOR_OPERATIONS_H__ |
| 26 | |
| 27 | #include "FixedPoint.h" |
| 28 | #include "Tensor.h" |
| 29 | #include "Types.h" |
| 30 | #include "Utils.h" |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 31 | #include "support/ToolchainSupport.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | |
| 33 | #include "FixedPoint.h" |
| 34 | #include "Types.h" |
| 35 | #include "arm_compute/core/FixedPoint.h" |
| 36 | #include "arm_compute/core/Types.h" |
| 37 | #include "tests/validation/FixedPoint.h" |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 38 | #include "tests/validation/ValidationUserConfiguration.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 39 | |
| 40 | #include <algorithm> |
| 41 | #include <array> |
| 42 | #include <cmath> |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 43 | #include <random> |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 44 | #include <string> |
Georgios Pinitas | d4f8c27 | 2017-06-30 16:16:19 +0100 | [diff] [blame] | 45 | #include <vector> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 46 | |
| 47 | namespace arm_compute |
| 48 | { |
| 49 | namespace test |
| 50 | { |
| 51 | namespace validation |
| 52 | { |
| 53 | namespace tensor_operations |
| 54 | { |
| 55 | namespace |
| 56 | { |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 57 | template <class T> |
| 58 | struct is_floating_point |
| 59 | : std::integral_constant < bool, |
| 60 | std::is_same<float, typename std::remove_cv<T>::type>::value || |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 61 | #ifdef ARM_COMPUTE_ENABLE_FP16 |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 62 | std::is_same<float16_t, typename std::remove_cv<T>::type>::value || |
Anthony Barbier | ac69aa1 | 2017-07-03 17:39:37 +0100 | [diff] [blame] | 63 | #endif /* ARM_COMPUTE_ENABLE_FP16 */ |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 64 | std::is_same<double, typename std::remove_cv<T>::type>::value || std::is_same<long double, typename std::remove_cv<T>::type>::value > |
| 65 | { |
| 66 | }; |
| 67 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 68 | bool is_valid_pixel(int i, int min, int max) |
| 69 | { |
| 70 | return (i >= min && i < max); |
| 71 | } |
| 72 | |
| 73 | // 3D convolution for floating point type |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 74 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 75 | void convolution3d(const T *in, const T *weights, const T *bias, T *out, int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights, int8_t fixed_point_position) |
| 76 | { |
| 77 | const int half_width_weights = width_weights / 2; |
| 78 | const int half_height_weights = height_weights / 2; |
| 79 | |
| 80 | // Reset accumulator |
| 81 | T acc = static_cast<T>(0); |
| 82 | |
| 83 | // Compute a 2D convolution for each IFM and accumulate the result |
| 84 | for(int ifm = 0; ifm < depth_in; ++ifm) |
| 85 | { |
| 86 | // Compute the offset for the input slice |
| 87 | const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in; |
| 88 | |
| 89 | // Compute 2D convolution |
| 90 | for(int yk = -half_height_weights; yk <= half_height_weights; ++yk) |
| 91 | { |
| 92 | for(int xk = -half_width_weights; xk <= half_width_weights; ++xk) |
| 93 | { |
| 94 | // Check if the pixel is out-of-bound |
| 95 | if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in)) |
| 96 | { |
| 97 | const int idx = xk + half_width_weights; |
| 98 | const int idy = yk + half_height_weights; |
| 99 | |
| 100 | const T i_value = in[offset_slice_in + xk + yk * width_in]; |
| 101 | const T w_value = weights[idx + idy * width_weights + ifm * width_weights * height_weights]; |
| 102 | |
| 103 | acc += i_value * w_value; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Accumulate the bias and store the result |
| 110 | *out = acc + (*bias); |
| 111 | } |
| 112 | |
| 113 | // 3D convolution for fixed point type |
| 114 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 115 | void convolution3d(const T *in, const T *weights, const T *bias, T *out, int xi, int yi, int width_in, int height_in, int depth_in, int width_weights, int height_weights, |
| 116 | int8_t fixed_point_position) |
| 117 | { |
| 118 | const int half_width_weights = width_weights / 2; |
| 119 | const int half_height_weights = height_weights / 2; |
| 120 | |
| 121 | using namespace fixed_point_arithmetic; |
| 122 | using promoted_type = typename fixed_point_arithmetic::traits::promote<T>::type; |
| 123 | |
| 124 | // Reset accumulator |
| 125 | fixed_point<promoted_type> acc(0, fixed_point_position); |
| 126 | |
| 127 | // Compute a 2D convolution for each IFM and accumulate the result |
| 128 | for(int ifm = 0; ifm < depth_in; ++ifm) |
| 129 | { |
| 130 | // Compute the offset for the input slice |
| 131 | const int offset_slice_in = xi + yi * width_in + ifm * width_in * height_in; |
| 132 | |
| 133 | // Compute 2D convolution |
| 134 | for(int yk = -half_height_weights; yk <= half_height_weights; ++yk) |
| 135 | { |
| 136 | for(int xk = -half_width_weights; xk <= half_width_weights; ++xk) |
| 137 | { |
| 138 | // Check if the pixel is out-of-bound |
| 139 | if(is_valid_pixel(xi + xk, 0, width_in) && is_valid_pixel(yi + yk, 0, height_in)) |
| 140 | { |
| 141 | const int idx = xk + half_width_weights; |
| 142 | const int idy = yk + half_height_weights; |
| 143 | |
| 144 | const fixed_point<promoted_type> i_value(in[offset_slice_in + xk + yk * width_in], fixed_point_position, true); |
| 145 | const fixed_point<promoted_type> w_value(weights[idx + idy * width_weights + ifm * width_weights * height_weights], fixed_point_position, true); |
| 146 | const fixed_point<promoted_type> iw = i_value * w_value; |
| 147 | acc = iw + acc; |
| 148 | } |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | // Get the bias |
| 154 | const fixed_point<promoted_type> b(*bias, fixed_point_position, true); |
| 155 | |
| 156 | // Accumulate the bias and covert back |
| 157 | acc = acc + b; |
| 158 | fixed_point<T> res(acc); |
| 159 | *out = res.raw(); |
| 160 | } |
| 161 | |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 162 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 163 | void vector_matrix_multiply(const T *in, const T *weights, const T *bias, T *out, int cols_weights, int rows_weights, uint8_t fixed_point_position) |
| 164 | { |
| 165 | for(int x = 0; x < cols_weights; ++x) |
| 166 | { |
| 167 | T acc = 0.0f; |
| 168 | for(int y = 0; y < rows_weights; ++y) |
| 169 | { |
| 170 | acc += in[y] * weights[x + y * cols_weights]; |
| 171 | } |
| 172 | out[x] = acc + bias[x]; |
| 173 | } |
| 174 | } |
| 175 | |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 176 | // Vector matrix multiply for fixed point type |
| 177 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 178 | void vector_matrix_multiply(const T *in, const T *weights, const T *bias, T *out, int cols_weights, int rows_weights, uint8_t fixed_point_position) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | { |
| 180 | using namespace fixed_point_arithmetic; |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 181 | using promoted_type = typename fixed_point_arithmetic::traits::promote<T>::type; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 182 | |
| 183 | for(int x = 0; x < cols_weights; ++x) |
| 184 | { |
| 185 | // Reset accumulator |
| 186 | fixed_point<promoted_type> acc(0, fixed_point_position); |
| 187 | |
| 188 | for(int y = 0; y < rows_weights; ++y) |
| 189 | { |
| 190 | const fixed_point<promoted_type> i_value(in[y], fixed_point_position, true); |
| 191 | const fixed_point<promoted_type> w_value(weights[x + y * cols_weights], fixed_point_position, true); |
| 192 | const fixed_point<promoted_type> iw = i_value * w_value; |
| 193 | acc = iw + acc; |
| 194 | } |
| 195 | |
| 196 | // Get the bias |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 197 | const fixed_point<T> b(bias[x], fixed_point_position, true); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 198 | |
| 199 | // Convert back and accumulate the bias |
Gian Marco Iodice | 2bbd964 | 2017-07-04 16:46:32 +0100 | [diff] [blame] | 200 | fixed_point<T> res(acc); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 201 | res = res + b; |
| 202 | |
| 203 | // Store the result |
| 204 | out[x] = res.raw(); |
| 205 | } |
| 206 | } |
| 207 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 208 | // Return a tensor element at a specified coordinate with different border modes |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 209 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type = 0> |
| 210 | T tensor_elem_at(const Tensor<T> &in, Coordinates &coord, BorderMode border_mode, T constant_border_value) |
| 211 | { |
| 212 | const int x = coord.x(); |
| 213 | const int y = coord.y(); |
| 214 | const int width = static_cast<int>(in.shape().x()); |
| 215 | const int height = static_cast<int>(in.shape().y()); |
| 216 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 217 | // If coordinates beyond range of tensor's width or height |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 218 | if(x < 0 || y < 0 || x >= width || y >= height) |
| 219 | { |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 220 | if(border_mode == BorderMode::REPLICATE) |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 221 | { |
| 222 | coord.set(0, std::max(0, std::min(x, width - 1))); |
| 223 | coord.set(1, std::max(0, std::min(y, height - 1))); |
| 224 | return in[coord2index(in.shape(), coord)]; |
| 225 | } |
| 226 | else |
| 227 | { |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 228 | return constant_border_value; |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 229 | } |
| 230 | } |
| 231 | else |
| 232 | { |
| 233 | return in[coord2index(in.shape(), coord)]; |
| 234 | } |
| 235 | } |
| 236 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 237 | /** Apply 2D spatial filter on a single element of @p in at coordinates @p coord |
| 238 | * |
| 239 | * - filter sizes have to be odd number |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 240 | * - Row major order of filter assumed |
| 241 | * - TO_ZERO rounding policy assumed |
| 242 | * - SATURATE convert policy assumed |
| 243 | * |
| 244 | */ |
| 245 | template <typename T1, typename T2, typename T3> |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 246 | void apply_2d_spatial_filter(Coordinates coord, const Tensor<T1> &in, Tensor<T3> &out, const TensorShape &filter_shape, const T2 *filter_itr, float scale, BorderMode border_mode, |
| 247 | T1 constant_border_value = 0) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 248 | { |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 249 | double val = 0; |
| 250 | const int x = coord.x(); |
| 251 | const int y = coord.y(); |
| 252 | for(int j = y - static_cast<int>(filter_shape[1] / 2); j <= y + static_cast<int>(filter_shape[1] / 2); ++j) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 253 | { |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 254 | for(int i = x - static_cast<int>(filter_shape[0] / 2); i <= x + static_cast<int>(filter_shape[0] / 2); ++i) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 255 | { |
| 256 | coord.set(0, i); |
| 257 | coord.set(1, j); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 258 | val += static_cast<double>(*filter_itr) * tensor_elem_at(in, coord, border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 259 | ++filter_itr; |
| 260 | } |
| 261 | } |
| 262 | coord.set(0, x); |
| 263 | coord.set(1, y); |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 264 | const double rounded_val = support::cpp11::trunc(val * static_cast<double>(scale)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 265 | out[coord2index(in.shape(), coord)] = saturate_cast<T3>(rounded_val); |
| 266 | } |
| 267 | } // namespace |
| 268 | |
Giorgio Arena | 50f9fd7 | 2017-06-19 17:05:30 +0100 | [diff] [blame] | 269 | // Sobel 3x3 |
| 270 | template <typename T1, typename T2> |
| 271 | void sobel_3x3(Tensor<T1> &in, Tensor<T2> &out_x, Tensor<T2> &out_y, BorderMode border_mode, uint8_t constant_border_value) |
| 272 | { |
| 273 | const std::array<int8_t, 9> sobel_x{ { -1, 0, 1, -2, 0, 2, -1, 0, 1 } }; |
| 274 | const std::array<int8_t, 9> sobel_y{ { -1, -2, -1, 0, 0, 0, 1, 2, 1 } }; |
| 275 | |
| 276 | for(int element_idx = 0; element_idx < in.num_elements(); ++element_idx) |
| 277 | { |
| 278 | const Coordinates id = index2coord(in.shape(), element_idx); |
| 279 | |
| 280 | apply_2d_spatial_filter(id, in, out_x, TensorShape(3U, 3U), sobel_x.data(), 1.f, border_mode, constant_border_value); |
| 281 | apply_2d_spatial_filter(id, in, out_y, TensorShape(3U, 3U), sobel_y.data(), 1.f, border_mode, constant_border_value); |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | // Sobel 5x5 |
| 286 | template <typename T1, typename T2> |
| 287 | void sobel_5x5(Tensor<T1> &in, Tensor<T2> &out_x, Tensor<T2> &out_y, BorderMode border_mode, uint8_t constant_border_value) |
| 288 | { |
| 289 | const std::array<int8_t, 25> sobel_x{ { |
| 290 | -1, -2, 0, 2, 1, |
| 291 | -4, -8, 0, 8, 4, |
| 292 | -6, -12, 0, 12, 6, |
| 293 | -4, -8, 0, 8, 4, |
| 294 | -1, -2, 0, 2, 1 |
| 295 | } }; |
| 296 | |
| 297 | const std::array<int8_t, 25> sobel_y{ { |
| 298 | -1, -4, -6, -4, -1, |
| 299 | -2, -8, -12, -8, -2, |
| 300 | 0, 0, 0, 0, 0, |
| 301 | 2, 8, 12, 8, 2, |
| 302 | 1, 4, 6, 4, 1 |
| 303 | } }; |
| 304 | |
| 305 | for(int element_idx = 0; element_idx < in.num_elements(); ++element_idx) |
| 306 | { |
| 307 | const Coordinates id = index2coord(in.shape(), element_idx); |
| 308 | |
| 309 | apply_2d_spatial_filter(id, in, out_x, TensorShape(5U, 5U), sobel_x.data(), 1.f, border_mode, constant_border_value); |
| 310 | apply_2d_spatial_filter(id, in, out_y, TensorShape(5U, 5U), sobel_y.data(), 1.f, border_mode, constant_border_value); |
| 311 | } |
| 312 | } |
| 313 | |
Giorgio Arena | 2ca209e | 2017-06-13 15:49:37 +0100 | [diff] [blame] | 314 | // Min max location |
| 315 | template <typename T1> |
| 316 | void min_max_location(const Tensor<T1> &in, int32_t &min, int32_t &max, Coordinates2DArray &min_loc, Coordinates2DArray &max_loc, uint32_t &min_count, uint32_t &max_count) |
| 317 | { |
| 318 | // Set min and max to first pixel |
| 319 | min = in[0]; |
| 320 | max = in[0]; |
| 321 | min_count = 0; |
| 322 | max_count = 0; |
| 323 | |
| 324 | const size_t width = in.shape().x(); |
| 325 | |
| 326 | // Look for min and max values |
| 327 | for(int i = 1; i < in.num_elements(); ++i) |
| 328 | { |
| 329 | if(static_cast<int32_t>(in[i]) < min) |
| 330 | { |
| 331 | min = in[i]; |
| 332 | } |
| 333 | if(static_cast<int32_t>(in[i]) > max) |
| 334 | { |
| 335 | max = in[i]; |
| 336 | } |
| 337 | } |
| 338 | |
| 339 | for(int i = 0; i < in.num_elements(); ++i) |
| 340 | { |
| 341 | if(static_cast<int32_t>(in[i]) == min) |
| 342 | { |
| 343 | Coordinates2D min_coord; |
| 344 | min_coord.x = static_cast<int32_t>(i % width); |
| 345 | min_coord.y = static_cast<int32_t>(i / width); |
| 346 | |
| 347 | min_loc.push_back(min_coord); |
| 348 | |
| 349 | min_count++; |
| 350 | } |
| 351 | if(static_cast<int32_t>(in[i]) == max) |
| 352 | { |
| 353 | Coordinates2D max_coord; |
| 354 | max_coord.x = static_cast<int32_t>(i % width); |
| 355 | max_coord.y = static_cast<int32_t>(i / width); |
| 356 | |
| 357 | max_loc.push_back(max_coord); |
| 358 | |
| 359 | max_count++; |
| 360 | } |
| 361 | } |
| 362 | } |
| 363 | |
Giorgio Arena | f795986 | 2017-06-13 15:19:51 +0100 | [diff] [blame] | 364 | // Mean Standard Deviation |
| 365 | template <typename T1> |
| 366 | void mean_and_standard_deviation(const Tensor<T1> &in, float &mean, float &std_dev) |
| 367 | { |
| 368 | int num_elements = in.num_elements(); |
| 369 | |
| 370 | // Calculate mean |
| 371 | mean = 0.f; |
| 372 | for(int i = 0; i < num_elements; ++i) |
| 373 | { |
| 374 | mean += in[i]; |
| 375 | } |
| 376 | mean /= num_elements; |
| 377 | |
| 378 | // Calculate standard deviation |
| 379 | std_dev = 0.f; |
| 380 | for(int i = 0; i < num_elements; ++i) |
| 381 | { |
| 382 | std_dev += (mean - in[i]) * (mean - in[i]); |
| 383 | } |
| 384 | std_dev = sqrt(std_dev / num_elements); |
| 385 | } |
| 386 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 387 | // Integral Image |
| 388 | void integral_image(const Tensor<uint8_t> &in, Tensor<uint32_t> &out) |
| 389 | { |
| 390 | // Length of dimensions |
| 391 | const size_t width = in.shape().x(); |
| 392 | const size_t height = in.shape().y(); |
| 393 | const size_t depth = in.shape().z() * in.shape()[3] * in.shape()[4] * in.shape()[5]; |
| 394 | |
| 395 | const size_t image_size = width * height; |
| 396 | |
| 397 | for(size_t z = 0; z < depth; ++z) |
| 398 | { |
| 399 | size_t current_image = z * image_size; |
| 400 | |
| 401 | //First element of each image |
| 402 | out[current_image] = in[current_image]; |
| 403 | |
| 404 | // First row of each image (add only pixel on the left) |
| 405 | for(size_t x = 1; x < width; ++x) |
| 406 | { |
| 407 | out[current_image + x] = static_cast<uint32_t>(in[current_image + x]) + out[current_image + x - 1]; |
| 408 | } |
| 409 | |
| 410 | // Subsequent rows |
| 411 | for(size_t y = 1; y < height; ++y) |
| 412 | { |
| 413 | size_t current_row = current_image + (width * y); |
| 414 | |
| 415 | // First element of each row (add only pixel up) |
| 416 | out[current_row] = static_cast<uint32_t>(in[current_row]) + out[current_row - width]; |
| 417 | |
| 418 | // Following row elements |
| 419 | for(size_t x = 1; x < width; ++x) |
| 420 | { |
| 421 | size_t current_pixel = current_row + x; |
| 422 | |
| 423 | // out = in + up(out) + left(out) - up_left(out) |
| 424 | out[current_pixel] = static_cast<uint32_t>(in[current_pixel]) + out[current_pixel - 1] |
| 425 | + out[current_pixel - width] - out[current_pixel - width - 1]; |
| 426 | } |
| 427 | } |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | // Absolute difference |
| 432 | template <typename T1, typename T2, typename T3> |
| 433 | void absolute_difference(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out) |
| 434 | { |
| 435 | using intermediate_type = typename common_promoted_signed_type<T1, T2, T3>::intermediate_type; |
| 436 | |
| 437 | for(int i = 0; i < in1.num_elements(); ++i) |
| 438 | { |
| 439 | intermediate_type val = std::abs(static_cast<intermediate_type>(in1[i]) - static_cast<intermediate_type>(in2[i])); |
| 440 | out[i] = saturate_cast<T3>(val); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Accumulate |
| 445 | template <typename T1, typename T2> |
| 446 | void accumulate(const Tensor<T1> &in, Tensor<T2> &out) |
| 447 | { |
| 448 | using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type; |
| 449 | |
| 450 | for(int i = 0; i < in.num_elements(); ++i) |
| 451 | { |
| 452 | intermediate_type val = static_cast<intermediate_type>(out[i]) + static_cast<intermediate_type>(in[i]); |
| 453 | out[i] = saturate_cast<T2>(val); |
| 454 | } |
| 455 | } |
| 456 | |
| 457 | // Accumulate squared |
| 458 | template <typename T1, typename T2> |
| 459 | void accumulate_squared(const Tensor<T1> &in, Tensor<T2> &out, uint32_t shift) |
| 460 | { |
| 461 | if(shift > 15) |
| 462 | { |
| 463 | ARM_COMPUTE_ERROR("Shift in accumulate_squared must be within the range [0, 15]"); |
| 464 | } |
| 465 | using intermediate_type = typename common_promoted_signed_type<T1, T2>::intermediate_type; |
| 466 | intermediate_type denom = 1 << shift; |
| 467 | |
| 468 | for(int i = 0; i < in.num_elements(); ++i) |
| 469 | { |
| 470 | intermediate_type val = static_cast<intermediate_type>(out[i]) + (static_cast<intermediate_type>(in[i]) * static_cast<intermediate_type>(in[i]) / denom); |
| 471 | out[i] = saturate_cast<T2>(val); |
| 472 | } |
| 473 | } |
| 474 | |
| 475 | // Accumulate weighted |
| 476 | template <typename T> |
| 477 | void accumulate_weighted(const Tensor<T> &in, Tensor<T> &out, float alpha) |
| 478 | { |
| 479 | if(alpha < 0.f || alpha > 1.f) |
| 480 | { |
| 481 | ARM_COMPUTE_ERROR("Weight (alpha) specified in accumulate_weighted must be within the range [0, 1]"); |
| 482 | } |
| 483 | using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type; |
| 484 | |
| 485 | for(int i = 0; i < in.num_elements(); ++i) |
| 486 | { |
| 487 | double val = (1. - static_cast<double>(alpha)) * static_cast<intermediate_type>(out[i]) + static_cast<double>(alpha) * static_cast<intermediate_type>(in[i]); |
| 488 | out[i] = static_cast<T>(val); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // Arithmetic addition |
| 493 | template <typename T1, typename T2, typename T3> |
| 494 | void arithmetic_addition(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out, ConvertPolicy convert_policy) |
| 495 | { |
| 496 | using intermediate_type = typename common_promoted_signed_type<T1, T2, T3>::intermediate_type; |
| 497 | |
| 498 | for(int i = 0; i < in1.num_elements(); ++i) |
| 499 | { |
| 500 | intermediate_type val = static_cast<intermediate_type>(in1[i]) + static_cast<intermediate_type>(in2[i]); |
| 501 | out[i] = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T3>(val) : static_cast<T3>(val); |
| 502 | } |
| 503 | } |
| 504 | |
| 505 | // Arithmetic Subtraction |
| 506 | template <typename T1, typename T2, typename T3> |
| 507 | void arithmetic_subtraction(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out, ConvertPolicy convert_policy) |
| 508 | { |
| 509 | using intermediate_type = typename common_promoted_signed_type<T1, T2, T3>::intermediate_type; |
| 510 | |
| 511 | for(int i = 0; i < in1.num_elements(); ++i) |
| 512 | { |
| 513 | intermediate_type val = static_cast<intermediate_type>(in1[i]) - static_cast<intermediate_type>(in2[i]); |
| 514 | out[i] = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T3>(val) : static_cast<T3>(val); |
| 515 | } |
| 516 | } |
| 517 | |
| 518 | // Bitwise and |
| 519 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 520 | void bitwise_and(const Tensor<T> &in1, const Tensor<T> &in2, Tensor<T> &out) |
| 521 | { |
| 522 | for(int i = 0; i < in1.num_elements(); ++i) |
| 523 | { |
| 524 | out[i] = in1[i] & in2[i]; |
| 525 | } |
| 526 | } |
| 527 | |
| 528 | // Bitwise or |
| 529 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 530 | void bitwise_or(const Tensor<T> &in1, const Tensor<T> &in2, Tensor<T> &out) |
| 531 | { |
| 532 | for(int i = 0; i < in1.num_elements(); ++i) |
| 533 | { |
| 534 | out[i] = in1[i] | in2[i]; |
| 535 | } |
| 536 | } |
| 537 | |
| 538 | // Bitwise xor |
| 539 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 540 | void bitwise_xor(const Tensor<T> &in1, const Tensor<T> &in2, Tensor<T> &out) |
| 541 | { |
| 542 | for(int i = 0; i < in1.num_elements(); ++i) |
| 543 | { |
| 544 | out[i] = in1[i] ^ in2[i]; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | // Bitwise not |
| 549 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 550 | void bitwise_not(const Tensor<T> &in, Tensor<T> &out) |
| 551 | { |
| 552 | for(int i = 0; i < in.num_elements(); ++i) |
| 553 | { |
| 554 | out[i] = ~in[i]; |
| 555 | } |
| 556 | } |
| 557 | |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 558 | // Box3x3 filter |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 559 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 560 | void box3x3(const Tensor<T> &in, Tensor<T> &out, BorderMode border_mode, T constant_border_value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 561 | { |
| 562 | const std::array<T, 9> filter{ { 1, 1, 1, 1, 1, 1, 1, 1, 1 } }; |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 563 | float scale = 1.f / static_cast<float>(filter.size()); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 564 | for(int element_idx = 0; element_idx < in.num_elements(); ++element_idx) |
| 565 | { |
| 566 | const Coordinates id = index2coord(in.shape(), element_idx); |
SiCong Li | bacaf9a | 2017-06-19 13:41:45 +0100 | [diff] [blame] | 567 | apply_2d_spatial_filter(id, in, out, TensorShape(3U, 3U), filter.data(), scale, border_mode, constant_border_value); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 568 | } |
| 569 | } |
| 570 | |
| 571 | // Depth conversion |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame^] | 572 | template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&is_floating_point<T2>::value, int >::type = 0 > |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 573 | void depth_convert(const Tensor<T1> &in, Tensor<T2> &out, ConvertPolicy policy, uint32_t shift) |
| 574 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 575 | using namespace fixed_point_arithmetic; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 576 | |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 577 | const int fixed_point_position = in.fixed_point_position(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 578 | for(int i = 0; i < in.num_elements(); ++i) |
| 579 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 580 | out[i] = static_cast<float>(fixed_point<T1>(in[i], fixed_point_position, true)); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 581 | } |
| 582 | } |
| 583 | |
Pablo Tello | 91654c4 | 2017-07-05 11:32:17 +0100 | [diff] [blame^] | 584 | template < typename T1, typename T2, typename std::enable_if < is_floating_point<T1>::value &&std::is_integral<T2>::value, int >::type = 0 > |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 585 | void depth_convert(const Tensor<T1> &in, Tensor<T2> &out, ConvertPolicy policy, uint32_t shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 586 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 587 | using namespace fixed_point_arithmetic; |
| 588 | |
| 589 | const int fixed_point_position = out.fixed_point_position(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 590 | for(int i = 0; i < in.num_elements(); ++i) |
| 591 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 592 | out[i] = fixed_point<T2>(in[i], fixed_point_position).raw(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 593 | } |
| 594 | } |
| 595 | |
Georgios Pinitas | e222941 | 2017-07-12 12:30:40 +0100 | [diff] [blame] | 596 | template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&std::is_integral<T2>::value &&!std::is_same<T1, T2>::value, int >::type = 0 > |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 597 | void depth_convert(const Tensor<T1> &in, Tensor<T2> &out, ConvertPolicy policy, uint32_t shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 598 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 599 | // Up-casting |
| 600 | if(std::numeric_limits<T1>::digits <= std::numeric_limits<T2>::digits) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 601 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 602 | for(int i = 0; i < in.num_elements(); ++i) |
| 603 | { |
| 604 | out[i] = static_cast<T2>(in[i]) << shift; |
| 605 | } |
| 606 | } |
| 607 | // Down-casting |
| 608 | else |
| 609 | { |
| 610 | for(int i = 0; i < in.num_elements(); ++i) |
| 611 | { |
| 612 | T1 val = in[i] >> shift; |
| 613 | out[i] = ((policy == ConvertPolicy::SATURATE) ? saturate_cast<T2>(val) : static_cast<T2>(val)); |
| 614 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 615 | } |
| 616 | } |
| 617 | |
Georgios Pinitas | e222941 | 2017-07-12 12:30:40 +0100 | [diff] [blame] | 618 | template < typename T1, typename T2, typename std::enable_if < std::is_integral<T1>::value &&std::is_integral<T2>::value &&std::is_same<T1, T2>::value, int >::type = 0 > |
| 619 | void depth_convert(const Tensor<T1> &in, Tensor<T2> &out, ConvertPolicy policy, uint32_t shift) |
| 620 | { |
| 621 | using namespace fixed_point_arithmetic; |
| 622 | bool is_in_place = (&in == &out); |
| 623 | |
| 624 | const int fixed_point_position_in = in.fixed_point_position(); |
| 625 | const int fixed_point_position_out = (is_in_place) ? static_cast<int>(shift) : out.fixed_point_position(); |
| 626 | |
| 627 | if(!is_in_place || (fixed_point_position_in != fixed_point_position_out)) |
| 628 | { |
| 629 | for(int i = 0; i < in.num_elements(); ++i) |
| 630 | { |
| 631 | auto x = fixed_point<T2>(in[i], fixed_point_position_in, true); |
| 632 | x.rescale(fixed_point_position_out); |
| 633 | out[i] = x.raw(); |
| 634 | } |
| 635 | } |
| 636 | } |
| 637 | |
Pablo Tello | 331fc74 | 2017-07-06 11:47:06 +0100 | [diff] [blame] | 638 | template < typename T1, typename T2, typename std::enable_if < is_floating_point<T1>::value &&is_floating_point<T2>::value, int >::type = 0 > |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 639 | void depth_convert(const Tensor<T1> &in, Tensor<T2> &out, ConvertPolicy policy, uint32_t shift) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 640 | { |
| 641 | for(int i = 0; i < in.num_elements(); ++i) |
| 642 | { |
Georgios Pinitas | 21efeb4 | 2017-07-04 12:47:17 +0100 | [diff] [blame] | 643 | out[i] = static_cast<T2>(in[i]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 644 | } |
| 645 | } |
| 646 | |
SiCong Li | 5a53664 | 2017-06-19 14:47:05 +0100 | [diff] [blame] | 647 | // Gaussian3x3 filter |
| 648 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 649 | void gaussian3x3(const Tensor<T> &in, Tensor<T> &out, BorderMode border_mode, T constant_border_value) |
| 650 | { |
| 651 | const std::array<T, 9> filter{ { 1, 2, 1, 2, 4, 2, 1, 2, 1 } }; |
| 652 | const float scale = 1.f / 16.f; |
| 653 | for(int element_idx = 0; element_idx < in.num_elements(); ++element_idx) |
| 654 | { |
| 655 | const Coordinates id = index2coord(in.shape(), element_idx); |
| 656 | apply_2d_spatial_filter(id, in, out, TensorShape(3U, 3U), filter.data(), scale, border_mode, constant_border_value); |
| 657 | } |
| 658 | } |
| 659 | |
SiCong Li | 3eb263e | 2017-06-19 15:31:43 +0100 | [diff] [blame] | 660 | // Gaussian5x5 filter |
| 661 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 662 | void gaussian5x5(const Tensor<T> &in, Tensor<T> &out, BorderMode border_mode, T constant_border_value) |
| 663 | { |
| 664 | const std::array<T, 25> filter{ { |
| 665 | 1, 4, 6, 4, 1, |
| 666 | 4, 16, 24, 16, 4, |
| 667 | 6, 24, 36, 24, 6, |
| 668 | 4, 16, 24, 16, 4, |
| 669 | 1, 4, 6, 4, 1 |
| 670 | } }; |
| 671 | const float scale = 1.f / 256.f; |
| 672 | for(int element_idx = 0; element_idx < in.num_elements(); ++element_idx) |
| 673 | { |
| 674 | const Coordinates id = index2coord(in.shape(), element_idx); |
| 675 | apply_2d_spatial_filter(id, in, out, TensorShape(5U, 5U), filter.data(), scale, border_mode, constant_border_value); |
| 676 | } |
| 677 | } |
| 678 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 679 | // Matrix multiplication for floating point type |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 680 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 681 | void gemm(const Tensor<T> &in1, const Tensor<T> &in2, const Tensor<T> &in3, Tensor<T> &out, float alpha, float beta) |
| 682 | { |
| 683 | const int M = out.shape().y(); |
| 684 | const int N = out.shape().x(); |
| 685 | const int K = in1.shape().x(); |
| 686 | |
| 687 | for(int r = 0; r < M; ++r) |
| 688 | { |
| 689 | for(int c = 0; c < N; ++c) |
| 690 | { |
| 691 | T acc = 0.0f; |
| 692 | |
| 693 | for(int k = 0; k < K; ++k) |
| 694 | { |
| 695 | const T a0 = in1[r * K + k]; |
| 696 | const T b0 = in2[k * N + c]; |
| 697 | |
| 698 | acc += a0 * b0; |
| 699 | } |
| 700 | |
| 701 | // Finalize the result: A * B * alpha + C * beta |
| 702 | const T c0 = in3[c + r * N]; |
| 703 | out[c + r * N] = alpha * acc + beta * c0; |
| 704 | } |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | // Matrix multiplication for fixed point type |
| 709 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 710 | void gemm(const Tensor<T> &in1, const Tensor<T> &in2, const Tensor<T> &in3, Tensor<T> &out, float alpha, float beta) |
| 711 | { |
| 712 | using namespace fixed_point_arithmetic; |
| 713 | |
| 714 | using promoted_type = typename fixed_point_arithmetic::traits::promote<T>::type; |
| 715 | |
| 716 | const int M = out.shape().y(); |
| 717 | const int N = out.shape().x(); |
| 718 | const int K = in1.shape().x(); |
| 719 | const int8_t fixed_point_position = static_cast<int8_t>(in1.fixed_point_position()); |
| 720 | |
| 721 | const fixed_point<T> alpha_q(alpha, fixed_point_position); |
| 722 | const fixed_point<T> beta_q(beta, fixed_point_position); |
| 723 | |
| 724 | for(int r = 0; r < M; ++r) |
| 725 | { |
| 726 | for(int c = 0; c < N; ++c) |
| 727 | { |
| 728 | fixed_point<promoted_type> acc_q(0, fixed_point_position); |
| 729 | |
| 730 | for(int k = 0; k < K; ++k) |
| 731 | { |
| 732 | const fixed_point<promoted_type> a0_q(in1[r * K + k], fixed_point_position, true); |
| 733 | const fixed_point<promoted_type> b0_q(in2[k * N + c], fixed_point_position, true); |
| 734 | const fixed_point<promoted_type> axb_q = a0_q * b0_q; |
| 735 | |
| 736 | acc_q = axb_q + acc_q; |
| 737 | } |
| 738 | |
| 739 | // Finalize the result: A * B * alpha + C * beta |
| 740 | const fixed_point<T> c0_q(in3[c + r * N], fixed_point_position, true); |
| 741 | |
| 742 | fixed_point<T> res_q(acc_q); |
| 743 | res_q = alpha_q * res_q; |
| 744 | res_q = (c0_q * beta_q) + res_q; |
| 745 | |
| 746 | // Store the result |
| 747 | out[c + r * N] = res_q.raw(); |
| 748 | } |
| 749 | } |
| 750 | } |
| 751 | |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 752 | // Non linear filter |
| 753 | template <typename T> |
| 754 | void non_linear_filter(const Tensor<T> &in, Tensor<T> &out, NonLinearFilterFunction function, unsigned int mask_size, |
| 755 | MatrixPattern pattern, const uint8_t *mask, BorderMode border_mode, uint8_t constant_border_value) |
| 756 | { |
SiCong Li | 7a03575 | 2017-06-28 15:27:02 +0100 | [diff] [blame] | 757 | ARM_COMPUTE_ERROR_ON(pattern == MatrixPattern::OTHER && mask == nullptr); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 758 | |
| 759 | using intermediate_type = typename common_promoted_signed_type<T>::intermediate_type; |
| 760 | |
| 761 | const int sq_mask_size = mask_size * mask_size; |
| 762 | const int half_mask_size = mask_size / 2; |
| 763 | std::vector<intermediate_type> vals(sq_mask_size); |
| 764 | intermediate_type current_value = 0; |
| 765 | |
SiCong Li | 7a03575 | 2017-06-28 15:27:02 +0100 | [diff] [blame] | 766 | const ValidRegion valid_region = shape_to_valid_region(in.shape(), border_mode == BorderMode::UNDEFINED, BorderSize(half_mask_size)); |
Isabella Gottardi | 3b77e9d | 2017-06-22 11:05:41 +0100 | [diff] [blame] | 767 | |
| 768 | for(int element_idx = 0, count = 0, index = 0; element_idx < in.num_elements(); ++element_idx, count = 0, index = 0) |
| 769 | { |
| 770 | Coordinates id = index2coord(in.shape(), element_idx); |
| 771 | if(is_in_valid_region(valid_region, id)) |
| 772 | { |
| 773 | int idx = id.x(); |
| 774 | int idy = id.y(); |
| 775 | for(int y = idy - half_mask_size; y <= idy + half_mask_size; ++y) |
| 776 | { |
| 777 | for(int x = idx - half_mask_size; x <= idx + half_mask_size; ++x, ++index) |
| 778 | { |
| 779 | id.set(0, x); |
| 780 | id.set(1, y); |
| 781 | current_value = tensor_elem_at(in, id, border_mode, constant_border_value); |
| 782 | |
| 783 | if(mask[index] == 255) |
| 784 | { |
| 785 | vals[count] = static_cast<intermediate_type>(current_value); |
| 786 | ++count; |
| 787 | } |
| 788 | } |
| 789 | } |
| 790 | std::sort(vals.begin(), vals.begin() + count); |
| 791 | switch(function) |
| 792 | { |
| 793 | case NonLinearFilterFunction::MIN: |
| 794 | out[element_idx] = saturate_cast<T>(vals[0]); |
| 795 | break; |
| 796 | case NonLinearFilterFunction::MAX: |
| 797 | out[element_idx] = saturate_cast<T>(vals[count - 1]); |
| 798 | break; |
| 799 | case NonLinearFilterFunction::MEDIAN: |
| 800 | out[element_idx] = saturate_cast<T>(vals[count / 2]); |
| 801 | break; |
| 802 | default: |
| 803 | ARM_COMPUTE_ERROR("Unsupported NonLinearFilter function."); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | } |
| 808 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 809 | // Pixel-wise multiplication |
| 810 | template <typename T1, typename T2, typename T3> |
| 811 | void pixel_wise_multiplication(const Tensor<T1> &in1, const Tensor<T2> &in2, Tensor<T3> &out, float scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy) |
| 812 | { |
| 813 | if(scale < 0) |
| 814 | { |
| 815 | ARM_COMPUTE_ERROR("Scale of pixel-wise multiplication must be non-negative"); |
| 816 | } |
| 817 | using intermediate_type = typename common_promoted_signed_type<T1, T2, T3>::intermediate_type; |
| 818 | for(int i = 0; i < in1.num_elements(); ++i) |
| 819 | { |
| 820 | double val = static_cast<intermediate_type>(in1[i]) * static_cast<intermediate_type>(in2[i]) * static_cast<double>(scale); |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 821 | if(is_floating_point<T3>::value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 822 | { |
| 823 | out[i] = val; |
| 824 | } |
| 825 | else |
| 826 | { |
| 827 | double rounded_val = 0; |
| 828 | switch(rounding_policy) |
| 829 | { |
| 830 | case(RoundingPolicy::TO_ZERO): |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 831 | rounded_val = support::cpp11::trunc(val); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 832 | break; |
| 833 | case(RoundingPolicy::TO_NEAREST_UP): |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 834 | rounded_val = round_half_up(val); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 835 | break; |
| 836 | case(RoundingPolicy::TO_NEAREST_EVEN): |
Moritz Pflanzer | d0ae8b8 | 2017-06-29 14:51:57 +0100 | [diff] [blame] | 837 | rounded_val = round_half_even(val); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 838 | break; |
| 839 | default: |
| 840 | ARM_COMPUTE_ERROR("Unsupported rounding policy"); |
| 841 | } |
| 842 | out[i] = (convert_policy == ConvertPolicy::SATURATE) ? saturate_cast<T3>(rounded_val) : static_cast<T3>(rounded_val); |
| 843 | } |
| 844 | } |
| 845 | } |
| 846 | |
| 847 | // Fixed-point Pixel-wise Multiplication |
| 848 | template <typename T, typename = typename std::enable_if<std::is_integral<T>::value>::type> |
| 849 | void fixed_point_pixel_wise_multiplication(const Tensor<T> &in1, const Tensor<T> &in2, Tensor<T> &out, int scale, ConvertPolicy convert_policy, RoundingPolicy rounding_policy) |
| 850 | { |
| 851 | using namespace fixed_point_arithmetic; |
| 852 | |
| 853 | const int fixed_point_position = in1.fixed_point_position(); |
| 854 | |
| 855 | ARM_COMPUTE_ERROR_ON_MSG(in1.data_type() != in2.data_type() || in1.data_type() != out.data_type(), |
| 856 | "Tensors must all have the same DataType"); |
| 857 | ARM_COMPUTE_ERROR_ON_MSG(fixed_point_position != in2.fixed_point_position() || fixed_point_position != out.fixed_point_position(), |
| 858 | "Fixed-point position must be the same for both inputs and outputs"); |
| 859 | |
| 860 | // Validate fixed_point_position |
| 861 | ARM_COMPUTE_ERROR_ON((in1.data_type() == DataType::QS8) && (fixed_point_position == 0 || fixed_point_position > 7)); |
| 862 | ARM_COMPUTE_ERROR_ON((in1.data_type() == DataType::QS16) && (fixed_point_position == 0 || fixed_point_position > 15)); |
| 863 | |
| 864 | fixed_point<T> fp_scale(scale, fixed_point_position); |
| 865 | const bool is_sat = convert_policy == ConvertPolicy::SATURATE; |
| 866 | const bool do_scaling = scale != 1; |
| 867 | |
| 868 | for(int i = 0; i < in1.num_elements(); ++i) |
| 869 | { |
| 870 | fixed_point<T> val1(in1[i], fixed_point_position, true); |
| 871 | fixed_point<T> val2(in2[i], fixed_point_position, true); |
| 872 | fixed_point<T> res = (is_sat) ? val1 * val2 : mul<OverflowPolicy::WRAP>(val1, val2); |
| 873 | if(do_scaling) |
| 874 | { |
| 875 | res = (is_sat) ? res * fp_scale : mul<OverflowPolicy::WRAP>(res, fp_scale); |
| 876 | } |
| 877 | out[i] = res.raw(); |
| 878 | } |
| 879 | } |
| 880 | |
Isabella Gottardi | b797fa2 | 2017-06-23 15:02:11 +0100 | [diff] [blame] | 881 | //Table Lookup |
| 882 | template <typename T, typename T1> |
| 883 | void table_lookup(const Tensor<T> &in, Tensor<T> &out, std::map<T1, T1> &lut) |
| 884 | { |
| 885 | for(int i = 0; i < in.num_elements(); ++i) |
| 886 | { |
| 887 | out[i] = static_cast<T>(lut[in[i]]); |
| 888 | } |
| 889 | } |
| 890 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 891 | // Threshold |
| 892 | template <typename T> |
| 893 | void threshold(const Tensor<T> &in, Tensor<T> &out, uint8_t threshold, uint8_t false_value, uint8_t true_value, ThresholdType type, uint8_t upper) |
| 894 | { |
| 895 | switch(type) |
| 896 | { |
| 897 | case ThresholdType::BINARY: |
| 898 | for(int i = 0; i < in.num_elements(); ++i) |
| 899 | { |
| 900 | out[i] = ((in[i] > threshold) ? true_value : false_value); |
| 901 | } |
| 902 | break; |
| 903 | case ThresholdType::RANGE: |
| 904 | for(int i = 0; i < in.num_elements(); ++i) |
| 905 | { |
| 906 | if(in[i] > upper) |
| 907 | { |
| 908 | out[i] = false_value; |
| 909 | } |
| 910 | else if(in[i] < threshold) |
| 911 | { |
| 912 | out[i] = false_value; |
| 913 | } |
| 914 | else |
| 915 | { |
| 916 | out[i] = true_value; |
| 917 | } |
| 918 | } |
| 919 | break; |
| 920 | default: |
| 921 | ARM_COMPUTE_ERROR("Thresholding type not recognised"); |
| 922 | break; |
| 923 | } |
| 924 | } |
| 925 | |
| 926 | // Activation Layer for floating point type |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 927 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 928 | void activation_layer(const Tensor<T> &in, Tensor<T> &out, ActivationLayerInfo act_info) |
| 929 | { |
| 930 | const T a = static_cast<T>(act_info.a()); |
| 931 | const T b = static_cast<T>(act_info.b()); |
| 932 | |
| 933 | for(int i = 0; i < in.num_elements(); ++i) |
| 934 | { |
| 935 | T x = in[i]; |
| 936 | switch(act_info.activation()) |
| 937 | { |
| 938 | case ActivationLayerInfo::ActivationFunction::ABS: |
| 939 | out[i] = std::abs(x); |
| 940 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 941 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 942 | out[i] = a * x + b; |
| 943 | break; |
| 944 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 945 | out[i] = static_cast<T>(1) / (static_cast<T>(1) + std::exp(-x)); |
| 946 | break; |
| 947 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 948 | out[i] = std::max<T>(0, x); |
| 949 | break; |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 950 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 951 | out[i] = std::min<T>(a, std::max<T>(0, x)); |
| 952 | break; |
| 953 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 954 | out[i] = (x > 0) ? x : a * x; |
| 955 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 956 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 957 | out[i] = std::log(static_cast<T>(1) + std::exp(x)); |
| 958 | break; |
| 959 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 960 | out[i] = std::sqrt(x); |
| 961 | break; |
| 962 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 963 | out[i] = x * x; |
| 964 | break; |
| 965 | case ActivationLayerInfo::ActivationFunction::TANH: |
| 966 | out[i] = a * std::tanh(b * x); |
| 967 | break; |
| 968 | default: |
| 969 | ARM_COMPUTE_ERROR("Activation function not recognised"); |
| 970 | break; |
| 971 | } |
| 972 | } |
| 973 | } |
| 974 | |
| 975 | // Activation Layer for fixed point type |
| 976 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 977 | void activation_layer(const Tensor<T> &in, Tensor<T> &out, ActivationLayerInfo act_info) |
| 978 | { |
| 979 | using namespace fixed_point_arithmetic; |
| 980 | int fixed_point_position = in.fixed_point_position(); |
| 981 | ActivationLayerInfo::ActivationFunction act_func = act_info.activation(); |
| 982 | const fixed_point<T> a(act_info.a(), fixed_point_position); |
| 983 | const fixed_point<T> b(act_info.b(), fixed_point_position); |
| 984 | const fixed_point<T> const_0(0, fixed_point_position); |
| 985 | const fixed_point<T> const_1(1, fixed_point_position); |
| 986 | |
| 987 | for(int i = 0; i < in.num_elements(); ++i) |
| 988 | { |
| 989 | fixed_point<T> x(in[i], fixed_point_position, true); |
| 990 | switch(act_func) |
| 991 | { |
| 992 | case ActivationLayerInfo::ActivationFunction::ABS: |
| 993 | out[i] = abs(x).raw(); |
| 994 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 995 | case ActivationLayerInfo::ActivationFunction::LINEAR: |
| 996 | out[i] = add(b, mul(a, x)).raw(); |
| 997 | break; |
| 998 | case ActivationLayerInfo::ActivationFunction::LOGISTIC: |
| 999 | out[i] = (const_1 / (const_1 + exp(-x))).raw(); |
| 1000 | break; |
| 1001 | case ActivationLayerInfo::ActivationFunction::RELU: |
| 1002 | out[i] = max(const_0, x).raw(); |
| 1003 | break; |
Georgios Pinitas | 579c049 | 2017-07-12 16:12:12 +0100 | [diff] [blame] | 1004 | case ActivationLayerInfo::ActivationFunction::BOUNDED_RELU: |
| 1005 | out[i] = min(a, max(const_0, x)).raw(); |
| 1006 | break; |
| 1007 | case ActivationLayerInfo::ActivationFunction::LEAKY_RELU: |
| 1008 | out[i] = (x > const_0) ? x.raw() : mul(a, x).raw(); |
| 1009 | break; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1010 | case ActivationLayerInfo::ActivationFunction::SOFT_RELU: |
| 1011 | out[i] = log(const_1 + exp(x)).raw(); |
| 1012 | break; |
| 1013 | case ActivationLayerInfo::ActivationFunction::SQRT: |
| 1014 | out[i] = (const_1 / inv_sqrt(x)).raw(); |
| 1015 | break; |
| 1016 | case ActivationLayerInfo::ActivationFunction::SQUARE: |
| 1017 | out[i] = mul(x, x).raw(); |
| 1018 | break; |
| 1019 | case ActivationLayerInfo::ActivationFunction::TANH: |
Georgios Pinitas | ccc65d4 | 2017-06-27 17:39:11 +0100 | [diff] [blame] | 1020 | out[i] = mul(a, tanh(mul(b, x))).raw(); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1021 | break; |
| 1022 | default: |
| 1023 | ARM_COMPUTE_ERROR("Activation function not recognised"); |
| 1024 | break; |
| 1025 | } |
| 1026 | } |
| 1027 | } |
| 1028 | |
| 1029 | // Batch Normalization Layer for fixed point type |
| 1030 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 1031 | void batch_normalization_layer(const Tensor<T> &in, Tensor<T> &out, const Tensor<T> &mean, const Tensor<T> &var, const Tensor<T> &beta, const Tensor<T> &gamma, float epsilon, int fixed_point_position) |
| 1032 | { |
| 1033 | const int cols = static_cast<int>(in.shape()[0]); |
| 1034 | const int rows = static_cast<int>(in.shape()[1]); |
| 1035 | const int depth = static_cast<int>(in.shape()[2]); |
| 1036 | int upper_dims = in.shape().total_size() / (cols * rows * depth); |
| 1037 | |
| 1038 | for(int r = 0; r < upper_dims; ++r) |
| 1039 | { |
| 1040 | for(int i = 0; i < depth; ++i) |
| 1041 | { |
| 1042 | for(int k = 0; k < rows; ++k) |
| 1043 | { |
| 1044 | for(int l = 0; l < cols; ++l) |
| 1045 | { |
| 1046 | const int pos = l + k * cols + i * rows * cols + r * cols * rows * depth; |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 1047 | fixed_point_arithmetic::fixed_point<T> in_qs(in[pos], fixed_point_position, true); |
| 1048 | fixed_point_arithmetic::fixed_point<T> var_qs(var[i], fixed_point_position, true); |
| 1049 | fixed_point_arithmetic::fixed_point<T> mean_qs(mean[i], fixed_point_position, true); |
| 1050 | fixed_point_arithmetic::fixed_point<T> beta_qs(beta[i], fixed_point_position, true); |
| 1051 | fixed_point_arithmetic::fixed_point<T> gamma_qs(gamma[i], fixed_point_position, true); |
| 1052 | fixed_point_arithmetic::fixed_point<T> epsilon_qs(epsilon, fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1053 | |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 1054 | auto denominator = fixed_point_arithmetic::inv_sqrt(var_qs + epsilon_qs); |
| 1055 | auto numerator = in_qs - mean_qs; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1056 | auto x_bar = numerator * denominator; |
Michalis Spyrou | 172e570 | 2017-06-26 14:18:47 +0100 | [diff] [blame] | 1057 | x_bar = beta_qs + x_bar * gamma_qs; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1058 | out[pos] = x_bar.raw(); |
| 1059 | } |
| 1060 | } |
| 1061 | } |
| 1062 | } |
| 1063 | } |
| 1064 | |
| 1065 | // Batch Normalization Layer for floating point type |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 1066 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1067 | void batch_normalization_layer(const Tensor<T> &in, Tensor<T> &out, const Tensor<T> &mean, const Tensor<T> &var, const Tensor<T> &beta, const Tensor<T> &gamma, float epsilon, int fixed_point_position) |
| 1068 | { |
| 1069 | const int cols = static_cast<int>(in.shape()[0]); |
| 1070 | const int rows = static_cast<int>(in.shape()[1]); |
| 1071 | const int depth = static_cast<int>(in.shape()[2]); |
| 1072 | int upper_dims = in.shape().total_size() / (cols * rows * depth); |
| 1073 | |
| 1074 | for(int r = 0; r < upper_dims; ++r) |
| 1075 | { |
| 1076 | for(int i = 0; i < depth; ++i) |
| 1077 | { |
| 1078 | for(int k = 0; k < rows; ++k) |
| 1079 | { |
| 1080 | for(int l = 0; l < cols; ++l) |
| 1081 | { |
| 1082 | const int pos = l + k * cols + i * rows * cols + r * cols * rows * depth; |
| 1083 | const float denominator = sqrt(var[i] + epsilon); |
| 1084 | const float numerator = in[pos] - mean[i]; |
| 1085 | const float x_bar = numerator / denominator; |
| 1086 | out[pos] = beta[i] + x_bar * gamma[i]; |
| 1087 | } |
| 1088 | } |
| 1089 | } |
| 1090 | } |
| 1091 | } |
| 1092 | |
Georgios Pinitas | ac4e873 | 2017-07-05 17:02:25 +0100 | [diff] [blame] | 1093 | // Depth Concatenate layer |
| 1094 | template <typename T> |
| 1095 | void depth_concatenate_layer(const std::vector<const Tensor<T> *> &srcs, Tensor<T> &out) |
| 1096 | { |
| 1097 | unsigned depth_offset = 0; |
| 1098 | const int width_out = out.shape().x(); |
| 1099 | const int height_out = out.shape().y(); |
| 1100 | const int depth_out = out.shape().z(); |
| 1101 | const int out_stride_z = width_out * height_out; |
| 1102 | const int batches = out.shape().total_size_upper(3); |
| 1103 | |
| 1104 | // Set output tensor to 0 |
| 1105 | memset(out.data(), 0, out.num_elements() * element_size_from_data_type(out.data_type())); |
| 1106 | |
| 1107 | for(unsigned int i = 0; i < srcs.size(); ++i) |
| 1108 | { |
| 1109 | ARM_COMPUTE_ERROR_ON(srcs[i] == nullptr); |
| 1110 | ARM_COMPUTE_ERROR_ON(srcs[i]->data_type() != out.data_type()); |
| 1111 | ARM_COMPUTE_ERROR_ON(depth_offset >= out.shape().z()); |
| 1112 | ARM_COMPUTE_ERROR_ON(batches != static_cast<int>(srcs[i]->shape().total_size_upper(3))); |
| 1113 | |
| 1114 | const Tensor<T> *src = srcs[i]; |
| 1115 | const int width = src->shape().x(); |
| 1116 | const int height = src->shape().y(); |
| 1117 | const int depth = src->shape().z(); |
| 1118 | const unsigned int x_diff = (width_out - width) / 2; |
| 1119 | const unsigned int y_diff = (height_out - height) / 2; |
| 1120 | |
| 1121 | const T *src_ptr = src->data(); |
| 1122 | for(int b = 0; b < batches; ++b) |
| 1123 | { |
| 1124 | const unsigned int offset_to_first_element = b * out_stride_z * depth_out + depth_offset * out_stride_z |
| 1125 | + y_diff * width_out + x_diff; |
| 1126 | for(int d = 0; d < depth; ++d) |
| 1127 | { |
| 1128 | for(int r = 0; r < height; ++r) |
| 1129 | { |
| 1130 | std::copy(src_ptr, src_ptr + width, out.data() + offset_to_first_element + d * out_stride_z + r * width_out); |
| 1131 | src_ptr += width; |
| 1132 | } |
| 1133 | } |
| 1134 | } |
| 1135 | |
| 1136 | depth_offset += depth; |
| 1137 | } |
| 1138 | } |
| 1139 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1140 | // Convolution layer |
| 1141 | template <typename T> |
| 1142 | void convolution_layer(const Tensor<T> &in, const Tensor<T> &weights, const Tensor<T> &bias, Tensor<T> &out, const PadStrideInfo &conv_info) |
| 1143 | { |
| 1144 | const int width_in = in.shape().x(); |
| 1145 | const int height_in = in.shape().y(); |
| 1146 | const int depth_in = in.shape().z(); |
| 1147 | const int width_out = out.shape().x(); |
| 1148 | const int height_out = out.shape().y(); |
| 1149 | const int depth_out = out.shape().z(); |
| 1150 | const int width_weights = weights.shape().x(); |
| 1151 | const int height_weights = weights.shape().y(); |
| 1152 | const int depth_weights = weights.shape().z(); |
| 1153 | const int pad_xi = std::min(static_cast<int>(conv_info.pad().first), width_weights / 2); |
| 1154 | const int pad_yi = std::min(static_cast<int>(conv_info.pad().second), height_weights / 2); |
| 1155 | const int start_xi = width_weights / 2 - pad_xi; |
| 1156 | const int start_yi = height_weights / 2 - pad_yi; |
| 1157 | const int end_xi = width_in - start_xi; |
| 1158 | const int end_yi = height_in - start_yi; |
| 1159 | const int stride_xi = conv_info.stride().first; |
| 1160 | const int stride_yi = conv_info.stride().second; |
| 1161 | const int num_batches = in.shape().total_size() / (width_in * height_in * depth_in); |
| 1162 | |
| 1163 | for(int r = 0; r < num_batches; ++r) |
| 1164 | { |
| 1165 | for(int yi = start_yi; yi < end_yi; yi += stride_yi) |
| 1166 | { |
| 1167 | for(int xi = start_xi; xi < end_xi; xi += stride_xi) |
| 1168 | { |
| 1169 | for(int ofm = 0; ofm < depth_out; ++ofm) |
| 1170 | { |
| 1171 | // Compute input and output offsets |
| 1172 | const int offset_in = r * width_in * height_in * depth_in; |
| 1173 | const int xo = (xi - start_xi) / stride_xi; |
| 1174 | const int yo = (yi - start_yi) / stride_yi; |
| 1175 | const int offset_out = xo + yo * width_out + ofm * width_out * height_out + r * width_out * height_out * depth_out; |
| 1176 | |
| 1177 | // Compute 3D convolution |
| 1178 | convolution3d(in.data() + offset_in, |
| 1179 | weights.data() + ofm * width_weights * height_weights * depth_weights, |
| 1180 | bias.data() + ofm, |
| 1181 | out.data() + offset_out, |
| 1182 | xi, yi, |
| 1183 | width_in, height_in, depth_in, |
| 1184 | width_weights, height_weights, |
| 1185 | static_cast<int8_t>(in.fixed_point_position())); |
| 1186 | } |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | } |
| 1191 | |
| 1192 | // Fully connected layer |
| 1193 | template <typename T> |
| 1194 | void fully_connected_layer(const Tensor<T> &in, const Tensor<T> &weights, const Tensor<T> &bias, Tensor<T> &out) |
| 1195 | { |
| 1196 | ARM_COMPUTE_ERROR_ON(weights.shape().x() != out.shape().x()); |
| 1197 | ARM_COMPUTE_ERROR_ON(weights.shape().y() != in.shape().x() * in.shape().y() * in.shape().z()); |
| 1198 | const int cols_weights = weights.shape().x(); |
| 1199 | const int rows_weights = weights.shape().y(); |
| 1200 | const int num_batches = in.shape().total_size() / rows_weights; |
| 1201 | |
| 1202 | for(int k = 0; k < num_batches; ++k) |
| 1203 | { |
| 1204 | vector_matrix_multiply<T>(in.data() + k * rows_weights, |
| 1205 | weights.data(), |
| 1206 | bias.data(), |
| 1207 | out.data() + k * cols_weights, |
| 1208 | cols_weights, |
| 1209 | rows_weights, |
| 1210 | in.fixed_point_position()); |
| 1211 | } |
| 1212 | } |
| 1213 | |
| 1214 | // Normalization Layer for floating point type |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 1215 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1216 | void normalization_layer(const Tensor<T> &in, Tensor<T> &out, NormalizationLayerInfo norm_info) |
| 1217 | { |
| 1218 | const uint32_t norm_size = norm_info.norm_size(); |
| 1219 | NormType type = norm_info.type(); |
| 1220 | float beta = norm_info.beta(); |
| 1221 | uint32_t kappa = norm_info.kappa(); |
| 1222 | |
| 1223 | const int cols = static_cast<int>(in.shape()[0]); |
| 1224 | const int rows = static_cast<int>(in.shape()[1]); |
| 1225 | const int depth = static_cast<int>(in.shape()[2]); |
| 1226 | int upper_dims = in.shape().total_size() / (cols * rows); |
| 1227 | |
| 1228 | float coeff = norm_info.scale_coeff(); |
| 1229 | int radius_cols = norm_size / 2; |
| 1230 | // IN_MAP_1D and CROSS_MAP normalize over a single axis only |
| 1231 | int radius_rows = (NormType::IN_MAP_2D == type) ? norm_size / 2 : 0; |
| 1232 | |
| 1233 | if(type == NormType::CROSS_MAP) |
| 1234 | { |
| 1235 | // Remove also depth from upper dimensions since it is the axes we want |
| 1236 | // to use for normalization |
| 1237 | upper_dims /= depth; |
| 1238 | for(int r = 0; r < upper_dims; ++r) |
| 1239 | { |
| 1240 | for(int i = 0; i < rows; ++i) |
| 1241 | { |
| 1242 | for(int k = 0; k < cols; ++k) |
| 1243 | { |
| 1244 | for(int l = 0; l < depth; ++l) |
| 1245 | { |
| 1246 | float accumulated_scale = 0.f; |
| 1247 | for(int j = -radius_cols; j <= radius_cols; ++j) |
| 1248 | { |
| 1249 | const int z = l + j; |
| 1250 | if(z >= 0 && z < depth) |
| 1251 | { |
| 1252 | const T value = in[k + i * cols + z * rows * cols + r * cols * rows * depth]; |
| 1253 | accumulated_scale += value * value; |
| 1254 | } |
| 1255 | } |
| 1256 | out[k + i * cols + l * rows * cols + r * cols * rows * depth] = kappa + accumulated_scale * coeff; |
| 1257 | } |
| 1258 | } |
| 1259 | } |
| 1260 | } |
| 1261 | } |
| 1262 | else |
| 1263 | { |
| 1264 | for(int r = 0; r < upper_dims; ++r) |
| 1265 | { |
| 1266 | for(int i = 0; i < rows; ++i) |
| 1267 | { |
| 1268 | for(int k = 0; k < cols; ++k) |
| 1269 | { |
| 1270 | float accumulated_scale = 0.f; |
| 1271 | for(int j = -radius_rows; j <= radius_rows; ++j) |
| 1272 | { |
| 1273 | const int y = i + j; |
| 1274 | for(int l = -radius_cols; l <= radius_cols; ++l) |
| 1275 | { |
| 1276 | const int x = k + l; |
| 1277 | if((x >= 0 && y >= 0) && (x < cols && y < rows)) |
| 1278 | { |
| 1279 | const T value = in[x + y * cols + r * cols * rows]; |
| 1280 | accumulated_scale += value * value; |
| 1281 | } |
| 1282 | } |
| 1283 | } |
| 1284 | out[k + i * cols + r * cols * rows] = kappa + accumulated_scale * coeff; |
| 1285 | } |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | |
| 1290 | if(beta == 1.f) |
| 1291 | { |
| 1292 | for(int i = 0; i < out.num_elements(); ++i) |
| 1293 | { |
| 1294 | out[i] = in[i] / out[i]; |
| 1295 | } |
| 1296 | } |
| 1297 | else if(beta == 0.5f) |
| 1298 | { |
| 1299 | for(int i = 0; i < out.num_elements(); ++i) |
| 1300 | { |
| 1301 | out[i] = in[i] / std::sqrt(out[i]); |
| 1302 | } |
| 1303 | } |
| 1304 | else |
| 1305 | { |
| 1306 | for(int i = 0; i < out.num_elements(); ++i) |
| 1307 | { |
| 1308 | out[i] = in[i] * std::exp(std::log(out[i]) * -beta); |
| 1309 | } |
| 1310 | } |
| 1311 | } |
| 1312 | // Normalization Layer for fixed-point types |
| 1313 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 1314 | void normalization_layer(const Tensor<T> &in, Tensor<T> &out, NormalizationLayerInfo norm_info) |
| 1315 | { |
| 1316 | using namespace fixed_point_arithmetic; |
| 1317 | |
| 1318 | const int fixed_point_position = in.fixed_point_position(); |
| 1319 | |
| 1320 | const uint32_t norm_size = norm_info.norm_size(); |
| 1321 | NormType type = norm_info.type(); |
| 1322 | fixed_point<T> beta(norm_info.beta(), fixed_point_position); |
| 1323 | fixed_point<T> kappa(norm_info.kappa(), fixed_point_position); |
| 1324 | |
| 1325 | const int cols = static_cast<int>(in.shape()[0]); |
| 1326 | const int rows = static_cast<int>(in.shape()[1]); |
| 1327 | const int depth = static_cast<int>(in.shape()[2]); |
| 1328 | int upper_dims = in.shape().total_size() / (cols * rows); |
| 1329 | |
| 1330 | fixed_point<T> coeff(norm_info.scale_coeff(), fixed_point_position); |
| 1331 | int radius_cols = norm_size / 2; |
| 1332 | // IN_MAP_1D and CROSS_MAP normalize over a single axis only |
| 1333 | int radius_rows = (NormType::IN_MAP_2D == type) ? norm_size / 2 : 0; |
| 1334 | |
| 1335 | if(type == NormType::CROSS_MAP) |
| 1336 | { |
| 1337 | // Remove also depth from upper dimensions since it is the axes we want |
| 1338 | // to use for normalization |
| 1339 | upper_dims /= depth; |
| 1340 | for(int r = 0; r < upper_dims; ++r) |
| 1341 | { |
| 1342 | for(int i = 0; i < rows; ++i) |
| 1343 | { |
| 1344 | for(int k = 0; k < cols; ++k) |
| 1345 | { |
| 1346 | for(int l = 0; l < depth; ++l) |
| 1347 | { |
| 1348 | fixed_point<T> accumulated_scale(0.f, fixed_point_position); |
| 1349 | for(int j = -radius_cols; j <= radius_cols; ++j) |
| 1350 | { |
| 1351 | const int z = l + j; |
| 1352 | if(z >= 0 && z < depth) |
| 1353 | { |
| 1354 | const T value = in[k + i * cols + z * rows * cols + r * cols * rows * depth]; |
| 1355 | const fixed_point<T> fp_value(value, fixed_point_position, true); |
| 1356 | accumulated_scale = add(accumulated_scale, mul(fp_value, fp_value)); |
| 1357 | } |
| 1358 | } |
| 1359 | accumulated_scale = add(kappa, mul(accumulated_scale, coeff)); |
| 1360 | out[k + i * cols + l * rows * cols + r * cols * rows * depth] = accumulated_scale.raw(); |
| 1361 | } |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | else |
| 1367 | { |
| 1368 | for(int r = 0; r < upper_dims; ++r) |
| 1369 | { |
| 1370 | for(int i = 0; i < rows; ++i) |
| 1371 | { |
| 1372 | for(int k = 0; k < cols; ++k) |
| 1373 | { |
| 1374 | fixed_point<T> accumulated_scale(0.f, fixed_point_position); |
| 1375 | for(int j = -radius_rows; j <= radius_rows; ++j) |
| 1376 | { |
| 1377 | const int y = i + j; |
| 1378 | for(int l = -radius_cols; l <= radius_cols; ++l) |
| 1379 | { |
| 1380 | const int x = k + l; |
| 1381 | if((x >= 0 && y >= 0) && (x < cols && y < rows)) |
| 1382 | { |
| 1383 | const T value = in[x + y * cols + r * cols * rows]; |
| 1384 | const fixed_point<T> fp_value(value, fixed_point_position, true); |
| 1385 | accumulated_scale = add(accumulated_scale, mul(fp_value, fp_value)); |
| 1386 | } |
| 1387 | } |
| 1388 | } |
| 1389 | accumulated_scale = add(kappa, mul(accumulated_scale, coeff)); |
| 1390 | out[k + i * cols + r * cols * rows] = accumulated_scale.raw(); |
| 1391 | } |
| 1392 | } |
| 1393 | } |
| 1394 | } |
| 1395 | |
| 1396 | if(norm_info.beta() == 1.f) |
| 1397 | { |
| 1398 | for(int i = 0; i < out.num_elements(); ++i) |
| 1399 | { |
| 1400 | fixed_point<T> res = div(fixed_point<T>(in[i], fixed_point_position, true), fixed_point<T>(out[i], fixed_point_position, true)); |
| 1401 | out[i] = res.raw(); |
| 1402 | } |
| 1403 | } |
| 1404 | else |
| 1405 | { |
| 1406 | const fixed_point<T> beta(norm_info.beta(), fixed_point_position); |
| 1407 | for(int i = 0; i < out.num_elements(); ++i) |
| 1408 | { |
| 1409 | fixed_point<T> res = pow(fixed_point<T>(out[i], fixed_point_position, true), beta); |
| 1410 | res = div(fixed_point<T>(in[i], fixed_point_position, true), res); |
| 1411 | out[i] = res.raw(); |
| 1412 | } |
| 1413 | } |
| 1414 | } |
| 1415 | |
| 1416 | // Pooling layer |
| 1417 | template <typename T> |
| 1418 | void pooling_layer(const Tensor<T> &in, Tensor<T> &out, PoolingLayerInfo pool_info, int fixed_point_position) |
| 1419 | { |
| 1420 | const int pool_size = pool_info.pool_size(); |
| 1421 | PoolingType type = pool_info.pool_type(); |
| 1422 | int pool_stride_x = 0; |
| 1423 | int pool_stride_y = 0; |
| 1424 | int pad_x = 0; |
| 1425 | int pad_y = 0; |
| 1426 | std::tie(pool_stride_x, pool_stride_y) = pool_info.pad_stride_info().stride(); |
| 1427 | std::tie(pad_x, pad_y) = pool_info.pad_stride_info().pad(); |
| 1428 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1429 | const int w_in = static_cast<int>(in.shape()[0]); |
| 1430 | const int h_in = static_cast<int>(in.shape()[1]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1431 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1432 | const int w_out = static_cast<int>(out.shape()[0]); |
| 1433 | const int h_out = static_cast<int>(out.shape()[1]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1434 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1435 | int upper_dims = in.shape().total_size() / (w_in * h_in); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1436 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1437 | int pooled_w = 0; |
| 1438 | int pooled_h = 0; |
| 1439 | if(pool_info.pad_stride_info().round() == DimensionRoundingType::CEIL) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1440 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1441 | pooled_w = static_cast<int>(ceil(static_cast<float>(w_in + 2 * pad_x - pool_size) / pool_stride_x)) + 1; |
| 1442 | pooled_h = static_cast<int>(ceil(static_cast<float>(h_in + 2 * pad_y - pool_size) / pool_stride_y)) + 1; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1443 | } |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1444 | else |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1445 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1446 | pooled_w = static_cast<int>(floor(static_cast<float>(w_in + 2 * pad_x - pool_size) / pool_stride_x)) + 1; |
| 1447 | pooled_h = static_cast<int>(floor(static_cast<float>(h_in + 2 * pad_y - pool_size) / pool_stride_y)) + 1; |
| 1448 | } |
| 1449 | |
| 1450 | if((pooled_w - 1) * pool_stride_x >= w_in + pad_x) |
| 1451 | { |
| 1452 | --pooled_w; |
| 1453 | } |
| 1454 | if((pooled_h - 1) * pool_stride_y >= h_in + pad_y) |
| 1455 | { |
| 1456 | --pooled_h; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1457 | } |
| 1458 | |
| 1459 | if(type == PoolingType::MAX) |
| 1460 | { |
| 1461 | for(int r = 0; r < upper_dims; ++r) |
| 1462 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1463 | for(int h = 0; h < pooled_h; ++h) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1464 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1465 | for(int w = 0; w < pooled_w; ++w) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1466 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1467 | int wstart = w * pool_stride_x - pad_x; |
| 1468 | int hstart = h * pool_stride_y - pad_y; |
| 1469 | int wend = std::min(wstart + pool_size, w_in); |
| 1470 | int hend = std::min(hstart + pool_size, h_in); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1471 | wstart = std::max(wstart, 0); |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1472 | hstart = std::max(hstart, 0); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1473 | |
| 1474 | T max_val = std::numeric_limits<T>::lowest(); |
| 1475 | for(int y = hstart; y < hend; ++y) |
| 1476 | { |
| 1477 | for(int x = wstart; x < wend; ++x) |
| 1478 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1479 | T val = in[r * h_in * w_in + y * w_in + x]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1480 | if(val > max_val) |
| 1481 | { |
| 1482 | max_val = val; |
| 1483 | } |
| 1484 | } |
| 1485 | } |
| 1486 | |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1487 | out[r * h_out * w_out + h * pooled_w + w] = max_val; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1488 | } |
| 1489 | } |
| 1490 | } |
| 1491 | } |
| 1492 | else // Average pooling |
| 1493 | { |
| 1494 | for(int r = 0; r < upper_dims; ++r) |
| 1495 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1496 | for(int h = 0; h < pooled_h; ++h) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1497 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1498 | for(int w = 0; w < pooled_w; ++w) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1499 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1500 | T avg_val = 0; |
| 1501 | int wstart = w * pool_stride_x - pad_x; |
| 1502 | int hstart = h * pool_stride_y - pad_y; |
| 1503 | int wend = std::min(wstart + pool_size, w_in + pad_x); |
| 1504 | int hend = std::min(hstart + pool_size, h_in + pad_y); |
| 1505 | int pool = (hend - hstart) * (wend - wstart); |
| 1506 | wstart = std::max(wstart, 0); |
| 1507 | hstart = std::max(hstart, 0); |
| 1508 | wend = std::min(wend, w_in); |
| 1509 | hend = std::min(hend, h_in); |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 1510 | if(is_floating_point<T>::value) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1511 | { |
| 1512 | for(int y = hstart; y < hend; ++y) |
| 1513 | { |
| 1514 | for(int x = wstart; x < wend; ++x) |
| 1515 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1516 | avg_val += in[r * h_in * w_in + y * w_in + x]; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1517 | } |
| 1518 | } |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1519 | out[r * h_out * w_out + h * pooled_w + w] = avg_val / pool; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1520 | } |
| 1521 | else |
| 1522 | { |
| 1523 | static std::array<qint8_t, 10> scale_values_q8 = |
| 1524 | { { 0x0, 0x0, 0x40, 0x2A, 0x20, 0x19, 0x15, 0x12, 0x10, 0xE } }; |
| 1525 | |
| 1526 | for(int y = hstart; y < hend; ++y) |
| 1527 | { |
| 1528 | for(int x = wstart; x < wend; ++x) |
| 1529 | { |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1530 | avg_val = sqadd_qs8(avg_val, in[r * h_in * w_in + y * w_in + x]); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1531 | } |
| 1532 | } |
Georgios Pinitas | ce09314 | 2017-06-19 16:11:53 +0100 | [diff] [blame] | 1533 | out[r * h_out * w_out + h * pooled_w + w] = sqmul_qs8(avg_val, (scale_values_q8[pool] >> (7 - fixed_point_position)), fixed_point_position); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1534 | } |
| 1535 | } |
| 1536 | } |
| 1537 | } |
| 1538 | } |
| 1539 | } |
| 1540 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1541 | // Pooling layer |
| 1542 | template <typename T> |
| 1543 | void roi_pooling_layer(const Tensor<T> &in, Tensor<T> &out, const std::vector<ROI> &rois, const ROIPoolingLayerInfo &pool_info) |
| 1544 | { |
| 1545 | const int num_rois = rois.size(); |
| 1546 | const int width_in = in.shape().x(); |
| 1547 | const int height_in = in.shape().y(); |
| 1548 | const int fms = in.shape().z(); |
| 1549 | const int volume_in = width_in * height_in * fms; |
| 1550 | const int pool_w = pool_info.pooled_width(); |
| 1551 | const int pool_h = pool_info.pooled_height(); |
| 1552 | const int volume_out = pool_w * pool_h * fms; |
| 1553 | const float roi_scale = pool_info.spatial_scale(); |
| 1554 | |
| 1555 | // Iterate through all rois |
| 1556 | for(int roi_idx = 0; roi_idx < num_rois; ++roi_idx) |
| 1557 | { |
| 1558 | // Get dimensions of current ROI |
| 1559 | const ROI &roi = rois[roi_idx]; |
| 1560 | |
| 1561 | int batch_id = roi.batch_idx; |
| 1562 | int roi_start_x = support::cpp11::round(roi.rect.x * roi_scale); |
| 1563 | int roi_start_y = support::cpp11::round(roi.rect.y * roi_scale); |
| 1564 | int roi_width = std::max(support::cpp11::round(roi.rect.width * roi_scale), 1.f); |
| 1565 | int roi_height = std::max(support::cpp11::round(roi.rect.height * roi_scale), 1.f); |
| 1566 | |
| 1567 | // Determine pooling regions |
| 1568 | float pool_region_size_x = static_cast<float>(roi_width) / pool_w; |
| 1569 | float pool_region_size_y = static_cast<float>(roi_height) / pool_h; |
| 1570 | |
| 1571 | // Iterate through all channel |
| 1572 | for(int fm = 0; fm < fms; ++fm) |
| 1573 | { |
| 1574 | // Calculate each output pixel |
| 1575 | for(int py = 0; py < pool_h; ++py) |
| 1576 | { |
| 1577 | for(int px = 0; px < pool_w; ++px) |
| 1578 | { |
| 1579 | int region_start_x = static_cast<int>(std::floor(px * pool_region_size_x)); |
| 1580 | int region_end_x = static_cast<int>(std::ceil((px + 1) * pool_region_size_x)); |
| 1581 | int region_start_y = static_cast<int>(std::floor(py * pool_region_size_y)); |
| 1582 | int region_end_y = static_cast<int>(std::ceil((py + 1) * pool_region_size_y)); |
| 1583 | |
| 1584 | region_start_x = std::min(std::max(region_start_x + roi_start_x, 0), width_in); |
| 1585 | region_end_x = std::min(std::max(region_end_x + roi_start_x, 0), width_in); |
| 1586 | region_start_y = std::min(std::max(region_start_y + roi_start_y, 0), height_in); |
| 1587 | region_end_y = std::min(std::max(region_end_y + roi_start_y, 0), height_in); |
| 1588 | |
| 1589 | // Iterate through each pixel in the pooling region |
| 1590 | if((region_end_x <= region_start_x) || (region_end_y <= region_start_y)) |
| 1591 | { |
| 1592 | out[roi_idx * volume_out + fm * pool_w * pool_h + py * pool_w + px] = 0; |
| 1593 | } |
| 1594 | else |
| 1595 | { |
| 1596 | T curr_max = std::numeric_limits<T>::lowest(); |
| 1597 | for(int j = region_start_y; j < region_end_y; ++j) |
| 1598 | { |
| 1599 | for(int i = region_start_x; i < region_end_x; ++i) |
| 1600 | { |
| 1601 | const auto val = in[batch_id * volume_in + fm * width_in * height_in + j * width_in + i]; |
| 1602 | curr_max = std::max(val, curr_max); |
| 1603 | } |
| 1604 | } |
| 1605 | out[roi_idx * volume_out + fm * pool_w * pool_h + py * pool_w + px] = curr_max; |
| 1606 | } |
| 1607 | } |
| 1608 | } |
| 1609 | } |
| 1610 | } |
| 1611 | } |
| 1612 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1613 | // Softmax Layer |
Pablo Tello | 383deec | 2017-06-23 10:40:05 +0100 | [diff] [blame] | 1614 | template <typename T, typename std::enable_if<is_floating_point<T>::value, int>::type * = nullptr> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1615 | void softmax_layer(const Tensor<T> &in, Tensor<T> &out) |
| 1616 | { |
| 1617 | const int cols = static_cast<int>(in.shape()[0]); |
| 1618 | const int upper_dims = in.shape().total_size() / cols; |
| 1619 | for(int r = 0; r < upper_dims; ++r) |
| 1620 | { |
| 1621 | // Find max |
| 1622 | T max = std::numeric_limits<T>::lowest(); |
| 1623 | for(int c = 0; c < cols; ++c) |
| 1624 | { |
| 1625 | const T x = in[r * cols + c]; |
| 1626 | if(x > max) |
| 1627 | { |
| 1628 | max = x; |
| 1629 | } |
| 1630 | } |
| 1631 | |
| 1632 | // Regularize |
| 1633 | T sum = 0; |
| 1634 | for(int c = 0; c < cols; ++c) |
| 1635 | { |
| 1636 | const T res = exp(in[r * cols + c] - max); |
| 1637 | out[r * cols + c] = res; |
| 1638 | sum += res; |
| 1639 | } |
| 1640 | |
| 1641 | // Normalize |
| 1642 | const T norm_val = 1 / sum; |
| 1643 | for(int c = 0; c < cols; ++c) |
| 1644 | { |
| 1645 | out[r * cols + c] *= norm_val; |
| 1646 | } |
| 1647 | } |
| 1648 | } |
| 1649 | template <typename T, typename std::enable_if<std::is_integral<T>::value, int>::type * = nullptr> |
| 1650 | void softmax_layer(const Tensor<T> &in, Tensor<T> &out) |
| 1651 | { |
| 1652 | using namespace fixed_point_arithmetic; |
| 1653 | using promoted_T = typename test::traits::promote<T>::type; |
| 1654 | |
| 1655 | const int fixed_point_position = in.fixed_point_position(); |
| 1656 | const int cols = static_cast<int>(in.shape()[0]); |
| 1657 | const int upper_dims = in.shape().total_size() / cols; |
| 1658 | |
| 1659 | for(int r = 0; r < upper_dims; ++r) |
| 1660 | { |
| 1661 | // Find max |
| 1662 | fixed_point<T> max(std::numeric_limits<T>::lowest(), fixed_point_position, true); |
| 1663 | for(int c = 0; c < cols; ++c) |
| 1664 | { |
| 1665 | const fixed_point<T> x(in[r * cols + c], fixed_point_position, true); |
| 1666 | if(x > max) |
| 1667 | { |
| 1668 | max = x; |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | // Regularize |
| 1673 | fixed_point<promoted_T> sum(0, fixed_point_position); |
| 1674 | for(int c = 0; c < cols; ++c) |
| 1675 | { |
| 1676 | const fixed_point<T> x(in[r * cols + c], fixed_point_position, true); |
| 1677 | fixed_point<T> res = exp(x - max); |
| 1678 | out[r * cols + c] = res.raw(); |
| 1679 | sum = add(sum, static_cast<fixed_point<promoted_T>>(res)); |
| 1680 | } |
| 1681 | |
| 1682 | // Normalize |
| 1683 | fixed_point<T> sat_sum(sum); |
| 1684 | for(int c = 0; c < cols; ++c) |
| 1685 | { |
| 1686 | const fixed_point<T> x(out[r * cols + c], fixed_point_position, true); |
| 1687 | out[r * cols + c] = div(x, sat_sum).raw(); |
| 1688 | } |
| 1689 | } |
| 1690 | } |
| 1691 | |
| 1692 | // Fixed point operations |
| 1693 | template <typename T> |
| 1694 | void fixed_point_operation(const Tensor<T> &in, Tensor<T> &out, FixedPointOp op) |
| 1695 | { |
| 1696 | int p = in.fixed_point_position(); |
| 1697 | switch(op) |
| 1698 | { |
| 1699 | case FixedPointOp::EXP: |
| 1700 | for(int i = 0; i < in.num_elements(); ++i) |
| 1701 | { |
| 1702 | out[i] = fixed_point_arithmetic::exp(fixed_point_arithmetic::fixed_point<T>(in[i], p, true)).raw(); |
| 1703 | } |
| 1704 | break; |
| 1705 | case FixedPointOp::LOG: |
| 1706 | for(int i = 0; i < in.num_elements(); ++i) |
| 1707 | { |
| 1708 | out[i] = fixed_point_arithmetic::log(fixed_point_arithmetic::fixed_point<T>(in[i], p, true)).raw(); |
| 1709 | } |
| 1710 | break; |
| 1711 | case FixedPointOp::INV_SQRT: |
| 1712 | for(int i = 0; i < in.num_elements(); ++i) |
| 1713 | { |
| 1714 | out[i] = fixed_point_arithmetic::inv_sqrt(fixed_point_arithmetic::fixed_point<T>(in[i], p, true)).raw(); |
| 1715 | } |
| 1716 | break; |
| 1717 | case FixedPointOp::RECIPROCAL: |
| 1718 | for(int i = 0; i < in.num_elements(); ++i) |
| 1719 | { |
| 1720 | out[i] = fixed_point_arithmetic::div(fixed_point_arithmetic::fixed_point<T>(1, p), fixed_point_arithmetic::fixed_point<T>(in[i], p, true)).raw(); |
| 1721 | } |
| 1722 | break; |
| 1723 | default: |
| 1724 | ARM_COMPUTE_ERROR("Fixed point operation not supported"); |
| 1725 | break; |
| 1726 | } |
| 1727 | } |
| 1728 | |
| 1729 | // Tensor print |
| 1730 | template <typename T> |
| 1731 | void print(const Tensor<T> &in, std::ostream &out) |
| 1732 | { |
| 1733 | out << "\n"; |
| 1734 | for(int i = 0; i < in.num_elements(); ++i) |
| 1735 | { |
| 1736 | out << in[i] << " "; |
| 1737 | } |
| 1738 | out << "\n"; |
| 1739 | } |
| 1740 | } // namespace tensor_operations |
| 1741 | } // namespace validation |
| 1742 | } // namespace test |
| 1743 | } // namespace arm_compute |
| 1744 | |
| 1745 | #endif /* __ARM_COMPUTE_TEST_TENSOR_OPERATIONS_H__ */ |