Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1 | /* |
Manuel Bottini | cc5171b | 2019-01-09 17:04:39 +0000 | [diff] [blame] | 2 | * Copyright (c) 2016-2019 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" |
Georgios Pinitas | 4c5469b | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 28 | #include "arm_compute/core/QuantizationInfo.h" |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 29 | #include "arm_compute/core/Size2D.h" |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 30 | #include "arm_compute/core/Strides.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 31 | #include "arm_compute/core/TensorShape.h" |
Georgios Pinitas | 583137c | 2017-08-31 18:12:42 +0100 | [diff] [blame] | 32 | #include "support/Half.h" |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 33 | |
Michel Iwaniec | 5dfeae6 | 2017-11-29 10:48:23 +0000 | [diff] [blame] | 34 | #include <cmath> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 35 | #include <cstddef> |
| 36 | #include <cstdint> |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 37 | #include <map> |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 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; |
Georgios Pinitas | 77589b5 | 2018-08-21 14:41:35 +0100 | [diff] [blame] | 48 | /** Bidirectional strides */ |
| 49 | using BiStrides = Coordinates; |
Georgios Pinitas | 8795ffb | 2017-12-01 16:13:40 +0000 | [diff] [blame] | 50 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 51 | /** Image colour formats */ |
| 52 | enum class Format |
| 53 | { |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 54 | UNKNOWN, /**< Unknown image format */ |
| 55 | U8, /**< 1 channel, 1 U8 per channel */ |
| 56 | S16, /**< 1 channel, 1 S16 per channel */ |
| 57 | U16, /**< 1 channel, 1 U16 per channel */ |
| 58 | S32, /**< 1 channel, 1 S32 per channel */ |
| 59 | U32, /**< 1 channel, 1 U32 per channel */ |
| 60 | F16, /**< 1 channel, 1 F16 per channel */ |
| 61 | F32, /**< 1 channel, 1 F32 per channel */ |
| 62 | UV88, /**< 2 channel, 1 U8 per channel */ |
| 63 | RGB888, /**< 3 channels, 1 U8 per channel */ |
| 64 | RGBA8888, /**< 4 channels, 1 U8 per channel */ |
| 65 | YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */ |
| 66 | YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */ |
| 67 | NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */ |
| 68 | NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */ |
| 69 | IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */ |
| 70 | 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] | 71 | }; |
| 72 | |
| 73 | /** Available data types */ |
| 74 | enum class DataType |
| 75 | { |
Michalis Spyrou | c853021 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 76 | UNKNOWN, /**< Unknown data type */ |
| 77 | U8, /**< unsigned 8-bit number */ |
| 78 | S8, /**< signed 8-bit number */ |
| 79 | QSYMM8, /**< quantized, symmetric fixed-point 8-bit number */ |
| 80 | QASYMM8, /**< quantized, asymmetric fixed-point 8-bit number */ |
| 81 | QSYMM8_PER_CHANNEL, /**< quantized, symmetric per channel fixed-point 8-bit number */ |
| 82 | QASYMM8_PER_CHANNEL, /**< quantized, asymmetric per channel fixed-point 8-bit number */ |
| 83 | U16, /**< unsigned 16-bit number */ |
| 84 | S16, /**< signed 16-bit number */ |
| 85 | QSYMM16, /**< quantized, symmetric fixed-point 16-bit number */ |
Michele Di Giorgio | 35ea9a7 | 2019-08-23 12:02:06 +0100 | [diff] [blame] | 86 | QASYMM16, /**< quantized, asymmetric fixed-point 16-bit number */ |
Michalis Spyrou | c853021 | 2019-08-22 11:44:04 +0100 | [diff] [blame] | 87 | U32, /**< unsigned 32-bit number */ |
| 88 | S32, /**< signed 32-bit number */ |
| 89 | U64, /**< unsigned 64-bit number */ |
| 90 | S64, /**< signed 64-bit number */ |
| 91 | F16, /**< 16-bit floating-point number */ |
| 92 | F32, /**< 32-bit floating-point number */ |
| 93 | F64, /**< 64-bit floating-point number */ |
| 94 | SIZET /**< size_t */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 95 | }; |
| 96 | |
Daniil Efremov | 02bf80d | 2017-11-22 00:26:51 +0700 | [diff] [blame] | 97 | /** Available Sampling Policies */ |
| 98 | enum class SamplingPolicy |
| 99 | { |
| 100 | CENTER, /**< Samples are taken at pixel center */ |
| 101 | TOP_LEFT /**< Samples are taken at pixel top left corner */ |
| 102 | }; |
| 103 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 104 | /** Constant value of the border pixels when using BorderMode::CONSTANT */ |
| 105 | constexpr uint8_t CONSTANT_BORDER_VALUE = 199; |
| 106 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 107 | /** Constant value used to indicate a half-scale pyramid */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 108 | constexpr float SCALE_PYRAMID_HALF = 0.5f; |
| 109 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 110 | /** Constant value used to indicate a ORB scaled pyramid */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 111 | constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01; |
| 112 | |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 113 | /** [DataLayout enum definition] **/ |
| 114 | |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 115 | /** Supported tensor data layouts */ |
| 116 | enum class DataLayout |
| 117 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 118 | UNKNOWN, /**< Unknown data layout */ |
| 119 | NCHW, /**< Num samples, channels, height, width */ |
| 120 | NHWC /**< Num samples, height, width, channels */ |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 121 | }; |
Vidhya Sudhan Loganathan | d646ae1 | 2018-11-19 15:18:20 +0000 | [diff] [blame] | 122 | /** [DataLayout enum definition] **/ |
Georgios Pinitas | 4074c99 | 2018-01-30 18:13:46 +0000 | [diff] [blame] | 123 | |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 124 | /** Supported tensor data layout dimensions */ |
| 125 | enum class DataLayoutDimension |
| 126 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 127 | CHANNEL, /**< channel */ |
| 128 | HEIGHT, /**< height */ |
| 129 | WIDTH, /**< width */ |
| 130 | BATCHES /**< batches */ |
Isabella Gottardi | d17a677 | 2018-02-27 17:41:55 +0000 | [diff] [blame] | 131 | }; |
| 132 | |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 133 | /** Available ConvolutionMethod*/ |
| 134 | enum class ConvolutionMethod |
| 135 | { |
Vidhya Sudhan Loganathan | 8ec0bb6 | 2019-04-23 10:40:44 +0100 | [diff] [blame] | 136 | GEMM, /**< Convolution using GEMM */ |
| 137 | DIRECT, /**< Direct convolution */ |
| 138 | WINOGRAD, /**< Convolution using Winograd */ |
| 139 | FFT /**< Convolution using FFT */ |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
giuros01 | 46a49a0 | 2019-04-01 13:50:22 +0100 | [diff] [blame] | 142 | /** Available DeconvolutionMethod*/ |
| 143 | enum class DeconvolutionMethod |
| 144 | { |
| 145 | GEMM, /**< Deconvolution using GEMM */ |
| 146 | DIRECT, /**< Direct deconvolution */ |
| 147 | }; |
| 148 | |
Manuel Bottini | 2732cca | 2019-05-28 11:44:41 +0100 | [diff] [blame] | 149 | /** Available FuseBatchNormalizationType*/ |
| 150 | enum class FuseBatchNormalizationType |
| 151 | { |
| 152 | CONVOLUTION, /**< For Convolution weights */ |
| 153 | DEPTHWISECONVOLUTION /**< For Depthwise Convolution weights*/ |
| 154 | }; |
| 155 | |
Usama Arif | 89890c6 | 2019-03-19 10:57:05 +0000 | [diff] [blame] | 156 | /** Padding mode to use for PadLayer */ |
| 157 | enum class PaddingMode |
| 158 | { |
| 159 | CONSTANT, |
| 160 | REFLECT, |
| 161 | SYMMETRIC |
| 162 | }; |
| 163 | |
Georgios Pinitas | 7900a9e | 2018-11-23 11:44:58 +0000 | [diff] [blame] | 164 | /** Supported comparison operations */ |
| 165 | enum class ComparisonOperation |
| 166 | { |
| 167 | Equal, /**< Equal comparison ( \f$ x == y \f$ ) */ |
| 168 | NotEqual, /**< NotEqual comparison ( \f$ x != y \f$ ) */ |
| 169 | Greater, /**< Greater comparison ( \f$ x > y \f$ ) */ |
| 170 | GreaterEqual, /**< Greater equal comparison ( \f$ x >= y \f$ ) */ |
| 171 | Less, /**< Less comparison ( \f$ x < y \f$ ) */ |
| 172 | LessEqual /**< Less equal comparison ( \f$ x <= y \f$ ) */ |
| 173 | }; |
| 174 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 175 | /** Container for valid region of a window */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 176 | struct ValidRegion |
| 177 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 178 | /** Default constructor */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 179 | ValidRegion() |
| 180 | : anchor{}, shape{} |
| 181 | { |
| 182 | } |
| 183 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 184 | /** Allow instances of this class to be copy constructed */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 185 | ValidRegion(const ValidRegion &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 186 | /** Allow instances of this class to be move constructed */ |
| 187 | ValidRegion(ValidRegion &&) = default; |
| 188 | /** Allow instances of this class to be copied */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 189 | ValidRegion &operator=(const ValidRegion &) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 190 | /** Allow instances of this class to be moved */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 191 | ValidRegion &operator=(ValidRegion &&) = default; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 192 | /** Default destructor */ |
| 193 | ~ValidRegion() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 194 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 195 | /** Constructor for a valid region with default number of dimensions |
| 196 | * |
| 197 | * @param[in] an_anchor Anchor for the start of the valid region. |
| 198 | * @param[in] a_shape Shape of the valid region. |
| 199 | * |
| 200 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 201 | ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape) |
| 202 | : anchor{ an_anchor }, shape{ a_shape } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 203 | { |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 204 | anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions())); |
| 205 | } |
| 206 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 207 | /** Constructor for a valid region with specified number of dimensions |
| 208 | * |
| 209 | * @param[in] an_anchor Anchor for the start of the valid region. |
| 210 | * @param[in] a_shape Shape of the valid region. |
| 211 | * @param[in] num_dimensions Number of dimensions (must be >= number of dimensions of anchor and shape). |
| 212 | * |
| 213 | */ |
Diego Lopez Recas | bcbc970 | 2017-12-18 11:28:27 +0000 | [diff] [blame] | 214 | ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions) |
| 215 | : anchor{ an_anchor }, shape{ a_shape } |
| 216 | { |
| 217 | ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions())); |
| 218 | anchor.set_num_dimensions(num_dimensions); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /** Return the start of the valid region for the given dimension @p d */ |
| 222 | int start(unsigned int d) const |
| 223 | { |
| 224 | return anchor[d]; |
| 225 | } |
| 226 | |
| 227 | /** Return the end of the valid region for the given dimension @p d */ |
| 228 | int end(unsigned int d) const |
| 229 | { |
| 230 | return anchor[d] + shape[d]; |
| 231 | } |
| 232 | |
Diego Lopez Recas | 35ceeb2 | 2017-12-04 18:56:10 +0000 | [diff] [blame] | 233 | /** Accessor to set the value of anchor and shape for one of the dimensions. |
| 234 | * |
| 235 | * @param[in] dimension Dimension for which the value is set. |
| 236 | * @param[in] start Value to be set in anchor for the dimension. |
| 237 | * @param[in] size Value to be set in shape for the dimension. |
| 238 | * |
| 239 | * @return *this. |
| 240 | */ |
| 241 | ValidRegion &set(size_t dimension, int start, size_t size) |
| 242 | { |
| 243 | anchor.set(dimension, start); |
| 244 | shape.set(dimension, size); |
| 245 | return *this; |
| 246 | } |
| 247 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 248 | Coordinates anchor; /**< Anchor for the start of the valid region. */ |
| 249 | TensorShape shape; /**< Shape of the valid region. */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 250 | }; |
| 251 | |
| 252 | /** Methods available to handle borders */ |
| 253 | enum class BorderMode |
| 254 | { |
| 255 | UNDEFINED, /**< Borders are left undefined */ |
| 256 | CONSTANT, /**< Pixels outside the image are assumed to have a constant value */ |
| 257 | REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */ |
| 258 | }; |
| 259 | |
| 260 | /** Container for 2D border size */ |
| 261 | struct BorderSize |
| 262 | { |
| 263 | /** Empty border, i.e. no border */ |
| 264 | constexpr BorderSize() |
| 265 | : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 } |
| 266 | { |
| 267 | } |
| 268 | |
| 269 | /** Border with equal size around the 2D plane */ |
Moritz Pflanzer | 7655a67 | 2017-09-23 11:57:33 +0100 | [diff] [blame] | 270 | explicit constexpr BorderSize(unsigned int size) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 271 | : top{ size }, right{ size }, bottom{ size }, left{ size } |
| 272 | { |
| 273 | } |
| 274 | |
| 275 | /** Border with same size for top/bottom and left/right */ |
| 276 | constexpr BorderSize(unsigned int top_bottom, unsigned int left_right) |
| 277 | : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right } |
| 278 | { |
| 279 | } |
| 280 | |
| 281 | /** Border with different sizes */ |
| 282 | constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left) |
| 283 | : top{ top }, right{ right }, bottom{ bottom }, left{ left } |
| 284 | { |
| 285 | } |
| 286 | |
| 287 | /** Check if the entire border is zero */ |
| 288 | constexpr bool empty() const |
| 289 | { |
| 290 | return top == 0 && right == 0 && bottom == 0 && left == 0; |
| 291 | } |
| 292 | |
| 293 | /** Check if the border is the same size on all sides */ |
| 294 | constexpr bool uniform() const |
| 295 | { |
| 296 | return top == right && top == bottom && top == left; |
| 297 | } |
| 298 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 299 | /** Scale this border size. |
| 300 | * |
| 301 | * @param[in] scale Scale to multiply border size by. |
| 302 | * |
| 303 | * @return *this. |
| 304 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 305 | BorderSize &operator*=(float scale) |
| 306 | { |
| 307 | top *= scale; |
| 308 | right *= scale; |
| 309 | bottom *= scale; |
| 310 | left *= scale; |
| 311 | |
| 312 | return *this; |
| 313 | } |
| 314 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 315 | /** Scale a copy of this border size. |
| 316 | * |
| 317 | * @param[in] scale Scale to multiply border size by. |
| 318 | * |
| 319 | * @return a scaled copy of this. |
| 320 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 321 | BorderSize operator*(float scale) |
| 322 | { |
| 323 | BorderSize size = *this; |
| 324 | size *= scale; |
| 325 | |
| 326 | return size; |
| 327 | } |
| 328 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 329 | /** Limit this border size. |
| 330 | * |
| 331 | * @param[in] limit Border size to limit this border size to. |
| 332 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 333 | void limit(const BorderSize &limit) |
| 334 | { |
| 335 | top = std::min(top, limit.top); |
| 336 | right = std::min(right, limit.right); |
| 337 | bottom = std::min(bottom, limit.bottom); |
| 338 | left = std::min(left, limit.left); |
| 339 | } |
| 340 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 341 | unsigned int top; /**< top of the border */ |
| 342 | unsigned int right; /**< right of the border */ |
| 343 | unsigned int bottom; /**< bottom of the border */ |
| 344 | unsigned int left; /**< left of the border */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 345 | }; |
| 346 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 347 | /** Container for 2D padding size */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 348 | using PaddingSize = BorderSize; |
| 349 | |
| 350 | /** Policy to handle overflow */ |
| 351 | enum class ConvertPolicy |
| 352 | { |
| 353 | WRAP, /**< Wrap around */ |
| 354 | SATURATE /**< Saturate */ |
| 355 | }; |
| 356 | |
| 357 | /** Interpolation method */ |
| 358 | enum class InterpolationPolicy |
| 359 | { |
| 360 | NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */ |
| 361 | BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */ |
| 362 | 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 */ |
| 363 | }; |
| 364 | |
| 365 | /** Bilinear Interpolation method used by LKTracker */ |
| 366 | enum class BilinearInterpolation |
| 367 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 368 | BILINEAR_OLD_NEW, /**< Old-new method */ |
| 369 | BILINEAR_SCHARR /**< Scharr method */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 370 | }; |
| 371 | |
| 372 | /** Threshold mode */ |
| 373 | enum class ThresholdType |
| 374 | { |
| 375 | BINARY, /**< Threshold with one value */ |
| 376 | RANGE /**< Threshold with two values*/ |
| 377 | }; |
| 378 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 379 | /** Termination criteria */ |
| 380 | enum class Termination |
| 381 | { |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 382 | TERM_CRITERIA_EPSILON, /**< Terminate when within epsilon of a threshold */ |
| 383 | TERM_CRITERIA_ITERATIONS, /**< Terminate after a maximum number of iterations */ |
| 384 | TERM_CRITERIA_BOTH /**< Terminate on whichever of the other conditions occurs first */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 385 | }; |
| 386 | |
| 387 | /** Magnitude calculation type. */ |
| 388 | enum class MagnitudeType |
| 389 | { |
| 390 | L1NORM, /**< L1 normalization type */ |
| 391 | L2NORM /**< L2 normalization type */ |
| 392 | }; |
| 393 | |
| 394 | /** Phase calculation type. |
| 395 | * |
| 396 | * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180 |
| 397 | */ |
| 398 | enum class PhaseType |
| 399 | { |
| 400 | SIGNED, /**< Angle range: [0, 360] */ |
| 401 | UNSIGNED /**< Angle range: [0, 180] */ |
| 402 | }; |
| 403 | |
| 404 | /** Keypoint type */ |
| 405 | struct KeyPoint |
| 406 | { |
| 407 | int32_t x{ 0 }; /**< X coordinates */ |
| 408 | int32_t y{ 0 }; /**< Y coordinates */ |
| 409 | float strength{ 0.f }; /**< Strength of the point */ |
| 410 | float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */ |
| 411 | float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */ |
| 412 | int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */ |
| 413 | float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */ |
| 414 | }; |
| 415 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 416 | /** Internal key point */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 417 | using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */ |
| 418 | |
| 419 | /** Rectangle type */ |
| 420 | struct Rectangle |
| 421 | { |
| 422 | uint16_t x; /**< Top-left x coordinate */ |
| 423 | uint16_t y; /**< Top-left y coordinate */ |
| 424 | uint16_t width; /**< Width of the rectangle */ |
| 425 | uint16_t height; /**< Height of the rectangle */ |
| 426 | }; |
| 427 | |
| 428 | /** Coordinate type */ |
| 429 | struct Coordinates2D |
| 430 | { |
| 431 | int32_t x; /**< X coordinates */ |
| 432 | int32_t y; /**< Y coordinates */ |
| 433 | }; |
| 434 | |
| 435 | /** Coordinate type */ |
| 436 | struct Coordinates3D |
| 437 | { |
| 438 | uint32_t x; /**< X coordinates */ |
| 439 | uint32_t y; /**< Y coordinates */ |
| 440 | uint32_t z; /**< Z coordinates */ |
| 441 | }; |
| 442 | |
Giuseppe Rossini | d7647d4 | 2018-07-17 18:13:13 +0100 | [diff] [blame] | 443 | /** Padding information as a pair of unsigned int start/end */ |
| 444 | using PaddingInfo = std::pair<uint32_t, uint32_t>; |
| 445 | |
| 446 | /** List of padding information */ |
| 447 | using PaddingList = std::vector<PaddingInfo>; |
| 448 | |
giuros01 | 3175fcf | 2018-11-21 09:59:17 +0000 | [diff] [blame] | 449 | /** Information to produce a tiled version of a Tensor */ |
| 450 | using Multiples = std::vector<uint32_t>; |
| 451 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 452 | /** Available channels */ |
| 453 | enum class Channel |
| 454 | { |
| 455 | UNKNOWN, /** Unknown channel format */ |
| 456 | C0, /**< First channel (used by formats with unknown channel types). */ |
| 457 | C1, /**< Second channel (used by formats with unknown channel types). */ |
| 458 | C2, /**< Third channel (used by formats with unknown channel types). */ |
| 459 | C3, /**< Fourth channel (used by formats with unknown channel types). */ |
| 460 | R, /**< Red channel. */ |
| 461 | G, /**< Green channel. */ |
| 462 | B, /**< Blue channel. */ |
| 463 | A, /**< Alpha channel. */ |
| 464 | Y, /**< Luma channel. */ |
| 465 | U, /**< Cb/U channel. */ |
| 466 | V /**< Cr/V/Value channel. */ |
| 467 | }; |
| 468 | |
| 469 | /** Available matrix patterns */ |
| 470 | enum class MatrixPattern |
| 471 | { |
| 472 | BOX, /**< Box pattern matrix. */ |
| 473 | CROSS, /**< Cross pattern matrix. */ |
| 474 | DISK, /**< Disk pattern matrix. */ |
| 475 | OTHER /**< Any other matrix pattern. */ |
| 476 | }; |
| 477 | |
| 478 | /** Available non linear functions. */ |
| 479 | enum class NonLinearFilterFunction : unsigned |
| 480 | { |
| 481 | MEDIAN = 0, /**< Non linear median filter. */ |
| 482 | MIN = 1, /**< Non linear erode. */ |
| 483 | MAX = 2, /**< Non linear dilate. */ |
| 484 | }; |
| 485 | |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 486 | /** Available reduction operations */ |
| 487 | enum class ReductionOperation |
| 488 | { |
Michalis Spyrou | 7930db4 | 2018-11-22 17:36:28 +0000 | [diff] [blame] | 489 | ARG_IDX_MAX, /**< Index of the max value */ |
Manuel Bottini | b412fab | 2018-12-10 17:40:23 +0000 | [diff] [blame] | 490 | ARG_IDX_MIN, /**< Index of the min value */ |
| 491 | MEAN_SUM, /**< Mean of sum */ |
| 492 | PROD, /**< Product */ |
| 493 | SUM_SQUARE, /**< Sum of squares */ |
Usama Arif | a4a08ad | 2019-05-20 12:38:33 +0100 | [diff] [blame] | 494 | SUM, /**< Sum */ |
| 495 | MIN, /**< Min */ |
Usama Arif | 28f0dd9 | 2019-05-20 13:44:34 +0100 | [diff] [blame] | 496 | MAX, /**< Max */ |
Georgios Pinitas | d976958 | 2017-08-03 10:19:40 +0100 | [diff] [blame] | 497 | }; |
| 498 | |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 499 | /** Available element-wise operations */ |
| 500 | enum class ArithmeticOperation |
| 501 | { |
| 502 | ADD, /**< (x + y) */ |
| 503 | SUB, /**< (x - y) */ |
| 504 | DIV, /**< (x / y) */ |
| 505 | MIN, /**< Min(x, y) */ |
| 506 | MAX, /**< Max(x, y) */ |
| 507 | SQUARED_DIFF, /**< (x - y)^2 */ |
Usama Arif | 81e671e | 2019-05-13 13:33:14 +0100 | [diff] [blame] | 508 | POWER, /**< x ^ y */ |
giuros01 | 1e6e1b8 | 2019-05-14 16:12:53 +0100 | [diff] [blame] | 509 | PRELU, /**< y*x if x < 0, x otherwise */ |
giuros01 | 164a272 | 2018-11-20 18:34:46 +0000 | [diff] [blame] | 510 | }; |
| 511 | |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 512 | /** Available element wise unary operations */ |
| 513 | enum class ElementWiseUnary |
| 514 | { |
| 515 | RSQRT, /**< Reverse square root */ |
| 516 | EXP, /**< Exponential */ |
Usama Arif | f6e475c | 2019-05-10 12:06:28 +0100 | [diff] [blame] | 517 | NEG, /**< Negate */ |
Usama Arif | c255aa7 | 2019-05-13 16:26:29 +0100 | [diff] [blame] | 518 | LOG, /**< Natural Logarithm */ |
Manuel Bottini | 6ac5992 | 2019-05-15 14:06:02 +0100 | [diff] [blame] | 519 | ABS, /**< Absolute value */ |
Michalis Spyrou | 0af4418 | 2019-05-17 14:04:47 +0100 | [diff] [blame] | 520 | SIN, /**< Sine */ |
Usama Arif | 0a5a57a | 2019-05-23 14:20:33 +0100 | [diff] [blame] | 521 | ROUND, /**< Round */ |
Michalis Spyrou | e936262 | 2018-11-23 17:41:37 +0000 | [diff] [blame] | 522 | }; |
| 523 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 524 | /** The normalization type used for the normalization layer */ |
| 525 | enum class NormType |
| 526 | { |
| 527 | IN_MAP_1D, /**< Normalization applied within the same map in 1D region */ |
| 528 | IN_MAP_2D, /**< Normalization applied within the same map in 2D region */ |
| 529 | CROSS_MAP /**< Normalization applied cross maps */ |
| 530 | }; |
| 531 | |
| 532 | /** Normalization type for Histogram of Oriented Gradients (HOG) */ |
| 533 | enum class HOGNormType |
| 534 | { |
| 535 | L2_NORM = 1, /**< L2-norm */ |
| 536 | L2HYS_NORM = 2, /**< L2-norm followed by clipping */ |
| 537 | L1_NORM = 3 /**< L1 norm */ |
| 538 | }; |
| 539 | |
| 540 | /** Detection window used for the object detection. The detection window keeps the following information: |
| 541 | * |
| 542 | * -# Geometry of the rectangular window (x/y of top-left corner and width/height) |
| 543 | * -# Index of the class used for evaluating which class the detection window belongs to |
| 544 | * -# Confidence value (score) obtained with the classifier |
| 545 | */ |
| 546 | struct DetectionWindow |
| 547 | { |
| 548 | uint16_t x{ 0 }; /**< Top-left x coordinate */ |
| 549 | uint16_t y{ 0 }; /**< Top-left y coordinate */ |
| 550 | uint16_t width{ 0 }; /**< Width of the detection window */ |
| 551 | uint16_t height{ 0 }; /**< Height of the detection window */ |
| 552 | uint16_t idx_class{ 0 }; /**< Index of the class */ |
| 553 | float score{ 0.f }; /**< Confidence value for the detection window */ |
| 554 | }; |
| 555 | |
| 556 | /** Dimension rounding type when down-scaling on CNNs |
| 557 | * @note Used in pooling and convolution layer |
| 558 | */ |
| 559 | enum class DimensionRoundingType |
| 560 | { |
| 561 | FLOOR, /**< Floor rounding */ |
| 562 | CEIL /**< Ceil rounding */ |
| 563 | }; |
| 564 | |
| 565 | /** Available pooling types */ |
| 566 | enum class PoolingType |
| 567 | { |
| 568 | MAX, /**< Max Pooling */ |
Georgios Pinitas | cdf5145 | 2017-08-31 14:21:36 +0100 | [diff] [blame] | 569 | AVG, /**< Average Pooling */ |
| 570 | L2 /**< L2 Pooling */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 571 | }; |
| 572 | |
Michalis Spyrou | 2709d61 | 2018-09-19 09:46:47 +0100 | [diff] [blame] | 573 | /** Available non maxima suppression types */ |
| 574 | enum class NMSType |
| 575 | { |
| 576 | LINEAR, /**< Linear NMS */ |
| 577 | GAUSSIAN, /**< Gaussian NMS */ |
| 578 | ORIGINAL /**< Original NMS */ |
| 579 | }; |
| 580 | |
| 581 | /** BoxWithNonMaximaSuppressionLimit Information class */ |
| 582 | class BoxNMSLimitInfo final |
| 583 | { |
| 584 | public: |
| 585 | /** Constructor |
| 586 | * |
| 587 | * @param[in] score_thresh (Optional) Score threshold. |
| 588 | * @param[in] nms (Optional) NMS value |
| 589 | * @param[in] detections (Optional) Number of detections |
| 590 | * @param[in] soft_nms_enabled (Optional) Enable SoftNMS |
| 591 | * @param[in] soft_nms_method (Optional) Soft NMS method |
| 592 | * @param[in] soft_nms_sigma (Optional) Soft NMS sigma value |
| 593 | * @param[in] soft_nms_min_score_thres (Optional) Soft NMS minimum score threshold |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 594 | * @param[in] suppress_size (Optional) Filter out boxes based on their size. Defaults to false |
| 595 | * @param[in] min_size (Optional) Smaller boxes than min_size will be filtered out. Defaults to 1 |
| 596 | * @param[in] im_width (Optional) Boxes whose centers (on the x axis) is beyond im_width will be filtered. Defaults to 1 |
| 597 | * @param[in] im_height (Optional) Boxes whose centers (on the y axis) is beyond im_height will be filtered. Defaults to 1 |
Michalis Spyrou | 2709d61 | 2018-09-19 09:46:47 +0100 | [diff] [blame] | 598 | */ |
| 599 | BoxNMSLimitInfo(float score_thresh = 0.05f, float nms = 0.3f, |
| 600 | int detections = 100, bool soft_nms_enabled = false, |
| 601 | NMSType soft_nms_method = NMSType::LINEAR, |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 602 | float soft_nms_sigma = 0.5f, float soft_nms_min_score_thres = 0.001f, bool suppress_size = false, float min_size = 1.0f, float im_width = 1.0f, float im_height = 1.0f) |
Michalis Spyrou | 2709d61 | 2018-09-19 09:46:47 +0100 | [diff] [blame] | 603 | : _score_thresh(score_thresh), _nms(nms), _detections_per_im(detections), _soft_nms_enabled(soft_nms_enabled), _soft_nms_method(soft_nms_method), _soft_nms_sigma(soft_nms_sigma), |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 604 | _soft_nms_min_score_thres(soft_nms_min_score_thres), _suppress_size(suppress_size), _min_size(min_size), _im_width(im_width), _im_height(im_height) |
Michalis Spyrou | 2709d61 | 2018-09-19 09:46:47 +0100 | [diff] [blame] | 605 | { |
| 606 | } |
| 607 | /** Get the score threshold */ |
| 608 | float score_thresh() const |
| 609 | { |
| 610 | return _score_thresh; |
| 611 | } |
| 612 | /** Get the NMS */ |
| 613 | float nms() const |
| 614 | { |
| 615 | return _nms; |
| 616 | } |
| 617 | /** Get the number of detections */ |
| 618 | int detections_per_im() const |
| 619 | { |
| 620 | return _detections_per_im; |
| 621 | } |
| 622 | /** Check if soft NMS is enabled */ |
| 623 | bool soft_nms_enabled() const |
| 624 | { |
| 625 | return _soft_nms_enabled; |
| 626 | } |
| 627 | /** Get soft NMS method */ |
| 628 | NMSType soft_nms_method() const |
| 629 | { |
| 630 | return _soft_nms_method; |
| 631 | } |
| 632 | /** Get soft NMS sigma */ |
| 633 | float soft_nms_sigma() const |
| 634 | { |
| 635 | return _soft_nms_sigma; |
| 636 | } |
| 637 | /** Get soft nms min score threshold */ |
| 638 | float soft_nms_min_score_thres() const |
| 639 | { |
| 640 | return _soft_nms_min_score_thres; |
| 641 | } |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 642 | /** Get if NMS will suppress boxes based on their size/position */ |
| 643 | bool suppress_size() const |
| 644 | { |
| 645 | return _suppress_size; |
| 646 | } |
| 647 | /** Get size suppression threshold */ |
| 648 | float min_size() const |
| 649 | { |
| 650 | return _min_size; |
| 651 | } |
| 652 | /** Get image width (NMS may suppress boxes whose center sits beyond the image width) */ |
| 653 | float im_width() const |
| 654 | { |
| 655 | return _im_width; |
| 656 | } |
| 657 | /** Get image height (NMS may suppress boxes whose center sits beyond the image height) */ |
| 658 | float im_height() const |
| 659 | { |
| 660 | return _im_height; |
| 661 | } |
Michalis Spyrou | 2709d61 | 2018-09-19 09:46:47 +0100 | [diff] [blame] | 662 | |
| 663 | private: |
| 664 | float _score_thresh; |
| 665 | float _nms; |
| 666 | int _detections_per_im; |
| 667 | bool _soft_nms_enabled; |
| 668 | NMSType _soft_nms_method; |
| 669 | float _soft_nms_sigma; |
| 670 | float _soft_nms_min_score_thres; |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 671 | bool _suppress_size; |
| 672 | float _min_size; |
| 673 | float _im_width; |
| 674 | float _im_height; |
Michalis Spyrou | 2709d61 | 2018-09-19 09:46:47 +0100 | [diff] [blame] | 675 | }; |
| 676 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 677 | /** Padding and stride information class */ |
| 678 | class PadStrideInfo |
| 679 | { |
| 680 | public: |
| 681 | /** Constructor |
| 682 | * |
| 683 | * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1. |
| 684 | * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1. |
| 685 | * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0. |
| 686 | * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0. |
| 687 | * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR. |
| 688 | */ |
| 689 | PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1, |
| 690 | unsigned int pad_x = 0, unsigned int pad_y = 0, |
| 691 | DimensionRoundingType round = DimensionRoundingType::FLOOR) |
| 692 | : _stride(std::make_pair(stride_x, stride_y)), |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 693 | _pad_left(pad_x), |
| 694 | _pad_top(pad_y), |
| 695 | _pad_right(pad_x), |
| 696 | _pad_bottom(pad_y), |
| 697 | _round_type(round) |
| 698 | { |
| 699 | } |
| 700 | /** Constructor |
| 701 | * |
| 702 | * @param[in] stride_x Stride, in elements, across x. |
| 703 | * @param[in] stride_y Stride, in elements, across y. |
| 704 | * @param[in] pad_left Padding across x on the left, in elements. |
| 705 | * @param[in] pad_top Padding across y on the top, in elements. |
| 706 | * @param[in] pad_right Padding across x on the right, in elements. |
| 707 | * @param[in] pad_bottom Padding across y on the bottom, in elements. |
| 708 | * @param[in] round Dimensions rounding. |
| 709 | */ |
| 710 | PadStrideInfo(unsigned int stride_x, unsigned int stride_y, |
| 711 | unsigned int pad_left, unsigned int pad_right, |
| 712 | unsigned int pad_top, unsigned int pad_bottom, |
| 713 | DimensionRoundingType round) |
| 714 | : _stride(std::make_pair(stride_x, stride_y)), |
| 715 | _pad_left(pad_left), |
| 716 | _pad_top(pad_top), |
| 717 | _pad_right(pad_right), |
| 718 | _pad_bottom(pad_bottom), |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 719 | _round_type(round) |
| 720 | { |
| 721 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 722 | /** Get the stride. |
| 723 | * |
| 724 | * @return a pair: stride x, stride y. |
| 725 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 726 | std::pair<unsigned int, unsigned int> stride() const |
| 727 | { |
| 728 | return _stride; |
| 729 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 730 | /** Check whether the padding is symmetric. |
| 731 | * |
| 732 | * @return True if the padding is symmetric. |
| 733 | */ |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 734 | bool padding_is_symmetric() const |
| 735 | { |
| 736 | return (_pad_left == _pad_right) && (_pad_top == _pad_bottom); |
| 737 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 738 | /** Get the padding. |
| 739 | * |
| 740 | * @note This should only be used when the padding is symmetric. |
| 741 | * |
| 742 | * @return a pair: padding left/right, padding top/bottom |
| 743 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 744 | std::pair<unsigned int, unsigned int> pad() const |
| 745 | { |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 746 | //this accessor should be used only when padding is symmetric |
Anthony Barbier | 21f67d6 | 2018-02-16 15:17:48 +0000 | [diff] [blame] | 747 | ARM_COMPUTE_ERROR_ON(!padding_is_symmetric()); |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 748 | return std::make_pair(_pad_left, _pad_top); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 749 | } |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 750 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 751 | /** Get the left padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 752 | unsigned int pad_left() const |
| 753 | { |
| 754 | return _pad_left; |
| 755 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 756 | /** Get the right padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 757 | unsigned int pad_right() const |
| 758 | { |
| 759 | return _pad_right; |
| 760 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 761 | /** Get the top padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 762 | unsigned int pad_top() const |
| 763 | { |
| 764 | return _pad_top; |
| 765 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 766 | /** Get the bottom padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 767 | unsigned int pad_bottom() const |
| 768 | { |
| 769 | return _pad_bottom; |
| 770 | } |
| 771 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 772 | /** Get the rounding type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 773 | DimensionRoundingType round() const |
| 774 | { |
| 775 | return _round_type; |
| 776 | } |
| 777 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 778 | /** Check whether this has any padding */ |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 779 | bool has_padding() const |
| 780 | { |
| 781 | return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0); |
| 782 | } |
| 783 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 784 | private: |
| 785 | std::pair<unsigned int, unsigned int> _stride; |
Jaroslaw Rzepecki | a1ed41f | 2017-10-13 11:13:58 +0100 | [diff] [blame] | 786 | unsigned int _pad_left; |
| 787 | unsigned int _pad_top; |
| 788 | unsigned int _pad_right; |
| 789 | unsigned int _pad_bottom; |
| 790 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 791 | DimensionRoundingType _round_type; |
| 792 | }; |
| 793 | |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 794 | /** Fully connected layer info */ |
| 795 | struct FullyConnectedLayerInfo |
| 796 | { |
| 797 | DataLayout weights_trained_layout{ DataLayout::NCHW }; /**< Layout that the weights have been trained with. */ |
| 798 | bool transpose_weights{ true }; /**< Transpose weights if true. */ |
| 799 | bool are_weights_reshaped{ false }; /**< Reshape the weights tensor if false. */ |
| 800 | bool retain_internal_weights{ false }; /**< Retain internal reshaped weights. */ |
Georgios Pinitas | c55cef1 | 2018-08-01 15:24:18 +0100 | [diff] [blame] | 801 | |
| 802 | /** Sets the weights trained data layout |
| 803 | * |
| 804 | * @param[in] layout Data layout that the weights were trained with |
| 805 | * |
| 806 | * @return Updated object |
| 807 | */ |
| 808 | FullyConnectedLayerInfo &set_weights_trained_layout(DataLayout layout) |
| 809 | { |
| 810 | weights_trained_layout = layout; |
| 811 | return *this; |
| 812 | } |
Georgios Pinitas | 195b0ba | 2018-08-02 17:18:51 +0100 | [diff] [blame] | 813 | /** Sets the transpose weights flag |
| 814 | * |
| 815 | * @param[in] should_transpose_weights Boolean flag indicating if weights should be transposed |
| 816 | * |
| 817 | * @return Updated object |
| 818 | */ |
| 819 | FullyConnectedLayerInfo &set_transpose_weights(bool should_transpose_weights) |
| 820 | { |
| 821 | transpose_weights = should_transpose_weights; |
| 822 | return *this; |
| 823 | } |
Georgios Pinitas | 7d66a8e | 2018-07-17 12:28:42 +0100 | [diff] [blame] | 824 | }; |
| 825 | |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 826 | /** PriorBox layer info */ |
| 827 | class PriorBoxLayerInfo final |
| 828 | { |
| 829 | public: |
| 830 | /** Default Constructor */ |
| 831 | PriorBoxLayerInfo() |
| 832 | : _min_sizes(), |
| 833 | _variances(), |
| 834 | _offset(), |
| 835 | _flip(true), |
| 836 | _clip(false), |
| 837 | _max_sizes(), |
| 838 | _aspect_ratios(), |
| 839 | _img_size(), |
| 840 | _steps() |
| 841 | { |
| 842 | } |
| 843 | /** Constructor |
| 844 | * |
| 845 | * @param[in] min_sizes Min sizes vector. |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 846 | * @param[in] variances Variances vector. |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 847 | * @param[in] offset Offset value. |
| 848 | * @param[in] flip (Optional) Flip the aspect ratios. |
| 849 | * @param[in] clip (Optional) Clip coordinates so that they're within [0,1]. |
| 850 | * @param[in] max_sizes (Optional) Max sizes vector. |
| 851 | * @param[in] aspect_ratios (Optional) Aspect ratios of the boxes. |
| 852 | * @param[in] img_size (Optional) Image size. |
| 853 | * @param[in] steps (Optional) Step values. |
| 854 | */ |
| 855 | PriorBoxLayerInfo(const std::vector<float> &min_sizes, const std::vector<float> &variances, float offset, bool flip = true, bool clip = false, |
Pablo Tello | 3252143 | 2018-11-15 14:43:10 +0000 | [diff] [blame] | 856 | const std::vector<float> &max_sizes = {}, const std::vector<float> &aspect_ratios = {}, |
| 857 | const Coordinates2D &img_size = Coordinates2D{ 0, 0 }, const std::array<float, 2> &steps = { { 0.f, 0.f } }) |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 858 | : _min_sizes(min_sizes), |
| 859 | _variances(variances), |
| 860 | _offset(offset), |
| 861 | _flip(flip), |
| 862 | _clip(clip), |
| 863 | _max_sizes(max_sizes), |
Michalis Spyrou | 721c4cb | 2018-09-04 15:27:25 +0100 | [diff] [blame] | 864 | _aspect_ratios(), |
Michalis Spyrou | 6c7c38e | 2018-08-29 16:28:11 +0100 | [diff] [blame] | 865 | _img_size(img_size), |
| 866 | _steps(steps) |
| 867 | { |
| 868 | _aspect_ratios.push_back(1.); |
| 869 | for(unsigned int i = 0; i < aspect_ratios.size(); ++i) |
| 870 | { |
| 871 | float ar = aspect_ratios[i]; |
| 872 | bool already_exist = false; |
| 873 | for(auto ar_new : _aspect_ratios) |
| 874 | { |
| 875 | if(fabs(ar - ar_new) < 1e-6) |
| 876 | { |
| 877 | already_exist = true; |
| 878 | break; |
| 879 | } |
| 880 | } |
| 881 | if(!already_exist) |
| 882 | { |
| 883 | _aspect_ratios.push_back(ar); |
| 884 | if(flip) |
| 885 | { |
| 886 | _aspect_ratios.push_back(1.f / ar); |
| 887 | } |
| 888 | } |
| 889 | } |
| 890 | } |
| 891 | /** Get min sizes. */ |
| 892 | std::vector<float> min_sizes() const |
| 893 | { |
| 894 | return _min_sizes; |
| 895 | } |
| 896 | /** Get min variances. */ |
| 897 | std::vector<float> variances() const |
| 898 | { |
| 899 | return _variances; |
| 900 | } |
| 901 | /** Get the step coordinates */ |
| 902 | std::array<float, 2> steps() const |
| 903 | { |
| 904 | return _steps; |
| 905 | } |
| 906 | /** Get the image size coordinates */ |
| 907 | Coordinates2D img_size() const |
| 908 | { |
| 909 | return _img_size; |
| 910 | } |
| 911 | /** Get the offset */ |
| 912 | float offset() const |
| 913 | { |
| 914 | return _offset; |
| 915 | } |
| 916 | /** Get the flip value */ |
| 917 | bool flip() const |
| 918 | { |
| 919 | return _flip; |
| 920 | } |
| 921 | /** Get the clip value */ |
| 922 | bool clip() const |
| 923 | { |
| 924 | return _clip; |
| 925 | } |
| 926 | /** Get max sizes. */ |
| 927 | std::vector<float> max_sizes() const |
| 928 | { |
| 929 | return _max_sizes; |
| 930 | } |
| 931 | /** Get aspect ratios. */ |
| 932 | std::vector<float> aspect_ratios() const |
| 933 | { |
| 934 | return _aspect_ratios; |
| 935 | } |
| 936 | |
| 937 | private: |
| 938 | std::vector<float> _min_sizes; |
| 939 | std::vector<float> _variances; |
| 940 | float _offset; |
| 941 | bool _flip; |
| 942 | bool _clip; |
| 943 | std::vector<float> _max_sizes; |
| 944 | std::vector<float> _aspect_ratios; |
| 945 | Coordinates2D _img_size; |
| 946 | std::array<float, 2> _steps; |
| 947 | }; |
| 948 | |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 949 | // Bounding Box [xmin, ymin, xmax, ymax] |
| 950 | using BBox = std::array<float, 4>; |
| 951 | // LabelBBox used for map label and bounding box |
| 952 | using LabelBBox = std::map<int, std::vector<BBox>>; |
| 953 | |
Isabella Gottardi | 05e5644 | 2018-11-16 11:26:52 +0000 | [diff] [blame] | 954 | /** Available Detection Output code types */ |
| 955 | enum class DetectionOutputLayerCodeType |
| 956 | { |
| 957 | CORNER, /**< Use box corners */ |
| 958 | CENTER_SIZE, /**< Use box centers and size */ |
| 959 | CORNER_SIZE, /**< Use box centers and size */ |
| 960 | TF_CENTER /**< Use box centers and size but flip x and y co-ordinates */ |
| 961 | }; |
| 962 | |
| 963 | /** Detection Output layer info */ |
| 964 | class DetectionOutputLayerInfo final |
| 965 | { |
| 966 | public: |
| 967 | /** Default Constructor */ |
| 968 | DetectionOutputLayerInfo() |
| 969 | : _num_classes(), |
| 970 | _share_location(), |
| 971 | _code_type(DetectionOutputLayerCodeType::CORNER), |
| 972 | _keep_top_k(), |
| 973 | _nms_threshold(), |
| 974 | _top_k(), |
| 975 | _background_label_id(), |
| 976 | _confidence_threshold(), |
| 977 | _variance_encoded_in_target(false), |
| 978 | _eta(), |
| 979 | _num_loc_classes() |
| 980 | { |
| 981 | _num_loc_classes = _share_location ? 1 : _num_classes; |
| 982 | } |
| 983 | /** Constructor |
| 984 | * |
| 985 | * @param[in] num_classes Number of classes to be predicted. |
| 986 | * @param[in] share_location If true, bounding box are shared among different classes. |
| 987 | * @param[in] code_type Type of coding method for bbox. |
| 988 | * @param[in] keep_top_k Number of total bounding boxes to be kept per image after NMS step. |
| 989 | * @param[in] nms_threshold Threshold to be used in NMS. |
| 990 | * @param[in] top_k (Optional) Number of boxes per image with top confidence scores that are fed into the NMS algorithm. Default set to -1. |
| 991 | * @param[in] background_label_id (Optional) Background label ID. If there is no background class, set it as -1. |
| 992 | * @param[in] confidence_threshold (Optional) Only consider detections whose confidences are larger than a threshold. Default set to -FLT_MAX. |
| 993 | * @param[in] variance_encoded_in_target (Optional) If true, variance is encoded in target. Otherwise we need to adjust the predicted offset accordingly.Default set to false. |
| 994 | * @param[in] eta (Optional) Eta. |
| 995 | */ |
| 996 | DetectionOutputLayerInfo(int num_classes, bool share_location, DetectionOutputLayerCodeType code_type, int keep_top_k, float nms_threshold, int top_k = -1, int background_label_id = -1, |
| 997 | float confidence_threshold = std::numeric_limits<float>::lowest(), bool variance_encoded_in_target = false, float eta = 1) |
| 998 | : _num_classes(num_classes), |
| 999 | _share_location(share_location), |
| 1000 | _code_type(code_type), |
| 1001 | _keep_top_k(keep_top_k), |
| 1002 | _nms_threshold(nms_threshold), |
| 1003 | _top_k(top_k), |
| 1004 | _background_label_id(background_label_id), |
| 1005 | _confidence_threshold(confidence_threshold), |
| 1006 | _variance_encoded_in_target(variance_encoded_in_target), |
| 1007 | _eta(eta), |
| 1008 | _num_loc_classes() |
| 1009 | { |
| 1010 | _num_loc_classes = _share_location ? 1 : _num_classes; |
| 1011 | } |
| 1012 | /** Get num classes. */ |
| 1013 | int num_classes() const |
| 1014 | { |
| 1015 | return _num_classes; |
| 1016 | } |
| 1017 | /** Get share location. */ |
| 1018 | bool share_location() const |
| 1019 | { |
| 1020 | return _share_location; |
| 1021 | } |
| 1022 | /** Get detection output code type. */ |
| 1023 | DetectionOutputLayerCodeType code_type() const |
| 1024 | { |
| 1025 | return _code_type; |
| 1026 | } |
| 1027 | /** Get if variance encoded in target. */ |
| 1028 | bool variance_encoded_in_target() const |
| 1029 | { |
| 1030 | return _variance_encoded_in_target; |
| 1031 | } |
| 1032 | /** Get the number of total bounding boxes to be kept per image. */ |
| 1033 | int keep_top_k() const |
| 1034 | { |
| 1035 | return _keep_top_k; |
| 1036 | } |
| 1037 | /** Get nms threshold. */ |
| 1038 | float nms_threshold() const |
| 1039 | { |
| 1040 | return _nms_threshold; |
| 1041 | } |
| 1042 | /** Get eta. */ |
| 1043 | float eta() const |
| 1044 | { |
| 1045 | return _eta; |
| 1046 | } |
| 1047 | /** Get background label ID. */ |
| 1048 | int background_label_id() const |
| 1049 | { |
| 1050 | return _background_label_id; |
| 1051 | } |
| 1052 | /** Get confidence threshold. */ |
| 1053 | float confidence_threshold() const |
| 1054 | { |
| 1055 | return _confidence_threshold; |
| 1056 | } |
| 1057 | /** Get top K. */ |
| 1058 | int top_k() const |
| 1059 | { |
| 1060 | return _top_k; |
| 1061 | } |
| 1062 | /** Get number of location classes. */ |
| 1063 | int num_loc_classes() const |
| 1064 | { |
| 1065 | return _num_loc_classes; |
| 1066 | } |
| 1067 | |
| 1068 | private: |
| 1069 | int _num_classes; |
| 1070 | bool _share_location; |
| 1071 | DetectionOutputLayerCodeType _code_type; |
| 1072 | int _keep_top_k; |
| 1073 | float _nms_threshold; |
| 1074 | int _top_k; |
| 1075 | int _background_label_id; |
| 1076 | float _confidence_threshold; |
| 1077 | bool _variance_encoded_in_target; |
| 1078 | float _eta; |
| 1079 | int _num_loc_classes; |
| 1080 | }; |
| 1081 | |
Isabella Gottardi | a7acb3c | 2019-01-08 13:48:44 +0000 | [diff] [blame] | 1082 | /** Detection Output layer info */ |
| 1083 | class DetectionPostProcessLayerInfo final |
| 1084 | { |
| 1085 | public: |
| 1086 | /** Default Constructor */ |
| 1087 | DetectionPostProcessLayerInfo() |
| 1088 | : _max_detections(), |
| 1089 | _max_classes_per_detection(), |
| 1090 | _nms_score_threshold(), |
| 1091 | _iou_threshold(), |
| 1092 | _num_classes(), |
| 1093 | _scales_values(), |
| 1094 | _use_regular_nms(), |
| 1095 | _detection_per_class() |
| 1096 | { |
| 1097 | } |
| 1098 | /** Constructor |
| 1099 | * |
| 1100 | * @param[in] max_detections Number of total detection. |
| 1101 | * @param[in] max_classes_per_detection Number of total classes to be kept after NMS step. Used in the Fast Non-Max-Suppression |
| 1102 | * @param[in] nms_score_threshold Threshold to be used in NMS |
| 1103 | * @param[in] iou_threshold Threshold to be used during the intersection over union. |
| 1104 | * @param[in] num_classes Number of classes. |
| 1105 | * @param[in] scales_values Scales values used for decode center size boxes. |
| 1106 | * @param[in] use_regular_nms (Optional) Boolean to determinate if use regular or fast nms. |
| 1107 | * @param[in] detection_per_class (Optional) Number of detection per class. Used in the Regular Non-Max-Suppression |
| 1108 | */ |
| 1109 | DetectionPostProcessLayerInfo(unsigned int max_detections, unsigned int max_classes_per_detection, float nms_score_threshold, float iou_threshold, unsigned int num_classes, |
| 1110 | std::array<float, 4> scales_values, bool use_regular_nms = false, unsigned int detection_per_class = 100) |
| 1111 | : _max_detections(max_detections), |
| 1112 | _max_classes_per_detection(max_classes_per_detection), |
| 1113 | _nms_score_threshold(nms_score_threshold), |
| 1114 | _iou_threshold(iou_threshold), |
| 1115 | _num_classes(num_classes), |
| 1116 | _scales_values(scales_values), |
| 1117 | _use_regular_nms(use_regular_nms), |
| 1118 | _detection_per_class(detection_per_class) |
| 1119 | { |
| 1120 | } |
| 1121 | /** Get max detections. */ |
| 1122 | unsigned int max_detections() const |
| 1123 | { |
| 1124 | return _max_detections; |
| 1125 | } |
| 1126 | /** Get max_classes per detection. Used in the Fast Non-Max-Suppression.*/ |
| 1127 | unsigned int max_classes_per_detection() const |
| 1128 | { |
| 1129 | return _max_classes_per_detection; |
| 1130 | } |
| 1131 | /** Get detection per class. Used in the Regular Non-Max-Suppression */ |
| 1132 | unsigned int detection_per_class() const |
| 1133 | { |
| 1134 | return _detection_per_class; |
| 1135 | } |
| 1136 | /** Get nms threshold. */ |
| 1137 | float nms_score_threshold() const |
| 1138 | { |
| 1139 | return _nms_score_threshold; |
| 1140 | } |
| 1141 | /** Get intersection over union threshold. */ |
| 1142 | float iou_threshold() const |
| 1143 | { |
| 1144 | return _iou_threshold; |
| 1145 | } |
| 1146 | /** Get num classes. */ |
| 1147 | unsigned int num_classes() const |
| 1148 | { |
| 1149 | return _num_classes; |
| 1150 | } |
| 1151 | /** Get if use regular nms. */ |
| 1152 | bool use_regular_nms() const |
| 1153 | { |
| 1154 | return _use_regular_nms; |
| 1155 | } |
| 1156 | /** Get y scale value. */ |
| 1157 | float scale_value_y() const |
| 1158 | { |
| 1159 | // Saved as [y,x,h,w] |
| 1160 | return _scales_values[0]; |
| 1161 | } |
| 1162 | /** Get x scale value. */ |
| 1163 | float scale_value_x() const |
| 1164 | { |
| 1165 | // Saved as [y,x,h,w] |
| 1166 | return _scales_values[1]; |
| 1167 | } |
| 1168 | /** Get h scale value. */ |
| 1169 | float scale_value_h() const |
| 1170 | { |
| 1171 | // Saved as [y,x,h,w] |
| 1172 | return _scales_values[2]; |
| 1173 | } |
| 1174 | /** Get w scale value. */ |
| 1175 | float scale_value_w() const |
| 1176 | { |
| 1177 | // Saved as [y,x,h,w] |
| 1178 | return _scales_values[3]; |
| 1179 | } |
| 1180 | |
| 1181 | private: |
| 1182 | unsigned int _max_detections; |
| 1183 | unsigned int _max_classes_per_detection; |
| 1184 | float _nms_score_threshold; |
| 1185 | float _iou_threshold; |
| 1186 | unsigned int _num_classes; |
| 1187 | std::array<float, 4> _scales_values; |
| 1188 | bool _use_regular_nms; |
| 1189 | unsigned int _detection_per_class; |
| 1190 | }; |
| 1191 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1192 | /** Pooling Layer Information class */ |
| 1193 | class PoolingLayerInfo |
| 1194 | { |
| 1195 | public: |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 1196 | /** Default Constructor */ |
| 1197 | PoolingLayerInfo() |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1198 | : _pool_type(PoolingType::MAX), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo()), _exclude_padding(false), _is_global_pooling(false), _fp_mixed_precision(false) |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 1199 | { |
| 1200 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1201 | /** Default Constructor |
| 1202 | * |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1203 | * @param[in] pool_type Pooling type @ref PoolingType. |
| 1204 | * @param[in] pool_size Pooling size, in elements, across x and y. |
| 1205 | * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo |
| 1206 | * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations. |
| 1207 | * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area). |
| 1208 | * Defaults to false; |
| 1209 | * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1210 | */ |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 1211 | explicit PoolingLayerInfo(PoolingType pool_type, |
| 1212 | unsigned int pool_size, |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1213 | PadStrideInfo pad_stride_info = PadStrideInfo(), |
| 1214 | bool exclude_padding = false, |
| 1215 | bool fp_mixed_precision = false) |
| 1216 | : _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), |
| 1217 | _fp_mixed_precision(fp_mixed_precision) |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 1218 | { |
| 1219 | } |
| 1220 | /** Default Constructor |
| 1221 | * |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1222 | * @param[in] pool_type Pooling type @ref PoolingType. |
| 1223 | * @param[in] pool_size Pooling size, in elements, across x and y. |
| 1224 | * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo |
| 1225 | * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations. |
| 1226 | * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area). |
| 1227 | * Defaults to false; |
| 1228 | * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy. |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 1229 | */ |
| 1230 | explicit PoolingLayerInfo(PoolingType pool_type, |
| 1231 | Size2D pool_size, |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1232 | PadStrideInfo pad_stride_info = PadStrideInfo(), |
| 1233 | bool exclude_padding = false, |
| 1234 | bool fp_mixed_precision = false) |
| 1235 | : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false), _fp_mixed_precision(fp_mixed_precision) |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 1236 | { |
| 1237 | } |
| 1238 | /** Default Constructor |
| 1239 | * |
| 1240 | * @note This constructor is used for global pooling |
| 1241 | * |
| 1242 | * @param[in] pool_type Pooling type @ref PoolingType. |
| 1243 | */ |
| 1244 | explicit PoolingLayerInfo(PoolingType pool_type) |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1245 | : _pool_type(pool_type), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo(1, 1, 0, 0)), _exclude_padding(false), _is_global_pooling(true), _fp_mixed_precision(false) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1246 | { |
| 1247 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1248 | /** Get the pooling type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1249 | PoolingType pool_type() const |
| 1250 | { |
| 1251 | return _pool_type; |
| 1252 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1253 | /** Get the pooling size */ |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 1254 | const Size2D &pool_size() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1255 | { |
| 1256 | return _pool_size; |
| 1257 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1258 | /** Get the padding and stride */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1259 | PadStrideInfo pad_stride_info() const |
| 1260 | { |
| 1261 | return _pad_stride_info; |
| 1262 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1263 | /** Check if padding is excluded in calculations */ |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 1264 | bool exclude_padding() const |
| 1265 | { |
| 1266 | return _exclude_padding; |
| 1267 | } |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1268 | /** Check if a wider accumulator should be used. */ |
| 1269 | bool fp_mixed_precision() const |
| 1270 | { |
| 1271 | return _fp_mixed_precision; |
| 1272 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1273 | /** Check if is global pooling */ |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 1274 | bool is_global_pooling() const |
| 1275 | { |
| 1276 | return _is_global_pooling; |
| 1277 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1278 | |
| 1279 | private: |
| 1280 | PoolingType _pool_type; |
Isabella Gottardi | 6e464c3 | 2018-01-26 12:32:45 +0000 | [diff] [blame] | 1281 | Size2D _pool_size; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1282 | PadStrideInfo _pad_stride_info; |
Georgios Pinitas | adaae7e | 2017-10-30 15:56:32 +0000 | [diff] [blame] | 1283 | bool _exclude_padding; |
Georgios Pinitas | 4c2dd54 | 2017-11-13 12:58:41 +0000 | [diff] [blame] | 1284 | bool _is_global_pooling; |
Sang-Hoon Park | 2aa7fd0 | 2019-09-18 13:39:00 +0100 | [diff] [blame^] | 1285 | bool _fp_mixed_precision; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1286 | }; |
| 1287 | |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1288 | /** ROI Pooling Layer Information class */ |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1289 | class ROIPoolingLayerInfo final |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1290 | { |
| 1291 | public: |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1292 | /** Constructor |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1293 | * |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1294 | * @param[in] pooled_width Pooled width of the layer. |
| 1295 | * @param[in] pooled_height Pooled height of the layer. |
| 1296 | * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions. |
| 1297 | * @param[in] sampling_ratio Number of samples to include in each pooling region (if set to zero, a ceil(roi_dims/pooling_dims)) |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1298 | */ |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1299 | ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale, unsigned int sampling_ratio = 0) |
| 1300 | : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale), _sampling_ratio(sampling_ratio) |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1301 | { |
| 1302 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1303 | /** Get the pooled width of the layer */ |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1304 | unsigned int pooled_width() const |
| 1305 | { |
| 1306 | return _pooled_width; |
| 1307 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1308 | /** Get the pooled height of the layer */ |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1309 | unsigned int pooled_height() const |
| 1310 | { |
| 1311 | return _pooled_height; |
| 1312 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1313 | /** Get the spatial scale */ |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1314 | float spatial_scale() const |
| 1315 | { |
| 1316 | return _spatial_scale; |
| 1317 | } |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1318 | /** Get sampling ratio */ |
| 1319 | unsigned int sampling_ratio() const |
| 1320 | { |
| 1321 | return _sampling_ratio; |
| 1322 | } |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1323 | |
| 1324 | private: |
| 1325 | unsigned int _pooled_width; |
| 1326 | unsigned int _pooled_height; |
| 1327 | float _spatial_scale; |
giuros01 | 1887081 | 2018-09-13 09:31:40 +0100 | [diff] [blame] | 1328 | unsigned int _sampling_ratio; |
Georgios Pinitas | 7b7858d | 2017-06-21 16:44:24 +0100 | [diff] [blame] | 1329 | }; |
| 1330 | |
Manuel Bottini | 5209be5 | 2019-02-13 16:34:56 +0000 | [diff] [blame] | 1331 | /** Generate Proposals Information class */ |
| 1332 | class GenerateProposalsInfo |
| 1333 | { |
| 1334 | public: |
| 1335 | /** Constructor |
| 1336 | * |
| 1337 | * @param[in] im_width Width of the original image |
| 1338 | * @param[in] im_height Height of the original image |
| 1339 | * @param[in] im_scale Scale applied to the original image |
| 1340 | * @param[in] spatial_scale (Optional)Scale applied to the feature map. Defaults to 1.0 |
| 1341 | * @param[in] pre_nms_topN (Optional)Number of the best scores to be selected from the transformations. Defaults to 6000. |
| 1342 | * @param[in] post_nms_topN (Optional)Number of the best scores to be selected from the NMS operation. Defaults to 300. |
| 1343 | * @param[in] nms_thres (Optional)NMS overlap threshold. Defaults to 0.7. |
| 1344 | * @param[in] min_size (Optional)Size used to validate the anchors produced. Defaults to 16. |
| 1345 | * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region of interest). Defaults to 4. |
| 1346 | */ |
| 1347 | GenerateProposalsInfo(float im_width, float im_height, float im_scale, float spatial_scale = 1.0, int pre_nms_topN = 6000, int post_nms_topN = 300, float nms_thres = 0.7, float min_size = 16.0, |
| 1348 | size_t values_per_roi = 4) |
| 1349 | : _im_height(im_height), _im_width(im_width), _im_scale(im_scale), _spatial_scale(spatial_scale), _pre_nms_topN(pre_nms_topN), _post_nms_topN(post_nms_topN), _nms_thres(nms_thres), |
| 1350 | _min_size(min_size), _values_per_roi(values_per_roi) |
| 1351 | { |
| 1352 | } |
| 1353 | |
| 1354 | /* Get the original height */ |
| 1355 | float im_height() const |
| 1356 | { |
| 1357 | return _im_height; |
| 1358 | } |
| 1359 | /* Get the original width */ |
| 1360 | float im_width() const |
| 1361 | { |
| 1362 | return _im_width; |
| 1363 | } |
| 1364 | /* Get the image scale */ |
| 1365 | float im_scale() const |
| 1366 | { |
| 1367 | return _im_scale; |
| 1368 | } |
| 1369 | /* Get the value of how many best scores to select (before NMS) */ |
| 1370 | int pre_nms_topN() const |
| 1371 | { |
| 1372 | return _pre_nms_topN; |
| 1373 | } |
| 1374 | /* Get the value of how many best scores to select (after NMS) */ |
| 1375 | int post_nms_topN() const |
| 1376 | { |
| 1377 | return _post_nms_topN; |
| 1378 | } |
| 1379 | /* Get the NMS overlap threshold */ |
| 1380 | float nms_thres() const |
| 1381 | { |
| 1382 | return _nms_thres; |
| 1383 | } |
| 1384 | /* Get the minimal size */ |
| 1385 | float min_size() const |
| 1386 | { |
| 1387 | return _min_size; |
| 1388 | } |
| 1389 | /* Get the spatial scale to be applied to the feature maps */ |
| 1390 | float spatial_scale() const |
| 1391 | { |
| 1392 | return _spatial_scale; |
| 1393 | } |
| 1394 | /* Get the values used to represent a ROI(Region of interest)*/ |
| 1395 | size_t values_per_roi() const |
| 1396 | { |
| 1397 | return _values_per_roi; |
| 1398 | } |
| 1399 | |
| 1400 | private: |
| 1401 | float _im_height; |
| 1402 | float _im_width; |
| 1403 | float _im_scale; |
| 1404 | float _spatial_scale; |
| 1405 | int _pre_nms_topN; |
| 1406 | int _post_nms_topN; |
| 1407 | float _nms_thres; |
| 1408 | float _min_size; |
| 1409 | size_t _values_per_roi; |
| 1410 | }; |
| 1411 | |
| 1412 | /** ComputeAnchors information class */ |
| 1413 | class ComputeAnchorsInfo |
| 1414 | { |
| 1415 | public: |
| 1416 | /** Constructor |
| 1417 | * |
| 1418 | * @param[in] feat_width Feature map width |
| 1419 | * @param[in] feat_height Feature map height |
| 1420 | * @param[in] spatial_scale Feature map scale |
| 1421 | * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region Of Interest). Defaults to 4 |
| 1422 | */ |
| 1423 | ComputeAnchorsInfo(float feat_width, float feat_height, float spatial_scale, size_t values_per_roi = 4) |
| 1424 | : _feat_height(feat_height), |
| 1425 | _feat_width(feat_width), |
| 1426 | _spatial_scale(spatial_scale), |
| 1427 | _values_per_roi(values_per_roi) |
| 1428 | { |
| 1429 | } |
| 1430 | |
| 1431 | /* Get the height of the feature map */ |
| 1432 | float feat_height() const |
| 1433 | { |
| 1434 | return _feat_height; |
| 1435 | } |
| 1436 | |
| 1437 | /* Get the width of the feature map */ |
| 1438 | float feat_width() const |
| 1439 | { |
| 1440 | return _feat_width; |
| 1441 | } |
| 1442 | |
| 1443 | /* Get the scale of the feature map */ |
| 1444 | float spatial_scale() const |
| 1445 | { |
| 1446 | return _spatial_scale; |
| 1447 | } |
| 1448 | |
| 1449 | /* Get the values used to represent a ROI(Region Of Interest)*/ |
| 1450 | size_t values_per_roi() const |
| 1451 | { |
| 1452 | return _values_per_roi; |
| 1453 | } |
| 1454 | |
| 1455 | private: |
| 1456 | float _feat_height; |
| 1457 | float _feat_width; |
| 1458 | float _spatial_scale; |
| 1459 | size_t _values_per_roi; |
| 1460 | }; |
| 1461 | |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1462 | /** Bounding Box Transform information class */ |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 1463 | class BoundingBoxTransformInfo final |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1464 | { |
| 1465 | public: |
| 1466 | /** Constructor |
| 1467 | * |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 1468 | * @param[in] img_width Width of the original image |
| 1469 | * @param[in] img_height Height, of the original image |
| 1470 | * @param[in] scale Scale of the original image |
| 1471 | * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false |
| 1472 | * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones |
| 1473 | * @param[in] correct_transform_coords (Optional)Correct bounding box transform coordinates. Defaults to false |
| 1474 | * @param[in] bbox_xform_clip (Optional)Minimum bounding box width and height after bounding box transformation in log-space. Defaults to log(1000/16) |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1475 | */ |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 1476 | BoundingBoxTransformInfo(float img_width, float img_height, float scale, bool apply_scale = false, const std::array<float, 4> weights = { { 1.f, 1.f, 1.f, 1.f } }, bool correct_transform_coords = |
| 1477 | false, |
| 1478 | float bbox_xform_clip = |
| 1479 | 4.135166556742356f) |
| 1480 | : _img_width(img_width), _img_height(img_height), _scale(scale), _apply_scale(apply_scale), _correct_transform_coords(correct_transform_coords), _weights(weights), _bbox_xform_clip(bbox_xform_clip) |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1481 | { |
| 1482 | } |
| 1483 | |
| 1484 | std::array<float, 4> weights() const |
| 1485 | { |
| 1486 | return _weights; |
| 1487 | } |
| 1488 | |
| 1489 | float bbox_xform_clip() const |
| 1490 | { |
| 1491 | return _bbox_xform_clip; |
| 1492 | } |
| 1493 | |
| 1494 | float img_height() const |
| 1495 | { |
| 1496 | return _img_height; |
| 1497 | } |
| 1498 | |
| 1499 | float img_width() const |
| 1500 | { |
| 1501 | return _img_width; |
| 1502 | } |
| 1503 | |
| 1504 | float scale() const |
| 1505 | { |
| 1506 | return _scale; |
| 1507 | } |
| 1508 | |
| 1509 | bool apply_scale() const |
| 1510 | { |
| 1511 | return _apply_scale; |
| 1512 | } |
| 1513 | |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 1514 | bool correct_transform_coords() const |
| 1515 | { |
| 1516 | return _correct_transform_coords; |
| 1517 | } |
| 1518 | |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1519 | private: |
| 1520 | float _img_width; |
| 1521 | float _img_height; |
| 1522 | float _scale; |
| 1523 | bool _apply_scale; |
giuros01 | d696cb6 | 2018-11-16 10:39:59 +0000 | [diff] [blame] | 1524 | bool _correct_transform_coords; |
giuros01 | c04a0e8 | 2018-10-03 12:44:35 +0100 | [diff] [blame] | 1525 | std::array<float, 4> _weights; |
| 1526 | float _bbox_xform_clip; |
| 1527 | }; |
| 1528 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1529 | /** Activation Layer Information class */ |
| 1530 | class ActivationLayerInfo |
| 1531 | { |
| 1532 | public: |
| 1533 | /** Available activation functions */ |
| 1534 | enum class ActivationFunction |
| 1535 | { |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 1536 | LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */ |
| 1537 | TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */ |
| 1538 | RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */ |
| 1539 | BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */ |
| 1540 | LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */ |
Manuel Bottini | 581c898 | 2019-02-07 10:31:57 +0000 | [diff] [blame] | 1541 | LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x) = \begin{cases} \alpha x & \quad \text{if } x \text{ < 0}\\ x & \quad \text{if } x \geq \text{ 0 } \end{cases} \f$ ) */ |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 1542 | SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */ |
Georgios Pinitas | fb0fdcd | 2019-08-22 17:10:04 +0100 | [diff] [blame] | 1543 | ELU, /**< Exponential Linear Unit ( \f$ f(x) = \begin{cases} \alpha (exp(x) - 1) & \quad \text{if } x \text{ < 0}\\ x & \quad \text{if } x \geq \text{ 0 } \end{cases} \f$ ) */ |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 1544 | ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */ |
| 1545 | SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/ |
| 1546 | SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/ |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 1547 | LINEAR, /**< Linear ( \f$ f(x)= ax + b \f$ ) */ |
| 1548 | IDENTITY /**< Identity ( \f$ f(x)= x \f$ ) */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1549 | }; |
| 1550 | |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 1551 | ActivationLayerInfo() = default; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1552 | /** Default Constructor |
| 1553 | * |
| 1554 | * @param[in] f The activation function to use. |
| 1555 | * @param[in] a (Optional) The alpha parameter used by some activation functions |
Georgios Pinitas | 64ebe5b | 2017-09-01 17:44:24 +0100 | [diff] [blame] | 1556 | * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH). |
| 1557 | * @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] | 1558 | */ |
| 1559 | ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f) |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 1560 | : _act(f), _a(a), _b(b), _enabled(true) |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1561 | { |
| 1562 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1563 | /** Get the type of activation function */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1564 | ActivationFunction activation() const |
| 1565 | { |
| 1566 | return _act; |
| 1567 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1568 | /** Get the alpha value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1569 | float a() const |
| 1570 | { |
| 1571 | return _a; |
| 1572 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1573 | /** Get the beta value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1574 | float b() const |
| 1575 | { |
| 1576 | return _b; |
| 1577 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1578 | /** Check if initialised */ |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 1579 | bool enabled() const |
| 1580 | { |
| 1581 | return _enabled; |
| 1582 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1583 | |
| 1584 | private: |
Usama Arif | 6a98a6e | 2019-05-10 17:07:27 +0100 | [diff] [blame] | 1585 | ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::IDENTITY }; |
Giorgio Arena | 1167487 | 2018-02-07 15:38:12 +0000 | [diff] [blame] | 1586 | float _a = {}; |
| 1587 | float _b = {}; |
| 1588 | bool _enabled = { false }; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1589 | }; |
| 1590 | |
| 1591 | /** Normalization Layer Information class */ |
| 1592 | class NormalizationLayerInfo |
| 1593 | { |
| 1594 | public: |
| 1595 | /** Default Constructor |
| 1596 | * |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 1597 | * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NormType::CROSS_MAP |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1598 | * @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] | 1599 | * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001. |
| 1600 | * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5. |
| 1601 | * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation. |
| 1602 | * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not. |
| 1603 | * Should be false to follow [Krichevksy 2012]. |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1604 | */ |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 1605 | NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true) |
| 1606 | : _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] | 1607 | { |
| 1608 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1609 | /** Get the normalization type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1610 | NormType type() const |
| 1611 | { |
| 1612 | return _type; |
| 1613 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1614 | /** Get the normalization size */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1615 | uint32_t norm_size() const |
| 1616 | { |
| 1617 | return _norm_size; |
| 1618 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1619 | /** Get the alpha value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1620 | float alpha() const |
| 1621 | { |
| 1622 | return _alpha; |
| 1623 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1624 | /** Get the beta value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1625 | float beta() const |
| 1626 | { |
| 1627 | return _beta; |
| 1628 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1629 | /** Get the kappa value */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1630 | float kappa() const |
| 1631 | { |
| 1632 | return _kappa; |
| 1633 | } |
Michele Di Giorgio | 9d3a831 | 2018-11-20 12:31:24 +0000 | [diff] [blame] | 1634 | /** Get the is_scaled value */ |
| 1635 | bool is_scaled() const |
| 1636 | { |
| 1637 | return _is_scaled; |
| 1638 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1639 | /** Check if normalization is cross map */ |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 1640 | bool is_cross_map() const |
| 1641 | { |
| 1642 | return _type == NormType::CROSS_MAP; |
| 1643 | } |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 1644 | /** Check if normalization is not cross map */ |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 1645 | bool is_in_map() const |
| 1646 | { |
| 1647 | return !is_cross_map(); |
| 1648 | } |
| 1649 | /** Return the scaling factor of the normalization function. |
| 1650 | * |
| 1651 | * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed, |
| 1652 | * 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] | 1653 | * |
| 1654 | * @return The normalization scaling factor. |
| 1655 | */ |
| 1656 | float scale_coeff() const |
| 1657 | { |
| 1658 | 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] | 1659 | return (_is_scaled) ? (_alpha / size) : _alpha; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1660 | } |
| 1661 | |
| 1662 | private: |
| 1663 | NormType _type; |
| 1664 | uint32_t _norm_size; |
| 1665 | float _alpha; |
| 1666 | float _beta; |
| 1667 | float _kappa; |
Georgios Pinitas | 41caa62 | 2017-11-16 14:37:08 +0000 | [diff] [blame] | 1668 | bool _is_scaled; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1669 | }; |
| 1670 | |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 1671 | /** 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] | 1672 | class WeightsInfo |
| 1673 | { |
| 1674 | public: |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1675 | /** Default constructor */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1676 | WeightsInfo() |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1677 | : _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] | 1678 | { |
| 1679 | } |
| 1680 | /** Constructor |
| 1681 | * |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1682 | * @param[in] are_reshaped True if the weights have been reshaped |
| 1683 | * @param[in] kernel_width Kernel width. |
| 1684 | * @param[in] kernel_height Kernel height. |
| 1685 | * @param[in] num_kernels Number of convolution kernels. |
| 1686 | * @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] | 1687 | */ |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1688 | WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels, bool retain_internal_weights = false) |
| 1689 | : _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] | 1690 | { |
| 1691 | } |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1692 | /** Flag which specifies if the weights tensor has been reshaped. |
| 1693 | * |
| 1694 | * @return True if the weights tensors has been reshaped |
| 1695 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1696 | bool are_reshaped() const |
| 1697 | { |
| 1698 | return _are_reshaped; |
| 1699 | }; |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 1700 | /** Return the number of convolution kernels |
| 1701 | * |
| 1702 | * @return The number of convolution kernels |
| 1703 | */ |
| 1704 | unsigned int num_kernels() const |
| 1705 | { |
| 1706 | return _num_kernels; |
| 1707 | }; |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1708 | /** Return the width and height of the kernel |
| 1709 | * |
| 1710 | * @return The width and height of the kernel |
| 1711 | */ |
| 1712 | std::pair<unsigned int, unsigned int> kernel_size() const |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1713 | { |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1714 | return std::make_pair(_kernel_width, _kernel_height); |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1715 | } |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1716 | bool retain_internal_weights() const |
| 1717 | { |
| 1718 | return _retain_internal_weights; |
| 1719 | } |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1720 | |
| 1721 | private: |
| 1722 | const bool _are_reshaped; |
Gian Marco Iodice | 4e28869 | 2017-06-27 11:41:59 +0100 | [diff] [blame] | 1723 | const unsigned int _kernel_width; |
| 1724 | const unsigned int _kernel_height; |
Gian Marco Iodice | 559d771 | 2017-08-08 08:38:09 +0100 | [diff] [blame] | 1725 | const unsigned int _num_kernels; |
Michele Di Giorgio | b62280a | 2018-05-31 17:31:05 +0100 | [diff] [blame] | 1726 | const bool _retain_internal_weights; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 1727 | }; |
| 1728 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1729 | /** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape. |
| 1730 | * |
Gian Marco Iodice | 5fc07aa | 2019-05-15 17:08:02 +0100 | [diff] [blame] | 1731 | * The matrix A can only be reshaped through @ref CLGEMMReshapeLHSMatrixKernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel |
| 1732 | * Note: Optionally just for @ref CLGEMMReshapeLHSMatrixKernel is it possible to set mult_interleave4x4_height, the multiplication factor for the height of the 4x4 interleaved block |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1733 | * |
giuros01 | 8b6b4a9 | 2018-12-18 19:01:33 +0000 | [diff] [blame] | 1734 | * The matrix B can only be reshaped through @ref CLGEMMReshapeRHSMatrixKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel |
| 1735 | * Note: Optionally just for @ref CLGEMMReshapeRHSMatrixKernel is it possible to set mult_transpose1xW_width, the multiplication factor for the width of the 1xW transposed block |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1736 | * |
| 1737 | */ |
| 1738 | class GEMMReshapeInfo final |
| 1739 | { |
| 1740 | public: |
| 1741 | /** Default constructor */ |
| 1742 | GEMMReshapeInfo() |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1743 | : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1), _depth_output_gemm3d(0), _reinterpret_input_as_3d(false), _broadcast_bias(false) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1744 | { |
| 1745 | } |
| 1746 | /** Constructor |
| 1747 | * |
| 1748 | * @param[in] m Number of matrix A rows |
| 1749 | * @param[in] n Number of matrix B columns |
| 1750 | * @param[in] k Number of matrix A columns or matrix B rows |
| 1751 | * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block |
| 1752 | * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 1753 | * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel. |
| 1754 | * If 0 the output will not be reinterpreted as 3D. Default 0 |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 1755 | * @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 |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1756 | * to perform 1x1 convolutions with the NHWC data layout) |
| 1757 | * @param[in] broadcast_bias (Optional) Broadcast the shape of the bias tensor from a vector to a matrix. |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1758 | */ |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1759 | GEMMReshapeInfo(int m, int n, int k, int mult_transpose1xW_width = 1, int mult_interleave4x4_height = 1, int depth_output_gemm3d = 0, bool reinterpret_input_as_3d = false, bool broadcast_bias = false) |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 1760 | : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height), _depth_output_gemm3d(depth_output_gemm3d), |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1761 | _reinterpret_input_as_3d(reinterpret_input_as_3d), _broadcast_bias(broadcast_bias) |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1762 | { |
| 1763 | } |
| 1764 | /** Number of matrix A rows |
| 1765 | * |
| 1766 | * @return the number of matrix A rows |
| 1767 | */ |
| 1768 | int m() const |
| 1769 | { |
| 1770 | return _m; |
| 1771 | } |
| 1772 | /** Number of matrix B columns |
| 1773 | * |
| 1774 | * @return the number of matrix B columns |
| 1775 | */ |
| 1776 | int n() const |
| 1777 | { |
| 1778 | return _n; |
| 1779 | } |
| 1780 | /** Number of matrix A columns or matrix B rows |
| 1781 | * |
| 1782 | * @return the number of matrix A columns or matrix B rows |
| 1783 | */ |
| 1784 | int k() const |
| 1785 | { |
| 1786 | return _k; |
| 1787 | } |
| 1788 | /** Multiplication factor for the width of the 1xW transposed block |
| 1789 | * |
| 1790 | * @return the multiplication factor for the width of the 1xW transposed block |
| 1791 | */ |
| 1792 | int mult_transpose1xW_width() const |
| 1793 | { |
| 1794 | return _mult_transpose1xW_width; |
| 1795 | } |
| 1796 | /** Multiplication factor for the height of the 4x4 interleaved block |
| 1797 | * |
| 1798 | * @return the multiplication factor for the height of the 4x4 interleaved block |
| 1799 | */ |
| 1800 | int mult_interleave4x4_height() const |
| 1801 | { |
| 1802 | return _mult_interleave4x4_height; |
| 1803 | } |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1804 | /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel |
| 1805 | * |
| 1806 | * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case: |
| 1807 | * m = depth_output_gemm3d * output_height |
| 1808 | * |
| 1809 | * @return the depth of the output tensor to be used with the GEMM3D kernel |
| 1810 | */ |
| 1811 | int depth_output_gemm3d() const |
| 1812 | { |
| 1813 | return _depth_output_gemm3d; |
| 1814 | } |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 1815 | /** Flag which specifies if the input tensor has to be reinterpreted as 3D |
| 1816 | * |
| 1817 | * @return True if the input tensor has to be reinterpreted as 3D tensor |
| 1818 | */ |
| 1819 | bool reinterpret_input_as_3d() const |
| 1820 | { |
| 1821 | return _reinterpret_input_as_3d; |
| 1822 | }; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1823 | /** Flag which specifies whether to broadcast the shape of the bias tensor. |
| 1824 | * |
| 1825 | * @return True if the shape of the bias tensor is to be broadcasted. |
| 1826 | */ |
| 1827 | bool broadcast_bias() const |
| 1828 | { |
| 1829 | return _broadcast_bias; |
| 1830 | }; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1831 | |
| 1832 | private: |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 1833 | const int _m; |
| 1834 | const int _n; |
| 1835 | const int _k; |
| 1836 | const int _mult_transpose1xW_width; |
| 1837 | const int _mult_interleave4x4_height; |
| 1838 | const int _depth_output_gemm3d; |
| 1839 | const bool _reinterpret_input_as_3d; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1840 | const bool _broadcast_bias; |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1841 | }; |
| 1842 | |
giuros01 | 6d10996 | 2019-01-07 17:47:19 +0000 | [diff] [blame] | 1843 | struct DepthwiseConvolutionReshapeInfo |
| 1844 | { |
| 1845 | unsigned int c0{ 1 }; /**< Number of channels processed by the depth-wise convolution */ |
| 1846 | bool transpose{ false }; /**< True if the block MxC0 (where M is the area of the filter i.e. KwxKh) has to be transposed */ |
| 1847 | }; |
| 1848 | |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1849 | /** GEMMLowp output stage type */ |
| 1850 | enum class GEMMLowpOutputStageType |
| 1851 | { |
| 1852 | NONE, /**< No quantization to uint8 */ |
| 1853 | QUANTIZE_DOWN, /**< Quantize to uint8 using an integer multiplication */ |
| 1854 | QUANTIZE_DOWN_FIXEDPOINT, /**< Quantize to uint8 using a fixed point multiplication */ |
| 1855 | QUANTIZE_DOWN_FLOAT /**< Quantize to uint8 using a floating point multiplication */ |
| 1856 | }; |
| 1857 | |
| 1858 | /** GEMMLowp output stage info */ |
| 1859 | struct GEMMLowpOutputStageInfo |
| 1860 | { |
| 1861 | GEMMLowpOutputStageType type{ GEMMLowpOutputStageType::NONE }; /**< GEMMLowp output stage type */ |
| 1862 | int gemmlowp_offset{ 0 }; /**< GEMMLowp output stage offset used for quantizing to QASYMM8 */ |
| 1863 | int gemmlowp_multiplier{ 0 }; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */ |
| 1864 | int gemmlowp_shift{ 0 }; /**< GEMMLowp output stage shift used for quantizing to uint8 */ |
| 1865 | int gemmlowp_min_bound{ 0 }; /**< GEMMLowp min value used to saturate down the output result before converting back to QASYMM8 */ |
| 1866 | int gemmlowp_max_bound{ 0 }; /**< GEMMLowp max value used to saturate down the output result before converting back to QASYMM8 */ |
| 1867 | }; |
| 1868 | |
Gian Marco Iodice | 5ba5e09 | 2018-12-06 17:13:09 +0000 | [diff] [blame] | 1869 | /** GEMM LHS (Left Hand Side) matrix information */ |
| 1870 | struct GEMMLHSMatrixInfo |
| 1871 | { |
| 1872 | unsigned int m0{ 1 }; /**< Number of rows processed by the matrix multiplication */ |
| 1873 | unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */ |
| 1874 | unsigned int v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */ |
| 1875 | bool transpose{ true }; /**< True if the (m0xk0) block has to be transposed before been stored */ |
| 1876 | bool interleave{ true }; /**< True if the v0 (m0xk0) blocks have to be interleaved in the output row */ |
| 1877 | }; |
| 1878 | |
Gian Marco Iodice | 3b0a265 | 2018-12-07 11:18:09 +0000 | [diff] [blame] | 1879 | /** GEMM RHS (Right Hand Side) matrix information */ |
| 1880 | struct GEMMRHSMatrixInfo |
| 1881 | { |
| 1882 | unsigned int n0{ 1 }; /**< Number of columns processed by the matrix multiplication */ |
| 1883 | unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */ |
| 1884 | unsigned int h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */ |
| 1885 | bool transpose{ true }; /**< True if the (k0xn0) block has to be transposed before been stored */ |
| 1886 | bool interleave{ true }; /**< True if the h0 (k0xn0) blocks have to be interleaved in the output row */ |
| 1887 | }; |
| 1888 | |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1889 | /** GEMM information class. This class stores the necessary information to compute GEMM functions |
| 1890 | * |
| 1891 | * This object also contains the information about how matrix A and matrix B have been reshaped |
| 1892 | * |
| 1893 | */ |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1894 | class GEMMInfo |
| 1895 | { |
| 1896 | public: |
| 1897 | /** Default constructor */ |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 1898 | GEMMInfo() noexcept |
| 1899 | : _is_a_reshaped(false), |
| 1900 | _is_b_reshaped(false), |
| 1901 | _reshape_b_only_on_first_run(true), |
| 1902 | _depth_output_gemm3d(0), |
| 1903 | _reinterpret_input_as_3d(false), |
| 1904 | _retain_internal_weights(false), |
| 1905 | _gemmlowp_output_stage(), |
| 1906 | _fp_mixed_precision(false), |
| 1907 | _broadcast_bias(false), |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 1908 | _pretranpose_B(true), |
| 1909 | _activation_info() |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1910 | { |
| 1911 | } |
| 1912 | /** Constructor |
| 1913 | * |
| 1914 | * @param[in] is_a_reshaped True if the matrix A has been reshaped |
| 1915 | * @param[in] is_b_reshaped True if the matrix B has been reshaped |
| 1916 | * @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] | 1917 | * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 1918 | * If 0 the output will not be reinterpreted as 3D. Default 0 |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 1919 | * @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 |
| 1920 | * to perform 1x1 convolutions with the NHWC data layout) |
Michele Di Giorgio | ba1ffe9 | 2018-08-22 14:28:30 +0100 | [diff] [blame] | 1921 | * @param[in] retain_internal_weights (Optional) Retain the weights tensor from previous run |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1922 | * @param[in] gemmlowp_output_stage (Optional) GEMMLowp Output stage info |
Vidhya Sudhan Loganathan | a25d16c | 2018-11-16 11:33:12 +0000 | [diff] [blame] | 1923 | * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy. |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 1924 | * @param[in] broadcast_bias (Optional) Broadcast the shape of the bias tensor from a vector to a matrix. |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 1925 | * @param[in] activation_info (Optional) Activation to apply after the matrix multiplication |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1926 | */ |
Gian Marco Iodice | 3139f03 | 2018-11-05 14:26:32 +0000 | [diff] [blame] | 1927 | GEMMInfo(bool is_a_reshaped, bool is_b_reshaped, bool reshape_b_only_on_first_run, int depth_output_gemm3d = 0, bool reinterpret_input_as_3d = false, bool retain_internal_weights = false, |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 1928 | GEMMLowpOutputStageInfo gemmlowp_output_stage = GEMMLowpOutputStageInfo(), bool fp_mixed_precision = false, bool broadcast_bias = false, |
| 1929 | const ActivationLayerInfo &activation_info = ActivationLayerInfo()) noexcept |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 1930 | : _is_a_reshaped(is_a_reshaped), |
| 1931 | _is_b_reshaped(is_b_reshaped), |
| 1932 | _reshape_b_only_on_first_run(reshape_b_only_on_first_run), |
| 1933 | _depth_output_gemm3d(depth_output_gemm3d), |
| 1934 | _reinterpret_input_as_3d(reinterpret_input_as_3d), |
| 1935 | _retain_internal_weights(retain_internal_weights), |
| 1936 | _gemmlowp_output_stage(gemmlowp_output_stage), |
| 1937 | _fp_mixed_precision(fp_mixed_precision), |
| 1938 | _broadcast_bias(broadcast_bias), |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 1939 | _pretranpose_B(reshape_b_only_on_first_run), |
| 1940 | _activation_info(activation_info) |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 1941 | { |
| 1942 | } |
| 1943 | /** Flag which specifies if the matrix A has been reshaped |
| 1944 | * |
| 1945 | * @return True if the matrix A has been reshaped |
| 1946 | */ |
| 1947 | bool is_a_reshaped() const |
| 1948 | { |
| 1949 | return _is_a_reshaped; |
| 1950 | }; |
| 1951 | /** Flag which specifies if the matrix B has been reshaped |
| 1952 | * |
| 1953 | * @return True if the matrix B has been reshaped |
| 1954 | */ |
| 1955 | bool is_b_reshaped() const |
| 1956 | { |
| 1957 | return _is_b_reshaped; |
| 1958 | }; |
| 1959 | /** Flag which specifies if the reshape of matrix B should executed only for the first |
| 1960 | * |
| 1961 | * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer |
| 1962 | * |
| 1963 | * @return True if the reshaped of matrix B happens only for the first run |
| 1964 | */ |
| 1965 | bool reshape_b_only_on_first_run() const |
| 1966 | { |
| 1967 | return _reshape_b_only_on_first_run; |
| 1968 | }; |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1969 | /** Depth of the output when GEMM output is reinterpreted as 3D tensor |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1970 | * |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1971 | * @return the depth of the output tensor |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1972 | */ |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1973 | int depth_output_gemm3d() const |
Gian Marco | 36a0a46 | 2018-01-12 10:21:40 +0000 | [diff] [blame] | 1974 | { |
Isabella Gottardi | 8e74f44 | 2018-03-01 16:42:00 +0000 | [diff] [blame] | 1975 | return _depth_output_gemm3d; |
| 1976 | }; |
Gian Marco Iodice | 68a3f56 | 2018-07-26 11:44:03 +0100 | [diff] [blame] | 1977 | /** Flag which specifies if the input tensor has to be reinterpreted as 3D |
| 1978 | * |
| 1979 | * @return True if the input tensor has to be reinterpreted as 3D tensor |
| 1980 | */ |
| 1981 | bool reinterpret_input_as_3d() const |
| 1982 | { |
| 1983 | return _reinterpret_input_as_3d; |
| 1984 | }; |
Michele Di Giorgio | ba1ffe9 | 2018-08-22 14:28:30 +0100 | [diff] [blame] | 1985 | /** Flag which specifies if the weights tensor has to be retained from previous run |
| 1986 | * |
| 1987 | * @return True if the weights tensor has to be retained |
| 1988 | */ |
| 1989 | bool retain_internal_weights() const |
| 1990 | { |
| 1991 | return _retain_internal_weights; |
| 1992 | }; |
Gian Marco Iodice | 4b90865 | 2018-10-18 10:21:02 +0100 | [diff] [blame] | 1993 | /** GEMMLowp output stage |
| 1994 | * |
| 1995 | * @return the GEMMLowp output stage info |
| 1996 | */ |
| 1997 | GEMMLowpOutputStageInfo gemmlowp_output_stage() const |
| 1998 | { |
| 1999 | return _gemmlowp_output_stage; |
| 2000 | }; |
Vidhya Sudhan Loganathan | a25d16c | 2018-11-16 11:33:12 +0000 | [diff] [blame] | 2001 | /** Flag which specifies if a wider accumulator should be used. |
| 2002 | * |
| 2003 | * @return True if a wider accumulator has to be used |
| 2004 | */ |
| 2005 | bool fp_mixed_precision() const |
| 2006 | { |
| 2007 | return _fp_mixed_precision; |
| 2008 | }; |
Georgios Pinitas | b0f342e | 2019-05-21 13:32:43 +0100 | [diff] [blame] | 2009 | /** Flag which specifies whether to broadcast the shape of the bias tensor. |
| 2010 | * |
| 2011 | * @return True if the shape of the bias tensor is to be broadcasted. |
| 2012 | */ |
| 2013 | bool broadcast_bias() const |
| 2014 | { |
| 2015 | return _broadcast_bias; |
| 2016 | }; |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 2017 | /** Flag which specifies whether b should be pre-transposed if supported. |
| 2018 | * |
| 2019 | * @return True if b should be pre-transposed else false. |
| 2020 | */ |
| 2021 | bool pretranpose_B() const |
| 2022 | { |
| 2023 | return _pretranpose_B; |
| 2024 | }; |
| 2025 | /** Set pre-transpose b flag |
| 2026 | * |
| 2027 | * @param[in] flag Flag to set |
| 2028 | */ |
| 2029 | void set_pretranpose_B(bool flag) |
| 2030 | { |
| 2031 | _pretranpose_B = flag; |
| 2032 | } |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 2033 | /** Activation layer to apply after the matrix multiplication |
| 2034 | * |
| 2035 | * @return ActivationLayerInfo object |
| 2036 | */ |
| 2037 | ActivationLayerInfo activation_info() const |
| 2038 | { |
| 2039 | return _activation_info; |
| 2040 | } |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 2041 | |
| 2042 | private: |
Georgios Pinitas | 37d080f | 2019-06-21 18:43:12 +0100 | [diff] [blame] | 2043 | bool _is_a_reshaped; |
| 2044 | bool _is_b_reshaped; |
| 2045 | bool _reshape_b_only_on_first_run; |
| 2046 | int _depth_output_gemm3d; |
| 2047 | bool _reinterpret_input_as_3d; |
| 2048 | bool _retain_internal_weights; |
| 2049 | GEMMLowpOutputStageInfo _gemmlowp_output_stage; |
| 2050 | bool _fp_mixed_precision; |
| 2051 | bool _broadcast_bias; |
| 2052 | bool _pretranpose_B; |
Gian Marco Iodice | f3622be | 2019-07-29 14:27:16 +0100 | [diff] [blame] | 2053 | ActivationLayerInfo _activation_info; |
Chunosov | 5124be5 | 2017-11-22 20:42:13 +0700 | [diff] [blame] | 2054 | }; |
| 2055 | |
Gian Marco Iodice | 247f52c | 2018-03-22 11:24:56 +0000 | [diff] [blame] | 2056 | /** Winograd information */ |
| 2057 | struct WinogradInfo |
| 2058 | { |
| 2059 | /** Default constructor |
| 2060 | * |
| 2061 | * @param[in] output_tile_sz Width and height of the output tile |
| 2062 | * @param[in] kernel_sz Width and height of the kernel |
| 2063 | * @param[in] input_dims Width and height of the input tensor before the convolution is applied |
| 2064 | * @param[in] conv_info Convolution info (Pads, strides) |
| 2065 | * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied |
| 2066 | */ |
| 2067 | WinogradInfo(Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout) |
| 2068 | : output_tile_size(output_tile_sz), kernel_size(kernel_sz), input_dimensions(input_dims), convolution_info(conv_info), output_data_layout(data_layout) |
| 2069 | { |
| 2070 | } |
| 2071 | |
| 2072 | Size2D output_tile_size{}; /**< Width and height of the output tile */ |
| 2073 | Size2D kernel_size{}; /**< Width and height of the kernel*/ |
| 2074 | Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */ |
| 2075 | PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */ |
| 2076 | DataLayout output_data_layout{ DataLayout::NCHW }; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */ |
| 2077 | }; |
| 2078 | |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 2079 | /** IO formatting information class*/ |
| 2080 | struct IOFormatInfo |
| 2081 | { |
| 2082 | /** Precision type used when printing floating point numbers */ |
| 2083 | enum class PrecisionType |
| 2084 | { |
| 2085 | Default, /**< Default precision to the one that the current stream has */ |
| 2086 | Custom, /**< Custom precision specified by the user using the precision parameter */ |
| 2087 | Full /**< The maximum precision of the floating point representation */ |
| 2088 | }; |
| 2089 | |
| 2090 | /** Specifies the area to be printed, used by Tensor objects */ |
| 2091 | enum class PrintRegion |
| 2092 | { |
| 2093 | ValidRegion, /**< Prints the valid region of the Tensor object */ |
| 2094 | NoPadding, /**< Prints the Tensor object without the padding */ |
| 2095 | Full /**< Print the tensor object including padding */ |
| 2096 | }; |
| 2097 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2098 | /** Construct a set of IO formatting information. |
| 2099 | * |
| 2100 | * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion. |
| 2101 | * @param[in] precision_type Precision type for floating point numbers. Default: stream default. |
| 2102 | * @param[in] precision Precision value for float point numbers. Default: 10. |
| 2103 | * @param[in] align_columns Whether to align columns when printed. Default: true. |
| 2104 | * @param[in] element_delim Delimeter between elements. Default: " ". |
| 2105 | * @param[in] row_delim Delimenter between rows. Default: "\n". |
| 2106 | */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 2107 | IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion, |
| 2108 | PrecisionType precision_type = PrecisionType::Default, |
| 2109 | unsigned int precision = 10, |
| 2110 | bool align_columns = true, |
| 2111 | std::string element_delim = " ", |
| 2112 | std::string row_delim = "\n") |
| 2113 | : print_region(print_region), |
| 2114 | precision_type(precision_type), |
| 2115 | precision(precision), |
| 2116 | element_delim(element_delim), |
| 2117 | row_delim(row_delim), |
| 2118 | align_columns(align_columns) |
| 2119 | { |
| 2120 | } |
| 2121 | |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2122 | /** Area to be printed by Tensor objects */ |
| 2123 | PrintRegion print_region; |
| 2124 | /** Floating point precision type */ |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 2125 | PrecisionType precision_type; |
Alex Gilday | c357c47 | 2018-03-21 13:54:09 +0000 | [diff] [blame] | 2126 | /** Floating point precision */ |
| 2127 | unsigned int precision; |
| 2128 | /** Element delimeter */ |
| 2129 | std::string element_delim; |
| 2130 | /** Row delimeter */ |
| 2131 | std::string row_delim; |
| 2132 | /** Align columns */ |
| 2133 | bool align_columns; |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 2134 | }; |
Georgios Pinitas | d8734b5 | 2017-12-22 15:27:52 +0000 | [diff] [blame] | 2135 | } // namespace arm_compute |
Anthony Barbier | 6ff3b19 | 2017-09-04 18:44:23 +0100 | [diff] [blame] | 2136 | #endif /* __ARM_COMPUTE_TYPES_H__ */ |