cpp : Added documentation

This commit is contained in:
saundersp
2024-04-28 22:11:33 +02:00
parent f7ac38b93a
commit c71b04f00d
16 changed files with 797 additions and 295 deletions

View File

@ -1,7 +1,15 @@
#include "data.hpp"
#include "toolbox.hpp"
#include "config.hpp"
np::Array<uint32_t> set_integral_image_cpu(const np::Array<uint8_t>& set) noexcept {
#if GPU_BOOSTED == false
/**
* @brief Transform the input images in integrated images (CPU version).
*
* @param X Dataset of images
* @return Dataset of integrated images
*/
np::Array<uint32_t> set_integral_image(const np::Array<uint8_t>& set) noexcept {
np::Array<uint32_t> X_ii = np::empty<uint32_t>(set.shape);
size_t i, y, x, s;
@ -31,7 +39,14 @@ constexpr static inline int16_t __compute_feature__(const np::Array<uint32_t>& X
return X_ii[j + _yh + w] + X_ii[j + _y] - X_ii[j + _yh] - X_ii[j + _y + w];
}
np::Array<int32_t> apply_features_cpu(const np::Array<uint8_t>& feats, const np::Array<uint32_t>& X_ii) noexcept {
/**
* @brief Apply the features on a integrated image dataset (CPU version).
*
* @param feats Features to apply
* @param X_ii Integrated image dataset
* @return Applied features
*/
np::Array<int32_t> apply_features(const np::Array<uint8_t>& feats, const np::Array<uint32_t>& X_ii) noexcept {
np::Array<int32_t> X_feat = np::empty<int32_t>({ feats.shape[0], X_ii.shape[0] });
size_t j, feat_idx = 0;
@ -51,7 +66,7 @@ np::Array<int32_t> apply_features_cpu(const np::Array<uint8_t>& feats, const np:
return X_feat;
}
np::Array<float64_t> train_weak_clf_cpu(const np::Array<int32_t>& X_feat, const np::Array<uint16_t>& X_feat_argsort, const np::Array<uint8_t>& y, const np::Array<float64_t>& weights) noexcept {
np::Array<float64_t> train_weak_clf(const np::Array<int32_t>& X_feat, const np::Array<uint16_t>& X_feat_argsort, const np::Array<uint8_t>& y, const np::Array<float64_t>& weights) noexcept {
float64_t total_pos = 0.0, total_neg = 0.0;
for(size_t i = 0; i < y.shape[0]; ++i)
(y[i] == static_cast<uint8_t>(1) ? total_pos : total_neg) += weights[i];
@ -81,7 +96,69 @@ np::Array<float64_t> train_weak_clf_cpu(const np::Array<int32_t>& X_feat, const
return classifiers;
}
np::Array<uint16_t> argsort_2d_cpu(const np::Array<int32_t>& X_feat) noexcept {
/**
* @brief Perform an indirect sort of a given array within a given bound.
*
* @tparam T Inner type of the array
* @param a Array to sort
* @param indices Array of indices to write to
* @param low lower bound to sort
* @param high higher bound to sort
*/
template<typename T>
static void argsort(const T* const a, uint16_t* const indices, size_t low, size_t high) noexcept {
const size_t total = high - low + 1;
size_t* const stack = new size_t[total]{low, high};
//size_t stack[total];
//stack[0] = l;
//stack[1] = h;
size_t top = 1;
while (top <= total) {
high = stack[top--];
low = stack[top--];
if(low >= high)
break;
const size_t p = as_partition(a, indices, low, high);
if (p - 1 > low && p - 1 < total) {
stack[++top] = low;
stack[++top] = p - 1;
}
if (p + 1 < high) {
stack[++top] = p + 1;
stack[++top] = high;
}
}
delete[] stack;
}
/**
* @brief Apply argsort to every column of a given 2D array.
*
* @tparam T Inner type of the array
* @param a 2D Array to sort
* @return 2D Array of indices that sort the array
*/
template<typename T>
static np::Array<uint16_t> argsort_bounded(const np::Array<T>& a, const size_t& low, const size_t& high) noexcept {
np::Array<uint16_t> indices = np::empty(a.shape);
map(indices, [](const size_t& i, const uint16_t&) -> uint16_t { return i; });
argsort_bounded(a, indices, low, high);
return indices;
}
/**
* @brief Perform an indirect sort on each column of a given 2D array (CPU version).
*
* @param a 2D Array to sort
* @return 2D Array of indices that sort the array
*/
np::Array<uint16_t> argsort_2d(const np::Array<int32_t>& X_feat) noexcept {
const np::Array<uint16_t> indices = np::empty<uint16_t>(X_feat.shape);
const size_t length = np::prod(X_feat.shape);
for (size_t i = 0; i < length; i += X_feat.shape[1]) {
@ -91,3 +168,4 @@ np::Array<uint16_t> argsort_2d_cpu(const np::Array<int32_t>& X_feat) noexcept {
return indices;
}
#endif // GPU_BOOSTED == false