iVS3D v2.0.0
Loading...
Searching...
No Matches
NeuralUtil

Contains utility functions for tensor operations in neural networks. More...

Namespaces

namespace  NN
 NN Neural Network Library containing Tensor and NeuralNet classes for inference.
 
namespace  NN::Util
 Namespace for utility functions related to neural networks and tensors.
 

Functions

template<typename ReduceOp >
auto NN::Util::bind_reduce (ReduceOp op, int axis)
 Binds a reduction operation on a specified axis for a tensor.
 
template<typename ReduceIndexOp >
auto NN::Util::bind_reduceWithIndex (ReduceIndexOp op, int axis)
 Binds a reduction operation with index on a specified axis for a tensor.
 
template<typename Func >
auto NN::Util::bind_map (Func f)
 Binds a mapping operation on a tensor.
 
template<typename Func >
auto NN::Util::bind_map (Func f, int axis)
 Binds a mapping operation on a tensor that converts a each element to an array of new elements.
 
template<typename... Args>
auto NN::Util::bind_reshape (const std::vector< int64_t > &newShape)
 Binds a reshape operation to change the shape of a tensor.
 
template<typename... Args>
auto NN::Util::bind_squeeze ()
 Binds a squeeze operation to remove dimensions of size 1 from a tensor.
 
template<typename... Args>
auto NN::Util::bind_squeeze (int64_t axis)
 Binds a squeeze operation on a tensor along a specified axis.
 
template<typename... Args>
auto NN::Util::bind_unsqueeze (int64_t axis)
 Binds an unsqueeze operation to add a new dimension of size 1 at a specified axis in a tensor.
 
template<typename... Args>
auto NN::Util::bind_toCvMat ()
 Binds a conversion operation from a tensor to an OpenCV Mat format.
 
template<typename T , typename... Args>
auto NN::Util::bind_toVector ()
 Binds a conversion operation from a tensor to a vector.
 
template<typename... Args>
auto NN::Util::bind_inference (NN::NeuralNetPtr model)
 Binds an inference operation on a neural network model.
 

Detailed Description

Contains utility functions for tensor operations in neural networks.

This functional header provides utility functions to bind tensor operations such as reduction, mapping, reshaping, squeezing, unsqueezing, and converting to OpenCV Mat format. These functions are designed to be used with the Tensor class and can be easily integrated into neural network workflows by using tl::expected::and_then for chaining operations.

NeuralNetPtr model = ...; // some neural network model for semantic segmentation
cv::Mat image = ...; // some OpenCV RGB input image
std::vector<std::array<uint8_t,3>> colors = {
{0, 0, 0}, // background
{255, 0, 0}, // class 1
... // other classes
};
auto workflow = Tensor::fromCvMat(image)
.and_then(Util::bind_map([](uint8_t val){
return static_cast<float>(val) / 255.0f; // normalize to [0,1]
})
.and_then(Util::bind_inference(model))
.and_then(Util::bind_reduce(ReduceArgMax, 1))
.and_then(Util::bind_squeeze())
.and_then(Util::bind_map([colors](int64_t val) {
return colors[val]; // map class index to RGB color
}, 0))
.and_then(Util::bind_toCvMat());
if (!workflow) {
std::cerr << "Error occurred during workflow: " << workflow.error() << std::endl;
}
cv::Mat outputImage = workflow.value();
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)
Date
May 2025

Function Documentation

◆ bind_inference()

template<typename... Args>
auto NN::Util::bind_inference ( NN::NeuralNetPtr  model)

Binds an inference operation on a neural network model.

Parameters
modelThe neural network model to perform inference on.

This function allows you to perform inference on a given input tensor using the specified neural network model. It returns the output tensor after processing the input through the model.

See also
NeuralNet::infer for the main inference function that applies this operation.

◆ bind_map() [1/2]

template<typename Func >
auto NN::Util::bind_map ( Func  f)

Binds a mapping operation on a tensor.

Parameters
fThe function to apply to each element of the tensor.

