our_knp/examples/knp/tests/framework/inference_evaluation_test.cpp
int 8f43927575 Initial import: KNP experiments (examples, viz, cats-dogs, reports)
examples/ is based on KasperskyLab/knp at 55470ed6 with local changes
to the mnist-learn altai/blifat models, cuda-backend CMake and model_loader.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-14 18:17:15 +03:00

73 lines
2.5 KiB
C++

/**
* @file inference_evaluation_test.cpp
* @brief Inference evaluation test.
* @kaspersky_support D. Postnikov
* @date 21.08.2025
* @license Apache 2.0
* @copyright © 2025 AO Kaspersky Lab
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <knp/framework/inference_evaluation/classification/processor.h>
#include <tests_common.h>
class ClassificationTestHelper : public knp::framework::data_processing::classification::Dataset
{
public:
ClassificationTestHelper()
{
frames_amount_for_training_ = 0;
frames_amount_for_inference_ = 4;
steps_per_frame_ = 1;
classes_amount_ = 2;
dataset_ = {{1, {}}, {1, {}}, {0, {}}, {0, {}}};
}
};
TEST(InferenceEvaluation, Classification)
{
ClassificationTestHelper dataset;
std::vector<knp::core::messaging::SpikeMessage> spikes;
spikes.push_back({{knp::core::UID(false), 0}, {1, 3}});
spikes.push_back({{knp::core::UID(false), 1}, {5, 1}});
spikes.push_back({{knp::core::UID(false), 2}, {2, 0}});
spikes.push_back({{knp::core::UID(false), 3}, {3, 1}});
knp::framework::inference_evaluation::classification::InferenceResultsProcessor processor;
processor.process_inference_results(spikes, dataset);
auto const& res = processor.get_inference_results();
ASSERT_EQ(res[0].get_total_votes(), 2);
ASSERT_EQ(res[0].true_positives_, 1);
ASSERT_EQ(res[0].false_negatives_, 0);
ASSERT_EQ(res[0].false_positives_, 1);
ASSERT_EQ(res[0].true_negatives_, 2);
ASSERT_EQ(res[1].get_total_votes(), 2);
ASSERT_EQ(res[1].true_positives_, 2);
ASSERT_EQ(res[1].false_negatives_, 0);
ASSERT_EQ(res[1].false_positives_, 0);
ASSERT_EQ(res[1].true_negatives_, 2);
std::stringstream csv_res;
processor.write_inference_results_to_stream_as_csv(csv_res);
ASSERT_EQ(
csv_res.str(),
"CLASS,TOTAL_VOTES,TRUE_POSITIVES,FALSE_NEGATIVES,FALSE_POSITIVES,TRUE_NEGATIVES,PRECISION,RECALL,PREVALENCE,"
"ACCURACY,F_SCORE\n0,2,1,0,1,2,0.5,1,0.25,0.75,0.666667\n1,2,2,0,0,2,1,1,0.5,1,1\n");
}