Skip to content
Snippets Groups Projects
Commit e9a72040 authored by Houssem ROUIS's avatar Houssem ROUIS
Browse files

add doc for interpolation functions

parent 979d3bf4
No related branches found
No related tags found
No related merge requests found
Pipeline #75044 passed
......@@ -70,10 +70,16 @@ class InterpolationCPU : public Interpolation {
bool excludeOutside=false);
/**
* @brief performs linear interpolation on given points.
* @param[in] values: values to interpolate, since we only do an average of
* all values, their indexes isn't useful.
* @return interpolated value
* @brief Performs linear interpolation over a set of values.
*
* Applies recursive linear interpolation across all dimensions
* to estimate the value at the given coordinates.
*
* @param[in] originalCoords Coordinates at which to interpolate.
* @param[in] points Set of known points and their associated values.
* @param[in] scales Per-dimension scaling factors.
* @param[in] antialiasing Whether to apply antialiasing adjustment to weights.
* @return Interpolated value.
*/
template <typename T>
static T linear(const std::vector<float> &originalCoords,
......@@ -81,42 +87,40 @@ class InterpolationCPU : public Interpolation {
const std::vector<float> &scales,
bool antialiasing=false);
template <typename T>
// static T cubicInterpolate1D(const std::map<int, T>& indexedValues, float x, float scale, float cubic_coeff_a= -0.75f, bool antialiasing=false);
static T cubicInterpolate1D(const std::map<int, T>& indexedValues,
float x,
DimSize_t min_index, // bounds for exclude_outside
DimSize_t max_index,
float scale,
float a= -0.75f,
bool antialiasing=false,
bool exclude_outside=false);
template <typename T>
static T cubicRecurse(const std::vector<float> &coordToInterpolate,
const std::set<Point<T>> &points,
const std::vector<float> &scales,
const std::vector<DimSize_t> &inputDims,
DimIdx_t alongDim,
float cubic_coeff_a = -0.75f,
bool antialiasing=false,
bool excludeOutside=false);
/**
* @brief Performs full N-dimensional cubic interpolation.
*
* Calls `cubicRecurse()` starting from the first dimension to compute
* the interpolated value at the given coordinates.
*
* @param[in] coordToInterpolate Coordinates at which to interpolate.
* @param[in] pointsToInterpolate Set of points used for interpolation.
* @param[in] scales Per-dimension scale factors.
* @param[in] inputDims Dimensions of the input tensor.
* @param[in] cubic_coeff_a Coefficient used for cubic interpolation.
* @param[in] antialiasing Whether to apply antialiasing.
* @param[in] excludeOutside Whether to ignore values outside input bounds.
* @return Interpolated value.
*/
template <typename T>
static T cubic(const std::vector<float> &coordToInterpolate,
const std::set<Point<T>> &pointsToInterpolate,
const std::vector<float> &scales,
const std::vector<DimSize_t> &inputDims,
float cubic_coeff_a = -0.75f,
bool antialiasing=false,
bool excludeOutside=false);
template <typename T>
static T cubic(const std::vector<float> &coordToInterpolate,
const std::set<Point<T>> &pointsToInterpolate,
const std::vector<float> &scales,
const std::vector<DimSize_t> &inputDims,
float cubic_coeff_a = -0.75f,
bool antialiasing=false,
bool excludeOutside=false);
/**
* @brief performs nearest interpolation on given points.
* @note it is a wrapper for linearRecurse() private method
* @param[in] coordsToInterpolate: coordinates to interpolate
* @param[in] points: points to interpolate
* @param[in] interpMode: interpolation method, must be a Nearest...
* otherwise function will throw an error.
* @return interpolated value
* @brief Performs nearest neighbor interpolation.
*
* Returns the value of the nearest point based on the specified mode.
*
* @param[in] coordsToInterpolate Coordinates where interpolation is evaluated.
* @param[in] points Set of available input points and their values.
* @param[in] nearestMode Rounding strategy for nearest neighbor lookup.
* Must be a valid "Nearest" interpolation mode.
* @return Interpolated value.
*/
template <typename T>
static T nearest(const std::vector<float> &coordsToInterpolate,
......@@ -125,19 +129,17 @@ class InterpolationCPU : public Interpolation {
private:
/**
* @brief actual linear interpolation function.
* will :
* - Split all points along each dimension depending of if their coords at
* idx alongDim are above or under coordsToInterpolate until they are
* 1-to-1.
* - Perform interpolation in 2 leftover points and return interpolated
* point to parent call with a set of size 1.
* - repeat until all dimensions have been interpolated.
* @param[in] coordsToInterpolate: coordinates to interpolate
* @param[in] points: points to interpolate
* @param[in] alongDim: discriminant on along which dimension are being
* segregated.
* @return
* @brief Recursive implementation of linear interpolation.
*
* Performs binary interpolation along one dimension at a time until
* only one point remains, which corresponds to the final interpolated value.
*
* @param[in] coordsToInterpolate Target coordinates for interpolation.
* @param[in] points Set of available input points and their values.
* @param[in] scales Per-dimension scale factors.
* @param[in] alongDim Current axis along which to interpolate.
* @param[in] antialiasing Whether to apply antialiasing correction.
* @return A set with a single interpolated point (value at target coord).
*/
template <typename T>
static std::set<Interpolation::Point<T>>
......@@ -146,6 +148,59 @@ class InterpolationCPU : public Interpolation {
const std::vector<float> &scales,
const DimIdx_t alongDim = 0,
bool antialiasing = false);
/**
* @brief Performs 1D cubic interpolation using 4 neighboring values.
*
* Based on the provided point and surrounding samples, computes the
* interpolated value in 1D using the cubic Hermite spline method.
*
* @param[in] indexedValues Map of index to value, used for interpolation.
* @param[in] x Target float coordinate to interpolate.
* @param[in] min_index Minimum valid index (for excludeOutside).
* @param[in] max_index Maximum valid index (for excludeOutside).
* @param[in] scale Scale factor for the axis.
* @param[in] a Cubic coefficient (typically -0.5 to -0.75).
* @param[in] antialiasing Whether to apply antialiasing compensation.
* @param[in] exclude_outside Whether to ignore samples outside bounds.
* @return Interpolated value.
*/
template <typename T>
static T cubicInterpolate1D(const std::map<int, T>& indexedValues,
float x,
DimSize_t min_index, // bounds for exclude_outside
DimSize_t max_index,
float scale,
float a= -0.75f,
bool antialiasing=false,
bool exclude_outside=false);
/**
* @brief Recursively performs N-dimensional cubic interpolation.
*
* Starts from highest dimension and reduces the point set along
* each axis using cubic interpolation until a single interpolated
* value remains.
*
* @param[in] coordToInterpolate Coordinates where interpolation is evaluated.
* @param[in] points Set of known points and values.
* @param[in] scales Per-dimension scale factors.
* @param[in] inputDims Input tensor dimensions.
* @param[in] alongDim Current axis along which to interpolate.
* @param[in] cubic_coeff_a Cubic coefficient (default -0.75).
* @param[in] antialiasing Enable antialiasing adjustments.
* @param[in] excludeOutside Ignore points outside tensor bounds.
* @return Interpolated value.
*/
template <typename T>
static T cubicRecurse(const std::vector<float> &coordToInterpolate,
const std::set<Point<T>> &points,
const std::vector<float> &scales,
const std::vector<DimSize_t> &inputDims,
DimIdx_t alongDim,
float cubic_coeff_a = -0.75f,
bool antialiasing=false,
bool excludeOutside=false);
};
} // namespace Aidge
......
// /********************************************************************************
// * Copyright (c) 2025 CEA-List
// *
// * This program and the accompanying materials are made available under the
// * terms of the Eclipse Public License 2.0 which is available at
// * http://www.eclipse.org/legal/epl-2.0.
// *
// * SPDX-License-Identifier: EPL-2.0
// *
// ********************************************************************************/
// #include <memory>
// #include <catch2/catch_test_macros.hpp>
// #include "aidge/backend/cpu/data/TensorImpl.hpp"
// #include "aidge/backend/cpu/operator/PaddedAvgPoolingImpl.hpp"
// #include "aidge/data/DataType.hpp"
// #include "aidge/data/Tensor.hpp"
// #include "aidge/graph/Node.hpp"
// #include "aidge/operator/MetaOperatorDefs.hpp"
// using namespace Aidge;
// TEST_CASE("[cpu/operator] PaddedAvgPooling(forward)", "[PaddedAvgPooling][CPU]") {
// SECTION("test Padding") {
// std::shared_ptr<Node> myAvgPool = PaddedAvgPooling({2,2}, "myAvgPool", {2,2}, {1,1}, {1,1,1,1});
// auto op = std::static_pointer_cast<OperatorTensor>(myAvgPool -> getOperator());
// std::shared_ptr<Tensor> myInput = std::make_shared<Tensor>(Array4D<float,2,2,5,5> { //NCHW
// {
// {
// {{ 0, 1, 2, 3, 4},
// { 5, 6, 7, 8, 9},
// { 10, 11, 12, 13, 14},
// { 15, 16, 17, 18, 19},
// { 20, 21, 22, 23, 24}},
// {{ 25, 26, 27, 28, 29},
// { 30, 31, 32, 33, 34},
// { 35, 36, 37, 38, 39},
// { 40, 41, 42, 43, 44},
// { 45, 46, 47, 48, 49}}
// },
// {
// {{100, 101, 102, 103, 104},
// {105, 106, 107, 108, 109},
// {110, 111, 112, 113, 114},
// {115, 116, 117, 118, 119},
// {120, 121, 122, 123, 124}},
// {{125, 126, 127, 128, 129},
// {130, 131, 132, 133, 134},
// {135, 136, 137, 138, 139},
// {140, 141, 142, 143, 144},
// {145, 146, 147, 148, 149}}
// }
// }
// });
// // std::shared_ptr<Node> myAvgPool = AvgPooling({2,2}, "myAvgPool", {2,2}, {1,1,1,1}); // Adding padding {top, left, bottom, right}
// // auto op = std::static_pointer_cast<AvgPooling_Op<2>>(myAvgPool -> getOperator());
// std::shared_ptr<Tensor> myOutput = std::make_shared<Tensor>(Array4D<float,2,2,3,3> { // Adjusting output size for padding
// {
// {
// {{ 1.5, 2.5, 2.5},
// { 6.5, 7.5, 7.5},
// { 8.5, 9.5, 9.5}},
// {{ 15.5, 16.5, 16.5},
// { 20.5, 21.5, 21.5},
// { 22.5, 23.5, 23.5}}
// },
// {
// {{ 51.5, 52.5, 52.5},
// { 56.5, 57.5, 57.5},
// { 58.5, 59.5, 59.5}},
// {{ 65.5, 66.5, 66.5},
// { 70.5, 71.5, 71.5},
// { 72.5, 73.5, 73.5}}
// }
// }
// });
// op->associateInput(0,myInput);
// op->setDataType(DataType::Float32);
// op->setBackend("cpu");
// myAvgPool->forward();
// op->getOutput(0)->print();
// REQUIRE(*(op->getOutput(0)) == *myOutput);
// }
// }
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment