blob: 725567b9ae03472de1bc58d56599fde130f6724a [file] [log] [blame]
Anthony Barbier6ff3b192017-09-04 18:44:23 +01001/*
2 * Copyright (c) 2016, 2017 ARM Limited.
3 *
4 * SPDX-License-Identifier: MIT
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to
8 * deal in the Software without restriction, including without limitation the
9 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24#ifndef __ARM_COMPUTE_TYPES_H__
25#define __ARM_COMPUTE_TYPES_H__
26
27#include "arm_compute/core/Coordinates.h"
28#include "arm_compute/core/TensorShape.h"
29
30#include <cstddef>
31#include <cstdint>
32#include <string>
33#include <utility>
34
35namespace arm_compute
36{
37/** Image colour formats */
38enum class Format
39{
40 UNKNOWN, /** Unknown image format */
41 U8, /** 1 channel, 1 U8 per channel */
42 S16, /** 1 channel, 1 S16 per channel */
43 U16, /** 1 channel, 1 U16 per channel */
44 S32, /** 1 channel, 1 S32 per channel */
45 U32, /** 1 channel, 1 U32 per channel */
46 F16, /** 1 channel, 1 F16 per channel */
47 F32, /** 1 channel, 1 F32 per channel */
48 UV88, /** 2 channel, 1 U8 per channel */
49 RGB888, /** 3 channels, 1 U8 per channel */
50 RGBA8888, /** 4 channels, 1 U8 per channel */
51 YUV444, /** A 3 plane of 8 bit 4:4:4 sampled Y, U, V planes */
52 YUYV422, /** A single plane of 32-bit macro pixel of Y0, U0, Y1, V0 bytes */
53 NV12, /** A 2 plane YUV format of Luma (Y) and interleaved UV data at 4:2:0 sampling */
54 NV21, /** A 2 plane YUV format of Luma (Y) and interleaved VU data at 4:2:0 sampling */
55 IYUV, /** A 3 plane of 8-bit 4:2:0 sampled Y, U, V planes */
56 UYVY422 /** A single plane of 32-bit macro pixel of U0, Y0, V0, Y1 byte */
57};
58
59/** Available data types */
60enum class DataType
61{
62 UNKNOWN,
63 U8,
64 S8,
65 QS8,
66 U16,
67 S16,
68 QS16,
69 U32,
70 S32,
71 U64,
72 S64,
73 F16,
74 F32,
75 F64,
76 SIZET
77};
78
79/** Constant value of the border pixels when using BorderMode::CONSTANT */
80constexpr uint8_t CONSTANT_BORDER_VALUE = 199;
81
82/* Constant value used to indicate a half-scale pyramid */
83constexpr float SCALE_PYRAMID_HALF = 0.5f;
84
85/* Constant value used to indicate a ORB scaled pyramid */
86constexpr float SCALE_PYRAMID_ORB = 8.408964152537146130583778358414e-01;
87
88struct ValidRegion
89{
90 ValidRegion()
91 : anchor{}, shape{}
92 {
93 }
94
95 ValidRegion(const ValidRegion &) = default;
96 ValidRegion(ValidRegion &&) = default;
97 ValidRegion &operator=(const ValidRegion &) = default;
98 ValidRegion &operator=(ValidRegion &&) = default;
99 ~ValidRegion() = default;
100
101 ValidRegion(Coordinates anchor, TensorShape shape)
102 : anchor{ anchor }, shape{ shape }
103 {
104 }
105
106 /** Return the start of the valid region for the given dimension @p d */
107 int start(unsigned int d) const
108 {
109 return anchor[d];
110 }
111
112 /** Return the end of the valid region for the given dimension @p d */
113 int end(unsigned int d) const
114 {
115 return anchor[d] + shape[d];
116 }
117
118 Coordinates anchor;
119 TensorShape shape;
120};
121
122/** Methods available to handle borders */
123enum class BorderMode
124{
125 UNDEFINED, /**< Borders are left undefined */
126 CONSTANT, /**< Pixels outside the image are assumed to have a constant value */
127 REPLICATE /**< Pixels outside the image are assumed to have the same value as the closest image pixel */
128};
129
130/** Container for 2D border size */
131struct BorderSize
132{
133 /** Empty border, i.e. no border */
134 constexpr BorderSize()
135 : top{ 0 }, right{ 0 }, bottom{ 0 }, left{ 0 }
136 {
137 }
138
139 /** Border with equal size around the 2D plane */
140 constexpr BorderSize(unsigned int size)
141 : top{ size }, right{ size }, bottom{ size }, left{ size }
142 {
143 }
144
145 /** Border with same size for top/bottom and left/right */
146 constexpr BorderSize(unsigned int top_bottom, unsigned int left_right)
147 : top{ top_bottom }, right{ left_right }, bottom{ top_bottom }, left{ left_right }
148 {
149 }
150
151 /** Border with different sizes */
152 constexpr BorderSize(unsigned int top, unsigned int right, unsigned int bottom, unsigned int left)
153 : top{ top }, right{ right }, bottom{ bottom }, left{ left }
154 {
155 }
156
157 /** Check if the entire border is zero */
158 constexpr bool empty() const
159 {
160 return top == 0 && right == 0 && bottom == 0 && left == 0;
161 }
162
163 /** Check if the border is the same size on all sides */
164 constexpr bool uniform() const
165 {
166 return top == right && top == bottom && top == left;
167 }
168
169 BorderSize &operator*=(float scale)
170 {
171 top *= scale;
172 right *= scale;
173 bottom *= scale;
174 left *= scale;
175
176 return *this;
177 }
178
179 BorderSize operator*(float scale)
180 {
181 BorderSize size = *this;
182 size *= scale;
183
184 return size;
185 }
186
187 void limit(const BorderSize &limit)
188 {
189 top = std::min(top, limit.top);
190 right = std::min(right, limit.right);
191 bottom = std::min(bottom, limit.bottom);
192 left = std::min(left, limit.left);
193 }
194
195 unsigned int top;
196 unsigned int right;
197 unsigned int bottom;
198 unsigned int left;
199};
200
201using PaddingSize = BorderSize;
202
203/** Policy to handle overflow */
204enum class ConvertPolicy
205{
206 WRAP, /**< Wrap around */
207 SATURATE /**< Saturate */
208};
209
210/** Interpolation method */
211enum class InterpolationPolicy
212{
213 NEAREST_NEIGHBOR, /**< Output values are defined to match the source pixel whose center is nearest to the sample position */
214 BILINEAR, /**< Output values are defined by bilinear interpolation between the pixels */
215 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 */
216};
217
218/** Bilinear Interpolation method used by LKTracker */
219enum class BilinearInterpolation
220{
221 BILINEAR_OLD_NEW,
222 BILINEAR_SCHARR
223};
224
225/** Threshold mode */
226enum class ThresholdType
227{
228 BINARY, /**< Threshold with one value */
229 RANGE /**< Threshold with two values*/
230};
231
232/** Rounding method */
233enum class RoundingPolicy
234{
235 TO_ZERO, /**< Truncates the least significand values that are lost in operations. */
236 TO_NEAREST_UP, /**< Rounds to nearest value; half rounds up */
237 TO_NEAREST_EVEN /**< Rounds to nearest value; half rounds to nearest even */
238};
239
240/** Termination criteria */
241enum class Termination
242{
243 TERM_CRITERIA_EPSILON,
244 TERM_CRITERIA_ITERATIONS,
245 TERM_CRITERIA_BOTH
246};
247
248/** Magnitude calculation type. */
249enum class MagnitudeType
250{
251 L1NORM, /**< L1 normalization type */
252 L2NORM /**< L2 normalization type */
253};
254
255/** Phase calculation type.
256 *
257 * @note When PhaseType == SIGNED, each angle is mapped to the range 0 to 255 inclusive otherwise angles between 0 and 180
258 */
259enum class PhaseType
260{
261 SIGNED, /**< Angle range: [0, 360] */
262 UNSIGNED /**< Angle range: [0, 180] */
263};
264
265/** Keypoint type */
266struct KeyPoint
267{
268 int32_t x{ 0 }; /**< X coordinates */
269 int32_t y{ 0 }; /**< Y coordinates */
270 float strength{ 0.f }; /**< Strength of the point */
271 float scale{ 0.f }; /**< Scale initialized to 0 by the corner detector */
272 float orientation{ 0.f }; /**< Orientation initialized to 0 by the corner detector */
273 int32_t tracking_status{ 0 }; /**< Status initialized to 1 by the corner detector, set to 0 when the point is lost */
274 float error{ 0.f }; /**< Tracking error initialized to 0 by the corner detector */
275};
276
277using InternalKeypoint = std::tuple<float, float, float>; /* x,y,strength */
278
279/** Rectangle type */
280struct Rectangle
281{
282 uint16_t x; /**< Top-left x coordinate */
283 uint16_t y; /**< Top-left y coordinate */
284 uint16_t width; /**< Width of the rectangle */
285 uint16_t height; /**< Height of the rectangle */
286};
287
288/** Coordinate type */
289struct Coordinates2D
290{
291 int32_t x; /**< X coordinates */
292 int32_t y; /**< Y coordinates */
293};
294
295/** Coordinate type */
296struct Coordinates3D
297{
298 uint32_t x; /**< X coordinates */
299 uint32_t y; /**< Y coordinates */
300 uint32_t z; /**< Z coordinates */
301};
302
303/** Available channels */
304enum class Channel
305{
306 UNKNOWN, /** Unknown channel format */
307 C0, /**< First channel (used by formats with unknown channel types). */
308 C1, /**< Second channel (used by formats with unknown channel types). */
309 C2, /**< Third channel (used by formats with unknown channel types). */
310 C3, /**< Fourth channel (used by formats with unknown channel types). */
311 R, /**< Red channel. */
312 G, /**< Green channel. */
313 B, /**< Blue channel. */
314 A, /**< Alpha channel. */
315 Y, /**< Luma channel. */
316 U, /**< Cb/U channel. */
317 V /**< Cr/V/Value channel. */
318};
319
320/** Available matrix patterns */
321enum class MatrixPattern
322{
323 BOX, /**< Box pattern matrix. */
324 CROSS, /**< Cross pattern matrix. */
325 DISK, /**< Disk pattern matrix. */
326 OTHER /**< Any other matrix pattern. */
327};
328
329/** Available non linear functions. */
330enum class NonLinearFilterFunction : unsigned
331{
332 MEDIAN = 0, /**< Non linear median filter. */
333 MIN = 1, /**< Non linear erode. */
334 MAX = 2, /**< Non linear dilate. */
335};
336
337/** The normalization type used for the normalization layer */
338enum class NormType
339{
340 IN_MAP_1D, /**< Normalization applied within the same map in 1D region */
341 IN_MAP_2D, /**< Normalization applied within the same map in 2D region */
342 CROSS_MAP /**< Normalization applied cross maps */
343};
344
345/** Normalization type for Histogram of Oriented Gradients (HOG) */
346enum class HOGNormType
347{
348 L2_NORM = 1, /**< L2-norm */
349 L2HYS_NORM = 2, /**< L2-norm followed by clipping */
350 L1_NORM = 3 /**< L1 norm */
351};
352
353/** Detection window used for the object detection. The detection window keeps the following information:
354 *
355 * -# Geometry of the rectangular window (x/y of top-left corner and width/height)
356 * -# Index of the class used for evaluating which class the detection window belongs to
357 * -# Confidence value (score) obtained with the classifier
358 */
359struct DetectionWindow
360{
361 uint16_t x{ 0 }; /**< Top-left x coordinate */
362 uint16_t y{ 0 }; /**< Top-left y coordinate */
363 uint16_t width{ 0 }; /**< Width of the detection window */
364 uint16_t height{ 0 }; /**< Height of the detection window */
365 uint16_t idx_class{ 0 }; /**< Index of the class */
366 float score{ 0.f }; /**< Confidence value for the detection window */
367};
368
369/** Dimension rounding type when down-scaling on CNNs
370 * @note Used in pooling and convolution layer
371 */
372enum class DimensionRoundingType
373{
374 FLOOR, /**< Floor rounding */
375 CEIL /**< Ceil rounding */
376};
377
378/** Available pooling types */
379enum class PoolingType
380{
381 MAX, /**< Max Pooling */
382 AVG /**< Average Pooling */
383};
384
385/** Padding and stride information class */
386class PadStrideInfo
387{
388public:
389 /** Constructor
390 *
391 * @param[in] stride_x (Optional) Stride, in elements, across x. Defaults to 1.
392 * @param[in] stride_y (Optional) Stride, in elements, across y. Defaults to 1.
393 * @param[in] pad_x (Optional) Padding, in elements, across x. Defaults to 0.
394 * @param[in] pad_y (Optional) Padding, in elements, across y. Defaults to 0.
395 * @param[in] round (Optional) Dimensions rounding. Defaults to @ref FLOOR.
396 */
397 PadStrideInfo(unsigned int stride_x = 1, unsigned int stride_y = 1,
398 unsigned int pad_x = 0, unsigned int pad_y = 0,
399 DimensionRoundingType round = DimensionRoundingType::FLOOR)
400 : _stride(std::make_pair(stride_x, stride_y)),
401 _pad(std::make_pair(pad_x, pad_y)),
402 _round_type(round)
403 {
404 }
405 std::pair<unsigned int, unsigned int> stride() const
406 {
407 return _stride;
408 }
409 std::pair<unsigned int, unsigned int> pad() const
410 {
411 return _pad;
412 }
413 DimensionRoundingType round() const
414 {
415 return _round_type;
416 }
417
418private:
419 std::pair<unsigned int, unsigned int> _stride;
420 std::pair<unsigned int, unsigned int> _pad;
421 DimensionRoundingType _round_type;
422};
423
424/** Pooling Layer Information class */
425class PoolingLayerInfo
426{
427public:
428 /** Default Constructor
429 *
430 * @param[in] pool_type Pooling type @ref PoolingType. Defaults to @ref PoolingType::MAX
431 * @param[in] pool_size (Optional) Pooling size, in elements, across x and y. Defaults to 2.
432 * @param[in] pad_stride_info (Optional) Padding and stride information @ref PadStrideInfo
433 */
434 PoolingLayerInfo(PoolingType pool_type = PoolingType::MAX, unsigned int pool_size = 2, PadStrideInfo pad_stride_info = PadStrideInfo())
435 : _pool_type(pool_type), _pool_size(pool_size), _pad_stride_info(pad_stride_info)
436 {
437 }
438 PoolingType pool_type() const
439 {
440 return _pool_type;
441 }
442 unsigned int pool_size() const
443 {
444 return _pool_size;
445 }
446 PadStrideInfo pad_stride_info() const
447 {
448 return _pad_stride_info;
449 }
450
451private:
452 PoolingType _pool_type;
453 unsigned int _pool_size;
454 PadStrideInfo _pad_stride_info;
455};
456
457/** Activation Layer Information class */
458class ActivationLayerInfo
459{
460public:
461 /** Available activation functions */
462 enum class ActivationFunction
463 {
464 LOGISTIC, /**< Logistic */
465 TANH, /**< Hyperbolic tangent */
466 RELU, /**< Rectifier */
467 BOUNDED_RELU, /**< Bounded Rectifier */
468 SOFT_RELU, /**< Soft Rectifier */
469 ABS, /**< Absolute */
470 SQUARE, /**< Square */
471 SQRT, /**< Square root */
472 LINEAR /**< Linear */
473 };
474
475 /** Default Constructor
476 *
477 * @param[in] f The activation function to use.
478 * @param[in] a (Optional) The alpha parameter used by some activation functions
479 * (@ref ActivationFunction::BOUNDED_RELU, @ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
480 * @param[in] b (Optional) The beta parameter used by some activation functions (@ref ActivationFunction::LINEAR, @ref ActivationFunction::TANH).
481 */
482 ActivationLayerInfo(ActivationFunction f, float a = 0.0f, float b = 0.0f)
483 : _act(f), _a(a), _b(b)
484 {
485 }
486 ActivationFunction activation() const
487 {
488 return _act;
489 }
490 float a() const
491 {
492 return _a;
493 }
494 float b() const
495 {
496 return _b;
497 }
498
499private:
500 ActivationFunction _act;
501 float _a;
502 float _b;
503};
504
505/** Normalization Layer Information class */
506class NormalizationLayerInfo
507{
508public:
509 /** Default Constructor
510 *
511 * @param[in] type The normalization type. Can be @ref NormType::IN_MAP_1D, @ref NormType::IN_MAP_2D or @ref NORM_TYPE::CROSS_MAP
512 * @param[in] norm_size The normalization size is the number of elements to normalize across. Defaults to 5.
513 * @param[in] alpha Alpha parameter used by normalization equation. Defaults to 0.0001.
514 * @param[in] beta Beta parameter used by normalization equation. Defaults to 0.5.
515 * @param[in] kappa Kappa parameter used by [Krichevksy 2012] Across Channel Local Brightness Normalization equation.
516 */
517 NormalizationLayerInfo(NormType type, uint32_t norm_size = 5, float alpha = 0.0001f, float beta = 0.5f, float kappa = 1.f)
518 : _type(type), _norm_size(norm_size), _alpha(alpha), _beta(beta), _kappa(kappa)
519 {
520 }
521 NormType type() const
522 {
523 return _type;
524 }
525 uint32_t norm_size() const
526 {
527 return _norm_size;
528 }
529 float alpha() const
530 {
531 return _alpha;
532 }
533 float beta() const
534 {
535 return _beta;
536 }
537 float kappa() const
538 {
539 return _kappa;
540 }
541 /** Return the scaling factor of the normalization function. If kappa is not
542 * 1 then [Krichevksy 2012] normalization scaling is specified. Scaling
543 * factor takes into account the total number of elements used for the
544 * normalization, so in case of 2 dimensions this is _norm_size^2.
545 *
546 * @return The normalization scaling factor.
547 */
548 float scale_coeff() const
549 {
550 const uint32_t size = (_type == NormType::IN_MAP_2D) ? _norm_size * _norm_size : _norm_size;
551 return (_kappa == 1.f) ? (_alpha / size) : _alpha;
552 }
553
554private:
555 NormType _type;
556 uint32_t _norm_size;
557 float _alpha;
558 float _beta;
559 float _kappa;
560};
561
562/** Convolution Layer Weights Information class */
563class WeightsInfo
564{
565public:
566 WeightsInfo()
567 : _are_reshaped(false), _kernel_size(0)
568 {
569 }
570 /** Constructor
571 *
572 * @param[in] are_reshaped True if the weights have been reshaped
573 * @param[in] kernel_size The size of the kernel.
574 */
575 WeightsInfo(bool are_reshaped, unsigned int kernel_size)
576 : _are_reshaped(are_reshaped), _kernel_size(kernel_size)
577 {
578 }
579
580 bool are_reshaped() const
581 {
582 return _are_reshaped;
583 };
584 unsigned int kernel_size() const
585 {
586 return _kernel_size;
587 }
588
589private:
590 const bool _are_reshaped;
591 const unsigned int _kernel_size;
592};
593
594/** IO formatting information class*/
595struct IOFormatInfo
596{
597 /** Precision type used when printing floating point numbers */
598 enum class PrecisionType
599 {
600 Default, /**< Default precision to the one that the current stream has */
601 Custom, /**< Custom precision specified by the user using the precision parameter */
602 Full /**< The maximum precision of the floating point representation */
603 };
604
605 /** Specifies the area to be printed, used by Tensor objects */
606 enum class PrintRegion
607 {
608 ValidRegion, /**< Prints the valid region of the Tensor object */
609 NoPadding, /**< Prints the Tensor object without the padding */
610 Full /**< Print the tensor object including padding */
611 };
612
613 IOFormatInfo(PrintRegion print_region = PrintRegion::ValidRegion,
614 PrecisionType precision_type = PrecisionType::Default,
615 unsigned int precision = 10,
616 bool align_columns = true,
617 std::string element_delim = " ",
618 std::string row_delim = "\n")
619 : print_region(print_region),
620 precision_type(precision_type),
621 precision(precision),
622 element_delim(element_delim),
623 row_delim(row_delim),
624 align_columns(align_columns)
625 {
626 }
627
628 PrintRegion print_region;
629 PrecisionType precision_type;
630 unsigned int precision;
631 std::string element_delim;
632 std::string row_delim;
633 bool align_columns;
634};
635}
636#endif /* __ARM_COMPUTE_TYPES_H__ */