blob: 42e42cc2d6a5afaa48bb5bc81012eace533ec60a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Michele Di Giorgiod9eaf612020-07-08 11:12:57 +01002 * Copyright (c) 2016-2020 Arm Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
Michalis Spyrouf4643372019-11-29 16:17:13 +000024#ifndef ARM_COMPUTE_TYPES_H
25#define ARM_COMPUTE_TYPES_H
Anthony Barbier6ff3b192017-09-04 18:44:23 +010026
27#include "arm_compute/core/Coordinates.h"
Georgios Pinitas4c5469b2019-05-21 13:32:43 +010028#include "arm_compute/core/QuantizationInfo.h"
Isabella Gottardi6e464c32018-01-26 12:32:45 +000029#include "arm_compute/core/Size2D.h"
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000030#include "arm_compute/core/Strides.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010031#include "arm_compute/core/TensorShape.h"
Sang-Hoon Park11fedda2020-01-15 14:44:04 +000032#include "arm_compute/core/utils/misc/Macros.h"
Georgios Pinitase8291ac2020-02-26 09:58:13 +000033#include "support/Bfloat16.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010034#include "support/Half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010035
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000036#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010037#include <cstddef>
38#include <cstdint>
Isabella Gottardia7acb3c2019-01-08 13:48:44 +000039#include <map>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010040#include <string>
41#include <utility>
42
43namespace arm_compute
44{
Georgios Pinitas583137c2017-08-31 18:12:42 +010045/** 16-bit floating point type */
46using half = half_float::half;
47
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000048/** Permutation vector */
49using PermutationVector = Strides;
Georgios Pinitas77589b52018-08-21 14:41:35 +010050/** Bidirectional strides */
51using BiStrides = Coordinates;
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000052
Anthony Barbier6ff3b192017-09-04 18:44:23 +010053/** Image colour formats */
54enum class Format
55{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070056 UNKNOWN, /**< Unknown image format */
57 U8, /**< 1 channel, 1 U8 per channel */
58 S16, /**< 1 channel, 1 S16 per channel */
59 U16, /**< 1 channel, 1 U16 per channel */
60 S32, /**< 1 channel, 1 S32 per channel */
61 U32, /**< 1 channel, 1 U32 per channel */
Georgios Pinitase8291ac2020-02-26 09:58:13 +000062 BFLOAT16, /**< 16-bit brain floating-point number */
Daniil Efremov02bf80d2017-11-22 00:26:51 +070063 F16, /**< 1 channel, 1 F16 per channel */
64 F32, /**< 1 channel, 1 F32 per channel */
65 UV88, /**< 2 channel, 1 U8 per channel */
66 RGB888, /**< 3 channels, 1 U8 per channel */
67 RGBA8888, /**< 4 channels, 1 U8 per channel */
68 YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */
69 YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */
70 NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */
71 NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */
72 IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */
73 UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010074};
75
76/** Available data types */
77enum class DataType
78{
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000079 UNKNOWN, /**< Unknown data type */
80 U8, /**< unsigned 8-bit number */
81 S8, /**< signed 8-bit number */
82 QSYMM8, /**< quantized, symmetric fixed-point 8-bit number */
83 QASYMM8, /**< quantized, asymmetric fixed-point 8-bit number unsigned */
84 QASYMM8_SIGNED, /**< quantized, asymmetric fixed-point 8-bit number signed */
85 QSYMM8_PER_CHANNEL, /**< quantized, symmetric per channel fixed-point 8-bit number */
86 U16, /**< unsigned 16-bit number */
87 S16, /**< signed 16-bit number */
88 QSYMM16, /**< quantized, symmetric fixed-point 16-bit number */
89 QASYMM16, /**< quantized, asymmetric fixed-point 16-bit number */
90 U32, /**< unsigned 32-bit number */
91 S32, /**< signed 32-bit number */
92 U64, /**< unsigned 64-bit number */
93 S64, /**< signed 64-bit number */
Georgios Pinitase8291ac2020-02-26 09:58:13 +000094 BFLOAT16, /**< 16-bit brain floating-point number */
Georgios Pinitas8217c8e2019-11-11 18:24:22 +000095 F16, /**< 16-bit floating-point number */
96 F32, /**< 32-bit floating-point number */
97 F64, /**< 64-bit floating-point number */
98 SIZET /**< size_t */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099};
100
Daniil Efremov02bf80d2017-11-22 00:26:51 +0700101/** Available Sampling Policies */
102enum class SamplingPolicy
103{
104 CENTER, /**< Samples are taken at pixel center */
105 TOP_LEFT /**< Samples are taken at pixel top left corner */
106};
107
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100108/** Constant value of the border pixels when using BorderMode::CONSTANT */
109constexpr uint8_t CONSTANT_BORDER_VALUE = 199;
110
Alex Gildayc357c472018-03-21 13:54:09 +0000111/** Constant value used to indicate a half-scale pyramid */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100112constexpr float SCALE_PYRAMID_HALF = 0.5f;
113
Alex Gildayc357c472018-03-21 13:54:09 +0000114/** Constant value used to indicate a ORB scaled pyramid */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100115constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01;
116
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000117/** [DataLayout enum definition] **/
118
Georgios Pinitas4074c992018-01-30 18:13:46 +0000119/** Supported tensor data layouts */
120enum class DataLayout
121{
Alex Gildayc357c472018-03-21 13:54:09 +0000122 UNKNOWN, /**< Unknown data layout */
123 NCHW, /**< Num samples, channels, height, width */
124 NHWC /**< Num samples, height, width, channels */
Georgios Pinitas4074c992018-01-30 18:13:46 +0000125};
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000126/** [DataLayout enum definition] **/
Georgios Pinitas4074c992018-01-30 18:13:46 +0000127
Isabella Gottardid17a6772018-02-27 17:41:55 +0000128/** Supported tensor data layout dimensions */
129enum class DataLayoutDimension
130{
Alex Gildayc357c472018-03-21 13:54:09 +0000131 CHANNEL, /**< channel */
132 HEIGHT, /**< height */
133 WIDTH, /**< width */
134 BATCHES /**< batches */
Isabella Gottardid17a6772018-02-27 17:41:55 +0000135};
136
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000137/** Available ConvolutionMethod*/
138enum class ConvolutionMethod
139{
Vidhya Sudhan Loganathan8ec0bb62019-04-23 10:40:44 +0100140 GEMM, /**< Convolution using GEMM */
141 DIRECT, /**< Direct convolution */
142 WINOGRAD, /**< Convolution using Winograd */
143 FFT /**< Convolution using FFT */
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000144};
145
Manuel Bottini05069f02019-09-26 17:18:26 +0100146/** Available DepthwiseConvolutionFunction*/
147enum class DepthwiseConvolutionFunction
148{
149 OPTIMIZED, /**< Optimized Depthwise Convolution */
150 GENERIC, /**< Generic Depthwise Convolution */
151};
152
giuros0146a49a02019-04-01 13:50:22 +0100153/** Available DeconvolutionMethod*/
154enum class DeconvolutionMethod
155{
156 GEMM, /**< Deconvolution using GEMM */
157 DIRECT, /**< Direct deconvolution */
158};
159
Manuel Bottini2732cca2019-05-28 11:44:41 +0100160/** Available FuseBatchNormalizationType*/
161enum class FuseBatchNormalizationType
162{
163 CONVOLUTION, /**< For Convolution weights */
164 DEPTHWISECONVOLUTION /**< For Depthwise Convolution weights*/
165};
166
Usama Arif89890c62019-03-19 10:57:05 +0000167/** Padding mode to use for PadLayer */
168enum class PaddingMode
169{
170 CONSTANT,
171 REFLECT,
172 SYMMETRIC
173};
174
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000175/** Supported comparison operations */
176enum class ComparisonOperation
177{
178 Equal, /**< Equal comparison ( \f$ x == y \f$ ) */
179 NotEqual, /**< NotEqual comparison ( \f$ x != y \f$ ) */
180 Greater, /**< Greater comparison ( \f$ x > y \f$ ) */
181 GreaterEqual, /**< Greater equal comparison ( \f$ x >= y \f$ ) */
182 Less, /**< Less comparison ( \f$ x < y \f$ ) */
183 LessEqual /**< Less equal comparison ( \f$ x <= y \f$ ) */
184};
185
Alex Gildayc357c472018-03-21 13:54:09 +0000186/** Container for valid region of a window */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100187struct ValidRegion
188{
Alex Gildayc357c472018-03-21 13:54:09 +0000189 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100190 ValidRegion()
191 : anchor{}, shape{}
192 {
193 }
194
Alex Gildayc357c472018-03-21 13:54:09 +0000195 /** Allow instances of this class to be copy constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100196 ValidRegion(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000197 /** Allow instances of this class to be move constructed */
198 ValidRegion(ValidRegion &&) = default;
199 /** Allow instances of this class to be copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100200 ValidRegion &operator=(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000201 /** Allow instances of this class to be moved */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100202 ValidRegion &operator=(ValidRegion &&) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000203 /** Default destructor */
204 ~ValidRegion() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100205
Alex Gildayc357c472018-03-21 13:54:09 +0000206 /** Constructor for a valid region with default number of dimensions
207 *
208 * @param[in] an_anchor Anchor for the start of the valid region.
209 * @param[in] a_shape Shape of the valid region.
210 *
211 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000212 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape)
213 : anchor{ an_anchor }, shape{ a_shape }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100214 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000215 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
216 }
217
Alex Gildayc357c472018-03-21 13:54:09 +0000218 /** Constructor for a valid region with specified number of dimensions
219 *
220 * @param[in] an_anchor Anchor for the start of the valid region.
221 * @param[in] a_shape Shape of the valid region.
222 * @param[in] num_dimensions Number of dimensions (must be >= number of dimensions of anchor and shape).
223 *
224 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000225 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
226 : anchor{ an_anchor }, shape{ a_shape }
227 {
228 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
229 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100230 }
231
232 /** Return the start of the valid region for the given dimension @p d */
233 int start(unsigned int d) const
234 {
235 return anchor[d];
236 }
237
238 /** Return the end of the valid region for the given dimension @p d */
239 int end(unsigned int d) const
240 {
241 return anchor[d] + shape[d];
242 }
243
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000244 /** Accessor to set the value of anchor and shape for one of the dimensions.
245 *
246 * @param[in] dimension Dimension for which the value is set.
247 * @param[in] start Value to be set in anchor for the dimension.
248 * @param[in] size Value to be set in shape for the dimension.
249 *
250 * @return *this.
251 */
252 ValidRegion &set(size_t dimension, int start, size_t size)
253 {
254 anchor.set(dimension, start);
255 shape.set(dimension, size);
256 return *this;
257 }
258
Alex Gildayc357c472018-03-21 13:54:09 +0000259 Coordinates anchor; /**< Anchor for the start of the valid region. */
260 TensorShape shape; /**< Shape of the valid region. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100261};
262
263/** Methods available to handle borders */
264enum class BorderMode
265{
266 UNDEFINED, /**< Borders are left undefined */
267 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
268 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
269};
270
271/** Container for 2D border size */
272struct BorderSize
273{
274 /** Empty border, i.e. no border */
275 constexpr BorderSize()
276 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
277 {
278 }
279
280 /** Border with equal size around the 2D plane */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100281 explicit constexpr BorderSize(unsigned int size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100282 : top{ size }, right{ size }, bottom{ size }, left{ size }
283 {
284 }
285
286 /** Border with same size for top/bottom and left/right */
287 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
288 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
289 {
290 }
291
292 /** Border with different sizes */
293 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
294 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
295 {
296 }
297
298 /** Check if the entire border is zero */
299 constexpr bool empty() const
300 {
301 return top == 0 && right == 0 && bottom == 0 && left == 0;
302 }
303
304 /** Check if the border is the same size on all sides */
305 constexpr bool uniform() const
306 {
307 return top == right && top == bottom && top == left;
308 }
309
Alex Gildayc357c472018-03-21 13:54:09 +0000310 /** Scale this border size.
311 *
312 * @param[in] scale Scale to multiply border size by.
313 *
314 * @return *this.
315 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100316 BorderSize &operator*=(float scale)
317 {
318 top *= scale;
319 right *= scale;
320 bottom *= scale;
321 left *= scale;
322
323 return *this;
324 }
325
Alex Gildayc357c472018-03-21 13:54:09 +0000326 /** Scale a copy of this border size.
327 *
328 * @param[in] scale Scale to multiply border size by.
329 *
330 * @return a scaled copy of this.
331 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100332 BorderSize operator*(float scale)
333 {
334 BorderSize size = *this;
335 size *= scale;
336
337 return size;
338 }
339
Giorgio Arena1e2af2a2020-10-15 17:39:41 +0100340 /** Check equality with another BorderSize struct
341 *
342 * @param[in] rhs other struct to check against
343 *
344 * @return true if they are equal
345 */
346 bool operator==(const BorderSize &rhs)
347 {
348 return (top == rhs.top) && (right == rhs.right) && (bottom == rhs.bottom) && (left == rhs.left);
349 }
350
351 /** Check non-equality with another BorderSize struct
352 *
353 * @param[in] rhs other struct to check against
354 *
355 * @return true if they are different
356 */
357 bool operator!=(const BorderSize &rhs)
358 {
359 return !(*this == rhs);
360 }
361
Alex Gildayc357c472018-03-21 13:54:09 +0000362 /** Limit this border size.
363 *
364 * @param[in] limit Border size to limit this border size to.
365 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100366 void limit(const BorderSize &limit)
367 {
368 top = std::min(top, limit.top);
369 right = std::min(right, limit.right);
370 bottom = std::min(bottom, limit.bottom);
371 left = std::min(left, limit.left);
372 }
373
Alex Gildayc357c472018-03-21 13:54:09 +0000374 unsigned int top; /**< top of the border */
375 unsigned int right; /**< right of the border */
376 unsigned int bottom; /**< bottom of the border */
377 unsigned int left; /**< left of the border */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100378};
379
Alex Gildayc357c472018-03-21 13:54:09 +0000380/** Container for 2D padding size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100381using PaddingSize = BorderSize;
382
383/** Policy to handle overflow */
384enum class ConvertPolicy
385{
386 WRAP, /**< Wrap around */
387 SATURATE /**< Saturate */
388};
389
390/** Interpolation method */
391enum class InterpolationPolicy
392{
393 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
394 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
395 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 */
396};
397
398/** Bilinear Interpolation method used by LKTracker */
399enum class BilinearInterpolation
400{
Alex Gildayc357c472018-03-21 13:54:09 +0000401 BILINEAR_OLD_NEW, /**< Old-new method */
402 BILINEAR_SCHARR /**< Scharr method */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100403};
404
405/** Threshold mode */
406enum class ThresholdType
407{
408 BINARY, /**< Threshold with one value */
409 RANGE /**< Threshold with two values*/
410};
411
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100412/** Termination criteria */
413enum class Termination
414{
Alex Gildayc357c472018-03-21 13:54:09 +0000415 TERM_CRITERIA_EPSILON, /**< Terminate when within epsilon of a threshold */
416 TERM_CRITERIA_ITERATIONS, /**< Terminate after a maximum number of iterations */
417 TERM_CRITERIA_BOTH /**< Terminate on whichever of the other conditions occurs first */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100418};
419
420/** Magnitude calculation type. */
421enum class MagnitudeType
422{
423 L1NORM, /**< L1 normalization type */
424 L2NORM /**< L2 normalization type */
425};
426
427/** Phase calculation type.
428 *
429 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
430 */
431enum class PhaseType
432{
433 SIGNED, /**< Angle range: [0, 360] */
434 UNSIGNED /**< Angle range: [0, 180] */
435};
436
437/** Keypoint type */
438struct KeyPoint
439{
440 int32_t x{ 0 }; /**< X coordinates */
441 int32_t y{ 0 }; /**< Y coordinates */
442 float strength{ 0.f }; /**< Strength of the point */
443 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
444 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
445 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
446 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
447};
448
Alex Gildayc357c472018-03-21 13:54:09 +0000449/** Internal key point */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100450using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
451
452/** Rectangle type */
453struct Rectangle
454{
455 uint16_t x; /**< Top-left x coordinate */
456 uint16_t y; /**< Top-left y coordinate */
457 uint16_t width; /**< Width of the rectangle */
458 uint16_t height; /**< Height of the rectangle */
459};
460
461/** Coordinate type */
462struct Coordinates2D
463{
464 int32_t x; /**< X coordinates */
465 int32_t y; /**< Y coordinates */
466};
467
468/** Coordinate type */
469struct Coordinates3D
470{
471 uint32_t x; /**< X coordinates */
472 uint32_t y; /**< Y coordinates */
473 uint32_t z; /**< Z coordinates */
474};
475
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100476/** Padding information as a pair of unsigned int start/end */
477using PaddingInfo = std::pair<uint32_t, uint32_t>;
478
479/** List of padding information */
480using PaddingList = std::vector<PaddingInfo>;
481
giuros013175fcf2018-11-21 09:59:17 +0000482/** Information to produce a tiled version of a Tensor */
483using Multiples = std::vector<uint32_t>;
484
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100485/** Available channels */
486enum class Channel
487{
488 UNKNOWN, /** Unknown channel format */
489 C0, /**< First channel (used by formats with unknown channel types). */
490 C1, /**< Second channel (used by formats with unknown channel types). */
491 C2, /**< Third channel (used by formats with unknown channel types). */
492 C3, /**< Fourth channel (used by formats with unknown channel types). */
493 R, /**< Red channel. */
494 G, /**< Green channel. */
495 B, /**< Blue channel. */
496 A, /**< Alpha channel. */
497 Y, /**< Luma channel. */
498 U, /**< Cb/U channel. */
499 V /**< Cr/V/Value channel. */
500};
501
502/** Available matrix patterns */
503enum class MatrixPattern
504{
505 BOX, /**< Box pattern matrix. */
506 CROSS, /**< Cross pattern matrix. */
507 DISK, /**< Disk pattern matrix. */
508 OTHER /**< Any other matrix pattern. */
509};
510
511/** Available non linear functions. */
512enum class NonLinearFilterFunction : unsigned
513{
514 MEDIAN = 0, /**< Non linear median filter. */
515 MIN = 1, /**< Non linear erode. */
516 MAX = 2, /**< Non linear dilate. */
517};
518
Georgios Pinitasd9769582017-08-03 10:19:40 +0100519/** Available reduction operations */
520enum class ReductionOperation
521{
Michalis Spyrou7930db42018-11-22 17:36:28 +0000522 ARG_IDX_MAX, /**< Index of the max value */
Manuel Bottinib412fab2018-12-10 17:40:23 +0000523 ARG_IDX_MIN, /**< Index of the min value */
524 MEAN_SUM, /**< Mean of sum */
525 PROD, /**< Product */
526 SUM_SQUARE, /**< Sum of squares */
Usama Arifa4a08ad2019-05-20 12:38:33 +0100527 SUM, /**< Sum */
528 MIN, /**< Min */
Usama Arif28f0dd92019-05-20 13:44:34 +0100529 MAX, /**< Max */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100530};
531
giuros01164a2722018-11-20 18:34:46 +0000532/** Available element-wise operations */
533enum class ArithmeticOperation
534{
535 ADD, /**< (x + y) */
536 SUB, /**< (x - y) */
537 DIV, /**< (x / y) */
538 MIN, /**< Min(x, y) */
539 MAX, /**< Max(x, y) */
540 SQUARED_DIFF, /**< (x - y)^2 */
Usama Arif81e671e2019-05-13 13:33:14 +0100541 POWER, /**< x ^ y */
giuros011e6e1b82019-05-14 16:12:53 +0100542 PRELU, /**< y*x if x < 0, x otherwise */
giuros01164a2722018-11-20 18:34:46 +0000543};
544
Michalis Spyroue9362622018-11-23 17:41:37 +0000545/** Available element wise unary operations */
546enum class ElementWiseUnary
547{
548 RSQRT, /**< Reverse square root */
549 EXP, /**< Exponential */
Usama Ariff6e475c2019-05-10 12:06:28 +0100550 NEG, /**< Negate */
Usama Arifc255aa72019-05-13 16:26:29 +0100551 LOG, /**< Natural Logarithm */
Manuel Bottini6ac59922019-05-15 14:06:02 +0100552 ABS, /**< Absolute value */
Michalis Spyrou0af44182019-05-17 14:04:47 +0100553 SIN, /**< Sine */
Usama Arif0a5a57a2019-05-23 14:20:33 +0100554 ROUND, /**< Round */
Michalis Spyroue9362622018-11-23 17:41:37 +0000555};
556
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100557/** The normalization type used for the normalization layer */
558enum class NormType
559{
560 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
561 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
562 CROSS_MAP /**< Normalization applied cross maps */
563};
564
565/** Normalization type for Histogram of Oriented Gradients (HOG) */
566enum class HOGNormType
567{
568 L2_NORM = 1, /**< L2-norm */
569 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
570 L1_NORM = 3 /**< L1 norm */
571};
572
573/** Detection window used for the object detection. The detection window keeps the following information:
574 *
575 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
576 * -# Index of the class used for evaluating which class the detection window belongs to
577 * -# Confidence value (score) obtained with the classifier
578 */
579struct DetectionWindow
580{
581 uint16_t x{ 0 }; /**< Top-left x coordinate */
582 uint16_t y{ 0 }; /**< Top-left y coordinate */
583 uint16_t width{ 0 }; /**< Width of the detection window */
584 uint16_t height{ 0 }; /**< Height of the detection window */
585 uint16_t idx_class{ 0 }; /**< Index of the class */
586 float score{ 0.f }; /**< Confidence value for the detection window */
587};
588
589/** Dimension rounding type when down-scaling on CNNs
590 * @note Used in pooling and convolution layer
591 */
592enum class DimensionRoundingType
593{
594 FLOOR, /**< Floor rounding */
595 CEIL /**< Ceil rounding */
596};
597
598/** Available pooling types */
599enum class PoolingType
600{
601 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100602 AVG, /**< Average Pooling */
603 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100604};
605
Michalis Spyrou2709d612018-09-19 09:46:47 +0100606/** Available non maxima suppression types */
607enum class NMSType
608{
609 LINEAR, /**< Linear NMS */
610 GAUSSIAN, /**< Gaussian NMS */
611 ORIGINAL /**< Original NMS */
612};
613
614/** BoxWithNonMaximaSuppressionLimit Information class */
615class BoxNMSLimitInfo final
616{
617public:
618 /** Constructor
619 *
620 * @param[in] score_thresh (Optional) Score threshold.
621 * @param[in] nms (Optional) NMS value
622 * @param[in] detections (Optional) Number of detections
623 * @param[in] soft_nms_enabled (Optional) Enable SoftNMS
624 * @param[in] soft_nms_method (Optional) Soft NMS method
625 * @param[in] soft_nms_sigma (Optional) Soft NMS sigma value
626 * @param[in] soft_nms_min_score_thres (Optional) Soft NMS minimum score threshold
Manuel Bottini5209be52019-02-13 16:34:56 +0000627 * @param[in] suppress_size (Optional) Filter out boxes based on their size. Defaults to false
628 * @param[in] min_size (Optional) Smaller boxes than min_size will be filtered out. Defaults to 1
629 * @param[in] im_width (Optional) Boxes whose centers (on the x axis) is beyond im_width will be filtered. Defaults to 1
630 * @param[in] im_height (Optional) Boxes whose centers (on the y axis) is beyond im_height will be filtered. Defaults to 1
Michalis Spyrou2709d612018-09-19 09:46:47 +0100631 */
632 BoxNMSLimitInfo(float score_thresh = 0.05f, float nms = 0.3f,
633 int detections = 100, bool soft_nms_enabled = false,
634 NMSType soft_nms_method = NMSType::LINEAR,
Manuel Bottini5209be52019-02-13 16:34:56 +0000635 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 Spyrou2709d612018-09-19 09:46:47 +0100636 : _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 Bottini5209be52019-02-13 16:34:56 +0000637 _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 Spyrou2709d612018-09-19 09:46:47 +0100638 {
639 }
640 /** Get the score threshold */
641 float score_thresh() const
642 {
643 return _score_thresh;
644 }
645 /** Get the NMS */
646 float nms() const
647 {
648 return _nms;
649 }
650 /** Get the number of detections */
651 int detections_per_im() const
652 {
653 return _detections_per_im;
654 }
655 /** Check if soft NMS is enabled */
656 bool soft_nms_enabled() const
657 {
658 return _soft_nms_enabled;
659 }
660 /** Get soft NMS method */
661 NMSType soft_nms_method() const
662 {
663 return _soft_nms_method;
664 }
665 /** Get soft NMS sigma */
666 float soft_nms_sigma() const
667 {
668 return _soft_nms_sigma;
669 }
670 /** Get soft nms min score threshold */
671 float soft_nms_min_score_thres() const
672 {
673 return _soft_nms_min_score_thres;
674 }
Manuel Bottini5209be52019-02-13 16:34:56 +0000675 /** Get if NMS will suppress boxes based on their size/position */
676 bool suppress_size() const
677 {
678 return _suppress_size;
679 }
680 /** Get size suppression threshold */
681 float min_size() const
682 {
683 return _min_size;
684 }
685 /** Get image width (NMS may suppress boxes whose center sits beyond the image width) */
686 float im_width() const
687 {
688 return _im_width;
689 }
690 /** Get image height (NMS may suppress boxes whose center sits beyond the image height) */
691 float im_height() const
692 {
693 return _im_height;
694 }
Michalis Spyrou2709d612018-09-19 09:46:47 +0100695
696private:
697 float _score_thresh;
698 float _nms;
699 int _detections_per_im;
700 bool _soft_nms_enabled;
701 NMSType _soft_nms_method;
702 float _soft_nms_sigma;
703 float _soft_nms_min_score_thres;
Manuel Bottini5209be52019-02-13 16:34:56 +0000704 bool _suppress_size;
705 float _min_size;
706 float _im_width;
707 float _im_height;
Michalis Spyrou2709d612018-09-19 09:46:47 +0100708};
709
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100710/** Padding and stride information class */
711class PadStrideInfo
712{
713public:
714 /** Constructor
715 *
716 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
717 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
718 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
719 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
720 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
721 */
722 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
723 unsigned int pad_x = 0, unsigned int pad_y = 0,
724 DimensionRoundingType round = DimensionRoundingType::FLOOR)
725 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100726 _pad_left(pad_x),
727 _pad_top(pad_y),
728 _pad_right(pad_x),
729 _pad_bottom(pad_y),
730 _round_type(round)
731 {
732 }
733 /** Constructor
734 *
735 * @param[in] stride_x Stride, in elements, across x.
736 * @param[in] stride_y Stride, in elements, across y.
737 * @param[in] pad_left Padding across x on the left, in elements.
738 * @param[in] pad_top Padding across y on the top, in elements.
739 * @param[in] pad_right Padding across x on the right, in elements.
740 * @param[in] pad_bottom Padding across y on the bottom, in elements.
741 * @param[in] round Dimensions rounding.
742 */
743 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
744 unsigned int pad_left, unsigned int pad_right,
745 unsigned int pad_top, unsigned int pad_bottom,
746 DimensionRoundingType round)
747 : _stride(std::make_pair(stride_x, stride_y)),
748 _pad_left(pad_left),
749 _pad_top(pad_top),
750 _pad_right(pad_right),
751 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100752 _round_type(round)
753 {
754 }
Alex Gildayc357c472018-03-21 13:54:09 +0000755 /** Get the stride.
756 *
757 * @return a pair: stride x, stride y.
758 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100759 std::pair<unsigned int, unsigned int> stride() const
760 {
761 return _stride;
762 }
Alex Gildayc357c472018-03-21 13:54:09 +0000763 /** Check whether the padding is symmetric.
764 *
765 * @return True if the padding is symmetric.
766 */
Anthony Barbier21f67d62018-02-16 15:17:48 +0000767 bool padding_is_symmetric() const
768 {
769 return (_pad_left == _pad_right) && (_pad_top == _pad_bottom);
770 }
Alex Gildayc357c472018-03-21 13:54:09 +0000771 /** Get the padding.
772 *
773 * @note This should only be used when the padding is symmetric.
774 *
775 * @return a pair: padding left/right, padding top/bottom
776 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100777 std::pair<unsigned int, unsigned int> pad() const
778 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100779 //this accessor should be used only when padding is symmetric
Anthony Barbier21f67d62018-02-16 15:17:48 +0000780 ARM_COMPUTE_ERROR_ON(!padding_is_symmetric());
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100781 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100782 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100783
Alex Gildayc357c472018-03-21 13:54:09 +0000784 /** Get the left padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100785 unsigned int pad_left() const
786 {
787 return _pad_left;
788 }
Alex Gildayc357c472018-03-21 13:54:09 +0000789 /** Get the right padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100790 unsigned int pad_right() const
791 {
792 return _pad_right;
793 }
Alex Gildayc357c472018-03-21 13:54:09 +0000794 /** Get the top padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100795 unsigned int pad_top() const
796 {
797 return _pad_top;
798 }
Alex Gildayc357c472018-03-21 13:54:09 +0000799 /** Get the bottom padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100800 unsigned int pad_bottom() const
801 {
802 return _pad_bottom;
803 }
804
Alex Gildayc357c472018-03-21 13:54:09 +0000805 /** Get the rounding type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100806 DimensionRoundingType round() const
807 {
808 return _round_type;
809 }
810
Alex Gildayc357c472018-03-21 13:54:09 +0000811 /** Check whether this has any padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100812 bool has_padding() const
813 {
814 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
815 }
816
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100817private:
818 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100819 unsigned int _pad_left;
820 unsigned int _pad_top;
821 unsigned int _pad_right;
822 unsigned int _pad_bottom;
823
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100824 DimensionRoundingType _round_type;
825};
826
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100827/** PriorBox layer info */
828class PriorBoxLayerInfo final
829{
830public:
831 /** Default Constructor */
832 PriorBoxLayerInfo()
833 : _min_sizes(),
834 _variances(),
835 _offset(),
836 _flip(true),
837 _clip(false),
838 _max_sizes(),
839 _aspect_ratios(),
840 _img_size(),
841 _steps()
842 {
843 }
844 /** Constructor
845 *
846 * @param[in] min_sizes Min sizes vector.
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100847 * @param[in] variances Variances vector.
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100848 * @param[in] offset Offset value.
849 * @param[in] flip (Optional) Flip the aspect ratios.
850 * @param[in] clip (Optional) Clip coordinates so that they're within [0,1].
851 * @param[in] max_sizes (Optional) Max sizes vector.
852 * @param[in] aspect_ratios (Optional) Aspect ratios of the boxes.
853 * @param[in] img_size (Optional) Image size.
854 * @param[in] steps (Optional) Step values.
855 */
856 PriorBoxLayerInfo(const std::vector<float> &min_sizes, const std::vector<float> &variances, float offset, bool flip = true, bool clip = false,
Pablo Tello32521432018-11-15 14:43:10 +0000857 const std::vector<float> &max_sizes = {}, const std::vector<float> &aspect_ratios = {},
858 const Coordinates2D &img_size = Coordinates2D{ 0, 0 }, const std::array<float, 2> &steps = { { 0.f, 0.f } })
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100859 : _min_sizes(min_sizes),
860 _variances(variances),
861 _offset(offset),
862 _flip(flip),
863 _clip(clip),
864 _max_sizes(max_sizes),
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100865 _aspect_ratios(),
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100866 _img_size(img_size),
867 _steps(steps)
868 {
869 _aspect_ratios.push_back(1.);
870 for(unsigned int i = 0; i < aspect_ratios.size(); ++i)
871 {
872 float ar = aspect_ratios[i];
873 bool already_exist = false;
874 for(auto ar_new : _aspect_ratios)
875 {
876 if(fabs(ar - ar_new) < 1e-6)
877 {
878 already_exist = true;
879 break;
880 }
881 }
882 if(!already_exist)
883 {
884 _aspect_ratios.push_back(ar);
885 if(flip)
886 {
887 _aspect_ratios.push_back(1.f / ar);
888 }
889 }
890 }
891 }
892 /** Get min sizes. */
893 std::vector<float> min_sizes() const
894 {
895 return _min_sizes;
896 }
897 /** Get min variances. */
898 std::vector<float> variances() const
899 {
900 return _variances;
901 }
902 /** Get the step coordinates */
903 std::array<float, 2> steps() const
904 {
905 return _steps;
906 }
907 /** Get the image size coordinates */
908 Coordinates2D img_size() const
909 {
910 return _img_size;
911 }
912 /** Get the offset */
913 float offset() const
914 {
915 return _offset;
916 }
917 /** Get the flip value */
918 bool flip() const
919 {
920 return _flip;
921 }
922 /** Get the clip value */
923 bool clip() const
924 {
925 return _clip;
926 }
927 /** Get max sizes. */
928 std::vector<float> max_sizes() const
929 {
930 return _max_sizes;
931 }
932 /** Get aspect ratios. */
933 std::vector<float> aspect_ratios() const
934 {
935 return _aspect_ratios;
936 }
937
938private:
939 std::vector<float> _min_sizes;
940 std::vector<float> _variances;
941 float _offset;
942 bool _flip;
943 bool _clip;
944 std::vector<float> _max_sizes;
945 std::vector<float> _aspect_ratios;
946 Coordinates2D _img_size;
947 std::array<float, 2> _steps;
948};
949
Isabella Gottardia7acb3c2019-01-08 13:48:44 +0000950// Bounding Box [xmin, ymin, xmax, ymax]
951using BBox = std::array<float, 4>;
952// LabelBBox used for map label and bounding box
953using LabelBBox = std::map<int, std::vector<BBox>>;
954
Isabella Gottardi05e56442018-11-16 11:26:52 +0000955/** Available Detection Output code types */
956enum class DetectionOutputLayerCodeType
957{
958 CORNER, /**< Use box corners */
959 CENTER_SIZE, /**< Use box centers and size */
960 CORNER_SIZE, /**< Use box centers and size */
961 TF_CENTER /**< Use box centers and size but flip x and y co-ordinates */
962};
963
964/** Detection Output layer info */
965class DetectionOutputLayerInfo final
966{
967public:
968 /** Default Constructor */
969 DetectionOutputLayerInfo()
970 : _num_classes(),
971 _share_location(),
972 _code_type(DetectionOutputLayerCodeType::CORNER),
973 _keep_top_k(),
974 _nms_threshold(),
975 _top_k(),
976 _background_label_id(),
977 _confidence_threshold(),
978 _variance_encoded_in_target(false),
979 _eta(),
980 _num_loc_classes()
981 {
982 _num_loc_classes = _share_location ? 1 : _num_classes;
983 }
984 /** Constructor
985 *
986 * @param[in] num_classes Number of classes to be predicted.
987 * @param[in] share_location If true, bounding box are shared among different classes.
988 * @param[in] code_type Type of coding method for bbox.
989 * @param[in] keep_top_k Number of total bounding boxes to be kept per image after NMS step.
990 * @param[in] nms_threshold Threshold to be used in NMS.
991 * @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.
992 * @param[in] background_label_id (Optional) Background label ID. If there is no background class, set it as -1.
993 * @param[in] confidence_threshold (Optional) Only consider detections whose confidences are larger than a threshold. Default set to -FLT_MAX.
994 * @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.
995 * @param[in] eta (Optional) Eta.
996 */
997 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,
998 float confidence_threshold = std::numeric_limits<float>::lowest(), bool variance_encoded_in_target = false, float eta = 1)
999 : _num_classes(num_classes),
1000 _share_location(share_location),
1001 _code_type(code_type),
1002 _keep_top_k(keep_top_k),
1003 _nms_threshold(nms_threshold),
1004 _top_k(top_k),
1005 _background_label_id(background_label_id),
1006 _confidence_threshold(confidence_threshold),
1007 _variance_encoded_in_target(variance_encoded_in_target),
1008 _eta(eta),
1009 _num_loc_classes()
1010 {
1011 _num_loc_classes = _share_location ? 1 : _num_classes;
1012 }
1013 /** Get num classes. */
1014 int num_classes() const
1015 {
1016 return _num_classes;
1017 }
1018 /** Get share location. */
1019 bool share_location() const
1020 {
1021 return _share_location;
1022 }
1023 /** Get detection output code type. */
1024 DetectionOutputLayerCodeType code_type() const
1025 {
1026 return _code_type;
1027 }
1028 /** Get if variance encoded in target. */
1029 bool variance_encoded_in_target() const
1030 {
1031 return _variance_encoded_in_target;
1032 }
1033 /** Get the number of total bounding boxes to be kept per image. */
1034 int keep_top_k() const
1035 {
1036 return _keep_top_k;
1037 }
1038 /** Get nms threshold. */
1039 float nms_threshold() const
1040 {
1041 return _nms_threshold;
1042 }
1043 /** Get eta. */
1044 float eta() const
1045 {
1046 return _eta;
1047 }
1048 /** Get background label ID. */
1049 int background_label_id() const
1050 {
1051 return _background_label_id;
1052 }
1053 /** Get confidence threshold. */
1054 float confidence_threshold() const
1055 {
1056 return _confidence_threshold;
1057 }
1058 /** Get top K. */
1059 int top_k() const
1060 {
1061 return _top_k;
1062 }
1063 /** Get number of location classes. */
1064 int num_loc_classes() const
1065 {
1066 return _num_loc_classes;
1067 }
1068
1069private:
1070 int _num_classes;
1071 bool _share_location;
1072 DetectionOutputLayerCodeType _code_type;
1073 int _keep_top_k;
1074 float _nms_threshold;
1075 int _top_k;
1076 int _background_label_id;
1077 float _confidence_threshold;
1078 bool _variance_encoded_in_target;
1079 float _eta;
1080 int _num_loc_classes;
1081};
1082
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001083/** Detection Output layer info */
1084class DetectionPostProcessLayerInfo final
1085{
1086public:
1087 /** Default Constructor */
1088 DetectionPostProcessLayerInfo()
1089 : _max_detections(),
1090 _max_classes_per_detection(),
1091 _nms_score_threshold(),
1092 _iou_threshold(),
1093 _num_classes(),
1094 _scales_values(),
1095 _use_regular_nms(),
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001096 _detection_per_class(),
1097 _dequantize_scores()
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001098 {
1099 }
1100 /** Constructor
1101 *
1102 * @param[in] max_detections Number of total detection.
1103 * @param[in] max_classes_per_detection Number of total classes to be kept after NMS step. Used in the Fast Non-Max-Suppression
1104 * @param[in] nms_score_threshold Threshold to be used in NMS
1105 * @param[in] iou_threshold Threshold to be used during the intersection over union.
1106 * @param[in] num_classes Number of classes.
1107 * @param[in] scales_values Scales values used for decode center size boxes.
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001108 * @param[in] use_regular_nms (Optional) Boolean to determinate if use regular or fast nms. Defaults to false.
1109 * @param[in] detection_per_class (Optional) Number of detection per class. Used in the Regular Non-Max-Suppression. Defaults to 100.
1110 * @param[in] dequantize_scores (Optional) If the scores need to be dequantized. Defaults to true.
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001111 */
1112 DetectionPostProcessLayerInfo(unsigned int max_detections, unsigned int max_classes_per_detection, float nms_score_threshold, float iou_threshold, unsigned int num_classes,
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001113 std::array<float, 4> scales_values, bool use_regular_nms = false, unsigned int detection_per_class = 100, bool dequantize_scores = true)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001114 : _max_detections(max_detections),
1115 _max_classes_per_detection(max_classes_per_detection),
1116 _nms_score_threshold(nms_score_threshold),
1117 _iou_threshold(iou_threshold),
1118 _num_classes(num_classes),
1119 _scales_values(scales_values),
1120 _use_regular_nms(use_regular_nms),
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001121 _detection_per_class(detection_per_class),
1122 _dequantize_scores(dequantize_scores)
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001123 {
1124 }
1125 /** Get max detections. */
1126 unsigned int max_detections() const
1127 {
1128 return _max_detections;
1129 }
1130 /** Get max_classes per detection. Used in the Fast Non-Max-Suppression.*/
1131 unsigned int max_classes_per_detection() const
1132 {
1133 return _max_classes_per_detection;
1134 }
1135 /** Get detection per class. Used in the Regular Non-Max-Suppression */
1136 unsigned int detection_per_class() const
1137 {
1138 return _detection_per_class;
1139 }
1140 /** Get nms threshold. */
1141 float nms_score_threshold() const
1142 {
1143 return _nms_score_threshold;
1144 }
1145 /** Get intersection over union threshold. */
1146 float iou_threshold() const
1147 {
1148 return _iou_threshold;
1149 }
1150 /** Get num classes. */
1151 unsigned int num_classes() const
1152 {
1153 return _num_classes;
1154 }
1155 /** Get if use regular nms. */
1156 bool use_regular_nms() const
1157 {
1158 return _use_regular_nms;
1159 }
1160 /** Get y scale value. */
1161 float scale_value_y() const
1162 {
1163 // Saved as [y,x,h,w]
1164 return _scales_values[0];
1165 }
1166 /** Get x scale value. */
1167 float scale_value_x() const
1168 {
1169 // Saved as [y,x,h,w]
1170 return _scales_values[1];
1171 }
1172 /** Get h scale value. */
1173 float scale_value_h() const
1174 {
1175 // Saved as [y,x,h,w]
1176 return _scales_values[2];
1177 }
1178 /** Get w scale value. */
1179 float scale_value_w() const
1180 {
1181 // Saved as [y,x,h,w]
1182 return _scales_values[3];
1183 }
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001184 /** Get dequantize_scores value. */
1185 bool dequantize_scores() const
1186 {
1187 return _dequantize_scores;
1188 }
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001189
1190private:
1191 unsigned int _max_detections;
1192 unsigned int _max_classes_per_detection;
1193 float _nms_score_threshold;
1194 float _iou_threshold;
1195 unsigned int _num_classes;
1196 std::array<float, 4> _scales_values;
1197 bool _use_regular_nms;
1198 unsigned int _detection_per_class;
Giuseppe Rossinid9853782019-10-25 11:11:44 +01001199 bool _dequantize_scores;
Isabella Gottardia7acb3c2019-01-08 13:48:44 +00001200};
1201
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001202/** Pooling Layer Information struct*/
1203struct PoolingLayerInfo
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001204{
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001205 /** Default Constructor */
1206 PoolingLayerInfo()
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001207 : pool_type(PoolingType::MAX),
1208 pool_size(Size2D()),
1209 data_layout(DataLayout::UNKNOWN),
1210 pad_stride_info(PadStrideInfo()),
1211 exclude_padding(false),
1212 is_global_pooling(false),
1213 fp_mixed_precision(false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001214 {
1215 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001216 /** Constructor
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001217 *
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001218 * @param[in] pool_type Pooling type @ref PoolingType.
1219 * @param[in] pool_size Pooling size, in elements, across x and y.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001220 * @param[in] data_layout Data layout used by the layer @ref DataLayout
1221 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1222 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1223 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1224 * Defaults to false;
1225 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
1226 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001227 explicit PoolingLayerInfo(PoolingType pool_type,
1228 unsigned int pool_size,
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001229 DataLayout data_layout,
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001230 PadStrideInfo pad_stride_info = PadStrideInfo(),
1231 bool exclude_padding = false,
1232 bool fp_mixed_precision = false)
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001233 : pool_type(pool_type),
1234 pool_size(Size2D(pool_size, pool_size)),
1235 data_layout(data_layout),
1236 pad_stride_info(pad_stride_info),
1237 exclude_padding(exclude_padding),
1238 is_global_pooling(false),
1239 fp_mixed_precision(fp_mixed_precision)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001240 {
1241 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001242
1243 /** Constructor
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001244 *
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001245 * @param[in] pool_type Pooling type @ref PoolingType.
1246 * @param[in] pool_size Pooling size, in elements, across x and y.
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001247 * @param[in] data_layout Data layout used by the layer @ref DataLayout
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001248 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1249 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1250 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1251 * Defaults to false;
1252 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001253 */
1254 explicit PoolingLayerInfo(PoolingType pool_type,
1255 Size2D pool_size,
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001256 DataLayout data_layout,
Sang-Hoon Park2aa7fd02019-09-18 13:39:00 +01001257 PadStrideInfo pad_stride_info = PadStrideInfo(),
1258 bool exclude_padding = false,
1259 bool fp_mixed_precision = false)
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001260 : pool_type(pool_type),
1261 pool_size(pool_size),
1262 data_layout(data_layout),
1263 pad_stride_info(pad_stride_info),
1264 exclude_padding(exclude_padding),
1265 is_global_pooling(false),
1266 fp_mixed_precision(fp_mixed_precision)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001267 {
1268 }
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001269
1270 /** Constructor
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001271 *
1272 * @note This constructor is used for global pooling
1273 *
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001274 * @param[in] pool_type Pooling type @ref PoolingType.
1275 * @param[in] data_layout Data layout used by the layer @ref DataLayout
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001276 */
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001277 explicit PoolingLayerInfo(PoolingType pool_type, DataLayout data_layout)
1278 : pool_type(pool_type),
1279 pool_size(Size2D()),
1280 data_layout(data_layout),
1281 pad_stride_info(PadStrideInfo(1, 1, 0, 0)),
1282 exclude_padding(false),
1283 is_global_pooling(true),
1284 fp_mixed_precision(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001285 {
1286 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001287
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001288 PoolingType pool_type;
1289 Size2D pool_size;
Sang-Hoon Park11fedda2020-01-15 14:44:04 +00001290 DataLayout data_layout;
Sang-Hoon Park0cb3da62020-01-15 12:39:56 +00001291 PadStrideInfo pad_stride_info;
1292 bool exclude_padding;
1293 bool is_global_pooling;
1294 bool fp_mixed_precision;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001295};
1296
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001297/** ROI Pooling Layer Information class */
giuros0118870812018-09-13 09:31:40 +01001298class ROIPoolingLayerInfo final
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001299{
1300public:
giuros0118870812018-09-13 09:31:40 +01001301 /** Constructor
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001302 *
giuros0118870812018-09-13 09:31:40 +01001303 * @param[in] pooled_width Pooled width of the layer.
1304 * @param[in] pooled_height Pooled height of the layer.
1305 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
1306 * @param[in] sampling_ratio Number of samples to include in each pooling region (if set to zero, a ceil(roi_dims/pooling_dims))
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001307 */
giuros0118870812018-09-13 09:31:40 +01001308 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale, unsigned int sampling_ratio = 0)
1309 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale), _sampling_ratio(sampling_ratio)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001310 {
1311 }
Alex Gildayc357c472018-03-21 13:54:09 +00001312 /** Get the pooled width of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001313 unsigned int pooled_width() const
1314 {
1315 return _pooled_width;
1316 }
Alex Gildayc357c472018-03-21 13:54:09 +00001317 /** Get the pooled height of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001318 unsigned int pooled_height() const
1319 {
1320 return _pooled_height;
1321 }
Alex Gildayc357c472018-03-21 13:54:09 +00001322 /** Get the spatial scale */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001323 float spatial_scale() const
1324 {
1325 return _spatial_scale;
1326 }
giuros0118870812018-09-13 09:31:40 +01001327 /** Get sampling ratio */
1328 unsigned int sampling_ratio() const
1329 {
1330 return _sampling_ratio;
1331 }
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001332
1333private:
1334 unsigned int _pooled_width;
1335 unsigned int _pooled_height;
1336 float _spatial_scale;
giuros0118870812018-09-13 09:31:40 +01001337 unsigned int _sampling_ratio;
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001338};
1339
Manuel Bottini5209be52019-02-13 16:34:56 +00001340/** Generate Proposals Information class */
1341class GenerateProposalsInfo
1342{
1343public:
1344 /** Constructor
1345 *
1346 * @param[in] im_width Width of the original image
1347 * @param[in] im_height Height of the original image
1348 * @param[in] im_scale Scale applied to the original image
1349 * @param[in] spatial_scale (Optional)Scale applied to the feature map. Defaults to 1.0
1350 * @param[in] pre_nms_topN (Optional)Number of the best scores to be selected from the transformations. Defaults to 6000.
1351 * @param[in] post_nms_topN (Optional)Number of the best scores to be selected from the NMS operation. Defaults to 300.
1352 * @param[in] nms_thres (Optional)NMS overlap threshold. Defaults to 0.7.
1353 * @param[in] min_size (Optional)Size used to validate the anchors produced. Defaults to 16.
1354 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region of interest). Defaults to 4.
1355 */
1356 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,
1357 size_t values_per_roi = 4)
1358 : _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),
1359 _min_size(min_size), _values_per_roi(values_per_roi)
1360 {
1361 }
1362
1363 /* Get the original height */
1364 float im_height() const
1365 {
1366 return _im_height;
1367 }
1368 /* Get the original width */
1369 float im_width() const
1370 {
1371 return _im_width;
1372 }
1373 /* Get the image scale */
1374 float im_scale() const
1375 {
1376 return _im_scale;
1377 }
1378 /* Get the value of how many best scores to select (before NMS) */
1379 int pre_nms_topN() const
1380 {
1381 return _pre_nms_topN;
1382 }
1383 /* Get the value of how many best scores to select (after NMS) */
1384 int post_nms_topN() const
1385 {
1386 return _post_nms_topN;
1387 }
1388 /* Get the NMS overlap threshold */
1389 float nms_thres() const
1390 {
1391 return _nms_thres;
1392 }
1393 /* Get the minimal size */
1394 float min_size() const
1395 {
1396 return _min_size;
1397 }
1398 /* Get the spatial scale to be applied to the feature maps */
1399 float spatial_scale() const
1400 {
1401 return _spatial_scale;
1402 }
1403 /* Get the values used to represent a ROI(Region of interest)*/
1404 size_t values_per_roi() const
1405 {
1406 return _values_per_roi;
1407 }
1408
1409private:
1410 float _im_height;
1411 float _im_width;
1412 float _im_scale;
1413 float _spatial_scale;
1414 int _pre_nms_topN;
1415 int _post_nms_topN;
1416 float _nms_thres;
1417 float _min_size;
1418 size_t _values_per_roi;
1419};
1420
1421/** ComputeAnchors information class */
1422class ComputeAnchorsInfo
1423{
1424public:
1425 /** Constructor
1426 *
1427 * @param[in] feat_width Feature map width
1428 * @param[in] feat_height Feature map height
1429 * @param[in] spatial_scale Feature map scale
1430 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region Of Interest). Defaults to 4
1431 */
1432 ComputeAnchorsInfo(float feat_width, float feat_height, float spatial_scale, size_t values_per_roi = 4)
1433 : _feat_height(feat_height),
1434 _feat_width(feat_width),
1435 _spatial_scale(spatial_scale),
1436 _values_per_roi(values_per_roi)
1437 {
1438 }
1439
1440 /* Get the height of the feature map */
1441 float feat_height() const
1442 {
1443 return _feat_height;
1444 }
1445
1446 /* Get the width of the feature map */
1447 float feat_width() const
1448 {
1449 return _feat_width;
1450 }
1451
1452 /* Get the scale of the feature map */
1453 float spatial_scale() const
1454 {
1455 return _spatial_scale;
1456 }
1457
1458 /* Get the values used to represent a ROI(Region Of Interest)*/
1459 size_t values_per_roi() const
1460 {
1461 return _values_per_roi;
1462 }
1463
1464private:
1465 float _feat_height;
1466 float _feat_width;
1467 float _spatial_scale;
1468 size_t _values_per_roi;
1469};
1470
giuros01c04a0e82018-10-03 12:44:35 +01001471/** Bounding Box Transform information class */
giuros01d696cb62018-11-16 10:39:59 +00001472class BoundingBoxTransformInfo final
giuros01c04a0e82018-10-03 12:44:35 +01001473{
1474public:
1475 /** Constructor
1476 *
giuros01d696cb62018-11-16 10:39:59 +00001477 * @param[in] img_width Width of the original image
1478 * @param[in] img_height Height, of the original image
1479 * @param[in] scale Scale of the original image
1480 * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false
1481 * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones
1482 * @param[in] correct_transform_coords (Optional)Correct bounding box transform coordinates. Defaults to false
1483 * @param[in] bbox_xform_clip (Optional)Minimum bounding box width and height after bounding box transformation in log-space. Defaults to log(1000/16)
giuros01c04a0e82018-10-03 12:44:35 +01001484 */
giuros01d696cb62018-11-16 10:39:59 +00001485 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 =
1486 false,
1487 float bbox_xform_clip =
1488 4.135166556742356f)
1489 : _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)
giuros01c04a0e82018-10-03 12:44:35 +01001490 {
1491 }
1492
1493 std::array<float, 4> weights() const
1494 {
1495 return _weights;
1496 }
1497
1498 float bbox_xform_clip() const
1499 {
1500 return _bbox_xform_clip;
1501 }
1502
1503 float img_height() const
1504 {
1505 return _img_height;
1506 }
1507
1508 float img_width() const
1509 {
1510 return _img_width;
1511 }
1512
1513 float scale() const
1514 {
1515 return _scale;
1516 }
1517
1518 bool apply_scale() const
1519 {
1520 return _apply_scale;
1521 }
1522
giuros01d696cb62018-11-16 10:39:59 +00001523 bool correct_transform_coords() const
1524 {
1525 return _correct_transform_coords;
1526 }
1527
giuros01c04a0e82018-10-03 12:44:35 +01001528private:
1529 float _img_width;
1530 float _img_height;
1531 float _scale;
1532 bool _apply_scale;
giuros01d696cb62018-11-16 10:39:59 +00001533 bool _correct_transform_coords;
giuros01c04a0e82018-10-03 12:44:35 +01001534 std::array<float, 4> _weights;
1535 float _bbox_xform_clip;
1536};
1537
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001538/** Activation Layer Information class */
1539class ActivationLayerInfo
1540{
1541public:
1542 /** Available activation functions */
1543 enum class ActivationFunction
1544 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001545 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
1546 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
1547 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
1548 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
1549 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
Manuel Bottini581c8982019-02-07 10:31:57 +00001550 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 Pinitas64ebe5b2017-09-01 17:44:24 +01001551 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
Georgios Pinitasfb0fdcd2019-08-22 17:10:04 +01001552 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 Pinitas64ebe5b2017-09-01 17:44:24 +01001553 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
1554 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
1555 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
Usama Arif6a98a6e2019-05-10 17:07:27 +01001556 LINEAR, /**< Linear ( \f$ f(x)= ax + b \f$ ) */
morgolock07df3d42020-02-27 11:46:28 +00001557 IDENTITY, /**< Identity ( \f$ f(x)= x \f$ ) */
1558 HARD_SWISH /**< Hard-swish ( \f$ f(x) = (x * relu6(x+3))/6 \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001559 };
1560
Giorgio Arena11674872018-02-07 15:38:12 +00001561 ActivationLayerInfo() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001562 /** Default Constructor
1563 *
1564 * @param[in] f The activation function to use.
1565 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001566 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
1567 * @param[in] b (Optional) The beta parameter used by some activation functions (@ref ActivationFunction::LINEAR, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::TANH).
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001568 */
1569 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
Giorgio Arena11674872018-02-07 15:38:12 +00001570 : _act(f), _a(a), _b(b), _enabled(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001571 {
1572 }
Alex Gildayc357c472018-03-21 13:54:09 +00001573 /** Get the type of activation function */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001574 ActivationFunction activation() const
1575 {
1576 return _act;
1577 }
Alex Gildayc357c472018-03-21 13:54:09 +00001578 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001579 float a() const
1580 {
1581 return _a;
1582 }
Alex Gildayc357c472018-03-21 13:54:09 +00001583 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001584 float b() const
1585 {
1586 return _b;
1587 }
Alex Gildayc357c472018-03-21 13:54:09 +00001588 /** Check if initialised */
Giorgio Arena11674872018-02-07 15:38:12 +00001589 bool enabled() const
1590 {
1591 return _enabled;
1592 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001593
1594private:
Usama Arif6a98a6e2019-05-10 17:07:27 +01001595 ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::IDENTITY };
Giorgio Arena11674872018-02-07 15:38:12 +00001596 float _a = {};
1597 float _b = {};
1598 bool _enabled = { false };
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001599};
1600
Giorgio Arena1856ff72020-02-07 13:46:45 +00001601/** Fully connected layer info */
1602struct FullyConnectedLayerInfo
1603{
1604 DataLayout weights_trained_layout{ DataLayout::NCHW }; /**< Layout that the weights have been trained with. */
1605 bool transpose_weights{ true }; /**< Transpose weights if true. */
1606 bool are_weights_reshaped{ false }; /**< Reshape the weights tensor if false. */
1607 bool retain_internal_weights{ false }; /**< Retain internal reshaped weights. */
1608 bool fp_mixed_precision{ false }; /**< Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy. */
1609 ActivationLayerInfo activation_info{}; /**< Fused activation to apply after the matrix multiplication. */
1610
1611 /** Sets the weights trained data layout
1612 *
1613 * @param[in] layout Data layout that the weights were trained with
1614 *
1615 * @return Updated object
1616 */
1617 FullyConnectedLayerInfo &set_weights_trained_layout(DataLayout layout)
1618 {
1619 weights_trained_layout = layout;
1620 return *this;
1621 }
1622 /** Sets the transpose weights flag
1623 *
1624 * @param[in] should_transpose_weights Boolean flag indicating if weights should be transposed
1625 *
1626 * @return Updated object
1627 */
1628 FullyConnectedLayerInfo &set_transpose_weights(bool should_transpose_weights)
1629 {
1630 transpose_weights = should_transpose_weights;
1631 return *this;
1632 }
1633};
1634
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001635/** Normalization Layer Information class */
1636class NormalizationLayerInfo
1637{
1638public:
1639 /** Default Constructor
1640 *
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001641 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NormType::CROSS_MAP
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001642 * @param[in] norm_size The normalization size is the number of elements to normalize across. Defaults to 5.
Georgios Pinitas41caa622017-11-16 14:37:08 +00001643 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
1644 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
1645 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
1646 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
1647 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001648 */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001649 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
1650 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001651 {
1652 }
Alex Gildayc357c472018-03-21 13:54:09 +00001653 /** Get the normalization type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001654 NormType type() const
1655 {
1656 return _type;
1657 }
Alex Gildayc357c472018-03-21 13:54:09 +00001658 /** Get the normalization size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001659 uint32_t norm_size() const
1660 {
1661 return _norm_size;
1662 }
Alex Gildayc357c472018-03-21 13:54:09 +00001663 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001664 float alpha() const
1665 {
1666 return _alpha;
1667 }
Alex Gildayc357c472018-03-21 13:54:09 +00001668 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001669 float beta() const
1670 {
1671 return _beta;
1672 }
Alex Gildayc357c472018-03-21 13:54:09 +00001673 /** Get the kappa value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001674 float kappa() const
1675 {
1676 return _kappa;
1677 }
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001678 /** Get the is_scaled value */
1679 bool is_scaled() const
1680 {
1681 return _is_scaled;
1682 }
Alex Gildayc357c472018-03-21 13:54:09 +00001683 /** Check if normalization is cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001684 bool is_cross_map() const
1685 {
1686 return _type == NormType::CROSS_MAP;
1687 }
Alex Gildayc357c472018-03-21 13:54:09 +00001688 /** Check if normalization is not cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001689 bool is_in_map() const
1690 {
1691 return !is_cross_map();
1692 }
1693 /** Return the scaling factor of the normalization function.
1694 *
1695 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
1696 * where alpha is returned plainly, else alpha is scaled by the total number of elements used for the normalization.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001697 *
1698 * @return The normalization scaling factor.
1699 */
1700 float scale_coeff() const
1701 {
1702 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001703 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001704 }
1705
1706private:
1707 NormType _type;
1708 uint32_t _norm_size;
1709 float _alpha;
1710 float _beta;
1711 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001712 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001713};
1714
thecha012bfadd92020-08-12 17:25:51 +01001715class StridedSliceLayerInfo
1716{
1717public:
1718 /** Default Constructor
1719 *
1720 * @param[in] begin_mask (Optional) If the ith bit of begin_mask is set, starts[i] is ignored and the fullest possible range in that dimension is used instead.
1721 * @param[in] end_mask (Optional) If the ith bit of end_mask is set, ends[i] is ignored and the fullest possible range in that dimension is used instead.
1722 * @param[in] shrink_axis_mask (Optional) If the ith bit of shrink_axis_mask is set, it implies that the ith specification shrinks the dimensionality by 1.
1723 */
1724 StridedSliceLayerInfo(int32_t begin_mask = 0, int32_t end_mask = 0, int32_t shrink_axis_mask = 0)
1725 : _begin_mask(begin_mask), _end_mask(end_mask), _shrink_axis_mask(shrink_axis_mask)
1726 {
1727 }
1728
1729 /* Get the begin mask value */
1730 int32_t begin_mask() const
1731 {
1732 return _begin_mask;
1733 }
1734
1735 /* Get the end mask value */
1736 int32_t end_mask() const
1737 {
1738 return _end_mask;
1739 }
1740
1741 /* Get the shrink axis mask value */
1742 int32_t shrink_axis_mask() const
1743 {
1744 return _shrink_axis_mask;
1745 }
1746
1747private:
1748 int32_t _begin_mask;
1749 int32_t _end_mask;
1750 int32_t _shrink_axis_mask;
1751};
1752
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001753/** Convolution Layer Weights Information class. This class stores the necessary information to compute convolution layer when the weights are already reshaped */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001754class WeightsInfo
1755{
1756public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001757 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001758 WeightsInfo()
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001759 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0), _retain_internal_weights(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001760 {
1761 }
1762 /** Constructor
1763 *
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001764 * @param[in] are_reshaped True if the weights have been reshaped
1765 * @param[in] kernel_width Kernel width.
1766 * @param[in] kernel_height Kernel height.
1767 * @param[in] num_kernels Number of convolution kernels.
1768 * @param[in] retain_internal_weights (Optional) True if internal reshaped weights must be retained. Used for reconfiguration purposes. Default is false.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001769 */
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001770 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels, bool retain_internal_weights = false)
1771 : _are_reshaped(are_reshaped), _kernel_width(kernel_width), _kernel_height(kernel_height), _num_kernels(num_kernels), _retain_internal_weights(retain_internal_weights)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001772 {
1773 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001774 /** Flag which specifies if the weights tensor has been reshaped.
1775 *
1776 * @return True if the weights tensors has been reshaped
1777 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001778 bool are_reshaped() const
1779 {
1780 return _are_reshaped;
1781 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001782 /** Return the number of convolution kernels
1783 *
1784 * @return The number of convolution kernels
1785 */
1786 unsigned int num_kernels() const
1787 {
1788 return _num_kernels;
1789 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001790 /** Return the width and height of the kernel
1791 *
1792 * @return The width and height of the kernel
1793 */
1794 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001795 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001796 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001797 }
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001798 bool retain_internal_weights() const
1799 {
1800 return _retain_internal_weights;
1801 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001802
1803private:
SiCong Liebd8fb42020-08-18 11:03:14 +01001804 bool _are_reshaped;
1805 unsigned int _kernel_width;
1806 unsigned int _kernel_height;
1807 unsigned int _num_kernels;
1808 bool _retain_internal_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001809};
1810
Gian Marco36a0a462018-01-12 10:21:40 +00001811/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
1812 *
Gian Marco Iodice5fc07aa2019-05-15 17:08:02 +01001813 * The matrix A can only be reshaped through @ref CLGEMMReshapeLHSMatrixKernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
1814 * 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 Marco36a0a462018-01-12 10:21:40 +00001815 *
giuros018b6b4a92018-12-18 19:01:33 +00001816 * The matrix B can only be reshaped through @ref CLGEMMReshapeRHSMatrixKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
1817 * 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 Marco36a0a462018-01-12 10:21:40 +00001818 *
1819 */
1820class GEMMReshapeInfo final
1821{
1822public:
1823 /** Default constructor */
1824 GEMMReshapeInfo()
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001825 : _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 Marco36a0a462018-01-12 10:21:40 +00001826 {
1827 }
1828 /** Constructor
1829 *
1830 * @param[in] m Number of matrix A rows
1831 * @param[in] n Number of matrix B columns
1832 * @param[in] k Number of matrix A columns or matrix B rows
1833 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
1834 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001835 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel.
1836 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001837 * @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 Pinitasb0f342e2019-05-21 13:32:43 +01001838 * to perform 1x1 convolutions with the NHWC data layout)
1839 * @param[in] broadcast_bias (Optional) Broadcast the shape of the bias tensor from a vector to a matrix.
Gian Marco36a0a462018-01-12 10:21:40 +00001840 */
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001841 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 Iodice68a3f562018-07-26 11:44:03 +01001842 : _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 Pinitasb0f342e2019-05-21 13:32:43 +01001843 _reinterpret_input_as_3d(reinterpret_input_as_3d), _broadcast_bias(broadcast_bias)
Gian Marco36a0a462018-01-12 10:21:40 +00001844 {
1845 }
1846 /** Number of matrix A rows
1847 *
1848 * @return the number of matrix A rows
1849 */
1850 int m() const
1851 {
1852 return _m;
1853 }
1854 /** Number of matrix B columns
1855 *
1856 * @return the number of matrix B columns
1857 */
1858 int n() const
1859 {
1860 return _n;
1861 }
1862 /** Number of matrix A columns or matrix B rows
1863 *
1864 * @return the number of matrix A columns or matrix B rows
1865 */
1866 int k() const
1867 {
1868 return _k;
1869 }
1870 /** Multiplication factor for the width of the 1xW transposed block
1871 *
1872 * @return the multiplication factor for the width of the 1xW transposed block
1873 */
1874 int mult_transpose1xW_width() const
1875 {
1876 return _mult_transpose1xW_width;
1877 }
1878 /** Multiplication factor for the height of the 4x4 interleaved block
1879 *
1880 * @return the multiplication factor for the height of the 4x4 interleaved block
1881 */
1882 int mult_interleave4x4_height() const
1883 {
1884 return _mult_interleave4x4_height;
1885 }
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001886 /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
1887 *
1888 * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case:
1889 * m = depth_output_gemm3d * output_height
1890 *
1891 * @return the depth of the output tensor to be used with the GEMM3D kernel
1892 */
1893 int depth_output_gemm3d() const
1894 {
1895 return _depth_output_gemm3d;
1896 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001897 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1898 *
1899 * @return True if the input tensor has to be reinterpreted as 3D tensor
1900 */
1901 bool reinterpret_input_as_3d() const
1902 {
1903 return _reinterpret_input_as_3d;
1904 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01001905 /** Flag which specifies whether to broadcast the shape of the bias tensor.
1906 *
1907 * @return True if the shape of the bias tensor is to be broadcasted.
1908 */
1909 bool broadcast_bias() const
1910 {
1911 return _broadcast_bias;
1912 };
Gian Marco36a0a462018-01-12 10:21:40 +00001913
1914private:
SiCong Liebd8fb42020-08-18 11:03:14 +01001915 int _m;
1916 int _n;
1917 int _k;
1918 int _mult_transpose1xW_width;
1919 int _mult_interleave4x4_height;
1920 int _depth_output_gemm3d;
1921 bool _reinterpret_input_as_3d;
1922 bool _broadcast_bias;
Gian Marco36a0a462018-01-12 10:21:40 +00001923};
1924
giuros016d109962019-01-07 17:47:19 +00001925struct DepthwiseConvolutionReshapeInfo
1926{
1927 unsigned int c0{ 1 }; /**< Number of channels processed by the depth-wise convolution */
1928 bool transpose{ false }; /**< True if the block MxC0 (where M is the area of the filter i.e. KwxKh) has to be transposed */
1929};
1930
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001931/** GEMMLowp output stage type */
1932enum class GEMMLowpOutputStageType
1933{
Manuel Bottini959c26d2019-12-02 16:22:35 +00001934 NONE, /**< No quantization */
1935 QUANTIZE_DOWN, /**< Quantize using an integer multiplication */
1936 QUANTIZE_DOWN_FIXEDPOINT, /**< Quantize using a fixed point multiplication */
1937 QUANTIZE_DOWN_FLOAT /**< Quantize using a floating point multiplication */
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001938};
1939
1940/** GEMMLowp output stage info */
1941struct GEMMLowpOutputStageInfo
1942{
Giorgio Arena1856ff72020-02-07 13:46:45 +00001943 GEMMLowpOutputStageType type{ GEMMLowpOutputStageType::NONE }; /**< GEMMLowp output stage type */
1944 int32_t gemmlowp_offset{ 0 }; /**< GEMMLowp output stage offset used for quantizing to QASYMM8 */
1945 int32_t gemmlowp_multiplier{ 0 }; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
1946 int32_t gemmlowp_shift{ 0 }; /**< GEMMLowp output stage shift used for quantizing to uint8 */
1947 int32_t gemmlowp_min_bound{ std::numeric_limits<int32_t>::lowest() }; /**< GEMMLowp min value used to saturate down the output result before converting back to QASYMM8 */
1948 int32_t gemmlowp_max_bound{ std::numeric_limits<int32_t>::max() }; /**< GEMMLowp max value used to saturate down the output result before converting back to QASYMM8 */
1949 std::vector<int32_t> gemmlowp_multipliers{}; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
1950 std::vector<int32_t> gemmlowp_shifts{}; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
Sheri Zhang1b14c752020-03-09 14:29:52 +00001951 float gemmlowp_real_multiplier{ 0 }; /**< GEMMLowp output stage real multiplier used for quantizing to QASYMM8 */
Giorgio Arena1856ff72020-02-07 13:46:45 +00001952 bool is_quantized_per_channel{ false }; /**< GEMMLowp quantized per-channel flag */
1953 DataType output_data_type{ DataType::UNKNOWN }; /**< Output tensor data type to use if the output is not initialized */
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001954};
1955
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001956/** GEMM LHS (Left Hand Side) matrix information */
1957struct GEMMLHSMatrixInfo
1958{
morgolockaba2f912020-05-05 16:28:19 +01001959 GEMMLHSMatrixInfo() = default;
1960 GEMMLHSMatrixInfo(unsigned int m, unsigned int k, unsigned int v, bool trans, bool inter)
1961 : m0(m), k0(k), v0(v), transpose(trans), interleave(inter)
1962 {
1963 }
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001964 unsigned int m0{ 1 }; /**< Number of rows processed by the matrix multiplication */
1965 unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */
1966 unsigned int v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
1967 bool transpose{ true }; /**< True if the (m0xk0) block has to be transposed before been stored */
1968 bool interleave{ true }; /**< True if the v0 (m0xk0) blocks have to be interleaved in the output row */
1969};
1970
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001971/** GEMM RHS (Right Hand Side) matrix information */
1972struct GEMMRHSMatrixInfo
1973{
morgolockaba2f912020-05-05 16:28:19 +01001974 GEMMRHSMatrixInfo() = default;
Gian Marco Iodicee3a849a2020-06-10 17:59:30 +01001975 GEMMRHSMatrixInfo(unsigned int n, unsigned int k, unsigned int h, bool trans, bool inter, bool export_to_cl_img)
1976 : n0(n), k0(k), h0(h), transpose(trans), interleave(inter), export_to_cl_image(export_to_cl_img)
morgolockaba2f912020-05-05 16:28:19 +01001977 {
1978 }
Gian Marco Iodicedd717c32020-05-28 10:22:03 +01001979 unsigned int n0{ 1 }; /**< Number of columns processed by the matrix multiplication */
1980 unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */
1981 unsigned int h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
1982 bool transpose{ true }; /**< True if the (k0xn0) block has to be transposed before been stored */
1983 bool interleave{ true }; /**< True if the h0 (k0xn0) blocks have to be interleaved in the output row */
1984 bool export_to_cl_image{ false }; /**< True if the reshaped rhs has to be exported to cl_image. n0 must be equal to 4 */
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001985};
1986
Gian Marco36a0a462018-01-12 10:21:40 +00001987/** GEMM information class. This class stores the necessary information to compute GEMM functions
1988 *
1989 * This object also contains the information about how matrix A and matrix B have been reshaped
1990 *
1991 */
Chunosov5124be52017-11-22 20:42:13 +07001992class GEMMInfo
1993{
1994public:
1995 /** Default constructor */
Georgios Pinitas37d080f2019-06-21 18:43:12 +01001996 GEMMInfo() noexcept
1997 : _is_a_reshaped(false),
1998 _is_b_reshaped(false),
1999 _reshape_b_only_on_first_run(true),
2000 _depth_output_gemm3d(0),
2001 _reinterpret_input_as_3d(false),
2002 _retain_internal_weights(false),
2003 _gemmlowp_output_stage(),
2004 _fp_mixed_precision(false),
2005 _broadcast_bias(false),
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002006 _pretranpose_B(true),
2007 _activation_info()
Chunosov5124be52017-11-22 20:42:13 +07002008 {
2009 }
2010 /** Constructor
2011 *
2012 * @param[in] is_a_reshaped True if the matrix A has been reshaped
2013 * @param[in] is_b_reshaped True if the matrix B has been reshaped
2014 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002015 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
Gian Marco Iodice3139f032018-11-05 14:26:32 +00002016 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01002017 * @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
2018 * to perform 1x1 convolutions with the NHWC data layout)
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01002019 * @param[in] retain_internal_weights (Optional) Retain the weights tensor from previous run
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002020 * @param[in] gemmlowp_output_stage (Optional) GEMMLowp Output stage info
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00002021 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01002022 * @param[in] broadcast_bias (Optional) Broadcast the shape of the bias tensor from a vector to a matrix.
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002023 * @param[in] activation_info (Optional) Activation to apply after the matrix multiplication
Chunosov5124be52017-11-22 20:42:13 +07002024 */
Gian Marco Iodice3139f032018-11-05 14:26:32 +00002025 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 Iodicef3622be2019-07-29 14:27:16 +01002026 GEMMLowpOutputStageInfo gemmlowp_output_stage = GEMMLowpOutputStageInfo(), bool fp_mixed_precision = false, bool broadcast_bias = false,
2027 const ActivationLayerInfo &activation_info = ActivationLayerInfo()) noexcept
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002028 : _is_a_reshaped(is_a_reshaped),
2029 _is_b_reshaped(is_b_reshaped),
2030 _reshape_b_only_on_first_run(reshape_b_only_on_first_run),
2031 _depth_output_gemm3d(depth_output_gemm3d),
2032 _reinterpret_input_as_3d(reinterpret_input_as_3d),
2033 _retain_internal_weights(retain_internal_weights),
2034 _gemmlowp_output_stage(gemmlowp_output_stage),
2035 _fp_mixed_precision(fp_mixed_precision),
2036 _broadcast_bias(broadcast_bias),
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002037 _pretranpose_B(reshape_b_only_on_first_run),
2038 _activation_info(activation_info)
Chunosov5124be52017-11-22 20:42:13 +07002039 {
2040 }
2041 /** Flag which specifies if the matrix A has been reshaped
2042 *
2043 * @return True if the matrix A has been reshaped
2044 */
2045 bool is_a_reshaped() const
2046 {
2047 return _is_a_reshaped;
2048 };
2049 /** Flag which specifies if the matrix B has been reshaped
2050 *
2051 * @return True if the matrix B has been reshaped
2052 */
2053 bool is_b_reshaped() const
2054 {
2055 return _is_b_reshaped;
2056 };
2057 /** Flag which specifies if the reshape of matrix B should executed only for the first
2058 *
2059 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
2060 *
2061 * @return True if the reshaped of matrix B happens only for the first run
2062 */
2063 bool reshape_b_only_on_first_run() const
2064 {
2065 return _reshape_b_only_on_first_run;
2066 };
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002067 /** Depth of the output when GEMM output is reinterpreted as 3D tensor
Gian Marco36a0a462018-01-12 10:21:40 +00002068 *
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002069 * @return the depth of the output tensor
Gian Marco36a0a462018-01-12 10:21:40 +00002070 */
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002071 int depth_output_gemm3d() const
Gian Marco36a0a462018-01-12 10:21:40 +00002072 {
Isabella Gottardi8e74f442018-03-01 16:42:00 +00002073 return _depth_output_gemm3d;
2074 };
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01002075 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
2076 *
2077 * @return True if the input tensor has to be reinterpreted as 3D tensor
2078 */
2079 bool reinterpret_input_as_3d() const
2080 {
2081 return _reinterpret_input_as_3d;
2082 };
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01002083 /** Flag which specifies if the weights tensor has to be retained from previous run
2084 *
2085 * @return True if the weights tensor has to be retained
2086 */
2087 bool retain_internal_weights() const
2088 {
2089 return _retain_internal_weights;
2090 };
Gian Marco Iodice4b908652018-10-18 10:21:02 +01002091 /** GEMMLowp output stage
2092 *
2093 * @return the GEMMLowp output stage info
2094 */
2095 GEMMLowpOutputStageInfo gemmlowp_output_stage() const
2096 {
2097 return _gemmlowp_output_stage;
2098 };
Georgios Pinitasdbdea0d2019-10-16 19:21:40 +01002099 /** Sets GEMMLowp output stage
2100 *
2101 * @param[in] output_stage Output stage to set
2102 */
2103 void set_gemmlowp_output_stage(GEMMLowpOutputStageInfo &output_stage)
2104 {
2105 _gemmlowp_output_stage = output_stage;
2106 };
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00002107 /** Flag which specifies if a wider accumulator should be used.
2108 *
2109 * @return True if a wider accumulator has to be used
2110 */
2111 bool fp_mixed_precision() const
2112 {
2113 return _fp_mixed_precision;
2114 };
Georgios Pinitasb0f342e2019-05-21 13:32:43 +01002115 /** Flag which specifies whether to broadcast the shape of the bias tensor.
2116 *
2117 * @return True if the shape of the bias tensor is to be broadcasted.
2118 */
2119 bool broadcast_bias() const
2120 {
2121 return _broadcast_bias;
2122 };
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002123 /** Flag which specifies whether b should be pre-transposed if supported.
2124 *
2125 * @return True if b should be pre-transposed else false.
2126 */
2127 bool pretranpose_B() const
2128 {
2129 return _pretranpose_B;
2130 };
2131 /** Set pre-transpose b flag
2132 *
2133 * @param[in] flag Flag to set
2134 */
2135 void set_pretranpose_B(bool flag)
2136 {
2137 _pretranpose_B = flag;
2138 }
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002139 /** Activation layer to apply after the matrix multiplication
2140 *
2141 * @return ActivationLayerInfo object
2142 */
2143 ActivationLayerInfo activation_info() const
2144 {
2145 return _activation_info;
2146 }
SiCongLi2e5fd632020-03-02 15:39:15 +00002147 /** Set activation layer info
2148 *
2149 * @param[in] activation_info ActivationLayerInfo object to set
2150 */
2151 void set_activation_info(const ActivationLayerInfo &activation_info)
2152 {
2153 _activation_info = activation_info;
2154 }
Chunosov5124be52017-11-22 20:42:13 +07002155
2156private:
Georgios Pinitas37d080f2019-06-21 18:43:12 +01002157 bool _is_a_reshaped;
2158 bool _is_b_reshaped;
2159 bool _reshape_b_only_on_first_run;
2160 int _depth_output_gemm3d;
2161 bool _reinterpret_input_as_3d;
2162 bool _retain_internal_weights;
2163 GEMMLowpOutputStageInfo _gemmlowp_output_stage;
2164 bool _fp_mixed_precision;
2165 bool _broadcast_bias;
2166 bool _pretranpose_B;
Gian Marco Iodicef3622be2019-07-29 14:27:16 +01002167 ActivationLayerInfo _activation_info;
Chunosov5124be52017-11-22 20:42:13 +07002168};
2169
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00002170/** Winograd information */
2171struct WinogradInfo
2172{
2173 /** Default constructor
2174 *
2175 * @param[in] output_tile_sz Width and height of the output tile
2176 * @param[in] kernel_sz Width and height of the kernel
2177 * @param[in] input_dims Width and height of the input tensor before the convolution is applied
2178 * @param[in] conv_info Convolution info (Pads, strides)
2179 * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied
2180 */
2181 WinogradInfo(Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout)
2182 : output_tile_size(output_tile_sz), kernel_size(kernel_sz), input_dimensions(input_dims), convolution_info(conv_info), output_data_layout(data_layout)
2183 {
2184 }
2185
2186 Size2D output_tile_size{}; /**< Width and height of the output tile */
2187 Size2D kernel_size{}; /**< Width and height of the kernel*/
2188 Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */
2189 PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */
2190 DataLayout output_data_layout{ DataLayout::NCHW }; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */
2191};
2192
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002193/** IO formatting information class*/
2194struct IOFormatInfo
2195{
2196 /** Precision type used when printing floating point numbers */
2197 enum class PrecisionType
2198 {
2199 Default, /**< Default precision to the one that the current stream has */
2200 Custom, /**< Custom precision specified by the user using the precision parameter */
2201 Full /**< The maximum precision of the floating point representation */
2202 };
2203
2204 /** Specifies the area to be printed, used by Tensor objects */
2205 enum class PrintRegion
2206 {
2207 ValidRegion, /**< Prints the valid region of the Tensor object */
2208 NoPadding, /**< Prints the Tensor object without the padding */
2209 Full /**< Print the tensor object including padding */
2210 };
2211
Alex Gildayc357c472018-03-21 13:54:09 +00002212 /** Construct a set of IO formatting information.
2213 *
2214 * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion.
2215 * @param[in] precision_type Precision type for floating point numbers. Default: stream default.
2216 * @param[in] precision Precision value for float point numbers. Default: 10.
2217 * @param[in] align_columns Whether to align columns when printed. Default: true.
2218 * @param[in] element_delim Delimeter between elements. Default: " ".
2219 * @param[in] row_delim Delimenter between rows. Default: "\n".
2220 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002221 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
2222 PrecisionType precision_type = PrecisionType::Default,
2223 unsigned int precision = 10,
2224 bool align_columns = true,
2225 std::string element_delim = " ",
2226 std::string row_delim = "\n")
2227 : print_region(print_region),
2228 precision_type(precision_type),
2229 precision(precision),
2230 element_delim(element_delim),
2231 row_delim(row_delim),
2232 align_columns(align_columns)
2233 {
2234 }
2235
Alex Gildayc357c472018-03-21 13:54:09 +00002236 /** Area to be printed by Tensor objects */
2237 PrintRegion print_region;
2238 /** Floating point precision type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002239 PrecisionType precision_type;
Alex Gildayc357c472018-03-21 13:54:09 +00002240 /** Floating point precision */
2241 unsigned int precision;
2242 /** Element delimeter */
2243 std::string element_delim;
2244 /** Row delimeter */
2245 std::string row_delim;
2246 /** Align columns */
2247 bool align_columns;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01002248};
Georgios Pinitasd8734b52017-12-22 15:27:52 +00002249} // namespace arm_compute
Michalis Spyrouf4643372019-11-29 16:17:13 +00002250#endif /* ARM_COMPUTE_TYPES_H */