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

@ -4,20 +4,13 @@
#include "config.hpp"
#include "toolbox_unit_test.hpp"
#include "ViolaJones.hpp"
#include "ViolaJones_device.hpp"
#if GPU_BOOSTED
#include "ViolaJonesGPU.hpp"
#include "gpu_unit_test.hpp"
#define LABEL "GPU"
#define apply_features apply_features_gpu
#define set_integral_image set_integral_image_gpu
#define argsort_2d argsort_2d_gpu
#else
#include "ViolaJonesCPU.hpp"
#define LABEL "CPU"
#define apply_features apply_features_cpu
#define set_integral_image set_integral_image_cpu
#define argsort_2d argsort_2d_cpu
#endif
/**
@ -28,7 +21,7 @@
* - Calculate features
* - Calculate integral images
* - Apply features to images
* - Calculate argsort of the featured images.
* - Calculate argsort of the featured images
*
* @return std::tuple<np::Array<int32_t>, np::Array<uint16_t>, np::Array<uint8_t>, np::Array<int32_t>, np::Array<uint8_t>> Tuple containing in order : training features, training features sorted indexes, training labels, testing features, testing labels
*/
@ -40,7 +33,7 @@ std::tuple<np::Array<int32_t>, np::Array<uint16_t>, np::Array<uint8_t>, np::Arra
const std::chrono::system_clock::time_point preproc_timestamp = perf_counter_ns();
const std::array<int32_t, 3> preproc_gaps = { 49, -18, 29 };
header({ "Preprocessing", "Time spent (ns)", "Formatted time spent" }, preproc_gaps);
header(preproc_gaps, { "Preprocessing", "Time spent (ns)", "Formatted time spent" });
const auto [ X_train, y_train, X_test, y_test ] = state_saver<uint8_t, 4>("Loading sets", preproc_gaps[0], { "X_train", "y_train", "X_test", "y_test" },
FORCE_REDO, SAVE_STATE, OUT_DIR, load_datasets);
@ -97,8 +90,7 @@ std::tuple<np::Array<int32_t>, np::Array<uint16_t>, np::Array<uint8_t>, np::Arra
print(X_test_feat, { IDX_INSPECT, IDX_INSPECT + IDX_INSPECT_OFFSET });
#endif
// const np::Array<int32_t> indices = measure_time_save<Array<int>>("Selecting best features", preproc_gaps[0], "indices", select_percentile, X_train_feat, d.y_train);
// const np::Array<int32_t> indices = measure_time<Array<int>>("Selecting best features", preproc_gaps[0], select_percentile, X_train_feat, d.y_train);
// const np::Array<int32_t> indices = state_saver<int32_t>("Selecting best features", preproc_gaps[0], "indices", select_percentile, X_train_feat, d.y_train);
#if __DEBUG
// print_feature(indices);
@ -113,13 +105,13 @@ std::tuple<np::Array<int32_t>, np::Array<uint16_t>, np::Array<uint8_t>, np::Arra
print(X_train_feat_argsort, { IDX_INSPECT, IDX_INSPECT + IDX_INSPECT_OFFSET });
#endif
// const np::Array<uint16_t> X_test_feat_argsort = state_saver<uint16_t>("Precalculating testing set argsort (" LABEL ")", preproc_gaps[0], "X_test_feat_argsort_" LABEL,
// FORCE_REDO, SAVE_STATE, OUT_DIR, argsort_2d, X_test_feat);
const np::Array<uint16_t> X_test_feat_argsort = state_saver<uint16_t>("Precalculating testing set argsort (" LABEL ")", preproc_gaps[0], "X_test_feat_argsort_" LABEL,
FORCE_REDO, SAVE_STATE, OUT_DIR, argsort_2d, X_test_feat);
#if __DEBUG
// printf("X_test_feat_argsort\n");
// print(X_test_feat_argsort.shape);
// print(X_test_feat_argsort, { IDX_INSPECT, IDX_INSPECT + IDX_INSPECT_OFFSET });
printf("X_test_feat_argsort\n");
print(X_test_feat_argsort.shape);
print(X_test_feat_argsort, { IDX_INSPECT, IDX_INSPECT + IDX_INSPECT_OFFSET });
#endif
const long long time_spent = duration_ns(perf_counter_ns() - preproc_timestamp);
formatted_line(preproc_gaps, "", "", "", "");
@ -131,15 +123,15 @@ std::tuple<np::Array<int32_t>, np::Array<uint16_t>, np::Array<uint8_t>, np::Arra
/**
* @brief Train the weak classifiers.
*
* @param X_train_feat Training images.
* @param X_train_feat_argsort Sorted indexes of the training images features.
* @param y_train Training labels.
* @return Trained models
* @param X_train_feat Training images
* @param X_train_feat_argsort Sorted indexes of the training images features
* @param y_train Training labels
* @return List of trained models
*/
std::array<std::array<np::Array<float64_t>, 2>, TS.size()> train(const np::Array<int32_t>& X_train_feat, const np::Array<uint16_t>& X_train_feat_argsort, const np::Array<uint8_t>& y_train) noexcept {
const std::chrono::system_clock::time_point training_timestamp = perf_counter_ns();
const std::array<int32_t, 3> training_gaps = { 26, -18, 29 };
header({ "Training", "Time spent (ns)", "Formatted time spent" }, training_gaps);
header(training_gaps, { "Training", "Time spent (ns)", "Formatted time spent" });
std::array<std::array<np::Array<float64_t>, 2>, TS.size()> models;
@ -173,14 +165,15 @@ std::array<std::array<np::Array<float64_t>, 2>, TS.size()> train(const np::Array
/**
* @brief Benchmark the trained classifiers on the training and testing sets.
*
* @param X_train_feat Training features.
* @param y_train Training labels.
* @param X_test_feat Testing features.
* @param y_test Testing labels.
* @param models List of trained models
* @param X_train_feat Training features
* @param y_train Training labels
* @param X_test_feat Testing features
* @param y_test Testing labels
*/
void testing_and_evaluating(const std::array<std::array<np::Array<float64_t>, 2>, TS.size()>& models, const np::Array<int32_t>& X_train_feat, const np::Array<uint8_t>& y_train, const np::Array<int32_t>& X_test_feat, const np::Array<uint8_t>& y_test) {
const std::array<int32_t, 5> testing_gaps = { 26, -19, 24, -19, 24 };
header({ "Testing", "Time spent (ns) (E)", "Formatted time spent (E)", "Time spent (ns) (T)", "Formatted time spent (T)" }, testing_gaps);
header(testing_gaps, { "Testing", "Time spent (ns) (E)", "Formatted time spent (E)", "Time spent (ns) (T)", "Formatted time spent (T)" });
std::array<std::array<float64_t, 8>, TS.size()> results;
size_t i = 0;
@ -216,7 +209,7 @@ void testing_and_evaluating(const std::array<std::array<np::Array<float64_t>, 2>
footer(testing_gaps);
const std::array<int32_t, 9> evaluating_gaps = { 19, -7, -6, -6, -6, -7, -6, -6, -6 };
header({ "Evaluating", "ACC (E)", "F1 (E)", "FN (E)", "FP (E)", "ACC (T)", "F1 (T)", "FN (T)", "FP (T)"}, evaluating_gaps);
header(evaluating_gaps, { "Evaluating", "ACC (E)", "F1 (E)", "FN (E)", "FP (E)", "ACC (T)", "F1 (T)", "FN (T)", "FP (T)"});
i = 0;
for (const size_t T : TS) {
@ -231,14 +224,13 @@ void testing_and_evaluating(const std::array<std::array<np::Array<float64_t>, 2>
/**
* @brief Test if the each result is equals to other devices.
*
* Given ViolaJones is a fully deterministic algorithm.
* The results, regardless the device, should be the same.
* This function check this assertion.
* Given ViolaJones is a fully deterministic algorithm. The results, regardless the device, should be the same,
* this function check this assertion.
*/
void unit_test(void) {
const std::chrono::system_clock::time_point unit_timestamp = perf_counter_ns();
const std::array<int32_t, 4> unit_gaps = { 37, -10, -18, 29};
header({ "Unit testing", "Test state", "Time spent (ns)", "Formatted time spent" }, unit_gaps);
header(unit_gaps, { "Unit testing", "Test state", "Time spent (ns)", "Formatted time spent" });
char title[BUFFER_SIZE] = { 0 };
char tmp_title[BUFFER_SIZE / 2] = { 0 };
@ -300,7 +292,7 @@ void unit_test(void) {
snprintf(tmp_title, BUFFER_SIZE / 2, "X_%s_feat_argsort", label);
snprintf(title, BUFFER_SIZE, "%-22s - CPU argsort", tmp_title);
test_fnc(title, [&X_feat, &X_feat_argsort_cpu, &file_cpu]{
X_feat_argsort_cpu = std::move(load<uint16_t>(file_cpu));
X_feat_argsort_cpu = load<uint16_t>(file_cpu);
return unit_test_argsort_2d<int32_t>(X_feat, X_feat_argsort_cpu);
});
}
@ -311,7 +303,7 @@ void unit_test(void) {
snprintf(tmp_title, BUFFER_SIZE / 2, "X_%s_feat_argsort", label);
snprintf(title, BUFFER_SIZE, "%-22s - GPU argsort", tmp_title);
test_fnc(title, [&X_feat, &X_feat_argsort_gpu, &file_gpu]{
X_feat_argsort_gpu = std::move(load<uint16_t>(file_gpu));
X_feat_argsort_gpu = load<uint16_t>(file_gpu);
return unit_test_argsort_2d<int32_t>(X_feat, X_feat_argsort_gpu);
});
}
@ -355,7 +347,7 @@ int32_t main(void){
const std::chrono::system_clock::time_point unit_timestamp = perf_counter_ns();
const std::array<int32_t, 3> unit_gaps = { 27, -18, 29 };
header({ "Unit testing", "Time spent (ns)", "Formatted time spent" }, unit_gaps);
header(unit_gaps, { "Unit testing", "Time spent (ns)", "Formatted time spent" });
#if GPU_BOOSTED
benchmark_function_void("Testing GPU capabilities 1D", unit_gaps[0], test_working, 50000);
benchmark_function_void("Testing GPU capabilities 2D", unit_gaps[0], test_working_2d, 200, 500);