Skip to content

Commit

Permalink
[output processing] start of work
Browse files Browse the repository at this point in the history
  • Loading branch information
a-sidorova committed Oct 5, 2023
1 parent 6a09923 commit a087ea8
Show file tree
Hide file tree
Showing 10 changed files with 152 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
path = src/cpp_dl_benchmark/thirdparty/gflags
url = https://github.com/gflags/gflags.git
branch = master
[submodule "src/output_processing/thirdparty/pybind11"]
path = src/output_processing/thirdparty/pybind11
url = https://github.com/pybind/pybind11.git
branch = master
21 changes: 21 additions & 0 deletions src/output_processing/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
cmake_minimum_required(VERSION 3.13)

project(output_processing_lib CXX)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY "${CMAKE_BINARY_DIR}/bin")

file(GLOB_RECURSE HEADERS "${CMAKE_CURRENT_SOURCE_DIR}/include/*")
file(GLOB_RECURSE SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*")

file(GLOB_RECURSE PY_SOURCES ${HEADERS} ${SOURCES} output_processing.cpp wrappers.hpp)

add_subdirectory(thirdparty/pybind11)

pybind11_add_module(${PROJECT_NAME}_py MODULE ${PY_SOURCES})
add_library(${PROJECT_NAME} ${HEADERS} ${SOURCES})

target_include_directories(${PROJECT_NAME}_py PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
target_include_directories(${PROJECT_NAME} PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/include")
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#pragma once

#define DLB_ASSERT(cond, msg) \
if (!(cond)) \
return false;
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#pragma once

#include <map>
#include <string>
#include <vector>


bool ClassificationTask(const std::map<std::string, std::vector<std::vector<float>>>& output_tensors,
const size_t number_top, const std::string& labels);
21 changes: 21 additions & 0 deletions src/output_processing/include/output_processing/utils.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#pragma once

#include <string>
#include <vector>
#include <algorithm>
#include <numeric>


std::vector<std::string> read_labels(const std::string& label_path);

template<typename T>
std::vector<size_t> argsort(const std::vector<T> &array) {
std::vector<size_t> indices(array.size());
std::iota(indices.begin(), indices.end(), 0);
std::sort(indices.begin(), indices.end(),
[&array](int left, int right) -> bool {
return array[left] < array[right];
});

return indices;
}
7 changes: 7 additions & 0 deletions src/output_processing/output_processing.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#include <pybind11/pybind11.h>

#include "wrappers.hpp"

PYBIND11_MODULE(output_processing, m) {
m.def("ClassificationTask", &ClassificationTaskPy);
};
34 changes: 34 additions & 0 deletions src/output_processing/src/output_handlers.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "output_processing/output_handlers.hpp"

#include "output_processing/exception_handler.hpp"
#include "output_processing/utils.hpp"

#include <iostream>
#include <iomanip>


bool ClassificationTask(const std::map<std::string, std::vector<std::vector<float>>>& output_tensors,
const size_t number_top, const std::string& label_file) {
DLB_ASSERT(output_tensors.size() == 1)

const auto result_tensor = *output_tensors.cbegin();
const auto layer_name = result_tensor.first;
const auto data = result_tensor.second;
const auto labels = read_labels(label_file);
const auto batch = data.size();

std::cout << "[ INFO ] Top " + std::to_string(number_top) + " results:\n";

for (size_t i = 0; i < batch; ++i) {
const auto batch_result = data[i];
const auto top_idxs = argsort(batch_result);

std::cout << "[ INFO ] Result for image " + std::to_string(i + 1) + ":\n";
for (size_t idx = 0; idx < number_top; ++idx) {
std::cout.precision(2);
const auto sorted_index = top_idxs[idx];
std::cout << std::setw(10) << batch_result[sorted_index] << std::setw(5) << labels[sorted_index] << "\n";
}
}
return true;
}
25 changes: 25 additions & 0 deletions src/output_processing/src/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#include "output_processing/utils.hpp"

#include <fstream>
#include <filesystem>


std::vector<std::string> read_labels(const std::string& label_path) {
std::vector<std::string> labels;

std::filesystem::path path = label_path;
std::ifstream in(path);
if (in.is_open()) {
std::string label;
while (std::getline(in, label)) {
labels.push_back(label);
}
}
in.close();
if (path.extension() == ".json") {
// Remove parenthless
labels.pop_back();
labels.erase(labels.begin());
}
return labels;
}
1 change: 1 addition & 0 deletions src/output_processing/thirdparty/pybind11
Submodule pybind11 added at 2b2e4c
25 changes: 25 additions & 0 deletions src/output_processing/wrappers.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once

#include <pybind11/pybind11.h>
#include <pybind11/stl.h>
#include <pybind11/numpy.h>

#include "output_processing/output_handlers.hpp"
#include "output_processing/exception_handler.hpp"

#include <string>
#include <vector>
#include <iostream>


namespace py = pybind11;


void ClassificationTaskPy(const py::dict& map, const size_t number_top,
const std::string& label_file) {
std::map<std::string, std::vector<std::vector<float>>> tensors;
for (const auto& it : map) {
const std::string& layer_name = py::cast<const std::string>(it.first);
const py::array& py_tensor = py::cast<const py::array>(it.second);
}
}

0 comments on commit a087ea8

Please sign in to comment.