Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2018 ARM Limited. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: MIT |
| 5 | * |
| 6 | * Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 | * of this software and associated documentation files (the "Software"), to |
| 8 | * deal in the Software without restriction, including without limitation the |
| 9 | * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 | * sell copies of the Software, and to permit persons to whom the Software is |
| 11 | * furnished to do so, subject to the following conditions: |
| 12 | * |
| 13 | * The above copyright notice and this permission notice shall be included in all |
| 14 | * copies or substantial portions of the Software. |
| 15 | * |
| 16 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 19 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 20 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 21 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 22 | * SOFTWARE. |
| 23 | */ |
| 24 | #ifndef __ARM_COMPUTE_TYPES_H__ |
| 25 | #define __ARM_COMPUTE_TYPES_H__ |
| 26 | |
| 27 | #include "arm_compute/core/Coordinates.h" |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 28 | #include "arm_compute/core/QAsymm8.h" |
| 29 | #include "arm_compute/core/Rounding.h" |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/Size2D.h" |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 31 | #include "arm_compute/core/Strides.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 32 | #include "arm_compute/core/TensorShape.h" |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 33 | #include "support/Half.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 34 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 35 | #include <cmath> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 36 | #include <cstddef> |
| 37 | #include <cstdint> |
| 38 | #include <string> |
| 39 | #include <utility> |
| 40 | |
| 41 | namespace arm_compute |
| 42 | { |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 43 | /** 16-bit floating point type */ |
| 44 | using half = half_float::half; |
| 45 | |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 46 | /** Permutation vector */ |
| 47 | using PermutationVector = Strides; |
| 48 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 49 | /** Image colour formats */ |
| 50 | enum class Format |
| 51 | { |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 52 | UNKNOWN, /**< Unknown image format */ |
| 53 | U8, /**< 1 channel, 1 U8 per channel */ |
| 54 | S16, /**< 1 channel, 1 S16 per channel */ |
| 55 | U16, /**< 1 channel, 1 U16 per channel */ |
| 56 | S32, /**< 1 channel, 1 S32 per channel */ |
| 57 | U32, /**< 1 channel, 1 U32 per channel */ |
| 58 | F16, /**< 1 channel, 1 F16 per channel */ |
| 59 | F32, /**< 1 channel, 1 F32 per channel */ |
| 60 | UV88, /**< 2 channel, 1 U8 per channel */ |
| 61 | RGB888, /**< 3 channels, 1 U8 per channel */ |
| 62 | RGBA8888, /**< 4 channels, 1 U8 per channel */ |
| 63 | YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */ |
| 64 | YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */ |
| 65 | NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */ |
| 66 | NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */ |
| 67 | IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */ |
| 68 | UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 69 | }; |
| 70 | |
| 71 | /** Available data types */ |
| 72 | enum class DataType |
| 73 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 74 | UNKNOWN, /**< Unknown data type */ |
| 75 | U8, /**< unsigned 8-bit number */ |
| 76 | S8, /**< signed 8-bit number */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 77 | QASYMM8, /**< quantized, asymmetric fixed-point 8-bit number */ |
| 78 | U16, /**< unsigned 16-bit number */ |
| 79 | S16, /**< signed 16-bit number */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 80 | U32, /**< unsigned 32-bit number */ |
| 81 | S32, /**< signed 32-bit number */ |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 82 | U64, /**< unsigned 64-bit number */ |
| 83 | S64, /**< signed 64-bit number */ |
| 84 | F16, /**< 16-bit floating-point number */ |
| 85 | F32, /**< 32-bit floating-point number */ |
| 86 | F64, /**< 64-bit floating-point number */ |
| 87 | SIZET /**< size_t */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 88 | }; |
| 89 | |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 90 | /** Available Sampling Policies */ |
| 91 | enum class SamplingPolicy |
| 92 | { |
| 93 | CENTER, /**< Samples are taken at pixel center */ |
| 94 | TOP_LEFT /**< Samples are taken at pixel top left corner */ |
| 95 | }; |
| 96 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 97 | /** Constant value of the border pixels when using BorderMode::CONSTANT */ |
| 98 | constexpr uint8_t CONSTANT_BORDER_VALUE = 199; |
| 99 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 100 | /** Constant value used to indicate a half-scale pyramid */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 101 | constexpr float SCALE_PYRAMID_HALF = 0.5f; |
| 102 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 103 | /** Constant value used to indicate a ORB scaled pyramid */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01; |
| 105 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 106 | /** Supported tensor data layouts */ |
| 107 | enum class DataLayout |
| 108 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 109 | UNKNOWN, /**< Unknown data layout */ |
| 110 | NCHW, /**< Num samples, channels, height, width */ |
| 111 | NHWC /**< Num samples, height, width, channels */ |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 112 | }; |
| 113 | |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 114 | /** Supported tensor data layout dimensions */ |
| 115 | enum class DataLayoutDimension |
| 116 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 117 | CHANNEL, /**< channel */ |
| 118 | HEIGHT, /**< height */ |
| 119 | WIDTH, /**< width */ |
| 120 | BATCHES /**< batches */ |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 121 | }; |
| 122 | |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 123 | /** Quantization settings (used for QASYMM8 data type) */ |
| 124 | struct QuantizationInfo |
| 125 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 126 | /** Default constructor */ |
Georgios Pinitas | f8d8f3a | 2018-06-06 17:57:04 +0100 | [diff] [blame] | 127 | QuantizationInfo() noexcept |
| 128 | : scale(0.0f), |
| 129 | offset(0) |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 130 | { |
| 131 | } |
| 132 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 133 | /** Construct quantization info. |
| 134 | * |
| 135 | * @param[in] scale Scale. |
| 136 | * @param[in] offset Offset. |
| 137 | */ |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 138 | QuantizationInfo(float scale, int offset) |
| 139 | : scale(scale), offset(offset) |
| 140 | { |
| 141 | } |
| 142 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 143 | /** Check whether equal to a given quantization info. |
| 144 | * |
| 145 | * @param[in] other Other quantization info. |
| 146 | * |
| 147 | * @return True if the given quantization info is the same. |
| 148 | */ |
Daniil Efremov | eed841c | 2017-11-09 19:05:25 +0700 | [diff] [blame] | 149 | bool operator==(const QuantizationInfo &other) |
| 150 | { |
| 151 | return scale == other.scale && offset == other.offset; |
| 152 | } |
| 153 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 154 | /** Check whether not equal to a given quantization info. |
| 155 | * |
| 156 | * @param[in] other Other quantization info. |
| 157 | * |
| 158 | * @return True if the given quantization info is not the same. |
| 159 | */ |
Daniil Efremov | eed841c | 2017-11-09 19:05:25 +0700 | [diff] [blame] | 160 | bool operator!=(const QuantizationInfo &other) |
| 161 | { |
| 162 | return !(*this == other); |
| 163 | } |
| 164 | |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 165 | float scale; /**< scale */ |
| 166 | int offset; /**< offset */ |
| 167 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 168 | /** Quantizes a value using the scale/offset in this QuantizationInfo |
| 169 | * |
| 170 | * @param[in] value Value to quantize. |
| 171 | * @param[in] rounding_policy Policy to use when rounding. |
| 172 | * |
| 173 | * @return the quantized value. |
| 174 | */ |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 175 | qasymm8_t quantize(float value, RoundingPolicy rounding_policy) const |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 176 | { |
| 177 | ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::quantize: scale == 0"); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 178 | return sqcvt_qasymm8_f32(value, scale, offset, rounding_policy); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 179 | } |
| 180 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 181 | /** Dequantizes a value using the scale/offset in this QuantizationInfo |
| 182 | * |
| 183 | * @param[in] value Value to dequantize. |
| 184 | * |
| 185 | * @return the original value before quantization. |
| 186 | */ |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 187 | float dequantize(qasymm8_t value) const |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 188 | { |
| 189 | ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::dequantize: scale == 0"); |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 190 | return scvt_f32_qasymm8(value, scale, offset); |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 193 | /** Indicates whether this QuantizationInfo has valid settings or not |
| 194 | * |
| 195 | * @return True if the this has invalid settings. |
| 196 | */ |
Michel Iwaniec | 0063380 | 2017-10-12 14:14:15 +0100 | [diff] [blame] | 197 | bool empty() const |
| 198 | { |
| 199 | return scale == 0; |
| 200 | } |
| 201 | }; |
| 202 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 203 | /** Container for valid region of a window */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 204 | struct ValidRegion |
| 205 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 206 | /** Default constructor */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 207 | ValidRegion() |
| 208 | : anchor{}, shape{} |
| 209 | { |
| 210 | } |
| 211 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 212 | /** Allow instances of this class to be copy constructed */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 213 | ValidRegion(const ValidRegion &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 214 | /** Allow instances of this class to be move constructed */ |
| 215 | ValidRegion(ValidRegion &&) = default; |
| 216 | /** Allow instances of this class to be copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 217 | ValidRegion &operator=(const ValidRegion &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 218 | /** Allow instances of this class to be moved */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | ValidRegion &operator=(ValidRegion &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 220 | /** Default destructor */ |
| 221 | ~ValidRegion() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 222 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 223 | /** Constructor for a valid region with default number of dimensions |
| 224 | * |
| 225 | * @param[in] an_anchor Anchor for the start of the valid region. |
| 226 | * @param[in] a_shape Shape of the valid region. |
| 227 | * |
| 228 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 229 | ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape) |
| 230 | : anchor{ an_anchor }, shape{ a_shape } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 231 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 232 | anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions())); |
| 233 | } |
| 234 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 235 | /** Constructor for a valid region with specified number of dimensions |
| 236 | * |
| 237 | * @param[in] an_anchor Anchor for the start of the valid region. |
| 238 | * @param[in] a_shape Shape of the valid region. |
| 239 | * @param[in] num_dimensions Number of dimensions (must be >= number of dimensions of anchor and shape). |
| 240 | * |
| 241 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 242 | ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions) |
| 243 | : anchor{ an_anchor }, shape{ a_shape } |
| 244 | { |
| 245 | ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions())); |
| 246 | anchor.set_num_dimensions(num_dimensions); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** Return the start of the valid region for the given dimension @p d */ |
| 250 | int start(unsigned int d) const |
| 251 | { |
| 252 | return anchor[d]; |
| 253 | } |
| 254 | |
| 255 | /** Return the end of the valid region for the given dimension @p d */ |
| 256 | int end(unsigned int d) const |
| 257 | { |
| 258 | return anchor[d] + shape[d]; |
| 259 | } |
| 260 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 261 | /** Accessor to set the value of anchor and shape for one of the dimensions. |
| 262 | * |
| 263 | * @param[in] dimension Dimension for which the value is set. |
| 264 | * @param[in] start Value to be set in anchor for the dimension. |
| 265 | * @param[in] size Value to be set in shape for the dimension. |
| 266 | * |
| 267 | * @return *this. |
| 268 | */ |
| 269 | ValidRegion &set(size_t dimension, int start, size_t size) |
| 270 | { |
| 271 | anchor.set(dimension, start); |
| 272 | shape.set(dimension, size); |
| 273 | return *this; |
| 274 | } |
| 275 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 276 | Coordinates anchor; /**< Anchor for the start of the valid region. */ |
| 277 | TensorShape shape; /**< Shape of the valid region. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 278 | }; |
| 279 | |
| 280 | /** Methods available to handle borders */ |
| 281 | enum class BorderMode |
| 282 | { |
| 283 | UNDEFINED, /**< Borders are left undefined */ |
| 284 | CONSTANT, /**< Pixels outside the image are assumed to have a constant value */ |
| 285 | REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */ |
| 286 | }; |
| 287 | |
| 288 | /** Container for 2D border size */ |
| 289 | struct BorderSize |
| 290 | { |
| 291 | /** Empty border, i.e. no border */ |
| 292 | constexpr BorderSize() |
| 293 | : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 } |
| 294 | { |
| 295 | } |
| 296 | |
| 297 | /** Border with equal size around the 2D plane */ |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 298 | explicit constexpr BorderSize(unsigned int size) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 299 | : top{ size }, right{ size }, bottom{ size }, left{ size } |
| 300 | { |
| 301 | } |
| 302 | |
| 303 | /** Border with same size for top/bottom and left/right */ |
| 304 | constexpr BorderSize(unsigned int top_bottom, unsigned int left_right) |
| 305 | : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right } |
| 306 | { |
| 307 | } |
| 308 | |
| 309 | /** Border with different sizes */ |
| 310 | constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left) |
| 311 | : top{ top }, right{ right }, bottom{ bottom }, left{ left } |
| 312 | { |
| 313 | } |
| 314 | |
| 315 | /** Check if the entire border is zero */ |
| 316 | constexpr bool empty() const |
| 317 | { |
| 318 | return top == 0 && right == 0 && bottom == 0 && left == 0; |
| 319 | } |
| 320 | |
| 321 | /** Check if the border is the same size on all sides */ |
| 322 | constexpr bool uniform() const |
| 323 | { |
| 324 | return top == right && top == bottom && top == left; |
| 325 | } |
| 326 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 327 | /** Scale this border size. |
| 328 | * |
| 329 | * @param[in] scale Scale to multiply border size by. |
| 330 | * |
| 331 | * @return *this. |
| 332 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | BorderSize &operator*=(float scale) |
| 334 | { |
| 335 | top *= scale; |
| 336 | right *= scale; |
| 337 | bottom *= scale; |
| 338 | left *= scale; |
| 339 | |
| 340 | return *this; |
| 341 | } |
| 342 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 343 | /** Scale a copy of this border size. |
| 344 | * |
| 345 | * @param[in] scale Scale to multiply border size by. |
| 346 | * |
| 347 | * @return a scaled copy of this. |
| 348 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 349 | BorderSize operator*(float scale) |
| 350 | { |
| 351 | BorderSize size = *this; |
| 352 | size *= scale; |
| 353 | |
| 354 | return size; |
| 355 | } |
| 356 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 357 | /** Limit this border size. |
| 358 | * |
| 359 | * @param[in] limit Border size to limit this border size to. |
| 360 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 361 | void limit(const BorderSize &limit) |
| 362 | { |
| 363 | top = std::min(top, limit.top); |
| 364 | right = std::min(right, limit.right); |
| 365 | bottom = std::min(bottom, limit.bottom); |
| 366 | left = std::min(left, limit.left); |
| 367 | } |
| 368 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 369 | unsigned int top; /**< top of the border */ |
| 370 | unsigned int right; /**< right of the border */ |
| 371 | unsigned int bottom; /**< bottom of the border */ |
| 372 | unsigned int left; /**< left of the border */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 373 | }; |
| 374 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 375 | /** Container for 2D padding size */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 376 | using PaddingSize = BorderSize; |
| 377 | |
| 378 | /** Policy to handle overflow */ |
| 379 | enum class ConvertPolicy |
| 380 | { |
| 381 | WRAP, /**< Wrap around */ |
| 382 | SATURATE /**< Saturate */ |
| 383 | }; |
| 384 | |
| 385 | /** Interpolation method */ |
| 386 | enum class InterpolationPolicy |
| 387 | { |
| 388 | NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */ |
| 389 | BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */ |
| 390 | AREA, /**< Output values are determined by averaging the source pixels whose areas fall under the area of the destination pixel, projected onto the source image */ |
| 391 | }; |
| 392 | |
| 393 | /** Bilinear Interpolation method used by LKTracker */ |
| 394 | enum class BilinearInterpolation |
| 395 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 396 | BILINEAR_OLD_NEW, /**< Old-new method */ |
| 397 | BILINEAR_SCHARR /**< Scharr method */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 398 | }; |
| 399 | |
| 400 | /** Threshold mode */ |
| 401 | enum class ThresholdType |
| 402 | { |
| 403 | BINARY, /**< Threshold with one value */ |
| 404 | RANGE /**< Threshold with two values*/ |
| 405 | }; |
| 406 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 407 | /** Termination criteria */ |
| 408 | enum class Termination |
| 409 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 410 | TERM_CRITERIA_EPSILON, /**< Terminate when within epsilon of a threshold */ |
| 411 | TERM_CRITERIA_ITERATIONS, /**< Terminate after a maximum number of iterations */ |
| 412 | TERM_CRITERIA_BOTH /**< Terminate on whichever of the other conditions occurs first */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 413 | }; |
| 414 | |
| 415 | /** Magnitude calculation type. */ |
| 416 | enum class MagnitudeType |
| 417 | { |
| 418 | L1NORM, /**< L1 normalization type */ |
| 419 | L2NORM /**< L2 normalization type */ |
| 420 | }; |
| 421 | |
| 422 | /** Phase calculation type. |
| 423 | * |
| 424 | * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180 |
| 425 | */ |
| 426 | enum class PhaseType |
| 427 | { |
| 428 | SIGNED, /**< Angle range: [0, 360] */ |
| 429 | UNSIGNED /**< Angle range: [0, 180] */ |
| 430 | }; |
| 431 | |
| 432 | /** Keypoint type */ |
| 433 | struct KeyPoint |
| 434 | { |
| 435 | int32_t x{ 0 }; /**< X coordinates */ |
| 436 | int32_t y{ 0 }; /**< Y coordinates */ |
| 437 | float strength{ 0.f }; /**< Strength of the point */ |
| 438 | float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */ |
| 439 | float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */ |
| 440 | int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */ |
| 441 | float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */ |
| 442 | }; |
| 443 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 444 | /** Internal key point */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 445 | using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */ |
| 446 | |
| 447 | /** Rectangle type */ |
| 448 | struct Rectangle |
| 449 | { |
| 450 | uint16_t x; /**< Top-left x coordinate */ |
| 451 | uint16_t y; /**< Top-left y coordinate */ |
| 452 | uint16_t width; /**< Width of the rectangle */ |
| 453 | uint16_t height; /**< Height of the rectangle */ |
| 454 | }; |
| 455 | |
| 456 | /** Coordinate type */ |
| 457 | struct Coordinates2D |
| 458 | { |
| 459 | int32_t x; /**< X coordinates */ |
| 460 | int32_t y; /**< Y coordinates */ |
| 461 | }; |
| 462 | |
| 463 | /** Coordinate type */ |
| 464 | struct Coordinates3D |
| 465 | { |
| 466 | uint32_t x; /**< X coordinates */ |
| 467 | uint32_t y; /**< Y coordinates */ |
| 468 | uint32_t z; /**< Z coordinates */ |
| 469 | }; |
| 470 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 471 | /** Region of interest */ |
| 472 | struct ROI |
| 473 | { |
| 474 | Rectangle rect; /**< Rectangle specifying the region of interest */ |
| 475 | uint16_t batch_idx; /**< The batch index of the region of interest */ |
| 476 | }; |
| 477 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 478 | /** Available channels */ |
| 479 | enum class Channel |
| 480 | { |
| 481 | UNKNOWN, /** Unknown channel format */ |
| 482 | C0, /**< First channel (used by formats with unknown channel types). */ |
| 483 | C1, /**< Second channel (used by formats with unknown channel types). */ |
| 484 | C2, /**< Third channel (used by formats with unknown channel types). */ |
| 485 | C3, /**< Fourth channel (used by formats with unknown channel types). */ |
| 486 | R, /**< Red channel. */ |
| 487 | G, /**< Green channel. */ |
| 488 | B, /**< Blue channel. */ |
| 489 | A, /**< Alpha channel. */ |
| 490 | Y, /**< Luma channel. */ |
| 491 | U, /**< Cb/U channel. */ |
| 492 | V /**< Cr/V/Value channel. */ |
| 493 | }; |
| 494 | |
| 495 | /** Available matrix patterns */ |
| 496 | enum class MatrixPattern |
| 497 | { |
| 498 | BOX, /**< Box pattern matrix. */ |
| 499 | CROSS, /**< Cross pattern matrix. */ |
| 500 | DISK, /**< Disk pattern matrix. */ |
| 501 | OTHER /**< Any other matrix pattern. */ |
| 502 | }; |
| 503 | |
| 504 | /** Available non linear functions. */ |
| 505 | enum class NonLinearFilterFunction : unsigned |
| 506 | { |
| 507 | MEDIAN = 0, /**< Non linear median filter. */ |
| 508 | MIN = 1, /**< Non linear erode. */ |
| 509 | MAX = 2, /**< Non linear dilate. */ |
| 510 | }; |
| 511 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 512 | /** Available reduction operations */ |
| 513 | enum class ReductionOperation |
| 514 | { |
| 515 | SUM_SQUARE, /**< Sum of squares */ |
Michalis Spyrou | 04f089c | 2017-08-08 17:42:38 +0100 | [diff] [blame] | 516 | SUM, /**< Sum */ |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 517 | }; |
| 518 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 519 | /** The normalization type used for the normalization layer */ |
| 520 | enum class NormType |
| 521 | { |
| 522 | IN_MAP_1D, /**< Normalization applied within the same map in 1D region */ |
| 523 | IN_MAP_2D, /**< Normalization applied within the same map in 2D region */ |
| 524 | CROSS_MAP /**< Normalization applied cross maps */ |
| 525 | }; |
| 526 | |
| 527 | /** Normalization type for Histogram of Oriented Gradients (HOG) */ |
| 528 | enum class HOGNormType |
| 529 | { |
| 530 | L2_NORM = 1, /**< L2-norm */ |
| 531 | L2HYS_NORM = 2, /**< L2-norm followed by clipping */ |
| 532 | L1_NORM = 3 /**< L1 norm */ |
| 533 | }; |
| 534 | |
| 535 | /** Detection window used for the object detection. The detection window keeps the following information: |
| 536 | * |
| 537 | * -# Geometry of the rectangular window (x/y of top-left corner and width/height) |
| 538 | * -# Index of the class used for evaluating which class the detection window belongs to |
| 539 | * -# Confidence value (score) obtained with the classifier |
| 540 | */ |
| 541 | struct DetectionWindow |
| 542 | { |
| 543 | uint16_t x{ 0 }; /**< Top-left x coordinate */ |
| 544 | uint16_t y{ 0 }; /**< Top-left y coordinate */ |
| 545 | uint16_t width{ 0 }; /**< Width of the detection window */ |
| 546 | uint16_t height{ 0 }; /**< Height of the detection window */ |
| 547 | uint16_t idx_class{ 0 }; /**< Index of the class */ |
| 548 | float score{ 0.f }; /**< Confidence value for the detection window */ |
| 549 | }; |
| 550 | |
| 551 | /** Dimension rounding type when down-scaling on CNNs |
| 552 | * @note Used in pooling and convolution layer |
| 553 | */ |
| 554 | enum class DimensionRoundingType |
| 555 | { |
| 556 | FLOOR, /**< Floor rounding */ |
| 557 | CEIL /**< Ceil rounding */ |
| 558 | }; |
| 559 | |
| 560 | /** Available pooling types */ |
| 561 | enum class PoolingType |
| 562 | { |
| 563 | MAX, /**< Max Pooling */ |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 564 | AVG, /**< Average Pooling */ |
| 565 | L2 /**< L2 Pooling */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 566 | }; |
| 567 | |
| 568 | /** Padding and stride information class */ |
| 569 | class PadStrideInfo |
| 570 | { |
| 571 | public: |
| 572 | /** Constructor |
| 573 | * |
| 574 | * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1. |
| 575 | * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1. |
| 576 | * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0. |
| 577 | * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0. |
| 578 | * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR. |
| 579 | */ |
| 580 | PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1, |
| 581 | unsigned int pad_x = 0, unsigned int pad_y = 0, |
| 582 | DimensionRoundingType round = DimensionRoundingType::FLOOR) |
| 583 | : _stride(std::make_pair(stride_x, stride_y)), |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 584 | _pad_left(pad_x), |
| 585 | _pad_top(pad_y), |
| 586 | _pad_right(pad_x), |
| 587 | _pad_bottom(pad_y), |
| 588 | _round_type(round) |
| 589 | { |
| 590 | } |
| 591 | /** Constructor |
| 592 | * |
| 593 | * @param[in] stride_x Stride, in elements, across x. |
| 594 | * @param[in] stride_y Stride, in elements, across y. |
| 595 | * @param[in] pad_left Padding across x on the left, in elements. |
| 596 | * @param[in] pad_top Padding across y on the top, in elements. |
| 597 | * @param[in] pad_right Padding across x on the right, in elements. |
| 598 | * @param[in] pad_bottom Padding across y on the bottom, in elements. |
| 599 | * @param[in] round Dimensions rounding. |
| 600 | */ |
| 601 | PadStrideInfo(unsigned int stride_x, unsigned int stride_y, |
| 602 | unsigned int pad_left, unsigned int pad_right, |
| 603 | unsigned int pad_top, unsigned int pad_bottom, |
| 604 | DimensionRoundingType round) |
| 605 | : _stride(std::make_pair(stride_x, stride_y)), |
| 606 | _pad_left(pad_left), |
| 607 | _pad_top(pad_top), |
| 608 | _pad_right(pad_right), |
| 609 | _pad_bottom(pad_bottom), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 610 | _round_type(round) |
| 611 | { |
| 612 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 613 | /** Get the stride. |
| 614 | * |
| 615 | * @return a pair: stride x, stride y. |
| 616 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 617 | std::pair<unsigned int, unsigned int> stride() const |
| 618 | { |
| 619 | return _stride; |
| 620 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 621 | /** Check whether the padding is symmetric. |
| 622 | * |
| 623 | * @return True if the padding is symmetric. |
| 624 | */ |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 625 | bool padding_is_symmetric() const |
| 626 | { |
| 627 | return (_pad_left == _pad_right) && (_pad_top == _pad_bottom); |
| 628 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 629 | /** Get the padding. |
| 630 | * |
| 631 | * @note This should only be used when the padding is symmetric. |
| 632 | * |
| 633 | * @return a pair: padding left/right, padding top/bottom |
| 634 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 635 | std::pair<unsigned int, unsigned int> pad() const |
| 636 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 637 | //this accessor should be used only when padding is symmetric |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 638 | ARM_COMPUTE_ERROR_ON(!padding_is_symmetric()); |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 639 | return std::make_pair(_pad_left, _pad_top); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 640 | } |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 641 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 642 | /** Get the left padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 643 | unsigned int pad_left() const |
| 644 | { |
| 645 | return _pad_left; |
| 646 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 647 | /** Get the right padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 648 | unsigned int pad_right() const |
| 649 | { |
| 650 | return _pad_right; |
| 651 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 652 | /** Get the top padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 653 | unsigned int pad_top() const |
| 654 | { |
| 655 | return _pad_top; |
| 656 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 657 | /** Get the bottom padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 658 | unsigned int pad_bottom() const |
| 659 | { |
| 660 | return _pad_bottom; |
| 661 | } |
| 662 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 663 | /** Get the rounding type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 664 | DimensionRoundingType round() const |
| 665 | { |
| 666 | return _round_type; |
| 667 | } |
| 668 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 669 | /** Check whether this has any padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 670 | bool has_padding() const |
| 671 | { |
| 672 | return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0); |
| 673 | } |
| 674 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 675 | private: |
| 676 | std::pair<unsigned int, unsigned int> _stride; |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 677 | unsigned int _pad_left; |
| 678 | unsigned int _pad_top; |
| 679 | unsigned int _pad_right; |
| 680 | unsigned int _pad_bottom; |
| 681 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 682 | DimensionRoundingType _round_type; |
| 683 | }; |
| 684 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 685 | /** Fully connected layer info */ |
| 686 | struct FullyConnectedLayerInfo |
| 687 | { |
| 688 | DataLayout weights_trained_layout{ DataLayout::NCHW }; /**< Layout that the weights have been trained with. */ |
| 689 | bool transpose_weights{ true }; /**< Transpose weights if true. */ |
| 690 | bool are_weights_reshaped{ false }; /**< Reshape the weights tensor if false. */ |
| 691 | bool retain_internal_weights{ false }; /**< Retain internal reshaped weights. */ |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 692 | |
| 693 | /** Sets the weights trained data layout |
| 694 | * |
| 695 | * @param[in] layout Data layout that the weights were trained with |
| 696 | * |
| 697 | * @return Updated object |
| 698 | */ |
| 699 | FullyConnectedLayerInfo &set_weights_trained_layout(DataLayout layout) |
| 700 | { |
| 701 | weights_trained_layout = layout; |
| 702 | return *this; |
| 703 | } |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 704 | }; |
| 705 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 706 | /** Pooling Layer Information class */ |
| 707 | class PoolingLayerInfo |
| 708 | { |
| 709 | public: |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 710 | /** Default Constructor */ |
| 711 | PoolingLayerInfo() |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 712 | : _pool_type(PoolingType::MAX), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo()), _exclude_padding(false), _is_global_pooling(false) |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 713 | { |
| 714 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 715 | /** Default Constructor |
| 716 | * |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 717 | * @param[in] pool_type Pooling type @ref PoolingType. |
| 718 | * @param[in] pool_size Pooling size, in elements, across x and y. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 719 | * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 720 | * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations. |
| 721 | * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area). |
| 722 | * Defaults to false; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 723 | */ |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 724 | explicit PoolingLayerInfo(PoolingType pool_type, |
| 725 | unsigned int pool_size, |
| 726 | PadStrideInfo pad_stride_info = PadStrideInfo(), |
| 727 | bool exclude_padding = false) |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 728 | : _pool_type(pool_type), _pool_size(Size2D(pool_size, pool_size)), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false) |
| 729 | { |
| 730 | } |
| 731 | /** Default Constructor |
| 732 | * |
| 733 | * @param[in] pool_type Pooling type @ref PoolingType. |
| 734 | * @param[in] pool_size Pooling size, in elements, across x and y. |
| 735 | * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo |
| 736 | * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations. |
| 737 | * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area). |
| 738 | * Defaults to false; |
| 739 | */ |
| 740 | explicit PoolingLayerInfo(PoolingType pool_type, |
| 741 | Size2D pool_size, |
| 742 | PadStrideInfo pad_stride_info = PadStrideInfo(), |
| 743 | bool exclude_padding = false) |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 744 | : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false) |
| 745 | { |
| 746 | } |
| 747 | /** Default Constructor |
| 748 | * |
| 749 | * @note This constructor is used for global pooling |
| 750 | * |
| 751 | * @param[in] pool_type Pooling type @ref PoolingType. |
| 752 | */ |
| 753 | explicit PoolingLayerInfo(PoolingType pool_type) |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 754 | : _pool_type(pool_type), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo(1, 1, 0, 0)), _exclude_padding(false), _is_global_pooling(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 755 | { |
| 756 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 757 | /** Get the pooling type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 758 | PoolingType pool_type() const |
| 759 | { |
| 760 | return _pool_type; |
| 761 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 762 | /** Get the pooling size */ |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 763 | const Size2D &pool_size() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 764 | { |
| 765 | return _pool_size; |
| 766 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 767 | /** Get the padding and stride */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 768 | PadStrideInfo pad_stride_info() const |
| 769 | { |
| 770 | return _pad_stride_info; |
| 771 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 772 | /** Check if padding is excluded in calculations */ |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 773 | bool exclude_padding() const |
| 774 | { |
| 775 | return _exclude_padding; |
| 776 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 777 | /** Check if is global pooling */ |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 778 | bool is_global_pooling() const |
| 779 | { |
| 780 | return _is_global_pooling; |
| 781 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 782 | |
| 783 | private: |
| 784 | PoolingType _pool_type; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 785 | Size2D _pool_size; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 786 | PadStrideInfo _pad_stride_info; |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 787 | bool _exclude_padding; |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 788 | bool _is_global_pooling; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 789 | }; |
| 790 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 791 | /** ROI Pooling Layer Information class */ |
| 792 | class ROIPoolingLayerInfo |
| 793 | { |
| 794 | public: |
| 795 | /** Default Constructor |
| 796 | * |
| 797 | * @param[in] pooled_width Pooled width of the layer. |
| 798 | * @param[in] pooled_height Pooled height of the layer. |
| 799 | * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions. |
| 800 | */ |
| 801 | ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale) |
| 802 | : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale) |
| 803 | { |
| 804 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 805 | /** Get the pooled width of the layer */ |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 806 | unsigned int pooled_width() const |
| 807 | { |
| 808 | return _pooled_width; |
| 809 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 810 | /** Get the pooled height of the layer */ |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 811 | unsigned int pooled_height() const |
| 812 | { |
| 813 | return _pooled_height; |
| 814 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 815 | /** Get the spatial scale */ |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 816 | float spatial_scale() const |
| 817 | { |
| 818 | return _spatial_scale; |
| 819 | } |
| 820 | |
| 821 | private: |
| 822 | unsigned int _pooled_width; |
| 823 | unsigned int _pooled_height; |
| 824 | float _spatial_scale; |
| 825 | }; |
| 826 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 827 | /** Activation Layer Information class */ |
| 828 | class ActivationLayerInfo |
| 829 | { |
| 830 | public: |
| 831 | /** Available activation functions */ |
| 832 | enum class ActivationFunction |
| 833 | { |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 834 | LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */ |
| 835 | TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */ |
| 836 | RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */ |
| 837 | BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */ |
| 838 | LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */ |
| 839 | LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */ |
| 840 | SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */ |
| 841 | ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */ |
| 842 | SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/ |
| 843 | SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/ |
| 844 | LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 845 | }; |
| 846 | |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 847 | ActivationLayerInfo() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 848 | /** Default Constructor |
| 849 | * |
| 850 | * @param[in] f The activation function to use. |
| 851 | * @param[in] a (Optional) The alpha parameter used by some activation functions |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 852 | * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH). |
| 853 | * @param[in] b (Optional) The beta parameter used by some activation functions (@ref ActivationFunction::LINEAR, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::TANH). |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 854 | */ |
| 855 | ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f) |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 856 | : _act(f), _a(a), _b(b), _enabled(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 857 | { |
| 858 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 859 | /** Get the type of activation function */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 860 | ActivationFunction activation() const |
| 861 | { |
| 862 | return _act; |
| 863 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 864 | /** Get the alpha value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 865 | float a() const |
| 866 | { |
| 867 | return _a; |
| 868 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 869 | /** Get the beta value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 870 | float b() const |
| 871 | { |
| 872 | return _b; |
| 873 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 874 | /** Check if initialised */ |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 875 | bool enabled() const |
| 876 | { |
| 877 | return _enabled; |
| 878 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 879 | |
| 880 | private: |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 881 | ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::LOGISTIC }; |
| 882 | float _a = {}; |
| 883 | float _b = {}; |
| 884 | bool _enabled = { false }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 885 | }; |
| 886 | |
| 887 | /** Normalization Layer Information class */ |
| 888 | class NormalizationLayerInfo |
| 889 | { |
| 890 | public: |
| 891 | /** Default Constructor |
| 892 | * |
| 893 | * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NORM_TYPE::CROSS_MAP |
| 894 | * @param[in] norm_size The normalization size is the number of elements to normalize across. Defaults to 5. |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 895 | * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001. |
| 896 | * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5. |
| 897 | * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation. |
| 898 | * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not. |
| 899 | * Should be false to follow [Krichevksy 2012]. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 900 | */ |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 901 | NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true) |
| 902 | : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 903 | { |
| 904 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 905 | /** Get the normalization type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 906 | NormType type() const |
| 907 | { |
| 908 | return _type; |
| 909 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 910 | /** Get the normalization size */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 911 | uint32_t norm_size() const |
| 912 | { |
| 913 | return _norm_size; |
| 914 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 915 | /** Get the alpha value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 916 | float alpha() const |
| 917 | { |
| 918 | return _alpha; |
| 919 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 920 | /** Get the beta value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 921 | float beta() const |
| 922 | { |
| 923 | return _beta; |
| 924 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 925 | /** Get the kappa value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 926 | float kappa() const |
| 927 | { |
| 928 | return _kappa; |
| 929 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 930 | /** Check if normalization is cross map */ |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 931 | bool is_cross_map() const |
| 932 | { |
| 933 | return _type == NormType::CROSS_MAP; |
| 934 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 935 | /** Check if normalization is not cross map */ |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 936 | bool is_in_map() const |
| 937 | { |
| 938 | return !is_cross_map(); |
| 939 | } |
| 940 | /** Return the scaling factor of the normalization function. |
| 941 | * |
| 942 | * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed, |
| 943 | * where alpha is returned plainly, else alpha is scaled by the total number of elements used for the normalization. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 944 | * |
| 945 | * @return The normalization scaling factor. |
| 946 | */ |
| 947 | float scale_coeff() const |
| 948 | { |
| 949 | const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size; |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 950 | return (_is_scaled) ? (_alpha / size) : _alpha; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 951 | } |
| 952 | |
| 953 | private: |
| 954 | NormType _type; |
| 955 | uint32_t _norm_size; |
| 956 | float _alpha; |
| 957 | float _beta; |
| 958 | float _kappa; |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 959 | bool _is_scaled; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 960 | }; |
| 961 | |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 962 | /** Convolution Layer Weights Information class. This class stores the necessary information to compute convolution layer when the weights are already reshaped */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 963 | class WeightsInfo |
| 964 | { |
| 965 | public: |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 966 | /** Default constructor */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 967 | WeightsInfo() |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 968 | : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0), _retain_internal_weights(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 969 | { |
| 970 | } |
| 971 | /** Constructor |
| 972 | * |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 973 | * @param[in] are_reshaped True if the weights have been reshaped |
| 974 | * @param[in] kernel_width Kernel width. |
| 975 | * @param[in] kernel_height Kernel height. |
| 976 | * @param[in] num_kernels Number of convolution kernels. |
| 977 | * @param[in] retain_internal_weights (Optional) True if internal reshaped weights must be retained. Used for reconfiguration purposes. Default is false. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 978 | */ |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 979 | WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels, bool retain_internal_weights = false) |
| 980 | : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels), _retain_internal_weights(retain_internal_weights) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 981 | { |
| 982 | } |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 983 | /** Flag which specifies if the weights tensor has been reshaped. |
| 984 | * |
| 985 | * @return True if the weights tensors has been reshaped |
| 986 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 987 | bool are_reshaped() const |
| 988 | { |
| 989 | return _are_reshaped; |
| 990 | }; |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 991 | /** Return the number of convolution kernels |
| 992 | * |
| 993 | * @return The number of convolution kernels |
| 994 | */ |
| 995 | unsigned int num_kernels() const |
| 996 | { |
| 997 | return _num_kernels; |
| 998 | }; |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 999 | /** Return the width and height of the kernel |
| 1000 | * |
| 1001 | * @return The width and height of the kernel |
| 1002 | */ |
| 1003 | std::pair<unsigned int, unsigned int> kernel_size() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1004 | { |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1005 | return std::make_pair(_kernel_width, _kernel_height); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1006 | } |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1007 | bool retain_internal_weights() const |
| 1008 | { |
| 1009 | return _retain_internal_weights; |
| 1010 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1011 | |
| 1012 | private: |
| 1013 | const bool _are_reshaped; |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1014 | const unsigned int _kernel_width; |
| 1015 | const unsigned int _kernel_height; |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 1016 | const unsigned int _num_kernels; |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1017 | const bool _retain_internal_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1018 | }; |
| 1019 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1020 | /** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape. |
| 1021 | * |
| 1022 | * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel |
| 1023 | * Note: Optionally just for @ref CLGEMMInterleave4x4Kernel is it possible to set mult_interleave4x4_height, the multiplication factor for the height of the 4x4 interleaved block |
| 1024 | * |
| 1025 | * The matrix B can only be reshaped through @ref CLGEMMTranspose1xWKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel |
| 1026 | * Note: Optionally just for @ref CLGEMMTranspose1xWKernel is it possible to set mult_transpose1xW_width, the multiplication factor for the width of the 1xW transposed block |
| 1027 | * |
| 1028 | */ |
| 1029 | class GEMMReshapeInfo final |
| 1030 | { |
| 1031 | public: |
| 1032 | /** Default constructor */ |
| 1033 | GEMMReshapeInfo() |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1034 | : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1), _depth_output_gemm3d(1), _reinterpret_input_as_3d(false) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1035 | { |
| 1036 | } |
| 1037 | /** Constructor |
| 1038 | * |
| 1039 | * @param[in] m Number of matrix A rows |
| 1040 | * @param[in] n Number of matrix B columns |
| 1041 | * @param[in] k Number of matrix A columns or matrix B rows |
| 1042 | * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block |
| 1043 | * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1044 | * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1045 | * @param[in] reinterpret_input_as_3d (Optional) Reinterpret the input as 3D tensor. (i.e. this flag should be set to true when GEMM is used |
| 1046 | * to perform 1x1 convolutions with the NHWC data layout) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1047 | */ |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1048 | GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1, int depth_output_gemm3d = 1, bool reinterpret_input_as_3d = false) |
| 1049 | : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height), _depth_output_gemm3d(depth_output_gemm3d), |
| 1050 | _reinterpret_input_as_3d(reinterpret_input_as_3d) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1051 | { |
| 1052 | } |
| 1053 | /** Number of matrix A rows |
| 1054 | * |
| 1055 | * @return the number of matrix A rows |
| 1056 | */ |
| 1057 | int m() const |
| 1058 | { |
| 1059 | return _m; |
| 1060 | } |
| 1061 | /** Number of matrix B columns |
| 1062 | * |
| 1063 | * @return the number of matrix B columns |
| 1064 | */ |
| 1065 | int n() const |
| 1066 | { |
| 1067 | return _n; |
| 1068 | } |
| 1069 | /** Number of matrix A columns or matrix B rows |
| 1070 | * |
| 1071 | * @return the number of matrix A columns or matrix B rows |
| 1072 | */ |
| 1073 | int k() const |
| 1074 | { |
| 1075 | return _k; |
| 1076 | } |
| 1077 | /** Multiplication factor for the width of the 1xW transposed block |
| 1078 | * |
| 1079 | * @return the multiplication factor for the width of the 1xW transposed block |
| 1080 | */ |
| 1081 | int mult_transpose1xW_width() const |
| 1082 | { |
| 1083 | return _mult_transpose1xW_width; |
| 1084 | } |
| 1085 | /** Multiplication factor for the height of the 4x4 interleaved block |
| 1086 | * |
| 1087 | * @return the multiplication factor for the height of the 4x4 interleaved block |
| 1088 | */ |
| 1089 | int mult_interleave4x4_height() const |
| 1090 | { |
| 1091 | return _mult_interleave4x4_height; |
| 1092 | } |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1093 | /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel |
| 1094 | * |
| 1095 | * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case: |
| 1096 | * m = depth_output_gemm3d * output_height |
| 1097 | * |
| 1098 | * @return the depth of the output tensor to be used with the GEMM3D kernel |
| 1099 | */ |
| 1100 | int depth_output_gemm3d() const |
| 1101 | { |
| 1102 | return _depth_output_gemm3d; |
| 1103 | } |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1104 | /** Flag which specifies if the input tensor has to be reinterpreted as 3D |
| 1105 | * |
| 1106 | * @return True if the input tensor has to be reinterpreted as 3D tensor |
| 1107 | */ |
| 1108 | bool reinterpret_input_as_3d() const |
| 1109 | { |
| 1110 | return _reinterpret_input_as_3d; |
| 1111 | }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1112 | |
| 1113 | private: |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1114 | const int _m; |
| 1115 | const int _n; |
| 1116 | const int _k; |
| 1117 | const int _mult_transpose1xW_width; |
| 1118 | const int _mult_interleave4x4_height; |
| 1119 | const int _depth_output_gemm3d; |
| 1120 | const bool _reinterpret_input_as_3d; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1121 | }; |
| 1122 | |
| 1123 | /** GEMM information class. This class stores the necessary information to compute GEMM functions |
| 1124 | * |
| 1125 | * This object also contains the information about how matrix A and matrix B have been reshaped |
| 1126 | * |
| 1127 | */ |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1128 | class GEMMInfo |
| 1129 | { |
| 1130 | public: |
| 1131 | /** Default constructor */ |
| 1132 | GEMMInfo() |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1133 | : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(false), _depth_output_gemm3d(1), _reinterpret_input_as_3d(false) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1134 | { |
| 1135 | } |
| 1136 | /** Constructor |
| 1137 | * |
| 1138 | * @param[in] is_a_reshaped True if the matrix A has been reshaped |
| 1139 | * @param[in] is_b_reshaped True if the matrix B has been reshaped |
| 1140 | * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1141 | * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1142 | * @param[in] reinterpret_input_as_3d (Optional) Reinterpret the input as 3D tensor. (i.e. this flag should be set to true when GEMM is used |
| 1143 | * to perform 1x1 convolutions with the NHWC data layout) |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1144 | * |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1145 | */ |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1146 | GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, int depth_output_gemm3d = 1, bool reinterpret_input_as_3d = false) |
| 1147 | : _is_a_reshaped(is_a_reshaped), _is_b_reshaped(is_b_reshaped), _reshape_b_only_on_first_run(reshape_b_only_on_first_run), _depth_output_gemm3d(depth_output_gemm3d), |
| 1148 | _reinterpret_input_as_3d(reinterpret_input_as_3d) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1149 | { |
| 1150 | } |
| 1151 | /** Flag which specifies if the matrix A has been reshaped |
| 1152 | * |
| 1153 | * @return True if the matrix A has been reshaped |
| 1154 | */ |
| 1155 | bool is_a_reshaped() const |
| 1156 | { |
| 1157 | return _is_a_reshaped; |
| 1158 | }; |
| 1159 | /** Flag which specifies if the matrix B has been reshaped |
| 1160 | * |
| 1161 | * @return True if the matrix B has been reshaped |
| 1162 | */ |
| 1163 | bool is_b_reshaped() const |
| 1164 | { |
| 1165 | return _is_b_reshaped; |
| 1166 | }; |
| 1167 | /** Flag which specifies if the reshape of matrix B should executed only for the first |
| 1168 | * |
| 1169 | * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer |
| 1170 | * |
| 1171 | * @return True if the reshaped of matrix B happens only for the first run |
| 1172 | */ |
| 1173 | bool reshape_b_only_on_first_run() const |
| 1174 | { |
| 1175 | return _reshape_b_only_on_first_run; |
| 1176 | }; |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1177 | /** Depth of the output when GEMM output is reinterpreted as 3D tensor |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1178 | * |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1179 | * @return the depth of the output tensor |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1180 | */ |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1181 | int depth_output_gemm3d() const |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1182 | { |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1183 | return _depth_output_gemm3d; |
| 1184 | }; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1185 | /** Flag which specifies if the input tensor has to be reinterpreted as 3D |
| 1186 | * |
| 1187 | * @return True if the input tensor has to be reinterpreted as 3D tensor |
| 1188 | */ |
| 1189 | bool reinterpret_input_as_3d() const |
| 1190 | { |
| 1191 | return _reinterpret_input_as_3d; |
| 1192 | }; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1193 | |
| 1194 | private: |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1195 | const bool _is_a_reshaped; |
| 1196 | const bool _is_b_reshaped; |
| 1197 | const bool _reshape_b_only_on_first_run; |
| 1198 | const int _depth_output_gemm3d; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame^] | 1199 | const bool _reinterpret_input_as_3d; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1200 | }; |
| 1201 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 1202 | /** Winograd information */ |
| 1203 | struct WinogradInfo |
| 1204 | { |
| 1205 | /** Default constructor |
| 1206 | * |
| 1207 | * @param[in] output_tile_sz Width and height of the output tile |
| 1208 | * @param[in] kernel_sz Width and height of the kernel |
| 1209 | * @param[in] input_dims Width and height of the input tensor before the convolution is applied |
| 1210 | * @param[in] conv_info Convolution info (Pads, strides) |
| 1211 | * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied |
| 1212 | */ |
| 1213 | WinogradInfo(Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout) |
| 1214 | : output_tile_size(output_tile_sz), kernel_size(kernel_sz), input_dimensions(input_dims), convolution_info(conv_info), output_data_layout(data_layout) |
| 1215 | { |
| 1216 | } |
| 1217 | |
| 1218 | Size2D output_tile_size{}; /**< Width and height of the output tile */ |
| 1219 | Size2D kernel_size{}; /**< Width and height of the kernel*/ |
| 1220 | Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */ |
| 1221 | PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */ |
| 1222 | DataLayout output_data_layout{ DataLayout::NCHW }; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */ |
| 1223 | }; |
| 1224 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1225 | /** IO formatting information class*/ |
| 1226 | struct IOFormatInfo |
| 1227 | { |
| 1228 | /** Precision type used when printing floating point numbers */ |
| 1229 | enum class PrecisionType |
| 1230 | { |
| 1231 | Default, /**< Default precision to the one that the current stream has */ |
| 1232 | Custom, /**< Custom precision specified by the user using the precision parameter */ |
| 1233 | Full /**< The maximum precision of the floating point representation */ |
| 1234 | }; |
| 1235 | |
| 1236 | /** Specifies the area to be printed, used by Tensor objects */ |
| 1237 | enum class PrintRegion |
| 1238 | { |
| 1239 | ValidRegion, /**< Prints the valid region of the Tensor object */ |
| 1240 | NoPadding, /**< Prints the Tensor object without the padding */ |
| 1241 | Full /**< Print the tensor object including padding */ |
| 1242 | }; |
| 1243 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1244 | /** Construct a set of IO formatting information. |
| 1245 | * |
| 1246 | * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion. |
| 1247 | * @param[in] precision_type Precision type for floating point numbers. Default: stream default. |
| 1248 | * @param[in] precision Precision value for float point numbers. Default: 10. |
| 1249 | * @param[in] align_columns Whether to align columns when printed. Default: true. |
| 1250 | * @param[in] element_delim Delimeter between elements. Default: " ". |
| 1251 | * @param[in] row_delim Delimenter between rows. Default: "\n". |
| 1252 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1253 | IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion, |
| 1254 | PrecisionType precision_type = PrecisionType::Default, |
| 1255 | unsigned int precision = 10, |
| 1256 | bool align_columns = true, |
| 1257 | std::string element_delim = " ", |
| 1258 | std::string row_delim = "\n") |
| 1259 | : print_region(print_region), |
| 1260 | precision_type(precision_type), |
| 1261 | precision(precision), |
| 1262 | element_delim(element_delim), |
| 1263 | row_delim(row_delim), |
| 1264 | align_columns(align_columns) |
| 1265 | { |
| 1266 | } |
| 1267 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1268 | /** Area to be printed by Tensor objects */ |
| 1269 | PrintRegion print_region; |
| 1270 | /** Floating point precision type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1271 | PrecisionType precision_type; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1272 | /** Floating point precision */ |
| 1273 | unsigned int precision; |
| 1274 | /** Element delimeter */ |
| 1275 | std::string element_delim; |
| 1276 | /** Row delimeter */ |
| 1277 | std::string row_delim; |
| 1278 | /** Align columns */ |
| 1279 | bool align_columns; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1280 | }; |
Isabella Gottardi | f07d28d | 2018-02-06 14:52:43 +0000 | [diff] [blame] | 1281 | |
| 1282 | /** Available ConvolutionMethod*/ |
| 1283 | enum class ConvolutionMethod |
| 1284 | { |
| 1285 | GEMM, /**< Convolution using GEMM */ |
| 1286 | DIRECT, /**< Direct convolution */ |
| 1287 | WINOGRAD /**< Convolution using Winograd */ |
| 1288 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 1289 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1290 | #endif /* __ARM_COMPUTE_TYPES_H__ */ |