Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions examples/mnist-client/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,8 @@ endif()
project(mnist-client)
add_executable(mnist-client
main.cpp
graph_physics.cpp
inference.cpp
process_data.cpp
visualize_network.cpp
)

target_link_libraries(mnist-client PRIVATE KNP::BaseFramework::Core ${OpenCV_LIBS} ${Boost_LIBRARIES})
10 changes: 6 additions & 4 deletions examples/mnist-client/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
#include <knp/framework/monitoring/observer.h>
#include <knp/framework/network.h>
#include <knp/framework/sonata/network_io.h>
#include <knp/framework/visualizer/visualize_network.h>

#include <iostream>

#include <boost/program_options.hpp>

#include "inference.h"
#include "visualize_network.h"


// Namespace for program options.
Expand Down Expand Up @@ -75,12 +75,14 @@ int main(int argc, char **argv)
// https://click.kaspersky.com/?hl=en-US&version=2.0&pid=KNP&link=online_help&helpid=274991
knp::framework::Network network = knp::framework::sonata::load_network(network_path);
// Constructs a network graph.
const NetworkGraph net_graph(network);
const knp::framework::NetworkGraph net_graph(network);
// Prints descriptions of graph connections.
print_network_description(net_graph);
knp::framework::print_network_description(net_graph);
// Draws a subgraph in the OpenCV window.
// Press `Esc` to exit the OpenCV window.
position_network_test(NetworkGraph(network), divide_graph_by_connectivity(net_graph)[0], {1000, 700});
knp::framework::position_network_test(
knp::framework::NetworkGraph(network), knp::framework::divide_graph_by_connectivity(net_graph)[0],
{1000, 700});
}

// If `task=infer`, the function loads a network and runs inference.
Expand Down
35 changes: 18 additions & 17 deletions examples/mnist-client/process_data.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
* @brief Functions to process a binary file.
* @date 13.09.2024
* @license Apache 2.0
* @copyright © 2024 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
* @copyright © 2024 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.
*/

Expand All @@ -33,11 +33,11 @@

/**
* @brief Filesystem namespace alias.
*/
*/
namespace fs = std::filesystem;