This function allows you to apply a function to each element of the tensor. It can be used to transform the data in the tensor, such as normalizing values or converting types.

See also
Tensor::map for the main mapping function that applies this operation.

◆ bind_map() [2/2]

template<typename Func >
auto NN::Util::bind_map ( Func  f,
int  axis 
)

Binds a mapping operation on a tensor that converts a each element to an array of new elements.

Parameters
fThe function to apply to each element of the tensor.
axiswhere to insert the new dimension from the array.

This function allows you to apply a function to each element of the tensor and convert it to an array of new elements. It can be used to transform the data in the tensor, such as mapping each element to a color.

See also
Tensor::map for the main mapping function that applies this operation.

◆ bind_reduce()

template<typename ReduceOp >
auto NN::Util::bind_reduce ( ReduceOp  op,
int  axis 
)

Binds a reduction operation on a specified axis for a tensor.

Parameters
opThe reduction operation to apply, such as ReduceSum, ReduceMin, ReduceMax.
axisThe axis along which to perform the reduction.
See also
Tensor::reduce for the main reduction function that applies this operation.

◆ bind_reduceWithIndex()

template<typename ReduceIndexOp >
auto NN::Util::bind_reduceWithIndex ( ReduceIndexOp  op,
int  axis 
)

Binds a reduction operation with index on a specified axis for a tensor.

Parameters
opThe reduction operation with index to apply, such as ReduceArgMin, ReduceArgMax.
axisThe axis along which to perform the reduction.
See also
Tensor::reduceWithIndex for the main reduction function that applies this operation.

◆ bind_reshape()

template<typename... Args>
auto NN::Util::bind_reshape ( const std::vector< int64_t > &  newShape)

Binds a reshape operation to change the shape of a tensor.

Parameters
newShapeThe new shape to reshape the tensor to.

This function allows you to reshape a tensor to a new shape specified by the newShape vector. The new shape must be compatible with the number of elements in the tensor.

See also
Tensor::reshape for the main reshape function that applies this operation.

◆ bind_squeeze() [1/2]

template<typename... Args>
auto NN::Util::bind_squeeze ( )

Binds a squeeze operation to remove dimensions of size 1 from a tensor.

This function allows you to remove dimensions of size 1 from the tensor, effectively reducing its dimensionality. It can be used to simplify the shape of the tensor after operations that may have added unnecessary dimensions.

See also
Tensor::squeeze for the main squeeze function that applies this operation.

◆ bind_squeeze() [2/2]

template<typename... Args>
auto NN::Util::bind_squeeze ( int64_t  axis)

Binds a squeeze operation on a tensor along a specified axis.

Parameters
axisThe axis to squeeze. This must be in the range [0, shape.size()-1] and the dimension at this axis must be 1.

This function allows you to remove a specific dimension of size 1 from the tensor. It can be used to simplify the shape of the tensor after operations that may have added unnecessary dimensions. If the specified axis is invalid or the dimension at that axis is not 1, an error message will be returned.

See also
Tensor::squeeze for the main squeeze function that applies this operation.

◆ bind_toCvMat()

template<typename... Args>
auto NN::Util::bind_toCvMat ( )

Binds a conversion operation from a tensor to an OpenCV Mat format.

See also
Tensor::toCvMat for the main conversion function that applies this operation.

◆ bind_toVector()

template<typename T , typename... Args>
auto NN::Util::bind_toVector ( )

Binds a conversion operation from a tensor to a vector.

See also
Tensor::toVector for the main conversion function that applies this operation.

◆ bind_unsqueeze()

template<typename... Args>
auto NN::Util::bind_unsqueeze ( int64_t  axis)

Binds an unsqueeze operation to add a new dimension of size 1 at a specified axis in a tensor.

Parameters
axisThe axis to insert the new dimension. This must be in the range [0, shape.size()].

This function allows you to add a new dimension of size 1 at the specified axis in the tensor. It can be used to prepare the tensor for operations that require a specific dimensionality. If the specified axis is invalid, an error message will be returned.

See also
Tensor::unsqueeze for the main unsqueeze function that applies this operation.