JeVois  1.21
JeVois Smart Embedded Machine Vision Toolkit
Share this page:
Loading...
Searching...
No Matches
Utils.H
Go to the documentation of this file.
1// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2//
3// JeVois Smart Embedded Machine Vision Toolkit - Copyright (C) 2020 by Laurent Itti, the University of Southern
4// California (USC), and iLab at USC. See http://iLab.usc.edu and http://jevois.org for information about this project.
5//
6// This file is part of the JeVois Smart Embedded Machine Vision Toolkit. This program is free software; you can
7// redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software
8// Foundation, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
9// without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
10// License for more details. You should have received a copy of the GNU General Public License along with this program;
11// if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
12//
13// Contact information: Laurent Itti - 3641 Watt Way, HNB-07A - Los Angeles, CA 90089-2520 - USA.
14// Tel: +1 213 740 3527 - itti@pollux.usc.edu - http://iLab.usc.edu - http://jevois.org
15// ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////
16/*! \file */
17
18#pragma once
19
20#include <map>
21#include <string>
22#include <opencv2/core/core.hpp>
23#include <tensorflow/lite/c/common.h> // for TfLiteType
24#include <ovxlib/vsi_nn_pub.h> // for data types and quantization types
25
26#ifdef JEVOIS_PRO
27#include <hailo/hailort.h>
28#include <onnxruntime_cxx_api.h>
29#endif
30
31namespace jevois
32{
33 //! Deep neural networks
34 namespace dnn
35 {
36 /*! \defgroup dnn Tensor/Neural Processing networks
37
38 Classes and utilities to provide abstraction to deep neural networks. Provides interfacing to OpenCV backends
39 (CPU, OpenCL), tensor processing units (TPU) such as Coral Edge TPU and neural processing units (NPU) such as
40 Amlogic A311D NPU. */
41
42 /*! @{ */ // **********************************************************************
43
44 //! Read a label file
45 /*! Two formats are allowed: one class name per line, or one class number followed by one class name per file.*/
46 std::map<int, std::string> readLabelsFile(std::string const & fname);
47
48 //! Get a label from an id
49 /*! If no entry is found in the map, return the id as a string. */
50 std::string getLabel(std::map<int, std::string> const & labels, int id);
51
52 //! Compute a color from a label name
53 int stringToRGBA(std::string const & label, unsigned char alpha = 128);
54
55 //! Get top-k entries and their indices
56 void topK(float const * pfProb, float * pfMaxProb, uint32_t * pMaxClass, uint32_t outputCount, uint32_t topNum);
57
58 //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE
59 std::string shapestr(cv::Mat const & m);
60
61 //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional TfLiteTensor with data type TYPE
62 std::string shapestr(TfLiteTensor const * t);
63
64 //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional NPU tensor with data type TYPE
65 std::string shapestr(vsi_nn_tensor_attr_t const & attr);
66
67 //! Get a vector of size_t from a string containing AxBxC...
68 std::vector<size_t> strshape(std::string const & str);
69
70 //! Convert from TensorFlow data type to OpenCV
71 int tf2cv(TfLiteType t);
72
73 //! Convert from TensorFlow data type to vsi_nn
74 vsi_nn_type_e tf2vsi(TfLiteType t);
75
76 //! Convert from NPU data type to OpenCV
77 int vsi2cv(vsi_nn_type_e t);
78
79 //! Clamp a rectangle to within given image width and height
80 void clamp(cv::Rect & r, int width, int height);
81
82 //! Clamp a rectangle to within given image width and height
83 void clamp(cv::Rect2f & r, float width, float height);
84
85 //! Parse tensor specification
86 /*! If the specification is empty, an empty vector is returned. Throws std::range_error on any parsing error. */
87 std::vector<vsi_nn_tensor_attr_t> parseTensorSpecs(std::string const & specs);
88
89 //! Construct a cv::Mat from attr and possibly data pointer
90 /*! If dataptr is nullptr, new memory will be allocated for the cv::Mat. Caller must ensure data outlives the
91 cv::Mat, and is responsible for eventually de-allocating the data. Usually, with non-null dataptr, this is only
92 to be used as a temporary re-casting, e.g., to recast a received tensor into a Mat before dequantizing it, then
93 forgetting about that Mat. */
94 cv::Mat attrmat(vsi_nn_tensor_attr_t const & attr, void * dataptr = nullptr);
95
96 //! Get a tensor dims as a vector of int, useful to construct a matching cv::Mat
97 std::vector<int> attrdims(vsi_nn_tensor_attr_t const & attr);
98
99 //! Get a tensor's (width, height) size in cv::Size format, skipping over other dimensions
100 cv::Size attrsize(vsi_nn_tensor_attr_t const & attr);
101
102 //! Get a string describing the specs of a tensor, including quantification specs (not provided by shapestr())
103 std::string attrstr(vsi_nn_tensor_attr_t const & attr);
104
105 //! Check that a cv::Mat blob matches exactly the spec of an attr
106 bool attrmatch(vsi_nn_tensor_attr_t const & attr, cv::Mat const & blob);
107
108 //! Get tensor shape and type attributes for a TensorFlow Lite tensor
109 vsi_nn_tensor_attr_t tensorattr(TfLiteTensor const * t);
110
111 //! Apply softmax to a float vector
112 /*! n is the number of elements to process, stride is the increment in the arrays from one element to the next. So
113 the arrays should have size n * stride. Returns the index in [0..n*stride[ of the highest scoring element. If
114 maxonly is true, only output[returned index] is valid. */
115 size_t softmax(float const * input, size_t const n, size_t const stride, float const fac, float * output,
116 bool maxonly);
117
118 //! Quantize from float32 to fixed-point according to the quantization spec in attr
119 /*! m should be float32, typically normalized to [0..1[ or [-1..1[ already. attr is the desired quantized type and
120 method (DFP, AA, etc) */
121 cv::Mat quantize(cv::Mat const & m, vsi_nn_tensor_attr_t const & attr);
122
123 //! Dequantize an output to float32 according to the quantization spec in attr
124 /*! attr should have the type and quantization details of m, returned tensor is float32 */
125 cv::Mat dequantize(cv::Mat const & m, vsi_nn_tensor_attr_t const & attr);
126
127 //! Returns the number of non-unit dims in a cv::Mat
128 /*! For example, returns 2 for a 4D Mat with size 1x1x224x224, since it effectively is a 224x224 2D array */
129 size_t effectiveDims(cv::Mat const & m);
130
131 //! Concatenate several tensors into one
132 /*! Axis may be positive starting at 0 for the first dimension (when reading dims from left to right), or negative
133 starting at -1 for the last dimension. For example, for a 10x20x30 tensor, axis 0 has size 10 and is also axis
134 -3, axis 1 has size 20 and is also axis -2, and axis 2 has size 30 and is also axis -1. The input tensors must
135 all have the same number of dimensions, same pixel type, and sizes must match for all dimensions except the one
136 that is being concatenated. */
137 cv::Mat concatenate(std::vector<cv::Mat> const & tensors, int axis);
138
139#ifdef JEVOIS_PRO
140 //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional Hailo tensor with data type TYPE
141 std::string shapestr(hailo_vstream_info_t const & vi);
142
143 //! Get tensor shape and type attributes for a Hailo tensor
144 vsi_nn_tensor_attr_t tensorattr(hailo_vstream_info_t const & vi);
145
146 //! Convert from Hailo data type to vsi_nn
147 vsi_nn_type_e hailo2vsi(hailo_format_type_t t);
148
149 //! Convert from ONNX-Runtime data type to vsi_nn
150 vsi_nn_type_e onnx2vsi(ONNXTensorElementDataType t);
151
152 //! Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional ONNX tensor with data type TYPE
153 std::string shapestr(Ort::ConstTensorTypeAndShapeInfo const & ti);
154
155 //! Get tensor shape and type attributes for an ONNX-runtime tensor
156 vsi_nn_tensor_attr_t tensorattr(Ort::ConstTensorTypeAndShapeInfo const & ti);
157#endif
158
159 /*! @} */ // **********************************************************************
160
161
162 /*! \defgroup pydnn DNN-related processors written in python
163
164 In addition to writing DNN pre/net/post processors in C++, JeVois supports writing them in Python.
165
166 \ingroup dnn */
167
168
169 } // namespace dnn
170} // namespace jevois
int tf2cv(TfLiteType t)
Convert from TensorFlow data type to OpenCV.
Definition Utils.C:286
int vsi2cv(vsi_nn_type_e t)
Convert from NPU data type to OpenCV.
Definition Utils.C:308
size_t softmax(float const *input, size_t const n, size_t const stride, float const fac, float *output, bool maxonly)
Apply softmax to a float vector.
Definition Utils.C:668
vsi_nn_tensor_attr_t tensorattr(TfLiteTensor const *t)
Get tensor shape and type attributes for a TensorFlow Lite tensor.
Definition Utils.C:563
cv::Mat quantize(cv::Mat const &m, vsi_nn_tensor_attr_t const &attr)
Quantize from float32 to fixed-point according to the quantization spec in attr.
Definition Utils.C:718
std::map< int, std::string > readLabelsFile(std::string const &fname)
Read a label file.
Definition Utils.C:25
vsi_nn_type_e onnx2vsi(ONNXTensorElementDataType t)
Convert from ONNX-Runtime data type to vsi_nn.
Definition Utils.C:208
std::vector< vsi_nn_tensor_attr_t > parseTensorSpecs(std::string const &specs)
Parse tensor specification.
Definition Utils.C:387
void clamp(cv::Rect &r, int width, int height)
Clamp a rectangle to within given image width and height.
Definition Utils.C:367
std::string attrstr(vsi_nn_tensor_attr_t const &attr)
Get a string describing the specs of a tensor, including quantification specs (not provided by shapes...
Definition Utils.C:512
cv::Mat attrmat(vsi_nn_tensor_attr_t const &attr, void *dataptr=nullptr)
Construct a cv::Mat from attr and possibly data pointer.
Definition Utils.C:471
size_t effectiveDims(cv::Mat const &m)
Returns the number of non-unit dims in a cv::Mat.
Definition Utils.C:832
vsi_nn_type_e hailo2vsi(hailo_format_type_t t)
Convert from Hailo data type to vsi_nn.
Definition Utils.C:353
cv::Mat concatenate(std::vector< cv::Mat > const &tensors, int axis)
Concatenate several tensors into one.
Definition Utils.C:842
int stringToRGBA(std::string const &label, unsigned char alpha=128)
Compute a color from a label name.
Definition Utils.C:76
cv::Size attrsize(vsi_nn_tensor_attr_t const &attr)
Get a tensor's (width, height) size in cv::Size format, skipping over other dimensions.
Definition Utils.C:487
std::string getLabel(std::map< int, std::string > const &labels, int id)
Get a label from an id.
Definition Utils.C:68
cv::Mat dequantize(cv::Mat const &m, vsi_nn_tensor_attr_t const &attr)
Dequantize an output to float32 according to the quantization spec in attr.
Definition Utils.C:793
vsi_nn_type_e tf2vsi(TfLiteType t)
Convert from TensorFlow data type to vsi_nn.
Definition Utils.C:330
void topK(float const *pfProb, float *pfMaxProb, uint32_t *pMaxClass, uint32_t outputCount, uint32_t topNum)
Get top-k entries and their indices.
Definition Utils.C:85
std::string shapestr(cv::Mat const &m)
Get a string of the form: "nD AxBxC... TYPE" from an n-dimensional cv::Mat with data type TYPE.
Definition Utils.C:105
std::vector< int > attrdims(vsi_nn_tensor_attr_t const &attr)
Get a tensor dims as a vector of int, useful to construct a matching cv::Mat.
Definition Utils.C:478
std::vector< size_t > strshape(std::string const &str)
Get a vector of size_t from a string containing AxBxC...
Definition Utils.C:277
bool attrmatch(vsi_nn_tensor_attr_t const &attr, cv::Mat const &blob)
Check that a cv::Mat blob matches exactly the spec of an attr.
Definition Utils.C:704
Main namespace for all JeVois classes and functions.
Definition Concepts.dox:2