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>
195 lines
6.3 KiB
C++
195 lines
6.3 KiB
C++
/**
|
|
* @file parse_arguments.cpp
|
|
* @brief Parsing of command line arguments.
|
|
* @kaspersky_support D. Postnikov
|
|
* @date 03.02.2026
|
|
* @license Apache 2.0
|
|
* @copyright © 2026 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 "parse_arguments.h"
|
|
|
|
#include <knp/framework/logging.h>
|
|
|
|
#include <iostream>
|
|
#include <string>
|
|
|
|
#include <boost/program_options.hpp>
|
|
|
|
|
|
namespace po = boost::program_options;
|
|
|
|
std::optional<ModelDescription> parse_arguments(int argc, char** argv)
|
|
{
|
|
po::options_description desc("Usage");
|
|
desc.add_options()("help,h", "print available options")(
|
|
"model,m", po::value<std::string>()->default_value("blifat"), "model type: blifat or altai")(
|
|
"train_iters,t", po::value<size_t>()->default_value(60000), "number of images for training")(
|
|
"inference_iters,i", po::value<size_t>()->default_value(10000), "number of images for inference")(
|
|
"images", po::value<std::string>()->default_value("MNIST.bin"), "path to raw images file")(
|
|
"labels", po::value<std::string>()->default_value("MNIST.target"), "path to images labels file")(
|
|
"log_path", po::value<std::string>()->default_value(""),
|
|
"path for saving logs (if empty, no logs are saved)")(
|
|
"inference_only", "skip training and use `model_path` to load a pre-trained model")(
|
|
"training_backend", po::value<std::string>()->default_value("knp-cpu-single-threaded-backend"),
|
|
"path to backend used for training")(
|
|
"inference_backend", po::value<std::string>(),
|
|
"path to backend for inference (if empty, `training_backend` is used)")(
|
|
"extensive_logs_path", po::value<std::string>()->default_value(""),
|
|
"path for storing extensive logs (if empty, no extensive logs are produced)")(
|
|
"model_path", po::value<std::string>()->default_value(""),
|
|
"path for saving trained model (if empty, model is not saved)")(
|
|
"logging_level,l", po::value<std::string>()->default_value("info"),
|
|
"logging level: trace, debug, info, warn, error, critical, or none");
|
|
|
|
po::variables_map vm;
|
|
po::store(po::parse_command_line(argc, argv, desc), vm);
|
|
po::notify(vm);
|
|
|
|
if (vm.count("help"))
|
|
{
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
ModelDescription model_desc;
|
|
auto common_path = std::filesystem::weakly_canonical(std::filesystem::path(argv[0]).parent_path());
|
|
|
|
if (vm.count("model"))
|
|
{
|
|
std::string model_type = vm["model"].as<std::string>();
|
|
if (model_type == "blifat")
|
|
{
|
|
model_desc.type_ = SupportedModelType::BLIFAT;
|
|
}
|
|
else if (model_type == "altai")
|
|
{
|
|
model_desc.type_ = SupportedModelType::AltAI;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Not supported model type." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Model type not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (vm.count("train_iters"))
|
|
{
|
|
model_desc.train_images_amount_ = vm["train_iters"].as<size_t>();
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Train iterations not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (vm.count("inference_iters"))
|
|
{
|
|
model_desc.inference_images_amount_ = vm["inference_iters"].as<size_t>();
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Inference iterations not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (vm.count("images"))
|
|
{
|
|
model_desc.images_file_path_ = vm["images"].as<std::string>();
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Images path not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (vm.count("labels"))
|
|
{
|
|
model_desc.labels_file_path_ = vm["labels"].as<std::string>();
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Labels path not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (vm.count("training_backend"))
|
|
{
|
|
model_desc.training_backend_path_ = common_path / vm["training_backend"].as<std::string>();
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Training backend path not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
if (!vm.count("inference_backend") || vm["inference_backend"].as<std::string>().empty())
|
|
{
|
|
model_desc.inference_backend_path_ = model_desc.training_backend_path_;
|
|
}
|
|
else
|
|
{
|
|
model_desc.inference_backend_path_ = common_path / vm["inference_backend"].as<std::string>();
|
|
}
|
|
|
|
if (vm.count("extensive_logs_path"))
|
|
{
|
|
model_desc.log_path_ = vm["extensive_logs_path"].as<std::string>();
|
|
}
|
|
else
|
|
{
|
|
model_desc.log_path_ = "";
|
|
}
|
|
|
|
if (vm.count("model_path"))
|
|
{
|
|
model_desc.model_saving_path_ = vm["model_path"].as<std::string>();
|
|
}
|
|
else
|
|
{
|
|
model_desc.model_saving_path_ = "";
|
|
}
|
|
|
|
model_desc.inference_only_ = vm.count("inference_only") > 0;
|
|
|
|
if (vm.count("logging_level"))
|
|
{
|
|
knp::framework::logging::Level logging_level =
|
|
knp::framework::logging::str_to_level(vm["logging_level"].as<std::string>());
|
|
knp::framework::logging::set_level(logging_level);
|
|
std::cout << "Set logging level to \"" << knp::framework::logging::level_to_str(logging_level) << "\""
|
|
<< std::endl;
|
|
}
|
|
else
|
|
{
|
|
std::cout << "Logging level not specified." << std::endl;
|
|
std::cout << desc << std::endl;
|
|
return std::nullopt;
|
|
}
|
|
|
|
return model_desc;
|
|
}
|