|
| template<typename T > |
| tl::expected< std::vector< T >, NeuralError > | toVector () const |
| | Create a vector containing the data from the Tensor.
|
| |
| tl::expected< cv::Mat, NeuralError > | toCvMat () const |
| | Create a cv::Mat from a Tensor. In case of 2/3 dimensions this will convert back to CVs HWC layout. Returns an error for dimensions other than 2 or 3.
|
| |
| const Shape & | shape () const |
| | Readonly access to the shape of the tensor. Valid tensors ensure the shape is static, so no dimension is -1.
|
| |
| std::string | toString () const |
| | Create a human-readable string representation containing the Tensors shape and data type.
|
| |
| tl::expected< void, NeuralError > | reshape (const Shape &newShape) |
| | Reshape will interprete the data elements contained in the Tensor as a different shape. This does not move any elements!
|
| |
| int64_t | numElements () const |
| | Returns the number of elements contained in the Tensor.
|
| |
| bool | empty () const |
| | Check if the tensor is empty, so to say it contains no elements.
|
| |
| TensorType | dtype () const |
| | Check the data type of the Tensor. This is deduced from the data type of the contained elements.
|
| |
| template<typename Op > |
| tl::expected< Tensor, NeuralError > | reduce (const Op &op, uint64_t axis) const |
| | Reduce the Tensor along a given axis by applying an accumulative operation.
|
| |
| template<typename Op > |
| tl::expected< Tensor, NeuralError > | reduceWithIndex (const Op &op, uint64_t axis) const |
| | Reduce the Tensor along a given axis by applying an accumulative operation. The dimension in the rduced axis will be 1 after this.
|
| |
| template<typename Func > |
| tl::expected< Tensor, NeuralError > | map (Func &&f) const |
| | Map each element of the Tensor to a new value by applying a given function element-wise. This can be used to change the datatype as well.
|
| |
| template<typename Func > |
| tl::expected< Tensor, NeuralError > | map (Func &&func, int axis) const |
| | Map each element of the Tensor to an array of new values by applying a given function element-wise. This adds a new dimension in the given axis.
|
| |
| tl::expected< void, NeuralError > | squeeze () |
| | Squeeze the Tensor by removing dimensions of size 1. This operation is performed inplace.
|
| |
| tl::expected< void, NeuralError > | squeeze (int64_t axis) |
| | Squeeze the Tensor by removing a specific dimension of size 1.
|
| |
| tl::expected< void, NeuralError > | unsqueeze (int64_t axis) |
| | Add a new dimension of size 1 at the specified axis.
|
| |
|
| static tl::expected< Tensor, NeuralError > | fromCvMat (const cv::Mat &mat) |
| | Create a new Tensor object from a cv::Mat. This will convert from CVs HWC layout to ONNX standard layout [N]CHW. In this case the N -dimsension is NOT created as it would be 1!
|
| |
| static tl::expected< Tensor, NeuralError > | fromCvMat (const cv::Mat &mat, const Shape &shape, float scale=1.0f, std::vector< float > mean={}, std::vector< float > std={}) |
| | Create a new Tensor object from a cv::Mat with a specific shape, optional scaling, and per-channel mean-subtraction.
|
| |
| static tl::expected< Tensor, NeuralError > | fromCvMats (const std::vector< cv::Mat > &mats) |
| | Create a new Tensor object from a vector of cv::Mat objects. The cv::Mat objects must have the same size and type.
|
| |
| static tl::expected< Tensor, NeuralError > | fromCvMats (const std::vector< cv::Mat > &mats, const Shape &shape, float scale=1.0f, std::vector< float > mean={}, std::vector< float > std={}) |
| | Create a new Tensor object from a vector of cv::Mat objects with a specific shape, optional scaling, and per-channel mean-subtraction.
|
| |
| template<typename T > |
| static tl::expected< Tensor, NeuralError > | fromData (const std::vector< T > &data, const Shape &shape) |
| | Create a new Tensor object from a given data vector and shape. The number of elements in the vector must match shapeNumElements(shape).
|
| |
| template<typename T > |
| static tl::expected< Tensor, NeuralError > | fromData (std::vector< T > &&data, const Shape &shape) |
| | Create a new Tensor object from a given data vector and shape. The number of elements in the vector must match shapeNumElements(shape).
|
| |
|
| template<typename T , int CV_TYPE> |
| static tl::expected< Tensor, NeuralError > | fromCvMatsTyped (const std::vector< cv::Mat > &mats) |
| | Create a new Tensor object from a vector of cv::Mat objects with a specific type.
|
| |
|
static cv::Mat | preprocessCvMat (const cv::Mat &mat, const Shape &shape, float scale) |
| | Preprocess a cv::Mat to match the given shape by resizing, color conversion from BGR to RGB, and scaling.
|
| |
|
static cv::Mat | preprocessCvMat (const cv::Mat &mat, const Shape &shape, float scale, const std::vector< float > &mean, const std::vector< float > &std) |
| | Preprocess a cv::Mat to match the given shape by resizing, color conversion from BGR to RGB, scaling, and applying mean and std.
|
| |
A Tensor represents a N-dimensional array containing elements of the same type. Can be used as input and output for inference.
Supported element types are:
- See also
- TensorType for the supported types.
Tensors can be created from and converted to cv::Mat or std::vector<T>. The element type is passed as a Template argument or deduced from the cv::Mat::depth property.
- Date
- May 2025
- Author
- Dominik Wüst (domin.nosp@m.ik.w.nosp@m.uest@.nosp@m.iosb.nosp@m..frau.nosp@m.nhof.nosp@m.er.de)
| tl::expected< Tensor, NeuralError > NN::Tensor::fromCvMat |
( |
const cv::Mat & |
mat, |
|
|
const Shape & |
shape, |
|
|
float |
scale = 1.0f, |
|
|
std::vector< float > |
mean = {}, |
|
|
std::vector< float > |
std = {} |
|
) |
| |
|
static |
Create a new Tensor object from a cv::Mat with a specific shape, optional scaling, and per-channel mean-subtraction.
- Parameters
-
| mat | The cv::Mat containing the data in HWC layout for the Tensor. |
| shape | The desired shape of the Tensor. |
| scale | The scale factor to apply to the input data (default: 1.0). |
| mean | The mean values to subtract from each channel (default: empty vector). |
| std | The standard deviation values to divide each channel by (default: empty vector). |
- Returns
- tl::expected<Tensor, NeuralError> A Tensor in CHW layout or an error object.
This function creates a Tensor from a cv::Mat with the specified shape. The shape must be valid, meaning it must have at least two dimensions [...,H,W]. If the Channel dimension is present, it must match the number of channels in the cv::Mat or be -1 (dynamic).
- If the cv::Mat is grayscale, it will be converted to a single channel Tensor.
- If the cv::Mat has 3 channels, it will be converted from BGR to RGB.
- If the cv::Mat has more than 3 channels, it will be treated as a multi-channel Tensor.
The following steps are applied (in this order):
- Resize the cv::Mat to match W and H of the shape if necessary.
- If input is BGR, convert it to RGB.
- The data will be converted to float32 and scaled by the given scale factor.
- If a mean vector is provided, it will be subtracted from each channel.
- If a std vector is provided, each channel will be divided by the corresponding std value.
The resulting Tensor will be of type float32. Its shape will be squeezed, i.e. leading dimensions of size 1 and dynamic dimensions will be removed.
| tl::expected< Tensor, NeuralError > NN::Tensor::fromCvMats |
( |
const std::vector< cv::Mat > & |
mats | ) |
|
|
static |
Create a new Tensor object from a vector of cv::Mat objects. The cv::Mat objects must have the same size and type.
- Parameters
-
| mats | The vector of cv::Mat objects to convert. |
- Returns
- tl::expected<Tensor, NeuralError> A Tensor object containing the stacked cv::Mat data in NCHW layout or an error object.
The function stacks the cv::Mat objects along the first dimension (N) and converts them to a Tensor in NCHW layout. The following requirements must be met:
- All cv::Mat objects must have the same size (rows, cols, channels) and type.
- The cv::Mat objects must not be empty.
- The cv::Mat objects must have a type of either CV_8U or CV_32F.
The resulting Tensor will have a shape of [N,C,H,W], where:
- N is the number of cv::Mat objects in the input vector.
- C is the number of channels in the cv::Mat objects.
- H is the height (rows) of the cv::Mat objects.
- W is the width (cols) of the cv::Mat objects.
The datatype of the Tensor will be determined by the cv::Mat type.
| tl::expected< Tensor, NeuralError > NN::Tensor::fromCvMats |
( |
const std::vector< cv::Mat > & |
mats, |
|
|
const Shape & |
shape, |
|
|
float |
scale = 1.0f, |
|
|
std::vector< float > |
mean = {}, |
|
|
std::vector< float > |
std = {} |
|
) |
| |
|
static |
Create a new Tensor object from a vector of cv::Mat objects with a specific shape, optional scaling, and per-channel mean-subtraction.
- Parameters
-
| mats | The vector of cv::Mat objects to convert. |
| shape | The desired shape of the Tensor. |
| scale | The scale factor to apply to the input data (default: 1.0). |
| mean | The mean values to subtract from each channel (default: empty vector). |
- Returns
- tl::expected<Tensor, NeuralError> A Tensor object containing the stacked cv::Mat data in NCHW layout or an error object.
This function creates a Tensor from a vector of cv::Mat objects with the specified shape. The shape must match the following requirements:
- The shape must have four dimensions [N,C,H,W].
- The first dimension (N) is the number of cv::Mat objects in the input vector.
- The second dimension (C) is the number of channels in the cv::Mat objects.
If the size of the cv::Mat objects does not match the height and width specified in the shape, they will be resized accordingly. The following steps are applied (in this order):
- Resize each cv::Mat to match the height and width specified in the shape.
- The cv:Mat is converted from BGR to RGB.
- The data will be converted to float32 and scaled by the scale factor.
- If a mean vector is provided, it will be subtracted per-channel.
- If a std vector is provided, each channel will be divided by the corresponding std value.