blob: 1ce44ee2e8994ce794b5e6e95cd003afba81801d [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Manuel Bottinicc5171b2019-01-09 17:04:39 +00002 * Copyright (c) 2016-2019 ARM Limited.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01003 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_TYPES_H__
25#define __ARM_COMPUTE_TYPES_H__
26
27#include "arm_compute/core/Coordinates.h"
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000028#include "arm_compute/core/QAsymm8.h"
29#include "arm_compute/core/Rounding.h"
Isabella Gottardi6e464c32018-01-26 12:32:45 +000030#include "arm_compute/core/Size2D.h"
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000031#include "arm_compute/core/Strides.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010032#include "arm_compute/core/TensorShape.h"
Georgios Pinitas583137c2017-08-31 18:12:42 +010033#include "support/Half.h"
Anthony Barbier6ff3b192017-09-04 18:44:23 +010034
Michel Iwaniec5dfeae62017-11-29 10:48:23 +000035#include <cmath>
Anthony Barbier6ff3b192017-09-04 18:44:23 +010036#include <cstddef>
37#include <cstdint>
38#include <string>
39#include <utility>
40
41namespace arm_compute
42{
Georgios Pinitas583137c2017-08-31 18:12:42 +010043/** 16-bit floating point type */
44using half = half_float::half;
45
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000046/** Permutation vector */
47using PermutationVector = Strides;
Georgios Pinitas77589b52018-08-21 14:41:35 +010048/** Bidirectional strides */
49using BiStrides = Coordinates;
Georgios Pinitas8795ffb2017-12-01 16:13:40 +000050
Anthony Barbier6ff3b192017-09-04 18:44:23 +010051/** Image colour formats */
52enum class Format
53{
Daniil Efremov02bf80d2017-11-22 00:26:51 +070054 UNKNOWN, /**< Unknown image format */
55 U8, /**< 1 channel, 1 U8 per channel */
56 S16, /**< 1 channel, 1 S16 per channel */
57 U16, /**< 1 channel, 1 U16 per channel */
58 S32, /**< 1 channel, 1 S32 per channel */
59 U32, /**< 1 channel, 1 U32 per channel */
60 F16, /**< 1 channel, 1 F16 per channel */
61 F32, /**< 1 channel, 1 F32 per channel */
62 UV88, /**< 2 channel, 1 U8 per channel */
63 RGB888, /**< 3 channels, 1 U8 per channel */
64 RGBA8888, /**< 4 channels, 1 U8 per channel */
65 YUV444, /**< A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */
66 YUYV422, /**< A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */
67 NV12, /**< A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */
68 NV21, /**< A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */
69 IYUV, /**< A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */
70 UYVY422 /**< A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010071};
72
73/** Available data types */
74enum class DataType
75{
Alex Gildayc357c472018-03-21 13:54:09 +000076 UNKNOWN, /**< Unknown data type */
77 U8, /**< unsigned 8-bit number */
78 S8, /**< signed 8-bit number */
Alex Gildayc357c472018-03-21 13:54:09 +000079 QASYMM8, /**< quantized, asymmetric fixed-point 8-bit number */
80 U16, /**< unsigned 16-bit number */
81 S16, /**< signed 16-bit number */
Alex Gildayc357c472018-03-21 13:54:09 +000082 U32, /**< unsigned 32-bit number */
83 S32, /**< signed 32-bit number */
Alex Gildayc357c472018-03-21 13:54:09 +000084 U64, /**< unsigned 64-bit number */
85 S64, /**< signed 64-bit number */
86 F16, /**< 16-bit floating-point number */
87 F32, /**< 32-bit floating-point number */
88 F64, /**< 64-bit floating-point number */
89 SIZET /**< size_t */
Anthony Barbier6ff3b192017-09-04 18:44:23 +010090};
91
Daniil Efremov02bf80d2017-11-22 00:26:51 +070092/** Available Sampling Policies */
93enum class SamplingPolicy
94{
95 CENTER, /**< Samples are taken at pixel center */
96 TOP_LEFT /**< Samples are taken at pixel top left corner */
97};
98
Anthony Barbier6ff3b192017-09-04 18:44:23 +010099/** Constant value of the border pixels when using BorderMode::CONSTANT */
100constexpr uint8_t CONSTANT_BORDER_VALUE = 199;
101
Alex Gildayc357c472018-03-21 13:54:09 +0000102/** Constant value used to indicate a half-scale pyramid */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100103constexpr float SCALE_PYRAMID_HALF = 0.5f;
104
Alex Gildayc357c472018-03-21 13:54:09 +0000105/** Constant value used to indicate a ORB scaled pyramid */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100106constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01;
107
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000108/** [DataLayout enum definition] **/
109
Georgios Pinitas4074c992018-01-30 18:13:46 +0000110/** Supported tensor data layouts */
111enum class DataLayout
112{
Alex Gildayc357c472018-03-21 13:54:09 +0000113 UNKNOWN, /**< Unknown data layout */
114 NCHW, /**< Num samples, channels, height, width */
115 NHWC /**< Num samples, height, width, channels */
Georgios Pinitas4074c992018-01-30 18:13:46 +0000116};
Vidhya Sudhan Loganathand646ae12018-11-19 15:18:20 +0000117/** [DataLayout enum definition] **/
Georgios Pinitas4074c992018-01-30 18:13:46 +0000118
Isabella Gottardid17a6772018-02-27 17:41:55 +0000119/** Supported tensor data layout dimensions */
120enum class DataLayoutDimension
121{
Alex Gildayc357c472018-03-21 13:54:09 +0000122 CHANNEL, /**< channel */
123 HEIGHT, /**< height */
124 WIDTH, /**< width */
125 BATCHES /**< batches */
Isabella Gottardid17a6772018-02-27 17:41:55 +0000126};
127
Georgios Pinitas7900a9e2018-11-23 11:44:58 +0000128/** Available ConvolutionMethod*/
129enum class ConvolutionMethod
130{
131 GEMM, /**< Convolution using GEMM */
132 DIRECT, /**< Direct convolution */
133 WINOGRAD /**< Convolution using Winograd */
134};
135
136/** Supported comparison operations */
137enum class ComparisonOperation
138{
139 Equal, /**< Equal comparison ( \f$ x == y \f$ ) */
140 NotEqual, /**< NotEqual comparison ( \f$ x != y \f$ ) */
141 Greater, /**< Greater comparison ( \f$ x > y \f$ ) */
142 GreaterEqual, /**< Greater equal comparison ( \f$ x >= y \f$ ) */
143 Less, /**< Less comparison ( \f$ x < y \f$ ) */
144 LessEqual /**< Less equal comparison ( \f$ x <= y \f$ ) */
145};
146
Michel Iwaniec00633802017-10-12 14:14:15 +0100147/** Quantization settings (used for QASYMM8 data type) */
148struct QuantizationInfo
149{
Alex Gildayc357c472018-03-21 13:54:09 +0000150 /** Default constructor */
Georgios Pinitasf8d8f3a2018-06-06 17:57:04 +0100151 QuantizationInfo() noexcept
152 : scale(0.0f),
153 offset(0)
Michel Iwaniec00633802017-10-12 14:14:15 +0100154 {
155 }
156
Alex Gildayc357c472018-03-21 13:54:09 +0000157 /** Construct quantization info.
158 *
159 * @param[in] scale Scale.
160 * @param[in] offset Offset.
161 */
Michel Iwaniec00633802017-10-12 14:14:15 +0100162 QuantizationInfo(float scale, int offset)
163 : scale(scale), offset(offset)
164 {
165 }
166
Alex Gildayc357c472018-03-21 13:54:09 +0000167 /** Check whether equal to a given quantization info.
168 *
169 * @param[in] other Other quantization info.
170 *
171 * @return True if the given quantization info is the same.
172 */
Georgios Pinitas08346e92018-10-16 19:10:46 +0100173 bool operator==(const QuantizationInfo &other) const
Daniil Efremoveed841c2017-11-09 19:05:25 +0700174 {
175 return scale == other.scale && offset == other.offset;
176 }
177
Alex Gildayc357c472018-03-21 13:54:09 +0000178 /** Check whether not equal to a given quantization info.
179 *
180 * @param[in] other Other quantization info.
181 *
182 * @return True if the given quantization info is not the same.
183 */
Georgios Pinitas08346e92018-10-16 19:10:46 +0100184 bool operator!=(const QuantizationInfo &other) const
Daniil Efremoveed841c2017-11-09 19:05:25 +0700185 {
186 return !(*this == other);
187 }
188
Michel Iwaniec00633802017-10-12 14:14:15 +0100189 float scale; /**< scale */
190 int offset; /**< offset */
191
Alex Gildayc357c472018-03-21 13:54:09 +0000192 /** Quantizes a value using the scale/offset in this QuantizationInfo
193 *
194 * @param[in] value Value to quantize.
195 * @param[in] rounding_policy Policy to use when rounding.
196 *
197 * @return the quantized value.
198 */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000199 qasymm8_t quantize(float value, RoundingPolicy rounding_policy) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100200 {
201 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::quantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000202 return sqcvt_qasymm8_f32(value, scale, offset, rounding_policy);
Michel Iwaniec00633802017-10-12 14:14:15 +0100203 }
204
Alex Gildayc357c472018-03-21 13:54:09 +0000205 /** Dequantizes a value using the scale/offset in this QuantizationInfo
206 *
207 * @param[in] value Value to dequantize.
208 *
209 * @return the original value before quantization.
210 */
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000211 float dequantize(qasymm8_t value) const
Michel Iwaniec00633802017-10-12 14:14:15 +0100212 {
213 ARM_COMPUTE_ERROR_ON_MSG(scale == 0, "QuantizationInfo::dequantize: scale == 0");
Michel Iwaniec5dfeae62017-11-29 10:48:23 +0000214 return scvt_f32_qasymm8(value, scale, offset);
Michel Iwaniec00633802017-10-12 14:14:15 +0100215 }
216
Alex Gildayc357c472018-03-21 13:54:09 +0000217 /** Indicates whether this QuantizationInfo has valid settings or not
218 *
219 * @return True if the this has invalid settings.
220 */
Michel Iwaniec00633802017-10-12 14:14:15 +0100221 bool empty() const
222 {
223 return scale == 0;
224 }
225};
226
Alex Gildayc357c472018-03-21 13:54:09 +0000227/** Container for valid region of a window */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100228struct ValidRegion
229{
Alex Gildayc357c472018-03-21 13:54:09 +0000230 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100231 ValidRegion()
232 : anchor{}, shape{}
233 {
234 }
235
Alex Gildayc357c472018-03-21 13:54:09 +0000236 /** Allow instances of this class to be copy constructed */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100237 ValidRegion(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000238 /** Allow instances of this class to be move constructed */
239 ValidRegion(ValidRegion &&) = default;
240 /** Allow instances of this class to be copied */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100241 ValidRegion &operator=(const ValidRegion &) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000242 /** Allow instances of this class to be moved */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100243 ValidRegion &operator=(ValidRegion &&) = default;
Alex Gildayc357c472018-03-21 13:54:09 +0000244 /** Default destructor */
245 ~ValidRegion() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100246
Alex Gildayc357c472018-03-21 13:54:09 +0000247 /** Constructor for a valid region with default number of dimensions
248 *
249 * @param[in] an_anchor Anchor for the start of the valid region.
250 * @param[in] a_shape Shape of the valid region.
251 *
252 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000253 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape)
254 : anchor{ an_anchor }, shape{ a_shape }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100255 {
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000256 anchor.set_num_dimensions(std::max(anchor.num_dimensions(), shape.num_dimensions()));
257 }
258
Alex Gildayc357c472018-03-21 13:54:09 +0000259 /** Constructor for a valid region with specified number of dimensions
260 *
261 * @param[in] an_anchor Anchor for the start of the valid region.
262 * @param[in] a_shape Shape of the valid region.
263 * @param[in] num_dimensions Number of dimensions (must be >= number of dimensions of anchor and shape).
264 *
265 */
Diego Lopez Recasbcbc9702017-12-18 11:28:27 +0000266 ValidRegion(const Coordinates &an_anchor, const TensorShape &a_shape, size_t num_dimensions)
267 : anchor{ an_anchor }, shape{ a_shape }
268 {
269 ARM_COMPUTE_ERROR_ON(num_dimensions < std::max(anchor.num_dimensions(), shape.num_dimensions()));
270 anchor.set_num_dimensions(num_dimensions);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100271 }
272
273 /** Return the start of the valid region for the given dimension @p d */
274 int start(unsigned int d) const
275 {
276 return anchor[d];
277 }
278
279 /** Return the end of the valid region for the given dimension @p d */
280 int end(unsigned int d) const
281 {
282 return anchor[d] + shape[d];
283 }
284
Diego Lopez Recas35ceeb22017-12-04 18:56:10 +0000285 /** Accessor to set the value of anchor and shape for one of the dimensions.
286 *
287 * @param[in] dimension Dimension for which the value is set.
288 * @param[in] start Value to be set in anchor for the dimension.
289 * @param[in] size Value to be set in shape for the dimension.
290 *
291 * @return *this.
292 */
293 ValidRegion &set(size_t dimension, int start, size_t size)
294 {
295 anchor.set(dimension, start);
296 shape.set(dimension, size);
297 return *this;
298 }
299
Alex Gildayc357c472018-03-21 13:54:09 +0000300 Coordinates anchor; /**< Anchor for the start of the valid region. */
301 TensorShape shape; /**< Shape of the valid region. */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100302};
303
304/** Methods available to handle borders */
305enum class BorderMode
306{
307 UNDEFINED, /**< Borders are left undefined */
308 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
309 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
310};
311
312/** Container for 2D border size */
313struct BorderSize
314{
315 /** Empty border, i.e. no border */
316 constexpr BorderSize()
317 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
318 {
319 }
320
321 /** Border with equal size around the 2D plane */
Moritz Pflanzer7655a672017-09-23 11:57:33 +0100322 explicit constexpr BorderSize(unsigned int size)
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100323 : top{ size }, right{ size }, bottom{ size }, left{ size }
324 {
325 }
326
327 /** Border with same size for top/bottom and left/right */
328 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
329 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
330 {
331 }
332
333 /** Border with different sizes */
334 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
335 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
336 {
337 }
338
339 /** Check if the entire border is zero */
340 constexpr bool empty() const
341 {
342 return top == 0 && right == 0 && bottom == 0 && left == 0;
343 }
344
345 /** Check if the border is the same size on all sides */
346 constexpr bool uniform() const
347 {
348 return top == right && top == bottom && top == left;
349 }
350
Alex Gildayc357c472018-03-21 13:54:09 +0000351 /** Scale this border size.
352 *
353 * @param[in] scale Scale to multiply border size by.
354 *
355 * @return *this.
356 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100357 BorderSize &operator*=(float scale)
358 {
359 top *= scale;
360 right *= scale;
361 bottom *= scale;
362 left *= scale;
363
364 return *this;
365 }
366
Alex Gildayc357c472018-03-21 13:54:09 +0000367 /** Scale a copy of this border size.
368 *
369 * @param[in] scale Scale to multiply border size by.
370 *
371 * @return a scaled copy of this.
372 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100373 BorderSize operator*(float scale)
374 {
375 BorderSize size = *this;
376 size *= scale;
377
378 return size;
379 }
380
Alex Gildayc357c472018-03-21 13:54:09 +0000381 /** Limit this border size.
382 *
383 * @param[in] limit Border size to limit this border size to.
384 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100385 void limit(const BorderSize &limit)
386 {
387 top = std::min(top, limit.top);
388 right = std::min(right, limit.right);
389 bottom = std::min(bottom, limit.bottom);
390 left = std::min(left, limit.left);
391 }
392
Alex Gildayc357c472018-03-21 13:54:09 +0000393 unsigned int top; /**< top of the border */
394 unsigned int right; /**< right of the border */
395 unsigned int bottom; /**< bottom of the border */
396 unsigned int left; /**< left of the border */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100397};
398
Alex Gildayc357c472018-03-21 13:54:09 +0000399/** Container for 2D padding size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100400using PaddingSize = BorderSize;
401
402/** Policy to handle overflow */
403enum class ConvertPolicy
404{
405 WRAP, /**< Wrap around */
406 SATURATE /**< Saturate */
407};
408
409/** Interpolation method */
410enum class InterpolationPolicy
411{
412 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
413 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
414 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 */
415};
416
417/** Bilinear Interpolation method used by LKTracker */
418enum class BilinearInterpolation
419{
Alex Gildayc357c472018-03-21 13:54:09 +0000420 BILINEAR_OLD_NEW, /**< Old-new method */
421 BILINEAR_SCHARR /**< Scharr method */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100422};
423
424/** Threshold mode */
425enum class ThresholdType
426{
427 BINARY, /**< Threshold with one value */
428 RANGE /**< Threshold with two values*/
429};
430
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100431/** Termination criteria */
432enum class Termination
433{
Alex Gildayc357c472018-03-21 13:54:09 +0000434 TERM_CRITERIA_EPSILON, /**< Terminate when within epsilon of a threshold */
435 TERM_CRITERIA_ITERATIONS, /**< Terminate after a maximum number of iterations */
436 TERM_CRITERIA_BOTH /**< Terminate on whichever of the other conditions occurs first */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100437};
438
439/** Magnitude calculation type. */
440enum class MagnitudeType
441{
442 L1NORM, /**< L1 normalization type */
443 L2NORM /**< L2 normalization type */
444};
445
446/** Phase calculation type.
447 *
448 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
449 */
450enum class PhaseType
451{
452 SIGNED, /**< Angle range: [0, 360] */
453 UNSIGNED /**< Angle range: [0, 180] */
454};
455
456/** Keypoint type */
457struct KeyPoint
458{
459 int32_t x{ 0 }; /**< X coordinates */
460 int32_t y{ 0 }; /**< Y coordinates */
461 float strength{ 0.f }; /**< Strength of the point */
462 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
463 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
464 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
465 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
466};
467
Alex Gildayc357c472018-03-21 13:54:09 +0000468/** Internal key point */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100469using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
470
471/** Rectangle type */
472struct Rectangle
473{
474 uint16_t x; /**< Top-left x coordinate */
475 uint16_t y; /**< Top-left y coordinate */
476 uint16_t width; /**< Width of the rectangle */
477 uint16_t height; /**< Height of the rectangle */
478};
479
480/** Coordinate type */
481struct Coordinates2D
482{
483 int32_t x; /**< X coordinates */
484 int32_t y; /**< Y coordinates */
485};
486
487/** Coordinate type */
488struct Coordinates3D
489{
490 uint32_t x; /**< X coordinates */
491 uint32_t y; /**< Y coordinates */
492 uint32_t z; /**< Z coordinates */
493};
494
Giuseppe Rossinid7647d42018-07-17 18:13:13 +0100495/** Padding information as a pair of unsigned int start/end */
496using PaddingInfo = std::pair<uint32_t, uint32_t>;
497
498/** List of padding information */
499using PaddingList = std::vector<PaddingInfo>;
500
giuros013175fcf2018-11-21 09:59:17 +0000501/** Information to produce a tiled version of a Tensor */
502using Multiples = std::vector<uint32_t>;
503
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100504/** Available channels */
505enum class Channel
506{
507 UNKNOWN, /** Unknown channel format */
508 C0, /**< First channel (used by formats with unknown channel types). */
509 C1, /**< Second channel (used by formats with unknown channel types). */
510 C2, /**< Third channel (used by formats with unknown channel types). */
511 C3, /**< Fourth channel (used by formats with unknown channel types). */
512 R, /**< Red channel. */
513 G, /**< Green channel. */
514 B, /**< Blue channel. */
515 A, /**< Alpha channel. */
516 Y, /**< Luma channel. */
517 U, /**< Cb/U channel. */
518 V /**< Cr/V/Value channel. */
519};
520
521/** Available matrix patterns */
522enum class MatrixPattern
523{
524 BOX, /**< Box pattern matrix. */
525 CROSS, /**< Cross pattern matrix. */
526 DISK, /**< Disk pattern matrix. */
527 OTHER /**< Any other matrix pattern. */
528};
529
530/** Available non linear functions. */
531enum class NonLinearFilterFunction : unsigned
532{
533 MEDIAN = 0, /**< Non linear median filter. */
534 MIN = 1, /**< Non linear erode. */
535 MAX = 2, /**< Non linear dilate. */
536};
537
Georgios Pinitasd9769582017-08-03 10:19:40 +0100538/** Available reduction operations */
539enum class ReductionOperation
540{
Michalis Spyrou7930db42018-11-22 17:36:28 +0000541 ARG_IDX_MAX, /**< Index of the max value */
Manuel Bottinib412fab2018-12-10 17:40:23 +0000542 ARG_IDX_MIN, /**< Index of the min value */
543 MEAN_SUM, /**< Mean of sum */
544 PROD, /**< Product */
545 SUM_SQUARE, /**< Sum of squares */
546 SUM /**< Sum */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100547};
548
giuros01164a2722018-11-20 18:34:46 +0000549/** Available element-wise operations */
550enum class ArithmeticOperation
551{
552 ADD, /**< (x + y) */
553 SUB, /**< (x - y) */
554 DIV, /**< (x / y) */
555 MIN, /**< Min(x, y) */
556 MAX, /**< Max(x, y) */
557 SQUARED_DIFF, /**< (x - y)^2 */
558};
559
Michalis Spyroue9362622018-11-23 17:41:37 +0000560/** Available element wise unary operations */
561enum class ElementWiseUnary
562{
563 RSQRT, /**< Reverse square root */
564 EXP, /**< Exponential */
565};
566
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100567/** The normalization type used for the normalization layer */
568enum class NormType
569{
570 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
571 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
572 CROSS_MAP /**< Normalization applied cross maps */
573};
574
575/** Normalization type for Histogram of Oriented Gradients (HOG) */
576enum class HOGNormType
577{
578 L2_NORM = 1, /**< L2-norm */
579 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
580 L1_NORM = 3 /**< L1 norm */
581};
582
583/** Detection window used for the object detection. The detection window keeps the following information:
584 *
585 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
586 * -# Index of the class used for evaluating which class the detection window belongs to
587 * -# Confidence value (score) obtained with the classifier
588 */
589struct DetectionWindow
590{
591 uint16_t x{ 0 }; /**< Top-left x coordinate */
592 uint16_t y{ 0 }; /**< Top-left y coordinate */
593 uint16_t width{ 0 }; /**< Width of the detection window */
594 uint16_t height{ 0 }; /**< Height of the detection window */
595 uint16_t idx_class{ 0 }; /**< Index of the class */
596 float score{ 0.f }; /**< Confidence value for the detection window */
597};
598
599/** Dimension rounding type when down-scaling on CNNs
600 * @note Used in pooling and convolution layer
601 */
602enum class DimensionRoundingType
603{
604 FLOOR, /**< Floor rounding */
605 CEIL /**< Ceil rounding */
606};
607
608/** Available pooling types */
609enum class PoolingType
610{
611 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100612 AVG, /**< Average Pooling */
613 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100614};
615
Michalis Spyrou2709d612018-09-19 09:46:47 +0100616/** Available non maxima suppression types */
617enum class NMSType
618{
619 LINEAR, /**< Linear NMS */
620 GAUSSIAN, /**< Gaussian NMS */
621 ORIGINAL /**< Original NMS */
622};
623
624/** BoxWithNonMaximaSuppressionLimit Information class */
625class BoxNMSLimitInfo final
626{
627public:
628 /** Constructor
629 *
630 * @param[in] score_thresh (Optional) Score threshold.
631 * @param[in] nms (Optional) NMS value
632 * @param[in] detections (Optional) Number of detections
633 * @param[in] soft_nms_enabled (Optional) Enable SoftNMS
634 * @param[in] soft_nms_method (Optional) Soft NMS method
635 * @param[in] soft_nms_sigma (Optional) Soft NMS sigma value
636 * @param[in] soft_nms_min_score_thres (Optional) Soft NMS minimum score threshold
giuros01cd96a262018-10-03 12:44:35 +0100637 * @param[in] suppress_size (Optional) Filter out boxes based on their size. Defaults to false
638 * @param[in] min_size (Optional) Smaller boxes than min_size will be filtered out. Defaults to 1
639 * @param[in] im_width (Optional) Boxes whose centers (on the x axis) is beyond im_width will be filtered. Defaults to 1
640 * @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 +0100641 */
642 BoxNMSLimitInfo(float score_thresh = 0.05f, float nms = 0.3f,
643 int detections = 100, bool soft_nms_enabled = false,
644 NMSType soft_nms_method = NMSType::LINEAR,
giuros01cd96a262018-10-03 12:44:35 +0100645 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 +0100646 : _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),
giuros01cd96a262018-10-03 12:44:35 +0100647 _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 +0100648 {
649 }
650 /** Get the score threshold */
651 float score_thresh() const
652 {
653 return _score_thresh;
654 }
655 /** Get the NMS */
656 float nms() const
657 {
658 return _nms;
659 }
660 /** Get the number of detections */
661 int detections_per_im() const
662 {
663 return _detections_per_im;
664 }
665 /** Check if soft NMS is enabled */
666 bool soft_nms_enabled() const
667 {
668 return _soft_nms_enabled;
669 }
670 /** Get soft NMS method */
671 NMSType soft_nms_method() const
672 {
673 return _soft_nms_method;
674 }
675 /** Get soft NMS sigma */
676 float soft_nms_sigma() const
677 {
678 return _soft_nms_sigma;
679 }
680 /** Get soft nms min score threshold */
681 float soft_nms_min_score_thres() const
682 {
683 return _soft_nms_min_score_thres;
684 }
giuros01cd96a262018-10-03 12:44:35 +0100685 /** Get if NMS will suppress boxes based on their size/position */
686 bool suppress_size() const
687 {
688 return _suppress_size;
689 }
690 /** Get size suppression threshold */
691 float min_size() const
692 {
693 return _min_size;
694 }
695 /** Get image width (NMS may suppress boxes whose center sits beyond the image width) */
696 float im_width() const
697 {
698 return _im_width;
699 }
700 /** Get image height (NMS may suppress boxes whose center sits beyond the image height) */
701 float im_height() const
702 {
703 return _im_height;
704 }
Michalis Spyrou2709d612018-09-19 09:46:47 +0100705
706private:
707 float _score_thresh;
708 float _nms;
709 int _detections_per_im;
710 bool _soft_nms_enabled;
711 NMSType _soft_nms_method;
712 float _soft_nms_sigma;
713 float _soft_nms_min_score_thres;
giuros01cd96a262018-10-03 12:44:35 +0100714 bool _suppress_size;
715 float _min_size;
716 float _im_width;
717 float _im_height;
Michalis Spyrou2709d612018-09-19 09:46:47 +0100718};
719
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100720/** Padding and stride information class */
721class PadStrideInfo
722{
723public:
724 /** Constructor
725 *
726 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
727 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
728 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
729 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
730 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
731 */
732 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
733 unsigned int pad_x = 0, unsigned int pad_y = 0,
734 DimensionRoundingType round = DimensionRoundingType::FLOOR)
735 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100736 _pad_left(pad_x),
737 _pad_top(pad_y),
738 _pad_right(pad_x),
739 _pad_bottom(pad_y),
740 _round_type(round)
741 {
742 }
743 /** Constructor
744 *
745 * @param[in] stride_x Stride, in elements, across x.
746 * @param[in] stride_y Stride, in elements, across y.
747 * @param[in] pad_left Padding across x on the left, in elements.
748 * @param[in] pad_top Padding across y on the top, in elements.
749 * @param[in] pad_right Padding across x on the right, in elements.
750 * @param[in] pad_bottom Padding across y on the bottom, in elements.
751 * @param[in] round Dimensions rounding.
752 */
753 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
754 unsigned int pad_left, unsigned int pad_right,
755 unsigned int pad_top, unsigned int pad_bottom,
756 DimensionRoundingType round)
757 : _stride(std::make_pair(stride_x, stride_y)),
758 _pad_left(pad_left),
759 _pad_top(pad_top),
760 _pad_right(pad_right),
761 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100762 _round_type(round)
763 {
764 }
Alex Gildayc357c472018-03-21 13:54:09 +0000765 /** Get the stride.
766 *
767 * @return a pair: stride x, stride y.
768 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100769 std::pair<unsigned int, unsigned int> stride() const
770 {
771 return _stride;
772 }
Alex Gildayc357c472018-03-21 13:54:09 +0000773 /** Check whether the padding is symmetric.
774 *
775 * @return True if the padding is symmetric.
776 */
Anthony Barbier21f67d62018-02-16 15:17:48 +0000777 bool padding_is_symmetric() const
778 {
779 return (_pad_left == _pad_right) && (_pad_top == _pad_bottom);
780 }
Alex Gildayc357c472018-03-21 13:54:09 +0000781 /** Get the padding.
782 *
783 * @note This should only be used when the padding is symmetric.
784 *
785 * @return a pair: padding left/right, padding top/bottom
786 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100787 std::pair<unsigned int, unsigned int> pad() const
788 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100789 //this accessor should be used only when padding is symmetric
Anthony Barbier21f67d62018-02-16 15:17:48 +0000790 ARM_COMPUTE_ERROR_ON(!padding_is_symmetric());
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100791 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100792 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100793
Alex Gildayc357c472018-03-21 13:54:09 +0000794 /** Get the left padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100795 unsigned int pad_left() const
796 {
797 return _pad_left;
798 }
Alex Gildayc357c472018-03-21 13:54:09 +0000799 /** Get the right padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100800 unsigned int pad_right() const
801 {
802 return _pad_right;
803 }
Alex Gildayc357c472018-03-21 13:54:09 +0000804 /** Get the top padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100805 unsigned int pad_top() const
806 {
807 return _pad_top;
808 }
Alex Gildayc357c472018-03-21 13:54:09 +0000809 /** Get the bottom padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100810 unsigned int pad_bottom() const
811 {
812 return _pad_bottom;
813 }
814
Alex Gildayc357c472018-03-21 13:54:09 +0000815 /** Get the rounding type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100816 DimensionRoundingType round() const
817 {
818 return _round_type;
819 }
820
Alex Gildayc357c472018-03-21 13:54:09 +0000821 /** Check whether this has any padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100822 bool has_padding() const
823 {
824 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
825 }
826
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100827private:
828 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100829 unsigned int _pad_left;
830 unsigned int _pad_top;
831 unsigned int _pad_right;
832 unsigned int _pad_bottom;
833
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100834 DimensionRoundingType _round_type;
835};
836
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100837/** Fully connected layer info */
838struct FullyConnectedLayerInfo
839{
840 DataLayout weights_trained_layout{ DataLayout::NCHW }; /**< Layout that the weights have been trained with. */
841 bool transpose_weights{ true }; /**< Transpose weights if true. */
842 bool are_weights_reshaped{ false }; /**< Reshape the weights tensor if false. */
843 bool retain_internal_weights{ false }; /**< Retain internal reshaped weights. */
Georgios Pinitasc55cef12018-08-01 15:24:18 +0100844
845 /** Sets the weights trained data layout
846 *
847 * @param[in] layout Data layout that the weights were trained with
848 *
849 * @return Updated object
850 */
851 FullyConnectedLayerInfo &set_weights_trained_layout(DataLayout layout)
852 {
853 weights_trained_layout = layout;
854 return *this;
855 }
Georgios Pinitas195b0ba2018-08-02 17:18:51 +0100856 /** Sets the transpose weights flag
857 *
858 * @param[in] should_transpose_weights Boolean flag indicating if weights should be transposed
859 *
860 * @return Updated object
861 */
862 FullyConnectedLayerInfo &set_transpose_weights(bool should_transpose_weights)
863 {
864 transpose_weights = should_transpose_weights;
865 return *this;
866 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100867};
868
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100869/** PriorBox layer info */
870class PriorBoxLayerInfo final
871{
872public:
873 /** Default Constructor */
874 PriorBoxLayerInfo()
875 : _min_sizes(),
876 _variances(),
877 _offset(),
878 _flip(true),
879 _clip(false),
880 _max_sizes(),
881 _aspect_ratios(),
882 _img_size(),
883 _steps()
884 {
885 }
886 /** Constructor
887 *
888 * @param[in] min_sizes Min sizes vector.
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100889 * @param[in] variances Variances vector.
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100890 * @param[in] offset Offset value.
891 * @param[in] flip (Optional) Flip the aspect ratios.
892 * @param[in] clip (Optional) Clip coordinates so that they're within [0,1].
893 * @param[in] max_sizes (Optional) Max sizes vector.
894 * @param[in] aspect_ratios (Optional) Aspect ratios of the boxes.
895 * @param[in] img_size (Optional) Image size.
896 * @param[in] steps (Optional) Step values.
897 */
898 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 +0000899 const std::vector<float> &max_sizes = {}, const std::vector<float> &aspect_ratios = {},
900 const Coordinates2D &img_size = Coordinates2D{ 0, 0 }, const std::array<float, 2> &steps = { { 0.f, 0.f } })
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100901 : _min_sizes(min_sizes),
902 _variances(variances),
903 _offset(offset),
904 _flip(flip),
905 _clip(clip),
906 _max_sizes(max_sizes),
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100907 _aspect_ratios(),
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100908 _img_size(img_size),
909 _steps(steps)
910 {
911 _aspect_ratios.push_back(1.);
912 for(unsigned int i = 0; i < aspect_ratios.size(); ++i)
913 {
914 float ar = aspect_ratios[i];
915 bool already_exist = false;
916 for(auto ar_new : _aspect_ratios)
917 {
918 if(fabs(ar - ar_new) < 1e-6)
919 {
920 already_exist = true;
921 break;
922 }
923 }
924 if(!already_exist)
925 {
926 _aspect_ratios.push_back(ar);
927 if(flip)
928 {
929 _aspect_ratios.push_back(1.f / ar);
930 }
931 }
932 }
933 }
934 /** Get min sizes. */
935 std::vector<float> min_sizes() const
936 {
937 return _min_sizes;
938 }
939 /** Get min variances. */
940 std::vector<float> variances() const
941 {
942 return _variances;
943 }
944 /** Get the step coordinates */
945 std::array<float, 2> steps() const
946 {
947 return _steps;
948 }
949 /** Get the image size coordinates */
950 Coordinates2D img_size() const
951 {
952 return _img_size;
953 }
954 /** Get the offset */
955 float offset() const
956 {
957 return _offset;
958 }
959 /** Get the flip value */
960 bool flip() const
961 {
962 return _flip;
963 }
964 /** Get the clip value */
965 bool clip() const
966 {
967 return _clip;
968 }
969 /** Get max sizes. */
970 std::vector<float> max_sizes() const
971 {
972 return _max_sizes;
973 }
974 /** Get aspect ratios. */
975 std::vector<float> aspect_ratios() const
976 {
977 return _aspect_ratios;
978 }
979
980private:
981 std::vector<float> _min_sizes;
982 std::vector<float> _variances;
983 float _offset;
984 bool _flip;
985 bool _clip;
986 std::vector<float> _max_sizes;
987 std::vector<float> _aspect_ratios;
988 Coordinates2D _img_size;
989 std::array<float, 2> _steps;
990};
991
Isabella Gottardi05e56442018-11-16 11:26:52 +0000992/** Available Detection Output code types */
993enum class DetectionOutputLayerCodeType
994{
995 CORNER, /**< Use box corners */
996 CENTER_SIZE, /**< Use box centers and size */
997 CORNER_SIZE, /**< Use box centers and size */
998 TF_CENTER /**< Use box centers and size but flip x and y co-ordinates */
999};
1000
1001/** Detection Output layer info */
1002class DetectionOutputLayerInfo final
1003{
1004public:
1005 /** Default Constructor */
1006 DetectionOutputLayerInfo()
1007 : _num_classes(),
1008 _share_location(),
1009 _code_type(DetectionOutputLayerCodeType::CORNER),
1010 _keep_top_k(),
1011 _nms_threshold(),
1012 _top_k(),
1013 _background_label_id(),
1014 _confidence_threshold(),
1015 _variance_encoded_in_target(false),
1016 _eta(),
1017 _num_loc_classes()
1018 {
1019 _num_loc_classes = _share_location ? 1 : _num_classes;
1020 }
1021 /** Constructor
1022 *
1023 * @param[in] num_classes Number of classes to be predicted.
1024 * @param[in] share_location If true, bounding box are shared among different classes.
1025 * @param[in] code_type Type of coding method for bbox.
1026 * @param[in] keep_top_k Number of total bounding boxes to be kept per image after NMS step.
1027 * @param[in] nms_threshold Threshold to be used in NMS.
1028 * @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.
1029 * @param[in] background_label_id (Optional) Background label ID. If there is no background class, set it as -1.
1030 * @param[in] confidence_threshold (Optional) Only consider detections whose confidences are larger than a threshold. Default set to -FLT_MAX.
1031 * @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.
1032 * @param[in] eta (Optional) Eta.
1033 */
1034 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,
1035 float confidence_threshold = std::numeric_limits<float>::lowest(), bool variance_encoded_in_target = false, float eta = 1)
1036 : _num_classes(num_classes),
1037 _share_location(share_location),
1038 _code_type(code_type),
1039 _keep_top_k(keep_top_k),
1040 _nms_threshold(nms_threshold),
1041 _top_k(top_k),
1042 _background_label_id(background_label_id),
1043 _confidence_threshold(confidence_threshold),
1044 _variance_encoded_in_target(variance_encoded_in_target),
1045 _eta(eta),
1046 _num_loc_classes()
1047 {
1048 _num_loc_classes = _share_location ? 1 : _num_classes;
1049 }
1050 /** Get num classes. */
1051 int num_classes() const
1052 {
1053 return _num_classes;
1054 }
1055 /** Get share location. */
1056 bool share_location() const
1057 {
1058 return _share_location;
1059 }
1060 /** Get detection output code type. */
1061 DetectionOutputLayerCodeType code_type() const
1062 {
1063 return _code_type;
1064 }
1065 /** Get if variance encoded in target. */
1066 bool variance_encoded_in_target() const
1067 {
1068 return _variance_encoded_in_target;
1069 }
1070 /** Get the number of total bounding boxes to be kept per image. */
1071 int keep_top_k() const
1072 {
1073 return _keep_top_k;
1074 }
1075 /** Get nms threshold. */
1076 float nms_threshold() const
1077 {
1078 return _nms_threshold;
1079 }
1080 /** Get eta. */
1081 float eta() const
1082 {
1083 return _eta;
1084 }
1085 /** Get background label ID. */
1086 int background_label_id() const
1087 {
1088 return _background_label_id;
1089 }
1090 /** Get confidence threshold. */
1091 float confidence_threshold() const
1092 {
1093 return _confidence_threshold;
1094 }
1095 /** Get top K. */
1096 int top_k() const
1097 {
1098 return _top_k;
1099 }
1100 /** Get number of location classes. */
1101 int num_loc_classes() const
1102 {
1103 return _num_loc_classes;
1104 }
1105
1106private:
1107 int _num_classes;
1108 bool _share_location;
1109 DetectionOutputLayerCodeType _code_type;
1110 int _keep_top_k;
1111 float _nms_threshold;
1112 int _top_k;
1113 int _background_label_id;
1114 float _confidence_threshold;
1115 bool _variance_encoded_in_target;
1116 float _eta;
1117 int _num_loc_classes;
1118};
1119
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001120/** Pooling Layer Information class */
1121class PoolingLayerInfo
1122{
1123public:
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001124 /** Default Constructor */
1125 PoolingLayerInfo()
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001126 : _pool_type(PoolingType::MAX), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo()), _exclude_padding(false), _is_global_pooling(false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001127 {
1128 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001129 /** Default Constructor
1130 *
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001131 * @param[in] pool_type Pooling type @ref PoolingType.
1132 * @param[in] pool_size Pooling size, in elements, across x and y.
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001133 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
Georgios Pinitasadaae7e2017-10-30 15:56:32 +00001134 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1135 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1136 * Defaults to false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001137 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001138 explicit PoolingLayerInfo(PoolingType pool_type,
1139 unsigned int pool_size,
1140 PadStrideInfo pad_stride_info = PadStrideInfo(),
1141 bool exclude_padding = false)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001142 : _pool_type(pool_type), _pool_size(Size2D(pool_size, pool_size)), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
1143 {
1144 }
1145 /** Default Constructor
1146 *
1147 * @param[in] pool_type Pooling type @ref PoolingType.
1148 * @param[in] pool_size Pooling size, in elements, across x and y.
1149 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1150 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1151 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1152 * Defaults to false;
1153 */
1154 explicit PoolingLayerInfo(PoolingType pool_type,
1155 Size2D pool_size,
1156 PadStrideInfo pad_stride_info = PadStrideInfo(),
1157 bool exclude_padding = false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001158 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
1159 {
1160 }
1161 /** Default Constructor
1162 *
1163 * @note This constructor is used for global pooling
1164 *
1165 * @param[in] pool_type Pooling type @ref PoolingType.
1166 */
1167 explicit PoolingLayerInfo(PoolingType pool_type)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001168 : _pool_type(pool_type), _pool_size(Size2D()), _pad_stride_info(PadStrideInfo(1, 1, 0, 0)), _exclude_padding(false), _is_global_pooling(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001169 {
1170 }
Alex Gildayc357c472018-03-21 13:54:09 +00001171 /** Get the pooling type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001172 PoolingType pool_type() const
1173 {
1174 return _pool_type;
1175 }
Alex Gildayc357c472018-03-21 13:54:09 +00001176 /** Get the pooling size */
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001177 const Size2D &pool_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001178 {
1179 return _pool_size;
1180 }
Alex Gildayc357c472018-03-21 13:54:09 +00001181 /** Get the padding and stride */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001182 PadStrideInfo pad_stride_info() const
1183 {
1184 return _pad_stride_info;
1185 }
Alex Gildayc357c472018-03-21 13:54:09 +00001186 /** Check if padding is excluded in calculations */
Georgios Pinitasadaae7e2017-10-30 15:56:32 +00001187 bool exclude_padding() const
1188 {
1189 return _exclude_padding;
1190 }
Alex Gildayc357c472018-03-21 13:54:09 +00001191 /** Check if is global pooling */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001192 bool is_global_pooling() const
1193 {
1194 return _is_global_pooling;
1195 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001196
1197private:
1198 PoolingType _pool_type;
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001199 Size2D _pool_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001200 PadStrideInfo _pad_stride_info;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +00001201 bool _exclude_padding;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001202 bool _is_global_pooling;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001203};
1204
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001205/** ROI Pooling Layer Information class */
giuros0118870812018-09-13 09:31:40 +01001206class ROIPoolingLayerInfo final
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001207{
1208public:
giuros0118870812018-09-13 09:31:40 +01001209 /** Constructor
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001210 *
giuros0118870812018-09-13 09:31:40 +01001211 * @param[in] pooled_width Pooled width of the layer.
1212 * @param[in] pooled_height Pooled height of the layer.
1213 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
1214 * @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 +01001215 */
giuros0118870812018-09-13 09:31:40 +01001216 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale, unsigned int sampling_ratio = 0)
1217 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale), _sampling_ratio(sampling_ratio)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001218 {
1219 }
Alex Gildayc357c472018-03-21 13:54:09 +00001220 /** Get the pooled width of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001221 unsigned int pooled_width() const
1222 {
1223 return _pooled_width;
1224 }
Alex Gildayc357c472018-03-21 13:54:09 +00001225 /** Get the pooled height of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001226 unsigned int pooled_height() const
1227 {
1228 return _pooled_height;
1229 }
Alex Gildayc357c472018-03-21 13:54:09 +00001230 /** Get the spatial scale */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001231 float spatial_scale() const
1232 {
1233 return _spatial_scale;
1234 }
giuros0118870812018-09-13 09:31:40 +01001235 /** Get sampling ratio */
1236 unsigned int sampling_ratio() const
1237 {
1238 return _sampling_ratio;
1239 }
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001240
1241private:
1242 unsigned int _pooled_width;
1243 unsigned int _pooled_height;
1244 float _spatial_scale;
giuros0118870812018-09-13 09:31:40 +01001245 unsigned int _sampling_ratio;
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001246};
1247
giuros01cd96a262018-10-03 12:44:35 +01001248/** Generate Proposals Information class */
1249class GenerateProposalsInfo
1250{
1251public:
1252 /** Constructor
1253 *
1254 * @param[in] im_width Width of the original image
1255 * @param[in] im_height Height of the original image
1256 * @param[in] im_scale Scale applied to the original image
1257 * @param[in] spatial_scale (Optional)Scale applied to the feature map. Defaults to 1.0
1258 * @param[in] pre_nms_topN (Optional)Number of the best scores to be selected from the transformations. Defaults to 6000.
1259 * @param[in] post_nms_topN (Optional)Number of the best scores to be selected from the NMS operation. Defaults to 300.
1260 * @param[in] nms_thres (Optional)NMS overlap threshold. Defaults to 0.7.
1261 * @param[in] min_size (Optional)Size used to validate the anchors produced. Defaults to 16.
1262 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region of interest). Defaults to 4.
1263 */
1264 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,
1265 size_t values_per_roi = 4)
1266 : _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),
1267 _min_size(min_size), _values_per_roi(values_per_roi)
1268 {
1269 }
1270
1271 /* Get the original height */
1272 float im_height() const
1273 {
1274 return _im_height;
1275 }
1276 /* Get the original width */
1277 float im_width() const
1278 {
1279 return _im_width;
1280 }
1281 /* Get the image scale */
1282 float im_scale() const
1283 {
1284 return _im_scale;
1285 }
1286 /* Get the value of how many best scores to select (before NMS) */
1287 int pre_nms_topN() const
1288 {
1289 return _pre_nms_topN;
1290 }
1291 /* Get the value of how many best scores to select (after NMS) */
1292 int post_nms_topN() const
1293 {
1294 return _post_nms_topN;
1295 }
1296 /* Get the NMS overlap threshold */
1297 float nms_thres() const
1298 {
1299 return _nms_thres;
1300 }
1301 /* Get the minimal size */
1302 float min_size() const
1303 {
1304 return _min_size;
1305 }
1306 /* Get the spatial scale to be applied to the feature maps */
1307 float spatial_scale() const
1308 {
1309 return _spatial_scale;
1310 }
1311 /* Get the values used to represent a ROI(Region of interest)*/
1312 size_t values_per_roi() const
1313 {
1314 return _values_per_roi;
1315 }
1316
1317private:
1318 float _im_height;
1319 float _im_width;
1320 float _im_scale;
1321 float _spatial_scale;
1322 int _pre_nms_topN;
1323 int _post_nms_topN;
1324 float _nms_thres;
1325 float _min_size;
1326 size_t _values_per_roi;
1327};
1328
1329/** ComputeAnchors information class */
1330class ComputeAnchorsInfo
1331{
1332public:
1333 /** Constructor
1334 *
1335 * @param[in] feat_width Feature map width
1336 * @param[in] feat_height Feature map height
1337 * @param[in] spatial_scale Feature map scale
1338 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region Of Interest). Defaults to 4
1339 */
1340 ComputeAnchorsInfo(float feat_width, float feat_height, float spatial_scale, size_t values_per_roi = 4)
1341 : _feat_height(feat_height),
1342 _feat_width(feat_width),
1343 _spatial_scale(spatial_scale),
1344 _values_per_roi(values_per_roi)
1345 {
1346 }
1347
1348 /* Get the height of the feature map */
1349 float feat_height() const
1350 {
1351 return _feat_height;
1352 }
1353
1354 /* Get the width of the feature map */
1355 float feat_width() const
1356 {
1357 return _feat_width;
1358 }
1359
1360 /* Get the scale of the feature map */
1361 float spatial_scale() const
1362 {
1363 return _spatial_scale;
1364 }
1365
1366 /* Get the values used to represent a ROI(Region Of Interest)*/
1367 size_t values_per_roi() const
1368 {
1369 return _values_per_roi;
1370 }
1371
1372private:
1373 float _feat_height;
1374 float _feat_width;
1375 float _spatial_scale;
1376 size_t _values_per_roi;
1377};
1378
giuros01c04a0e82018-10-03 12:44:35 +01001379/** Bounding Box Transform information class */
giuros01d696cb62018-11-16 10:39:59 +00001380class BoundingBoxTransformInfo final
giuros01c04a0e82018-10-03 12:44:35 +01001381{
1382public:
1383 /** Constructor
1384 *
giuros01d696cb62018-11-16 10:39:59 +00001385 * @param[in] img_width Width of the original image
1386 * @param[in] img_height Height, of the original image
1387 * @param[in] scale Scale of the original image
1388 * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false
1389 * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones
1390 * @param[in] correct_transform_coords (Optional)Correct bounding box transform coordinates. Defaults to false
1391 * @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 +01001392 */
giuros01d696cb62018-11-16 10:39:59 +00001393 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 =
1394 false,
1395 float bbox_xform_clip =
1396 4.135166556742356f)
1397 : _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 +01001398 {
1399 }
1400
1401 std::array<float, 4> weights() const
1402 {
1403 return _weights;
1404 }
1405
1406 float bbox_xform_clip() const
1407 {
1408 return _bbox_xform_clip;
1409 }
1410
1411 float img_height() const
1412 {
1413 return _img_height;
1414 }
1415
1416 float img_width() const
1417 {
1418 return _img_width;
1419 }
1420
1421 float scale() const
1422 {
1423 return _scale;
1424 }
1425
1426 bool apply_scale() const
1427 {
1428 return _apply_scale;
1429 }
1430
giuros01d696cb62018-11-16 10:39:59 +00001431 bool correct_transform_coords() const
1432 {
1433 return _correct_transform_coords;
1434 }
1435
giuros01c04a0e82018-10-03 12:44:35 +01001436private:
1437 float _img_width;
1438 float _img_height;
1439 float _scale;
1440 bool _apply_scale;
giuros01d696cb62018-11-16 10:39:59 +00001441 bool _correct_transform_coords;
giuros01c04a0e82018-10-03 12:44:35 +01001442 std::array<float, 4> _weights;
1443 float _bbox_xform_clip;
1444};
1445
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001446/** Activation Layer Information class */
1447class ActivationLayerInfo
1448{
1449public:
1450 /** Available activation functions */
1451 enum class ActivationFunction
1452 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001453 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
1454 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
1455 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
1456 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
1457 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
Manuel Bottini581c8982019-02-07 10:31:57 +00001458 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 +01001459 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
1460 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
1461 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
1462 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
1463 LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001464 };
1465
Giorgio Arena11674872018-02-07 15:38:12 +00001466 ActivationLayerInfo() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001467 /** Default Constructor
1468 *
1469 * @param[in] f The activation function to use.
1470 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001471 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
1472 * @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 +01001473 */
1474 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
Giorgio Arena11674872018-02-07 15:38:12 +00001475 : _act(f), _a(a), _b(b), _enabled(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001476 {
1477 }
Alex Gildayc357c472018-03-21 13:54:09 +00001478 /** Get the type of activation function */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001479 ActivationFunction activation() const
1480 {
1481 return _act;
1482 }
Alex Gildayc357c472018-03-21 13:54:09 +00001483 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001484 float a() const
1485 {
1486 return _a;
1487 }
Alex Gildayc357c472018-03-21 13:54:09 +00001488 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001489 float b() const
1490 {
1491 return _b;
1492 }
Alex Gildayc357c472018-03-21 13:54:09 +00001493 /** Check if initialised */
Giorgio Arena11674872018-02-07 15:38:12 +00001494 bool enabled() const
1495 {
1496 return _enabled;
1497 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001498
1499private:
Giorgio Arena11674872018-02-07 15:38:12 +00001500 ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::LOGISTIC };
1501 float _a = {};
1502 float _b = {};
1503 bool _enabled = { false };
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001504};
1505
1506/** Normalization Layer Information class */
1507class NormalizationLayerInfo
1508{
1509public:
1510 /** Default Constructor
1511 *
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001512 * @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 +01001513 * @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 +00001514 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
1515 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
1516 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
1517 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
1518 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001519 */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001520 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
1521 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001522 {
1523 }
Alex Gildayc357c472018-03-21 13:54:09 +00001524 /** Get the normalization type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001525 NormType type() const
1526 {
1527 return _type;
1528 }
Alex Gildayc357c472018-03-21 13:54:09 +00001529 /** Get the normalization size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001530 uint32_t norm_size() const
1531 {
1532 return _norm_size;
1533 }
Alex Gildayc357c472018-03-21 13:54:09 +00001534 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001535 float alpha() const
1536 {
1537 return _alpha;
1538 }
Alex Gildayc357c472018-03-21 13:54:09 +00001539 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001540 float beta() const
1541 {
1542 return _beta;
1543 }
Alex Gildayc357c472018-03-21 13:54:09 +00001544 /** Get the kappa value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001545 float kappa() const
1546 {
1547 return _kappa;
1548 }
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001549 /** Get the is_scaled value */
1550 bool is_scaled() const
1551 {
1552 return _is_scaled;
1553 }
Alex Gildayc357c472018-03-21 13:54:09 +00001554 /** Check if normalization is cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001555 bool is_cross_map() const
1556 {
1557 return _type == NormType::CROSS_MAP;
1558 }
Alex Gildayc357c472018-03-21 13:54:09 +00001559 /** Check if normalization is not cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001560 bool is_in_map() const
1561 {
1562 return !is_cross_map();
1563 }
1564 /** Return the scaling factor of the normalization function.
1565 *
1566 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
1567 * 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 +01001568 *
1569 * @return The normalization scaling factor.
1570 */
1571 float scale_coeff() const
1572 {
1573 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001574 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001575 }
1576
1577private:
1578 NormType _type;
1579 uint32_t _norm_size;
1580 float _alpha;
1581 float _beta;
1582 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001583 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001584};
1585
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001586/** 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 +01001587class WeightsInfo
1588{
1589public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001590 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001591 WeightsInfo()
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001592 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0), _retain_internal_weights(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001593 {
1594 }
1595 /** Constructor
1596 *
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001597 * @param[in] are_reshaped True if the weights have been reshaped
1598 * @param[in] kernel_width Kernel width.
1599 * @param[in] kernel_height Kernel height.
1600 * @param[in] num_kernels Number of convolution kernels.
1601 * @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 +01001602 */
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001603 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels, bool retain_internal_weights = false)
1604 : _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 +01001605 {
1606 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001607 /** Flag which specifies if the weights tensor has been reshaped.
1608 *
1609 * @return True if the weights tensors has been reshaped
1610 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001611 bool are_reshaped() const
1612 {
1613 return _are_reshaped;
1614 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001615 /** Return the number of convolution kernels
1616 *
1617 * @return The number of convolution kernels
1618 */
1619 unsigned int num_kernels() const
1620 {
1621 return _num_kernels;
1622 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001623 /** Return the width and height of the kernel
1624 *
1625 * @return The width and height of the kernel
1626 */
1627 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001628 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001629 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001630 }
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001631 bool retain_internal_weights() const
1632 {
1633 return _retain_internal_weights;
1634 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001635
1636private:
1637 const bool _are_reshaped;
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001638 const unsigned int _kernel_width;
1639 const unsigned int _kernel_height;
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001640 const unsigned int _num_kernels;
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001641 const bool _retain_internal_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001642};
1643
Gian Marco36a0a462018-01-12 10:21:40 +00001644/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
1645 *
1646 * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
1647 * Note: Optionally just for @ref CLGEMMInterleave4x4Kernel is it possible to set mult_interleave4x4_height, the multiplication factor for the height of the 4x4 interleaved block
1648 *
giuros018b6b4a92018-12-18 19:01:33 +00001649 * The matrix B can only be reshaped through @ref CLGEMMReshapeRHSMatrixKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
1650 * 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 +00001651 *
1652 */
1653class GEMMReshapeInfo final
1654{
1655public:
1656 /** Default constructor */
1657 GEMMReshapeInfo()
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001658 : _m(1), _n(1), _k(1), _mult_transpose1xW_width(1), _mult_interleave4x4_height(1), _depth_output_gemm3d(0), _reinterpret_input_as_3d(false)
Gian Marco36a0a462018-01-12 10:21:40 +00001659 {
1660 }
1661 /** Constructor
1662 *
1663 * @param[in] m Number of matrix A rows
1664 * @param[in] n Number of matrix B columns
1665 * @param[in] k Number of matrix A columns or matrix B rows
1666 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
1667 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001668 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel.
1669 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001670 * @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
1671 * to perform 1x1 convolutions with the NHWC data layout)
Gian Marco36a0a462018-01-12 10:21:40 +00001672 */
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001673 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)
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001674 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height), _depth_output_gemm3d(depth_output_gemm3d),
1675 _reinterpret_input_as_3d(reinterpret_input_as_3d)
Gian Marco36a0a462018-01-12 10:21:40 +00001676 {
1677 }
1678 /** Number of matrix A rows
1679 *
1680 * @return the number of matrix A rows
1681 */
1682 int m() const
1683 {
1684 return _m;
1685 }
1686 /** Number of matrix B columns
1687 *
1688 * @return the number of matrix B columns
1689 */
1690 int n() const
1691 {
1692 return _n;
1693 }
1694 /** Number of matrix A columns or matrix B rows
1695 *
1696 * @return the number of matrix A columns or matrix B rows
1697 */
1698 int k() const
1699 {
1700 return _k;
1701 }
1702 /** Multiplication factor for the width of the 1xW transposed block
1703 *
1704 * @return the multiplication factor for the width of the 1xW transposed block
1705 */
1706 int mult_transpose1xW_width() const
1707 {
1708 return _mult_transpose1xW_width;
1709 }
1710 /** Multiplication factor for the height of the 4x4 interleaved block
1711 *
1712 * @return the multiplication factor for the height of the 4x4 interleaved block
1713 */
1714 int mult_interleave4x4_height() const
1715 {
1716 return _mult_interleave4x4_height;
1717 }
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001718 /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
1719 *
1720 * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case:
1721 * m = depth_output_gemm3d * output_height
1722 *
1723 * @return the depth of the output tensor to be used with the GEMM3D kernel
1724 */
1725 int depth_output_gemm3d() const
1726 {
1727 return _depth_output_gemm3d;
1728 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001729 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1730 *
1731 * @return True if the input tensor has to be reinterpreted as 3D tensor
1732 */
1733 bool reinterpret_input_as_3d() const
1734 {
1735 return _reinterpret_input_as_3d;
1736 };
Gian Marco36a0a462018-01-12 10:21:40 +00001737
1738private:
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001739 const int _m;
1740 const int _n;
1741 const int _k;
1742 const int _mult_transpose1xW_width;
1743 const int _mult_interleave4x4_height;
1744 const int _depth_output_gemm3d;
1745 const bool _reinterpret_input_as_3d;
Gian Marco36a0a462018-01-12 10:21:40 +00001746};
1747
giuros016d109962019-01-07 17:47:19 +00001748struct DepthwiseConvolutionReshapeInfo
1749{
1750 unsigned int c0{ 1 }; /**< Number of channels processed by the depth-wise convolution */
1751 bool transpose{ false }; /**< True if the block MxC0 (where M is the area of the filter i.e. KwxKh) has to be transposed */
1752};
1753
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001754/** GEMMLowp output stage type */
1755enum class GEMMLowpOutputStageType
1756{
1757 NONE, /**< No quantization to uint8 */
1758 QUANTIZE_DOWN, /**< Quantize to uint8 using an integer multiplication */
1759 QUANTIZE_DOWN_FIXEDPOINT, /**< Quantize to uint8 using a fixed point multiplication */
1760 QUANTIZE_DOWN_FLOAT /**< Quantize to uint8 using a floating point multiplication */
1761};
1762
1763/** GEMMLowp output stage info */
1764struct GEMMLowpOutputStageInfo
1765{
1766 GEMMLowpOutputStageType type{ GEMMLowpOutputStageType::NONE }; /**< GEMMLowp output stage type */
1767 int gemmlowp_offset{ 0 }; /**< GEMMLowp output stage offset used for quantizing to QASYMM8 */
1768 int gemmlowp_multiplier{ 0 }; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
1769 int gemmlowp_shift{ 0 }; /**< GEMMLowp output stage shift used for quantizing to uint8 */
1770 int gemmlowp_min_bound{ 0 }; /**< GEMMLowp min value used to saturate down the output result before converting back to QASYMM8 */
1771 int gemmlowp_max_bound{ 0 }; /**< GEMMLowp max value used to saturate down the output result before converting back to QASYMM8 */
1772};
1773
Gian Marco Iodice5ba5e092018-12-06 17:13:09 +00001774/** GEMM LHS (Left Hand Side) matrix information */
1775struct GEMMLHSMatrixInfo
1776{
1777 unsigned int m0{ 1 }; /**< Number of rows processed by the matrix multiplication */
1778 unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */
1779 unsigned int v0{ 1 }; /**< Number of vertical blocks of size (m0xk0) stored on the same output row */
1780 bool transpose{ true }; /**< True if the (m0xk0) block has to be transposed before been stored */
1781 bool interleave{ true }; /**< True if the v0 (m0xk0) blocks have to be interleaved in the output row */
1782};
1783
Gian Marco Iodice3b0a2652018-12-07 11:18:09 +00001784/** GEMM RHS (Right Hand Side) matrix information */
1785struct GEMMRHSMatrixInfo
1786{
1787 unsigned int n0{ 1 }; /**< Number of columns processed by the matrix multiplication */
1788 unsigned int k0{ 1 }; /**< Number of partial accumulations performed by the matrix multiplication */
1789 unsigned int h0{ 1 }; /**< Number of horizontal blocks of size (k0xn0) stored on the same output row */
1790 bool transpose{ true }; /**< True if the (k0xn0) block has to be transposed before been stored */
1791 bool interleave{ true }; /**< True if the h0 (k0xn0) blocks have to be interleaved in the output row */
1792};
1793
Gian Marco36a0a462018-01-12 10:21:40 +00001794/** GEMM information class. This class stores the necessary information to compute GEMM functions
1795 *
1796 * This object also contains the information about how matrix A and matrix B have been reshaped
1797 *
1798 */
Chunosov5124be52017-11-22 20:42:13 +07001799class GEMMInfo
1800{
1801public:
1802 /** Default constructor */
1803 GEMMInfo()
Anthony Barbier08a45172018-11-30 17:20:26 +00001804 : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(true), _depth_output_gemm3d(0), _reinterpret_input_as_3d(false), _retain_internal_weights(false), _gemmlowp_output_stage(),
1805 _fp_mixed_precision(false)
Chunosov5124be52017-11-22 20:42:13 +07001806 {
1807 }
1808 /** Constructor
1809 *
1810 * @param[in] is_a_reshaped True if the matrix A has been reshaped
1811 * @param[in] is_b_reshaped True if the matrix B has been reshaped
1812 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001813 * @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 +00001814 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001815 * @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
1816 * to perform 1x1 convolutions with the NHWC data layout)
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01001817 * @param[in] retain_internal_weights (Optional) Retain the weights tensor from previous run
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001818 * @param[in] gemmlowp_output_stage (Optional) GEMMLowp Output stage info
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001819 * @param[in] fp_mixed_precision (Optional) Use wider accumulators (32 bit instead of 16 for FP16) to improve accuracy.
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001820 *
Chunosov5124be52017-11-22 20:42:13 +07001821 */
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001822 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,
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001823 GEMMLowpOutputStageInfo gemmlowp_output_stage = GEMMLowpOutputStageInfo(), bool fp_mixed_precision = false)
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001824 : _is_a_reshaped(is_a_reshaped), _is_b_reshaped(is_b_reshaped), _reshape_b_only_on_first_run(reshape_b_only_on_first_run), _depth_output_gemm3d(depth_output_gemm3d),
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001825 _reinterpret_input_as_3d(reinterpret_input_as_3d), _retain_internal_weights(retain_internal_weights), _gemmlowp_output_stage(gemmlowp_output_stage), _fp_mixed_precision(fp_mixed_precision)
Chunosov5124be52017-11-22 20:42:13 +07001826 {
1827 }
1828 /** Flag which specifies if the matrix A has been reshaped
1829 *
1830 * @return True if the matrix A has been reshaped
1831 */
1832 bool is_a_reshaped() const
1833 {
1834 return _is_a_reshaped;
1835 };
1836 /** Flag which specifies if the matrix B has been reshaped
1837 *
1838 * @return True if the matrix B has been reshaped
1839 */
1840 bool is_b_reshaped() const
1841 {
1842 return _is_b_reshaped;
1843 };
1844 /** Flag which specifies if the reshape of matrix B should executed only for the first
1845 *
1846 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
1847 *
1848 * @return True if the reshaped of matrix B happens only for the first run
1849 */
1850 bool reshape_b_only_on_first_run() const
1851 {
1852 return _reshape_b_only_on_first_run;
1853 };
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001854 /** Depth of the output when GEMM output is reinterpreted as 3D tensor
Gian Marco36a0a462018-01-12 10:21:40 +00001855 *
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001856 * @return the depth of the output tensor
Gian Marco36a0a462018-01-12 10:21:40 +00001857 */
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001858 int depth_output_gemm3d() const
Gian Marco36a0a462018-01-12 10:21:40 +00001859 {
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001860 return _depth_output_gemm3d;
1861 };
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001862 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1863 *
1864 * @return True if the input tensor has to be reinterpreted as 3D tensor
1865 */
1866 bool reinterpret_input_as_3d() const
1867 {
1868 return _reinterpret_input_as_3d;
1869 };
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01001870 /** Flag which specifies if the weights tensor has to be retained from previous run
1871 *
1872 * @return True if the weights tensor has to be retained
1873 */
1874 bool retain_internal_weights() const
1875 {
1876 return _retain_internal_weights;
1877 };
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001878 /** GEMMLowp output stage
1879 *
1880 * @return the GEMMLowp output stage info
1881 */
1882 GEMMLowpOutputStageInfo gemmlowp_output_stage() const
1883 {
1884 return _gemmlowp_output_stage;
1885 };
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001886 /** Flag which specifies if a wider accumulator should be used.
1887 *
1888 * @return True if a wider accumulator has to be used
1889 */
1890 bool fp_mixed_precision() const
1891 {
1892 return _fp_mixed_precision;
1893 };
Chunosov5124be52017-11-22 20:42:13 +07001894
1895private:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001896 const bool _is_a_reshaped;
1897 const bool _is_b_reshaped;
1898 const bool _reshape_b_only_on_first_run;
1899 const int _depth_output_gemm3d;
1900 const bool _reinterpret_input_as_3d;
1901 const bool _retain_internal_weights;
1902 const GEMMLowpOutputStageInfo _gemmlowp_output_stage;
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001903 const bool _fp_mixed_precision;
Chunosov5124be52017-11-22 20:42:13 +07001904};
1905
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00001906/** Winograd information */
1907struct WinogradInfo
1908{
1909 /** Default constructor
1910 *
1911 * @param[in] output_tile_sz Width and height of the output tile
1912 * @param[in] kernel_sz Width and height of the kernel
1913 * @param[in] input_dims Width and height of the input tensor before the convolution is applied
1914 * @param[in] conv_info Convolution info (Pads, strides)
1915 * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied
1916 */
1917 WinogradInfo(Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout)
1918 : output_tile_size(output_tile_sz), kernel_size(kernel_sz), input_dimensions(input_dims), convolution_info(conv_info), output_data_layout(data_layout)
1919 {
1920 }
1921
1922 Size2D output_tile_size{}; /**< Width and height of the output tile */
1923 Size2D kernel_size{}; /**< Width and height of the kernel*/
1924 Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */
1925 PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */
1926 DataLayout output_data_layout{ DataLayout::NCHW }; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */
1927};
1928
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001929/** IO formatting information class*/
1930struct IOFormatInfo
1931{
1932 /** Precision type used when printing floating point numbers */
1933 enum class PrecisionType
1934 {
1935 Default, /**< Default precision to the one that the current stream has */
1936 Custom, /**< Custom precision specified by the user using the precision parameter */
1937 Full /**< The maximum precision of the floating point representation */
1938 };
1939
1940 /** Specifies the area to be printed, used by Tensor objects */
1941 enum class PrintRegion
1942 {
1943 ValidRegion, /**< Prints the valid region of the Tensor object */
1944 NoPadding, /**< Prints the Tensor object without the padding */
1945 Full /**< Print the tensor object including padding */
1946 };
1947
Alex Gildayc357c472018-03-21 13:54:09 +00001948 /** Construct a set of IO formatting information.
1949 *
1950 * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion.
1951 * @param[in] precision_type Precision type for floating point numbers. Default: stream default.
1952 * @param[in] precision Precision value for float point numbers. Default: 10.
1953 * @param[in] align_columns Whether to align columns when printed. Default: true.
1954 * @param[in] element_delim Delimeter between elements. Default: " ".
1955 * @param[in] row_delim Delimenter between rows. Default: "\n".
1956 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001957 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
1958 PrecisionType precision_type = PrecisionType::Default,
1959 unsigned int precision = 10,
1960 bool align_columns = true,
1961 std::string element_delim = " ",
1962 std::string row_delim = "\n")
1963 : print_region(print_region),
1964 precision_type(precision_type),
1965 precision(precision),
1966 element_delim(element_delim),
1967 row_delim(row_delim),
1968 align_columns(align_columns)
1969 {
1970 }
1971
Alex Gildayc357c472018-03-21 13:54:09 +00001972 /** Area to be printed by Tensor objects */
1973 PrintRegion print_region;
1974 /** Floating point precision type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001975 PrecisionType precision_type;
Alex Gildayc357c472018-03-21 13:54:09 +00001976 /** Floating point precision */
1977 unsigned int precision;
1978 /** Element delimeter */
1979 std::string element_delim;
1980 /** Row delimeter */
1981 std::string row_delim;
1982 /** Align columns */
1983 bool align_columns;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001984};
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001985} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001986#endif /* __ARM_COMPUTE_TYPES_H__ */