blob: 5ddd207100622931e01f80a3c6b391a5ba0c1999 [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
Gian Marco36a0a462018-01-12 10:21:40 +00002 * Copyright (c) 2016-2018 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
Georgios Pinitas7b7858d2017-06-21 16:44:24 +0100504/** Region of interest */
505struct ROI
506{
507 Rectangle rect; /**< Rectangle specifying the region of interest */
508 uint16_t batch_idx; /**< The batch index of the region of interest */
509};
510
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100511/** Available channels */
512enum class Channel
513{
514 UNKNOWN, /** Unknown channel format */
515 C0, /**< First channel (used by formats with unknown channel types). */
516 C1, /**< Second channel (used by formats with unknown channel types). */
517 C2, /**< Third channel (used by formats with unknown channel types). */
518 C3, /**< Fourth channel (used by formats with unknown channel types). */
519 R, /**< Red channel. */
520 G, /**< Green channel. */
521 B, /**< Blue channel. */
522 A, /**< Alpha channel. */
523 Y, /**< Luma channel. */
524 U, /**< Cb/U channel. */
525 V /**< Cr/V/Value channel. */
526};
527
528/** Available matrix patterns */
529enum class MatrixPattern
530{
531 BOX, /**< Box pattern matrix. */
532 CROSS, /**< Cross pattern matrix. */
533 DISK, /**< Disk pattern matrix. */
534 OTHER /**< Any other matrix pattern. */
535};
536
537/** Available non linear functions. */
538enum class NonLinearFilterFunction : unsigned
539{
540 MEDIAN = 0, /**< Non linear median filter. */
541 MIN = 1, /**< Non linear erode. */
542 MAX = 2, /**< Non linear dilate. */
543};
544
Georgios Pinitasd9769582017-08-03 10:19:40 +0100545/** Available reduction operations */
546enum class ReductionOperation
547{
548 SUM_SQUARE, /**< Sum of squares */
Michalis Spyrou04f089c2017-08-08 17:42:38 +0100549 SUM, /**< Sum */
Michalis Spyrou7e9391b2018-10-05 14:49:28 +0100550 MEAN_SUM, /**< Mean of sum */
Georgios Pinitasd9769582017-08-03 10:19:40 +0100551};
552
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100553/** The normalization type used for the normalization layer */
554enum class NormType
555{
556 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
557 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
558 CROSS_MAP /**< Normalization applied cross maps */
559};
560
561/** Normalization type for Histogram of Oriented Gradients (HOG) */
562enum class HOGNormType
563{
564 L2_NORM = 1, /**< L2-norm */
565 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
566 L1_NORM = 3 /**< L1 norm */
567};
568
569/** Detection window used for the object detection. The detection window keeps the following information:
570 *
571 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
572 * -# Index of the class used for evaluating which class the detection window belongs to
573 * -# Confidence value (score) obtained with the classifier
574 */
575struct DetectionWindow
576{
577 uint16_t x{ 0 }; /**< Top-left x coordinate */
578 uint16_t y{ 0 }; /**< Top-left y coordinate */
579 uint16_t width{ 0 }; /**< Width of the detection window */
580 uint16_t height{ 0 }; /**< Height of the detection window */
581 uint16_t idx_class{ 0 }; /**< Index of the class */
582 float score{ 0.f }; /**< Confidence value for the detection window */
583};
584
585/** Dimension rounding type when down-scaling on CNNs
586 * @note Used in pooling and convolution layer
587 */
588enum class DimensionRoundingType
589{
590 FLOOR, /**< Floor rounding */
591 CEIL /**< Ceil rounding */
592};
593
594/** Available pooling types */
595enum class PoolingType
596{
597 MAX, /**< Max Pooling */
Georgios Pinitascdf51452017-08-31 14:21:36 +0100598 AVG, /**< Average Pooling */
599 L2 /**< L2 Pooling */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100600};
601
Michalis Spyrou2709d612018-09-19 09:46:47 +0100602/** Available non maxima suppression types */
603enum class NMSType
604{
605 LINEAR, /**< Linear NMS */
606 GAUSSIAN, /**< Gaussian NMS */
607 ORIGINAL /**< Original NMS */
608};
609
610/** BoxWithNonMaximaSuppressionLimit Information class */
611class BoxNMSLimitInfo final
612{
613public:
614 /** Constructor
615 *
616 * @param[in] score_thresh (Optional) Score threshold.
617 * @param[in] nms (Optional) NMS value
618 * @param[in] detections (Optional) Number of detections
619 * @param[in] soft_nms_enabled (Optional) Enable SoftNMS
620 * @param[in] soft_nms_method (Optional) Soft NMS method
621 * @param[in] soft_nms_sigma (Optional) Soft NMS sigma value
622 * @param[in] soft_nms_min_score_thres (Optional) Soft NMS minimum score threshold
giuros01cd96a262018-10-03 12:44:35 +0100623 * @param[in] suppress_size (Optional) Filter out boxes based on their size. Defaults to false
624 * @param[in] min_size (Optional) Smaller boxes than min_size will be filtered out. Defaults to 1
625 * @param[in] im_width (Optional) Boxes whose centers (on the x axis) is beyond im_width will be filtered. Defaults to 1
626 * @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 +0100627 */
628 BoxNMSLimitInfo(float score_thresh = 0.05f, float nms = 0.3f,
629 int detections = 100, bool soft_nms_enabled = false,
630 NMSType soft_nms_method = NMSType::LINEAR,
giuros01cd96a262018-10-03 12:44:35 +0100631 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 +0100632 : _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 +0100633 _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 +0100634 {
635 }
636 /** Get the score threshold */
637 float score_thresh() const
638 {
639 return _score_thresh;
640 }
641 /** Get the NMS */
642 float nms() const
643 {
644 return _nms;
645 }
646 /** Get the number of detections */
647 int detections_per_im() const
648 {
649 return _detections_per_im;
650 }
651 /** Check if soft NMS is enabled */
652 bool soft_nms_enabled() const
653 {
654 return _soft_nms_enabled;
655 }
656 /** Get soft NMS method */
657 NMSType soft_nms_method() const
658 {
659 return _soft_nms_method;
660 }
661 /** Get soft NMS sigma */
662 float soft_nms_sigma() const
663 {
664 return _soft_nms_sigma;
665 }
666 /** Get soft nms min score threshold */
667 float soft_nms_min_score_thres() const
668 {
669 return _soft_nms_min_score_thres;
670 }
giuros01cd96a262018-10-03 12:44:35 +0100671 /** Get if NMS will suppress boxes based on their size/position */
672 bool suppress_size() const
673 {
674 return _suppress_size;
675 }
676 /** Get size suppression threshold */
677 float min_size() const
678 {
679 return _min_size;
680 }
681 /** Get image width (NMS may suppress boxes whose center sits beyond the image width) */
682 float im_width() const
683 {
684 return _im_width;
685 }
686 /** Get image height (NMS may suppress boxes whose center sits beyond the image height) */
687 float im_height() const
688 {
689 return _im_height;
690 }
Michalis Spyrou2709d612018-09-19 09:46:47 +0100691
692private:
693 float _score_thresh;
694 float _nms;
695 int _detections_per_im;
696 bool _soft_nms_enabled;
697 NMSType _soft_nms_method;
698 float _soft_nms_sigma;
699 float _soft_nms_min_score_thres;
giuros01cd96a262018-10-03 12:44:35 +0100700 bool _suppress_size;
701 float _min_size;
702 float _im_width;
703 float _im_height;
Michalis Spyrou2709d612018-09-19 09:46:47 +0100704};
705
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100706/** Padding and stride information class */
707class PadStrideInfo
708{
709public:
710 /** Constructor
711 *
712 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
713 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
714 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
715 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
716 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
717 */
718 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
719 unsigned int pad_x = 0, unsigned int pad_y = 0,
720 DimensionRoundingType round = DimensionRoundingType::FLOOR)
721 : _stride(std::make_pair(stride_x, stride_y)),
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100722 _pad_left(pad_x),
723 _pad_top(pad_y),
724 _pad_right(pad_x),
725 _pad_bottom(pad_y),
726 _round_type(round)
727 {
728 }
729 /** Constructor
730 *
731 * @param[in] stride_x Stride, in elements, across x.
732 * @param[in] stride_y Stride, in elements, across y.
733 * @param[in] pad_left Padding across x on the left, in elements.
734 * @param[in] pad_top Padding across y on the top, in elements.
735 * @param[in] pad_right Padding across x on the right, in elements.
736 * @param[in] pad_bottom Padding across y on the bottom, in elements.
737 * @param[in] round Dimensions rounding.
738 */
739 PadStrideInfo(unsigned int stride_x, unsigned int stride_y,
740 unsigned int pad_left, unsigned int pad_right,
741 unsigned int pad_top, unsigned int pad_bottom,
742 DimensionRoundingType round)
743 : _stride(std::make_pair(stride_x, stride_y)),
744 _pad_left(pad_left),
745 _pad_top(pad_top),
746 _pad_right(pad_right),
747 _pad_bottom(pad_bottom),
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100748 _round_type(round)
749 {
750 }
Alex Gildayc357c472018-03-21 13:54:09 +0000751 /** Get the stride.
752 *
753 * @return a pair: stride x, stride y.
754 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100755 std::pair<unsigned int, unsigned int> stride() const
756 {
757 return _stride;
758 }
Alex Gildayc357c472018-03-21 13:54:09 +0000759 /** Check whether the padding is symmetric.
760 *
761 * @return True if the padding is symmetric.
762 */
Anthony Barbier21f67d62018-02-16 15:17:48 +0000763 bool padding_is_symmetric() const
764 {
765 return (_pad_left == _pad_right) && (_pad_top == _pad_bottom);
766 }
Alex Gildayc357c472018-03-21 13:54:09 +0000767 /** Get the padding.
768 *
769 * @note This should only be used when the padding is symmetric.
770 *
771 * @return a pair: padding left/right, padding top/bottom
772 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100773 std::pair<unsigned int, unsigned int> pad() const
774 {
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100775 //this accessor should be used only when padding is symmetric
Anthony Barbier21f67d62018-02-16 15:17:48 +0000776 ARM_COMPUTE_ERROR_ON(!padding_is_symmetric());
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100777 return std::make_pair(_pad_left, _pad_top);
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100778 }
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100779
Alex Gildayc357c472018-03-21 13:54:09 +0000780 /** Get the left padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100781 unsigned int pad_left() const
782 {
783 return _pad_left;
784 }
Alex Gildayc357c472018-03-21 13:54:09 +0000785 /** Get the right padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100786 unsigned int pad_right() const
787 {
788 return _pad_right;
789 }
Alex Gildayc357c472018-03-21 13:54:09 +0000790 /** Get the top padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100791 unsigned int pad_top() const
792 {
793 return _pad_top;
794 }
Alex Gildayc357c472018-03-21 13:54:09 +0000795 /** Get the bottom padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100796 unsigned int pad_bottom() const
797 {
798 return _pad_bottom;
799 }
800
Alex Gildayc357c472018-03-21 13:54:09 +0000801 /** Get the rounding type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100802 DimensionRoundingType round() const
803 {
804 return _round_type;
805 }
806
Alex Gildayc357c472018-03-21 13:54:09 +0000807 /** Check whether this has any padding */
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100808 bool has_padding() const
809 {
810 return (_pad_left != 0 || _pad_top != 0 || _pad_right != 0 || _pad_bottom != 0);
811 }
812
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100813private:
814 std::pair<unsigned int, unsigned int> _stride;
Jaroslaw Rzepeckia1ed41f2017-10-13 11:13:58 +0100815 unsigned int _pad_left;
816 unsigned int _pad_top;
817 unsigned int _pad_right;
818 unsigned int _pad_bottom;
819
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100820 DimensionRoundingType _round_type;
821};
822
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100823/** Fully connected layer info */
824struct FullyConnectedLayerInfo
825{
826 DataLayout weights_trained_layout{ DataLayout::NCHW }; /**< Layout that the weights have been trained with. */
827 bool transpose_weights{ true }; /**< Transpose weights if true. */
828 bool are_weights_reshaped{ false }; /**< Reshape the weights tensor if false. */
829 bool retain_internal_weights{ false }; /**< Retain internal reshaped weights. */
Georgios Pinitasc55cef12018-08-01 15:24:18 +0100830
831 /** Sets the weights trained data layout
832 *
833 * @param[in] layout Data layout that the weights were trained with
834 *
835 * @return Updated object
836 */
837 FullyConnectedLayerInfo &set_weights_trained_layout(DataLayout layout)
838 {
839 weights_trained_layout = layout;
840 return *this;
841 }
Georgios Pinitas195b0ba2018-08-02 17:18:51 +0100842 /** Sets the transpose weights flag
843 *
844 * @param[in] should_transpose_weights Boolean flag indicating if weights should be transposed
845 *
846 * @return Updated object
847 */
848 FullyConnectedLayerInfo &set_transpose_weights(bool should_transpose_weights)
849 {
850 transpose_weights = should_transpose_weights;
851 return *this;
852 }
Georgios Pinitas7d66a8e2018-07-17 12:28:42 +0100853};
854
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100855/** PriorBox layer info */
856class PriorBoxLayerInfo final
857{
858public:
859 /** Default Constructor */
860 PriorBoxLayerInfo()
861 : _min_sizes(),
862 _variances(),
863 _offset(),
864 _flip(true),
865 _clip(false),
866 _max_sizes(),
867 _aspect_ratios(),
868 _img_size(),
869 _steps()
870 {
871 }
872 /** Constructor
873 *
874 * @param[in] min_sizes Min sizes vector.
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100875 * @param[in] variances Variances vector.
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100876 * @param[in] offset Offset value.
877 * @param[in] flip (Optional) Flip the aspect ratios.
878 * @param[in] clip (Optional) Clip coordinates so that they're within [0,1].
879 * @param[in] max_sizes (Optional) Max sizes vector.
880 * @param[in] aspect_ratios (Optional) Aspect ratios of the boxes.
881 * @param[in] img_size (Optional) Image size.
882 * @param[in] steps (Optional) Step values.
883 */
884 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 +0000885 const std::vector<float> &max_sizes = {}, const std::vector<float> &aspect_ratios = {},
886 const Coordinates2D &img_size = Coordinates2D{ 0, 0 }, const std::array<float, 2> &steps = { { 0.f, 0.f } })
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100887 : _min_sizes(min_sizes),
888 _variances(variances),
889 _offset(offset),
890 _flip(flip),
891 _clip(clip),
892 _max_sizes(max_sizes),
Michalis Spyrou721c4cb2018-09-04 15:27:25 +0100893 _aspect_ratios(),
Michalis Spyrou6c7c38e2018-08-29 16:28:11 +0100894 _img_size(img_size),
895 _steps(steps)
896 {
897 _aspect_ratios.push_back(1.);
898 for(unsigned int i = 0; i < aspect_ratios.size(); ++i)
899 {
900 float ar = aspect_ratios[i];
901 bool already_exist = false;
902 for(auto ar_new : _aspect_ratios)
903 {
904 if(fabs(ar - ar_new) < 1e-6)
905 {
906 already_exist = true;
907 break;
908 }
909 }
910 if(!already_exist)
911 {
912 _aspect_ratios.push_back(ar);
913 if(flip)
914 {
915 _aspect_ratios.push_back(1.f / ar);
916 }
917 }
918 }
919 }
920 /** Get min sizes. */
921 std::vector<float> min_sizes() const
922 {
923 return _min_sizes;
924 }
925 /** Get min variances. */
926 std::vector<float> variances() const
927 {
928 return _variances;
929 }
930 /** Get the step coordinates */
931 std::array<float, 2> steps() const
932 {
933 return _steps;
934 }
935 /** Get the image size coordinates */
936 Coordinates2D img_size() const
937 {
938 return _img_size;
939 }
940 /** Get the offset */
941 float offset() const
942 {
943 return _offset;
944 }
945 /** Get the flip value */
946 bool flip() const
947 {
948 return _flip;
949 }
950 /** Get the clip value */
951 bool clip() const
952 {
953 return _clip;
954 }
955 /** Get max sizes. */
956 std::vector<float> max_sizes() const
957 {
958 return _max_sizes;
959 }
960 /** Get aspect ratios. */
961 std::vector<float> aspect_ratios() const
962 {
963 return _aspect_ratios;
964 }
965
966private:
967 std::vector<float> _min_sizes;
968 std::vector<float> _variances;
969 float _offset;
970 bool _flip;
971 bool _clip;
972 std::vector<float> _max_sizes;
973 std::vector<float> _aspect_ratios;
974 Coordinates2D _img_size;
975 std::array<float, 2> _steps;
976};
977
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100978/** Pooling Layer Information class */
979class PoolingLayerInfo
980{
981public:
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000982 /** Default Constructor */
983 PoolingLayerInfo()
Isabella Gottardi6e464c32018-01-26 12:32:45 +0000984 : _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 +0000985 {
986 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100987 /** Default Constructor
988 *
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000989 * @param[in] pool_type Pooling type @ref PoolingType.
990 * @param[in] pool_size Pooling size, in elements, across x and y.
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100991 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
Georgios Pinitasadaae7e2017-10-30 15:56:32 +0000992 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
993 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
994 * Defaults to false;
Anthony Barbier6ff3b192017-09-04 18:44:23 +0100995 */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +0000996 explicit PoolingLayerInfo(PoolingType pool_type,
997 unsigned int pool_size,
998 PadStrideInfo pad_stride_info = PadStrideInfo(),
999 bool exclude_padding = false)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001000 : _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)
1001 {
1002 }
1003 /** Default Constructor
1004 *
1005 * @param[in] pool_type Pooling type @ref PoolingType.
1006 * @param[in] pool_size Pooling size, in elements, across x and y.
1007 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
1008 * @param[in] exclude_padding (Optional) Strategy when accounting padding in calculations.
1009 * True will exclude padding while false will not (Used in AVG/L2 pooling to determine the pooling area).
1010 * Defaults to false;
1011 */
1012 explicit PoolingLayerInfo(PoolingType pool_type,
1013 Size2D pool_size,
1014 PadStrideInfo pad_stride_info = PadStrideInfo(),
1015 bool exclude_padding = false)
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001016 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info), _exclude_padding(exclude_padding), _is_global_pooling(false)
1017 {
1018 }
1019 /** Default Constructor
1020 *
1021 * @note This constructor is used for global pooling
1022 *
1023 * @param[in] pool_type Pooling type @ref PoolingType.
1024 */
1025 explicit PoolingLayerInfo(PoolingType pool_type)
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001026 : _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 +01001027 {
1028 }
Alex Gildayc357c472018-03-21 13:54:09 +00001029 /** Get the pooling type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001030 PoolingType pool_type() const
1031 {
1032 return _pool_type;
1033 }
Alex Gildayc357c472018-03-21 13:54:09 +00001034 /** Get the pooling size */
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001035 const Size2D &pool_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001036 {
1037 return _pool_size;
1038 }
Alex Gildayc357c472018-03-21 13:54:09 +00001039 /** Get the padding and stride */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001040 PadStrideInfo pad_stride_info() const
1041 {
1042 return _pad_stride_info;
1043 }
Alex Gildayc357c472018-03-21 13:54:09 +00001044 /** Check if padding is excluded in calculations */
Georgios Pinitasadaae7e2017-10-30 15:56:32 +00001045 bool exclude_padding() const
1046 {
1047 return _exclude_padding;
1048 }
Alex Gildayc357c472018-03-21 13:54:09 +00001049 /** Check if is global pooling */
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001050 bool is_global_pooling() const
1051 {
1052 return _is_global_pooling;
1053 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001054
1055private:
1056 PoolingType _pool_type;
Isabella Gottardi6e464c32018-01-26 12:32:45 +00001057 Size2D _pool_size;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001058 PadStrideInfo _pad_stride_info;
Georgios Pinitasadaae7e2017-10-30 15:56:32 +00001059 bool _exclude_padding;
Georgios Pinitas4c2dd542017-11-13 12:58:41 +00001060 bool _is_global_pooling;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001061};
1062
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001063/** ROI Pooling Layer Information class */
giuros0118870812018-09-13 09:31:40 +01001064class ROIPoolingLayerInfo final
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001065{
1066public:
giuros0118870812018-09-13 09:31:40 +01001067 /** Constructor
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001068 *
giuros0118870812018-09-13 09:31:40 +01001069 * @param[in] pooled_width Pooled width of the layer.
1070 * @param[in] pooled_height Pooled height of the layer.
1071 * @param[in] spatial_scale Spatial scale to be applied to the ROI coordinates and dimensions.
1072 * @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 +01001073 */
giuros0118870812018-09-13 09:31:40 +01001074 ROIPoolingLayerInfo(unsigned int pooled_width, unsigned int pooled_height, float spatial_scale, unsigned int sampling_ratio = 0)
1075 : _pooled_width(pooled_width), _pooled_height(pooled_height), _spatial_scale(spatial_scale), _sampling_ratio(sampling_ratio)
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001076 {
1077 }
Alex Gildayc357c472018-03-21 13:54:09 +00001078 /** Get the pooled width of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001079 unsigned int pooled_width() const
1080 {
1081 return _pooled_width;
1082 }
Alex Gildayc357c472018-03-21 13:54:09 +00001083 /** Get the pooled height of the layer */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001084 unsigned int pooled_height() const
1085 {
1086 return _pooled_height;
1087 }
Alex Gildayc357c472018-03-21 13:54:09 +00001088 /** Get the spatial scale */
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001089 float spatial_scale() const
1090 {
1091 return _spatial_scale;
1092 }
giuros0118870812018-09-13 09:31:40 +01001093 /** Get sampling ratio */
1094 unsigned int sampling_ratio() const
1095 {
1096 return _sampling_ratio;
1097 }
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001098
1099private:
1100 unsigned int _pooled_width;
1101 unsigned int _pooled_height;
1102 float _spatial_scale;
giuros0118870812018-09-13 09:31:40 +01001103 unsigned int _sampling_ratio;
Georgios Pinitas7b7858d2017-06-21 16:44:24 +01001104};
1105
giuros01cd96a262018-10-03 12:44:35 +01001106/** Generate Proposals Information class */
1107class GenerateProposalsInfo
1108{
1109public:
1110 /** Constructor
1111 *
1112 * @param[in] im_width Width of the original image
1113 * @param[in] im_height Height of the original image
1114 * @param[in] im_scale Scale applied to the original image
1115 * @param[in] spatial_scale (Optional)Scale applied to the feature map. Defaults to 1.0
1116 * @param[in] pre_nms_topN (Optional)Number of the best scores to be selected from the transformations. Defaults to 6000.
1117 * @param[in] post_nms_topN (Optional)Number of the best scores to be selected from the NMS operation. Defaults to 300.
1118 * @param[in] nms_thres (Optional)NMS overlap threshold. Defaults to 0.7.
1119 * @param[in] min_size (Optional)Size used to validate the anchors produced. Defaults to 16.
1120 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region of interest). Defaults to 4.
1121 */
1122 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,
1123 size_t values_per_roi = 4)
1124 : _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),
1125 _min_size(min_size), _values_per_roi(values_per_roi)
1126 {
1127 }
1128
1129 /* Get the original height */
1130 float im_height() const
1131 {
1132 return _im_height;
1133 }
1134 /* Get the original width */
1135 float im_width() const
1136 {
1137 return _im_width;
1138 }
1139 /* Get the image scale */
1140 float im_scale() const
1141 {
1142 return _im_scale;
1143 }
1144 /* Get the value of how many best scores to select (before NMS) */
1145 int pre_nms_topN() const
1146 {
1147 return _pre_nms_topN;
1148 }
1149 /* Get the value of how many best scores to select (after NMS) */
1150 int post_nms_topN() const
1151 {
1152 return _post_nms_topN;
1153 }
1154 /* Get the NMS overlap threshold */
1155 float nms_thres() const
1156 {
1157 return _nms_thres;
1158 }
1159 /* Get the minimal size */
1160 float min_size() const
1161 {
1162 return _min_size;
1163 }
1164 /* Get the spatial scale to be applied to the feature maps */
1165 float spatial_scale() const
1166 {
1167 return _spatial_scale;
1168 }
1169 /* Get the values used to represent a ROI(Region of interest)*/
1170 size_t values_per_roi() const
1171 {
1172 return _values_per_roi;
1173 }
1174
1175private:
1176 float _im_height;
1177 float _im_width;
1178 float _im_scale;
1179 float _spatial_scale;
1180 int _pre_nms_topN;
1181 int _post_nms_topN;
1182 float _nms_thres;
1183 float _min_size;
1184 size_t _values_per_roi;
1185};
1186
1187/** ComputeAnchors information class */
1188class ComputeAnchorsInfo
1189{
1190public:
1191 /** Constructor
1192 *
1193 * @param[in] feat_width Feature map width
1194 * @param[in] feat_height Feature map height
1195 * @param[in] spatial_scale Feature map scale
1196 * @param[in] values_per_roi (Optional)Values used to represent a ROI(Region Of Interest). Defaults to 4
1197 */
1198 ComputeAnchorsInfo(float feat_width, float feat_height, float spatial_scale, size_t values_per_roi = 4)
1199 : _feat_height(feat_height),
1200 _feat_width(feat_width),
1201 _spatial_scale(spatial_scale),
1202 _values_per_roi(values_per_roi)
1203 {
1204 }
1205
1206 /* Get the height of the feature map */
1207 float feat_height() const
1208 {
1209 return _feat_height;
1210 }
1211
1212 /* Get the width of the feature map */
1213 float feat_width() const
1214 {
1215 return _feat_width;
1216 }
1217
1218 /* Get the scale of the feature map */
1219 float spatial_scale() const
1220 {
1221 return _spatial_scale;
1222 }
1223
1224 /* Get the values used to represent a ROI(Region Of Interest)*/
1225 size_t values_per_roi() const
1226 {
1227 return _values_per_roi;
1228 }
1229
1230private:
1231 float _feat_height;
1232 float _feat_width;
1233 float _spatial_scale;
1234 size_t _values_per_roi;
1235};
1236
giuros01c04a0e82018-10-03 12:44:35 +01001237/** Bounding Box Transform information class */
giuros01d696cb62018-11-16 10:39:59 +00001238class BoundingBoxTransformInfo final
giuros01c04a0e82018-10-03 12:44:35 +01001239{
1240public:
1241 /** Constructor
1242 *
giuros01d696cb62018-11-16 10:39:59 +00001243 * @param[in] img_width Width of the original image
1244 * @param[in] img_height Height, of the original image
1245 * @param[in] scale Scale of the original image
1246 * @param[in] apply_scale (Optional)Re-apply scaling after transforming the boxes. Defaults to false
1247 * @param[in] weights (Optional)Weights [wx, wy, ww, wh] for the deltas. Defaults to all ones
1248 * @param[in] correct_transform_coords (Optional)Correct bounding box transform coordinates. Defaults to false
1249 * @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 +01001250 */
giuros01d696cb62018-11-16 10:39:59 +00001251 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 =
1252 false,
1253 float bbox_xform_clip =
1254 4.135166556742356f)
1255 : _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 +01001256 {
1257 }
1258
1259 std::array<float, 4> weights() const
1260 {
1261 return _weights;
1262 }
1263
1264 float bbox_xform_clip() const
1265 {
1266 return _bbox_xform_clip;
1267 }
1268
1269 float img_height() const
1270 {
1271 return _img_height;
1272 }
1273
1274 float img_width() const
1275 {
1276 return _img_width;
1277 }
1278
1279 float scale() const
1280 {
1281 return _scale;
1282 }
1283
1284 bool apply_scale() const
1285 {
1286 return _apply_scale;
1287 }
1288
giuros01d696cb62018-11-16 10:39:59 +00001289 bool correct_transform_coords() const
1290 {
1291 return _correct_transform_coords;
1292 }
1293
giuros01c04a0e82018-10-03 12:44:35 +01001294private:
1295 float _img_width;
1296 float _img_height;
1297 float _scale;
1298 bool _apply_scale;
giuros01d696cb62018-11-16 10:39:59 +00001299 bool _correct_transform_coords;
giuros01c04a0e82018-10-03 12:44:35 +01001300 std::array<float, 4> _weights;
1301 float _bbox_xform_clip;
1302};
1303
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001304/** Activation Layer Information class */
1305class ActivationLayerInfo
1306{
1307public:
1308 /** Available activation functions */
1309 enum class ActivationFunction
1310 {
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001311 LOGISTIC, /**< Logistic ( \f$ f(x) = \frac{1}{1 + e^{-x}} \f$ ) */
1312 TANH, /**< Hyperbolic tangent ( \f$ f(x) = a \cdot tanh(b \cdot x) \f$ ) */
1313 RELU, /**< Rectifier ( \f$ f(x) = max(0,x) \f$ ) */
1314 BOUNDED_RELU, /**< Upper Bounded Rectifier ( \f$ f(x) = min(a, max(0,x)) \f$ ) */
1315 LU_BOUNDED_RELU, /**< Lower and Upper Bounded Rectifier ( \f$ f(x) = min(a, max(b,x)) \f$ ) */
1316 LEAKY_RELU, /**< Leaky Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
1317 SOFT_RELU, /**< Soft Rectifier ( \f$ f(x)= log(1+e^x) \f$ ) */
1318 ABS, /**< Absolute ( \f$ f(x)= |x| \f$ ) */
1319 SQUARE, /**< Square ( \f$ f(x)= x^2 \f$ )*/
1320 SQRT, /**< Square root ( \f$ f(x) = \sqrt{x} \f$ )*/
1321 LINEAR /**< Linear ( \f$ f(x)= ax + b \f$ ) */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001322 };
1323
Giorgio Arena11674872018-02-07 15:38:12 +00001324 ActivationLayerInfo() = default;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001325 /** Default Constructor
1326 *
1327 * @param[in] f The activation function to use.
1328 * @param[in] a (Optional) The alpha parameter used by some activation functions
Georgios Pinitas64ebe5b2017-09-01 17:44:24 +01001329 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LU_BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
1330 * @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 +01001331 */
1332 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
Giorgio Arena11674872018-02-07 15:38:12 +00001333 : _act(f), _a(a), _b(b), _enabled(true)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001334 {
1335 }
Alex Gildayc357c472018-03-21 13:54:09 +00001336 /** Get the type of activation function */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001337 ActivationFunction activation() const
1338 {
1339 return _act;
1340 }
Alex Gildayc357c472018-03-21 13:54:09 +00001341 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001342 float a() const
1343 {
1344 return _a;
1345 }
Alex Gildayc357c472018-03-21 13:54:09 +00001346 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001347 float b() const
1348 {
1349 return _b;
1350 }
Alex Gildayc357c472018-03-21 13:54:09 +00001351 /** Check if initialised */
Giorgio Arena11674872018-02-07 15:38:12 +00001352 bool enabled() const
1353 {
1354 return _enabled;
1355 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001356
1357private:
Giorgio Arena11674872018-02-07 15:38:12 +00001358 ActivationFunction _act = { ActivationLayerInfo::ActivationFunction::LOGISTIC };
1359 float _a = {};
1360 float _b = {};
1361 bool _enabled = { false };
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001362};
1363
1364/** Normalization Layer Information class */
1365class NormalizationLayerInfo
1366{
1367public:
1368 /** Default Constructor
1369 *
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001370 * @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 +01001371 * @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 +00001372 * @param[in] alpha (Optional) Alpha parameter used by normalization equation. Defaults to 0.0001.
1373 * @param[in] beta (Optional) Beta parameter used by normalization equation. Defaults to 0.5.
1374 * @param[in] kappa (Optional) Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
1375 * @param[in] is_scaled (Optional) Boolean that specifies if alpha will be scaled by the normalization size or not.
1376 * Should be false to follow [Krichevksy 2012].
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001377 */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001378 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f, bool is_scaled = true)
1379 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa), _is_scaled(is_scaled)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001380 {
1381 }
Alex Gildayc357c472018-03-21 13:54:09 +00001382 /** Get the normalization type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001383 NormType type() const
1384 {
1385 return _type;
1386 }
Alex Gildayc357c472018-03-21 13:54:09 +00001387 /** Get the normalization size */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001388 uint32_t norm_size() const
1389 {
1390 return _norm_size;
1391 }
Alex Gildayc357c472018-03-21 13:54:09 +00001392 /** Get the alpha value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001393 float alpha() const
1394 {
1395 return _alpha;
1396 }
Alex Gildayc357c472018-03-21 13:54:09 +00001397 /** Get the beta value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001398 float beta() const
1399 {
1400 return _beta;
1401 }
Alex Gildayc357c472018-03-21 13:54:09 +00001402 /** Get the kappa value */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001403 float kappa() const
1404 {
1405 return _kappa;
1406 }
Michele Di Giorgio9d3a8312018-11-20 12:31:24 +00001407 /** Get the is_scaled value */
1408 bool is_scaled() const
1409 {
1410 return _is_scaled;
1411 }
Alex Gildayc357c472018-03-21 13:54:09 +00001412 /** Check if normalization is cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001413 bool is_cross_map() const
1414 {
1415 return _type == NormType::CROSS_MAP;
1416 }
Alex Gildayc357c472018-03-21 13:54:09 +00001417 /** Check if normalization is not cross map */
Georgios Pinitas41caa622017-11-16 14:37:08 +00001418 bool is_in_map() const
1419 {
1420 return !is_cross_map();
1421 }
1422 /** Return the scaling factor of the normalization function.
1423 *
1424 * If is_scaled is set to false then [Krichevksy 2012] normalization scaling is performed,
1425 * 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 +01001426 *
1427 * @return The normalization scaling factor.
1428 */
1429 float scale_coeff() const
1430 {
1431 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001432 return (_is_scaled) ? (_alpha / size) : _alpha;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001433 }
1434
1435private:
1436 NormType _type;
1437 uint32_t _norm_size;
1438 float _alpha;
1439 float _beta;
1440 float _kappa;
Georgios Pinitas41caa622017-11-16 14:37:08 +00001441 bool _is_scaled;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001442};
1443
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001444/** 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 +01001445class WeightsInfo
1446{
1447public:
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001448 /** Default constructor */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001449 WeightsInfo()
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001450 : _are_reshaped(false), _kernel_width(0), _kernel_height(0), _num_kernels(0), _retain_internal_weights(false)
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001451 {
1452 }
1453 /** Constructor
1454 *
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001455 * @param[in] are_reshaped True if the weights have been reshaped
1456 * @param[in] kernel_width Kernel width.
1457 * @param[in] kernel_height Kernel height.
1458 * @param[in] num_kernels Number of convolution kernels.
1459 * @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 +01001460 */
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001461 WeightsInfo(bool are_reshaped, unsigned int kernel_width, unsigned int kernel_height, unsigned int num_kernels, bool retain_internal_weights = false)
1462 : _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 +01001463 {
1464 }
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001465 /** Flag which specifies if the weights tensor has been reshaped.
1466 *
1467 * @return True if the weights tensors has been reshaped
1468 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001469 bool are_reshaped() const
1470 {
1471 return _are_reshaped;
1472 };
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001473 /** Return the number of convolution kernels
1474 *
1475 * @return The number of convolution kernels
1476 */
1477 unsigned int num_kernels() const
1478 {
1479 return _num_kernels;
1480 };
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001481 /** Return the width and height of the kernel
1482 *
1483 * @return The width and height of the kernel
1484 */
1485 std::pair<unsigned int, unsigned int> kernel_size() const
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001486 {
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001487 return std::make_pair(_kernel_width, _kernel_height);
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001488 }
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001489 bool retain_internal_weights() const
1490 {
1491 return _retain_internal_weights;
1492 }
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001493
1494private:
1495 const bool _are_reshaped;
Gian Marco Iodice4e288692017-06-27 11:41:59 +01001496 const unsigned int _kernel_width;
1497 const unsigned int _kernel_height;
Gian Marco Iodice559d7712017-08-08 08:38:09 +01001498 const unsigned int _num_kernels;
Michele Di Giorgiob62280a2018-05-31 17:31:05 +01001499 const bool _retain_internal_weights;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001500};
1501
Gian Marco36a0a462018-01-12 10:21:40 +00001502/** GEMM reshape information class. This class stores the necessary information about matrix A and matrix B reshape.
1503 *
1504 * The matrix A can only be reshaped through @ref CLGEMMInterleave4x4Kernel or @ref NEGEMMInterleave4x4Kernel or @ref GCGEMMInterleave4x4Kernel
1505 * 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
1506 *
1507 * The matrix B can only be reshaped through @ref CLGEMMTranspose1xWKernel or @ref NEGEMMTranspose1xWKernel or @ref GCGEMMTranspose1xWKernel
1508 * Note: Optionally just for @ref CLGEMMTranspose1xWKernel is it possible to set mult_transpose1xW_width, the multiplication factor for the width of the 1xW transposed block
1509 *
1510 */
1511class GEMMReshapeInfo final
1512{
1513public:
1514 /** Default constructor */
1515 GEMMReshapeInfo()
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001516 : _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 +00001517 {
1518 }
1519 /** Constructor
1520 *
1521 * @param[in] m Number of matrix A rows
1522 * @param[in] n Number of matrix B columns
1523 * @param[in] k Number of matrix A columns or matrix B rows
1524 * @param[in] mult_transpose1xW_width (Optional) Multiplication factor for the width of the 1xW transposed block
1525 * @param[in] mult_interleave4x4_height (Optional) Multiplication factor for the height of the 4x4 interleaved block
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001526 * @param[in] depth_output_gemm3d (Optional) Depth (third dimension) of the output tensor to be used with the GEMM3D kernel.
1527 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001528 * @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
1529 * to perform 1x1 convolutions with the NHWC data layout)
Gian Marco36a0a462018-01-12 10:21:40 +00001530 */
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001531 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 +01001532 : _m(m), _n(n), _k(k), _mult_transpose1xW_width(mult_transpose1xW_width), _mult_interleave4x4_height(mult_interleave4x4_height), _depth_output_gemm3d(depth_output_gemm3d),
1533 _reinterpret_input_as_3d(reinterpret_input_as_3d)
Gian Marco36a0a462018-01-12 10:21:40 +00001534 {
1535 }
1536 /** Number of matrix A rows
1537 *
1538 * @return the number of matrix A rows
1539 */
1540 int m() const
1541 {
1542 return _m;
1543 }
1544 /** Number of matrix B columns
1545 *
1546 * @return the number of matrix B columns
1547 */
1548 int n() const
1549 {
1550 return _n;
1551 }
1552 /** Number of matrix A columns or matrix B rows
1553 *
1554 * @return the number of matrix A columns or matrix B rows
1555 */
1556 int k() const
1557 {
1558 return _k;
1559 }
1560 /** Multiplication factor for the width of the 1xW transposed block
1561 *
1562 * @return the multiplication factor for the width of the 1xW transposed block
1563 */
1564 int mult_transpose1xW_width() const
1565 {
1566 return _mult_transpose1xW_width;
1567 }
1568 /** Multiplication factor for the height of the 4x4 interleaved block
1569 *
1570 * @return the multiplication factor for the height of the 4x4 interleaved block
1571 */
1572 int mult_interleave4x4_height() const
1573 {
1574 return _mult_interleave4x4_height;
1575 }
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001576 /** Depth (third dimension) of the output tensor to be used with the GEMM3D kernel
1577 *
1578 * @note GEMM3D kernel is used when the output has to be reinterpret as 3D tensor. In that case:
1579 * m = depth_output_gemm3d * output_height
1580 *
1581 * @return the depth of the output tensor to be used with the GEMM3D kernel
1582 */
1583 int depth_output_gemm3d() const
1584 {
1585 return _depth_output_gemm3d;
1586 }
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001587 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1588 *
1589 * @return True if the input tensor has to be reinterpreted as 3D tensor
1590 */
1591 bool reinterpret_input_as_3d() const
1592 {
1593 return _reinterpret_input_as_3d;
1594 };
Gian Marco36a0a462018-01-12 10:21:40 +00001595
1596private:
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001597 const int _m;
1598 const int _n;
1599 const int _k;
1600 const int _mult_transpose1xW_width;
1601 const int _mult_interleave4x4_height;
1602 const int _depth_output_gemm3d;
1603 const bool _reinterpret_input_as_3d;
Gian Marco36a0a462018-01-12 10:21:40 +00001604};
1605
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001606/** GEMMLowp output stage type */
1607enum class GEMMLowpOutputStageType
1608{
1609 NONE, /**< No quantization to uint8 */
1610 QUANTIZE_DOWN, /**< Quantize to uint8 using an integer multiplication */
1611 QUANTIZE_DOWN_FIXEDPOINT, /**< Quantize to uint8 using a fixed point multiplication */
1612 QUANTIZE_DOWN_FLOAT /**< Quantize to uint8 using a floating point multiplication */
1613};
1614
1615/** GEMMLowp output stage info */
1616struct GEMMLowpOutputStageInfo
1617{
1618 GEMMLowpOutputStageType type{ GEMMLowpOutputStageType::NONE }; /**< GEMMLowp output stage type */
1619 int gemmlowp_offset{ 0 }; /**< GEMMLowp output stage offset used for quantizing to QASYMM8 */
1620 int gemmlowp_multiplier{ 0 }; /**< GEMMLowp output stage multiplier used for quantizing to QASYMM8 */
1621 int gemmlowp_shift{ 0 }; /**< GEMMLowp output stage shift used for quantizing to uint8 */
1622 int gemmlowp_min_bound{ 0 }; /**< GEMMLowp min value used to saturate down the output result before converting back to QASYMM8 */
1623 int gemmlowp_max_bound{ 0 }; /**< GEMMLowp max value used to saturate down the output result before converting back to QASYMM8 */
1624};
1625
Gian Marco36a0a462018-01-12 10:21:40 +00001626/** GEMM information class. This class stores the necessary information to compute GEMM functions
1627 *
1628 * This object also contains the information about how matrix A and matrix B have been reshaped
1629 *
1630 */
Chunosov5124be52017-11-22 20:42:13 +07001631class GEMMInfo
1632{
1633public:
1634 /** Default constructor */
1635 GEMMInfo()
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001636 : _is_a_reshaped(false), _is_b_reshaped(false), _reshape_b_only_on_first_run(false), _depth_output_gemm3d(0), _reinterpret_input_as_3d(false), _retain_internal_weights(false),
1637 _gemmlowp_output_stage(), _fp_mixed_precision(false)
Chunosov5124be52017-11-22 20:42:13 +07001638 {
1639 }
1640 /** Constructor
1641 *
1642 * @param[in] is_a_reshaped True if the matrix A has been reshaped
1643 * @param[in] is_b_reshaped True if the matrix B has been reshaped
1644 * @param[in] reshape_b_only_on_first_run Reshape matrix B only for the first run
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001645 * @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 +00001646 * If 0 the output will not be reinterpreted as 3D. Default 0
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001647 * @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
1648 * to perform 1x1 convolutions with the NHWC data layout)
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01001649 * @param[in] retain_internal_weights (Optional) Retain the weights tensor from previous run
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001650 * @param[in] gemmlowp_output_stage (Optional) GEMMLowp Output stage info
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001651 * @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 +00001652 *
Chunosov5124be52017-11-22 20:42:13 +07001653 */
Gian Marco Iodice3139f032018-11-05 14:26:32 +00001654 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 +00001655 GEMMLowpOutputStageInfo gemmlowp_output_stage = GEMMLowpOutputStageInfo(), bool fp_mixed_precision = false)
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001656 : _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 +00001657 _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 +07001658 {
1659 }
1660 /** Flag which specifies if the matrix A has been reshaped
1661 *
1662 * @return True if the matrix A has been reshaped
1663 */
1664 bool is_a_reshaped() const
1665 {
1666 return _is_a_reshaped;
1667 };
1668 /** Flag which specifies if the matrix B has been reshaped
1669 *
1670 * @return True if the matrix B has been reshaped
1671 */
1672 bool is_b_reshaped() const
1673 {
1674 return _is_b_reshaped;
1675 };
1676 /** Flag which specifies if the reshape of matrix B should executed only for the first
1677 *
1678 * @note This flag could be set to TRUE when GEMM is used to accelerate convolution layer
1679 *
1680 * @return True if the reshaped of matrix B happens only for the first run
1681 */
1682 bool reshape_b_only_on_first_run() const
1683 {
1684 return _reshape_b_only_on_first_run;
1685 };
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001686 /** Depth of the output when GEMM output is reinterpreted as 3D tensor
Gian Marco36a0a462018-01-12 10:21:40 +00001687 *
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001688 * @return the depth of the output tensor
Gian Marco36a0a462018-01-12 10:21:40 +00001689 */
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001690 int depth_output_gemm3d() const
Gian Marco36a0a462018-01-12 10:21:40 +00001691 {
Isabella Gottardi8e74f442018-03-01 16:42:00 +00001692 return _depth_output_gemm3d;
1693 };
Gian Marco Iodice68a3f562018-07-26 11:44:03 +01001694 /** Flag which specifies if the input tensor has to be reinterpreted as 3D
1695 *
1696 * @return True if the input tensor has to be reinterpreted as 3D tensor
1697 */
1698 bool reinterpret_input_as_3d() const
1699 {
1700 return _reinterpret_input_as_3d;
1701 };
Michele Di Giorgioba1ffe92018-08-22 14:28:30 +01001702 /** Flag which specifies if the weights tensor has to be retained from previous run
1703 *
1704 * @return True if the weights tensor has to be retained
1705 */
1706 bool retain_internal_weights() const
1707 {
1708 return _retain_internal_weights;
1709 };
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001710 /** GEMMLowp output stage
1711 *
1712 * @return the GEMMLowp output stage info
1713 */
1714 GEMMLowpOutputStageInfo gemmlowp_output_stage() const
1715 {
1716 return _gemmlowp_output_stage;
1717 };
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001718 /** Flag which specifies if a wider accumulator should be used.
1719 *
1720 * @return True if a wider accumulator has to be used
1721 */
1722 bool fp_mixed_precision() const
1723 {
1724 return _fp_mixed_precision;
1725 };
Chunosov5124be52017-11-22 20:42:13 +07001726
1727private:
Gian Marco Iodice4b908652018-10-18 10:21:02 +01001728 const bool _is_a_reshaped;
1729 const bool _is_b_reshaped;
1730 const bool _reshape_b_only_on_first_run;
1731 const int _depth_output_gemm3d;
1732 const bool _reinterpret_input_as_3d;
1733 const bool _retain_internal_weights;
1734 const GEMMLowpOutputStageInfo _gemmlowp_output_stage;
Vidhya Sudhan Loganathana25d16c2018-11-16 11:33:12 +00001735 const bool _fp_mixed_precision;
Chunosov5124be52017-11-22 20:42:13 +07001736};
1737
Gian Marco Iodice247f52c2018-03-22 11:24:56 +00001738/** Winograd information */
1739struct WinogradInfo
1740{
1741 /** Default constructor
1742 *
1743 * @param[in] output_tile_sz Width and height of the output tile
1744 * @param[in] kernel_sz Width and height of the kernel
1745 * @param[in] input_dims Width and height of the input tensor before the convolution is applied
1746 * @param[in] conv_info Convolution info (Pads, strides)
1747 * @param[in] data_layout Data layout to use for the output tensor once the convolution has been applied
1748 */
1749 WinogradInfo(Size2D output_tile_sz, Size2D kernel_sz, Size2D input_dims, PadStrideInfo conv_info, DataLayout data_layout)
1750 : output_tile_size(output_tile_sz), kernel_size(kernel_sz), input_dimensions(input_dims), convolution_info(conv_info), output_data_layout(data_layout)
1751 {
1752 }
1753
1754 Size2D output_tile_size{}; /**< Width and height of the output tile */
1755 Size2D kernel_size{}; /**< Width and height of the kernel*/
1756 Size2D input_dimensions{}; /**< Width and height of the input tensor before the convolution is applied */
1757 PadStrideInfo convolution_info{}; /**< Convolution info (Pads, strides,...) */
1758 DataLayout output_data_layout{ DataLayout::NCHW }; /**< Data layout to use for the output tensor once the convolution has been applied (NCHW or NHWC) */
1759};
1760
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001761/** IO formatting information class*/
1762struct IOFormatInfo
1763{
1764 /** Precision type used when printing floating point numbers */
1765 enum class PrecisionType
1766 {
1767 Default, /**< Default precision to the one that the current stream has */
1768 Custom, /**< Custom precision specified by the user using the precision parameter */
1769 Full /**< The maximum precision of the floating point representation */
1770 };
1771
1772 /** Specifies the area to be printed, used by Tensor objects */
1773 enum class PrintRegion
1774 {
1775 ValidRegion, /**< Prints the valid region of the Tensor object */
1776 NoPadding, /**< Prints the Tensor object without the padding */
1777 Full /**< Print the tensor object including padding */
1778 };
1779
Alex Gildayc357c472018-03-21 13:54:09 +00001780 /** Construct a set of IO formatting information.
1781 *
1782 * @param[in] print_region Area to be printed. Used by Tensor objects. Default: ValidRegion.
1783 * @param[in] precision_type Precision type for floating point numbers. Default: stream default.
1784 * @param[in] precision Precision value for float point numbers. Default: 10.
1785 * @param[in] align_columns Whether to align columns when printed. Default: true.
1786 * @param[in] element_delim Delimeter between elements. Default: " ".
1787 * @param[in] row_delim Delimenter between rows. Default: "\n".
1788 */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001789 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
1790 PrecisionType precision_type = PrecisionType::Default,
1791 unsigned int precision = 10,
1792 bool align_columns = true,
1793 std::string element_delim = " ",
1794 std::string row_delim = "\n")
1795 : print_region(print_region),
1796 precision_type(precision_type),
1797 precision(precision),
1798 element_delim(element_delim),
1799 row_delim(row_delim),
1800 align_columns(align_columns)
1801 {
1802 }
1803
Alex Gildayc357c472018-03-21 13:54:09 +00001804 /** Area to be printed by Tensor objects */
1805 PrintRegion print_region;
1806 /** Floating point precision type */
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001807 PrecisionType precision_type;
Alex Gildayc357c472018-03-21 13:54:09 +00001808 /** Floating point precision */
1809 unsigned int precision;
1810 /** Element delimeter */
1811 std::string element_delim;
1812 /** Row delimeter */
1813 std::string row_delim;
1814 /** Align columns */
1815 bool align_columns;
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001816};
Georgios Pinitasd8734b52017-12-22 15:27:52 +00001817} // namespace arm_compute
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001818#endif /* __ARM_COMPUTE_TYPES_H__ */