/**
/**
* @brief Visualize data instances.
* @param data vector of flattened images.
* @param size output image size.
Expand All @@ -59,15 +59,15 @@ cv::Mat draw_data(const std::vector<unsigned char> &data, const cv::Size &size)
}


/**
/**
* @brief Turn data frames into spikes.
* @param buf data frame.
* @param num_levels number of intensity levels that the original interval of `0 - 255` gets divided into.
* @param num_frames all number of steps per data instance.
*/
std::vector<std::vector<bool>> image_to_spikes(std::vector<unsigned char> &buf, int num_levels, int num_frames_all)
{
double delta = 256.0 / num_levels;
const double delta = 256.0 / num_levels;
std::vector<std::vector<bool>> result(num_frames_all);
for (size_t frame_num = 0; frame_num < std::min(num_frames_all, num_levels); ++frame_num)
{
Expand All @@ -91,6 +91,7 @@ std::vector<std::vector<unsigned char>> read_images_from_file(const fs::path &pa
std::ifstream file_stream(path_to_data, std::ios::binary);
std::vector<unsigned char> buffer;
std::vector<std::vector<unsigned char>> result;

while (file_stream.good())
{
buffer.resize(input_size);
Expand All @@ -103,7 +104,7 @@ std::vector<std::vector<unsigned char>> read_images_from_file(const fs::path &pa


/**
* @brief Convert binary files to boolean vectors, one for each step.
* @brief Convert binary files to boolean vectors, one for each step.
* @note The function only works for small datasets, such as MNIST. Dataset is considered small if it fits into RAM.
* @param path_to_data path to binary data file.
* @param input_size size of input buffer.
Expand Down
2 changes: 2 additions & 0 deletions knp/.cppcheck-suppress.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
<!-- Skip "unused members" in the interface structures -->
<id>unusedStructMember</id>
<fileName>knp/core-library/include/*</fileName>
<fileName>knp/neuron-traits-library/include/knp/neuron-traits/*</fileName>
<fileName>knp/synapse-traits-library/include/knp/synapse-traits/*</fileName>
</suppress>
<suppress>
<!-- Skip incorrect cpp-check working with literals -->
Expand Down
2 changes: 1 addition & 1 deletion knp/backends/cpu/cpu-multi-threaded-backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ include(GNUInstallDirs)
include(clang-tidy)
include(knp-functions)

find_package(Boost ${KNP_BOOST_MIN_VERSION} COMPONENTS system REQUIRED)
find_package(Boost ${KNP_BOOST_MIN_VERSION} REQUIRED)
Comment thread
artiomn marked this conversation as resolved.

file(GLOB_RECURSE ${PROJECT_NAME}_headers include/${${PROJECT_NAME}_PUBLIC_INCLUDE_DIR}/*.h)
source_group(source REGULAR_EXPRESSION "impl/.*")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@
#include <variant>
#include <vector>

#include <boost/asio.hpp>
#include <boost/config.hpp>
#include <boost/dll/alias.hpp>
#include <boost/mp11.hpp>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ include(GNUInstallDirs)
include(clang-tidy)
include(knp-functions)

find_package(Boost ${KNP_BOOST_MIN_VERSION} COMPONENTS system filesystem REQUIRED)
find_package(Boost ${KNP_BOOST_MIN_VERSION} REQUIRED)

Comment thread
DavidIkov marked this conversation as resolved.
file(GLOB_RECURSE ${PROJECT_NAME}_headers include/${${PROJECT_NAME}_PUBLIC_INCLUDE_DIR}/*.h)
source_group(source REGULAR_EXPRESSION "impl/.*")
Expand Down
3 changes: 2 additions & 1 deletion knp/backends/gpu/cuda-backend/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ include(clang-tidy)
include(knp-functions)

find_package(CUDAToolkit REQUIRED)
find_package(Boost ${KNP_BOOST_MIN_VERSION} COMPONENTS system filesystem REQUIRED)

find_package(Boost ${KNP_BOOST_MIN_VERSION} COMPONENTS filesystem REQUIRED)

file(GLOB_RECURSE ${PROJECT_NAME}_headers include/${${PROJECT_NAME}_PUBLIC_INCLUDE_DIR}/*.h)
file(GLOB_RECURSE ${PROJECT_NAME}_cuda_headers include/${${PROJECT_NAME}_PUBLIC_INCLUDE_DIR}/*.cuh)
Expand Down
4 changes: 4 additions & 0 deletions knp/base-framework/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ project(knp-base-framework VERSION "${KNP_VERSION}" LANGUAGES C CXX
set(${PROJECT_NAME}_PUBLIC_INCLUDE_DIR "knp/framework")

find_package(Boost ${KNP_BOOST_MIN_VERSION} COMPONENTS filesystem REQUIRED)
find_package(OpenCV REQUIRED)

include(GNUInstallDirs)
include(knp-functions)
Expand Down Expand Up @@ -76,10 +77,13 @@ knp_add_library("${PROJECT_NAME}-core"
impl/inference_evaluation/classification/processor.cpp
impl/observer.cpp
impl/logging.cpp
impl/visualizer/graph_physics.cpp
impl/visualizer/visualize_network.cpp
${${PROJECT_NAME}_headers}
ALIAS KNP::BaseFramework::Core
LINK_PRIVATE
spdlog::spdlog_header_only Boost::headers Boost::filesystem HighFive hdf5-static csv2
${OpenCV_LIBS}
# Hack to build with CLang.
${ADD_LIBS}
LINK_PUBLIC
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
* @brief Implementing a graph physics.
* @date 21.08.2024
* @license Apache 2.0
* @copyright © 2024 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
* @copyright © 2024 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.
*/

Expand Down Expand Up @@ -49,8 +49,8 @@ VisualGraph::VisualGraph(const std::vector<int> &nodes, const std::vector<std::v
}


// We show graph using a following physical model: all nodes repel each other, all connected nodes are connected
// as if by springs of a fixed length. There is also a drag force to dampen oscillations, it's calculated
// We show graph using a following physical model: all nodes repel each other, all connected nodes are connected
// as if by springs of a fixed length. There is also a drag force to dampen oscillations, it's calculated
// independently. This function calculates forces between two graph nodes.
cv::Vec2d VisualGraph::get_force(const PhysicsPoint &target, const PhysicsPoint &influence, bool has_edge) const
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@
* @brief Building a graph structure.
* @date 26.07.2024
* @license Apache 2.0
* @copyright © 2024 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
* @copyright © 2024 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.
*/

Expand All @@ -30,20 +30,22 @@
*/
struct PhysicsPoint
{
// cppcheck-suppress unusedStructMember
/**
* @brief Point index.
*/
// cppcheck-suppress unusedStructMember
int index_;

/**
* @brief Point position.
*/
// cppcheck-suppress unusedStructMember
cv::Vec2d pos_;

/**
* @brief Point velocity.
*/
// cppcheck-suppress unusedStructMember
cv::Vec2d vel_;
};

Expand Down Expand Up @@ -87,16 +89,23 @@ class VisualGraph

private:
// Data.
// cppcheck-suppress unusedStructMember
std::vector<std::vector<size_t>> base_graph_;
// Edges. If `true`, then there is an edge.
// cppcheck-suppress unusedStructMember
std::vector<std::vector<bool>> edges_mat_;
// Positions and velocities of the nodes.
// cppcheck-suppress unusedStructMember
std::vector<PhysicsPoint> points_;

// Hyperparameters.
// cppcheck-suppress unusedStructMember
double spring_strength_ = 1.0f;
// cppcheck-suppress unusedStructMember
double spring_len_ = 1.0f;
// cppcheck-suppress unusedStructMember
double repel_coeff_ = 0.3f;
// cppcheck-suppress unusedStructMember
double resistance_ = 1.0f;

// Calculate force from one graph point to another. Force consists of "repel", "spring" and "resistance" components.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,8 @@
* limitations under the License.
*/

#include "visualize_network.h"

#include <knp/framework/network.h>
#include <knp/framework/visualizer/visualize_network.h>

#include <spdlog/spdlog.h>

Expand All @@ -34,6 +33,11 @@

#include "graph_physics.h"


// TODO: Draw network as a set of subgraphs.
namespace knp::framework
{

/**
* @brief Adjacency list is a representation of a graph where there is a list of adjacent nodes for each node.
* @see https://en.wikipedia.org/wiki/Adjacency_list.
Expand Down Expand Up @@ -404,7 +408,7 @@ std::vector<std::vector<int>> divide_graph_by_connectivity(const NetworkGraph &g
std::unordered_set<int> remaining_nodes;
std::vector<std::vector<int>> connected_sets;
std::unordered_set<int> ignored_nodes{static_cast<int>(adj_list.size() - 1)};
for (int i = 0; i < adj_list.size(); ++i) remaining_nodes.insert(i);
for (size_t i = 0; i < adj_list.size(); ++i) remaining_nodes.insert(i);

remaining_nodes.erase(static_cast<int>(adj_list.size()) - 1); // delete input node
while (!remaining_nodes.empty())
Expand Down Expand Up @@ -512,4 +516,4 @@ std::vector<cv::Point2i> position_network(
return result;
}

// TODO: Draw network as a set of subgraphs.
} // namespace knp::framework
24 changes: 12 additions & 12 deletions knp/base-framework/include/knp/framework/backend_loader.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,18 @@
* @kaspersky_support Artiom N.
* @date 16.03.2023
* @license Apache 2.0
* @copyright © 2024 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
* @copyright © 2024 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.
*/

Expand Down
Loading
Loading