iVS3D v2.0.0
Loading...
Searching...
No Matches
NeuralUtil.h
Go to the documentation of this file.
1#pragma once
2
44#include <Tensor.h>
45#include <NeuralNet.h>
46
56namespace NN::Util {
57
67template<typename ReduceOp>
68auto bind_reduce(ReduceOp op, int axis) {
69 return [=](NN::Tensor&& t){
70 return t.reduce(op, axis);
71 };
72}
73
83template<typename ReduceIndexOp>
84auto bind_reduceWithIndex(ReduceIndexOp op, int axis) {
85 return [=](NN::Tensor&& t) {
86 return t.reduceWithIndex(op, axis);
87 };
88}
89
101template<typename Func>
102auto bind_map(Func f) {
103 return [=](NN::Tensor&& t) {
104 return t.map(f);
105 };
106}
107
120template<typename Func>
121auto bind_map(Func f, int axis) {
122 return [=](NN::Tensor&& t) {
123 return t.map(f, axis);
124 };
125}
126
138template<typename... Args>
139auto bind_reshape(const std::vector<int64_t>& newShape) {
140 return [=](NN::Tensor&& t) {
141 return t.reshape(newShape);
142 };
143}
144
154template<typename... Args>
156 return [=](NN::Tensor&& tensor) -> tl::expected<NN::Tensor, NN::NeuralError> {
157 auto result = tensor.squeeze();
158 if (!result) {
159 return tl::unexpected(result.error());
160 }
161 return std::move(tensor);
162 };
163}
164
176template<typename... Args>
177auto bind_squeeze(int64_t axis) {
178 return [=](NN::Tensor&& tensor) -> tl::expected<NN::Tensor,NN::NeuralError> {
179 auto result = tensor.squeeze(axis);
180 if (!result) {
181 return tl::unexpected(result.error());
182 }
183 return std::move(tensor);
184 };
185}
186
199template<typename... Args>
200auto bind_unsqueeze(int64_t axis) {
201 return [=](NN::Tensor&& tensor) -> tl::expected<NN::Tensor,NN::NeuralError> {
202 auto result = tensor.unsqueeze(axis);
203 if (!result) {
204 return tl::unexpected(result.error());
205 }
206 return std::move(tensor);
207 };
208}
209
216template<typename... Args>
218 return [=](NN::Tensor&& tensor) {
219 return tensor.toCvMat();
220 };
221}
222
229template<typename T, typename... Args>
231 return [=](NN::Tensor&& tensor) {
232 return tensor.toVector<T>();
233 };
234}
235
247template<typename... Args>
249 return [=](NN::Tensor&& input) {
250 return model->infer(input);
251 };
252}
253
254} // namespace NN::Util
Contains the NeuralNet interface for neural network inference.
Contains the Tensor class for representing N-dimensional arrays with various data types.
A Tensor represents a N-dimensional array containing elements of the same type. Can be used as input ...
Definition Tensor.h:201
std::shared_ptr< NeuralNet > NeuralNetPtr
Smart pointer type for managing NeuralNet instances.
Definition NeuralNet.h:95
auto bind_map(Func f)
Binds a mapping operation on a tensor.
Definition NeuralUtil.h:102
auto bind_toVector()
Binds a conversion operation from a tensor to a vector.
Definition NeuralUtil.h:230
auto bind_reshape(const std::vector< int64_t > &newShape)
Binds a reshape operation to change the shape of a tensor.
Definition NeuralUtil.h:139
auto bind_inference(NN::NeuralNetPtr model)
Binds an inference operation on a neural network model.
Definition NeuralUtil.h:248
auto bind_toCvMat()
Binds a conversion operation from a tensor to an OpenCV Mat format.
Definition NeuralUtil.h:217
auto bind_squeeze()
Binds a squeeze operation to remove dimensions of size 1 from a tensor.
Definition NeuralUtil.h:155
auto bind_reduceWithIndex(ReduceIndexOp op, int axis)
Binds a reduction operation with index on a specified axis for a tensor.
Definition NeuralUtil.h:84
auto bind_reduce(ReduceOp op, int axis)
Binds a reduction operation on a specified axis for a tensor.
Definition NeuralUtil.h:68
auto bind_unsqueeze(int64_t axis)
Binds an unsqueeze operation to add a new dimension of size 1 at a specified axis in a tensor.
Definition NeuralUtil.h:200
Namespace for utility functions related to neural networks and tensors.
Definition NeuralUtil.h:56