diff --git a/.clang-format b/.clang-format deleted file mode 100644 index 9d1919a..0000000 --- a/.clang-format +++ /dev/null @@ -1,28 +0,0 @@ -BasedOnStyle: Google -IndentWidth: 4 -UseTab: Never -ColumnLimit: 120 - -Language: Cpp -Standard: Cpp17 - -AccessModifierOffset: -4 -AlignConsecutiveMacros: true -AllowAllArgumentsOnNextLine: false -AllowAllConstructorInitializersOnNextLine: false -AllowAllParametersOfDeclarationOnNextLine: false -AllowShortFunctionsOnASingleLine: Empty -AllowShortIfStatementsOnASingleLine: Never -AllowShortLambdasOnASingleLine: Empty -AllowShortLoopsOnASingleLine: false -AlwaysBreakBeforeMultilineStrings: false -BinPackArguments: false -BinPackParameters: false -CommentPragmas: '^#' -DerivePointerAlignment: false -FixNamespaceComments: true -IndentCaseLabels: false -IndentPPDirectives: AfterHash -ForEachMacros: - - foreach - - FOREACH_CHILD diff --git a/CMakeLists.txt b/CMakeLists.txt deleted file mode 100644 index 9a81b01..0000000 --- a/CMakeLists.txt +++ /dev/null @@ -1,61 +0,0 @@ -cmake_minimum_required (VERSION 3.10) - - -# Enable CMAKE__COMPILER_ID AppleClang -if(POLICY CMP0025) - cmake_policy(SET CMP0025 NEW) -endif() - -project(infer) - -set (CMAKE_CXX_STANDARD 17) -set (CMAKE_CXX_EXTENSIONS OFF) -set (CMAKE_CXX_STANDARD_REQUIRED ON) -set (CMAKE_EXPORT_COMPILE_COMMANDS ON) - -option(WITH_OPENVINO "Build with OpenVINO" OFF) -option(WITH_ONNXRUNTIME "Build with ONNXRUNTIME" OFF) -option(WITH_TENSORRT "Build with TensorRT" ON) - - -find_package(OpenCV REQUIRED) -if(OpenCV_FOUND) - message(STATUS "OpenCV found") - message(STATUS " directory: ${OpenCV_DIR}") - message(STATUS " version : ${OpenCV_VERSION}") - include_directories(${OpenCV_INCLUDE_DIRS}) - link_directories(${OpenCV_LIB_DIRS}) -else() - message(FATAL_ERROR "OpenCV not found") -endif() - -set(SOURCES src/empty.cpp ) -if (WITH_OPENVINO) - add_compile_definitions(WITH_OPENVINO) - find_package(OpenVINO REQUIRED COMPONENTS Runtime) # only Runtime component - if (OpenVINO_FOUND) - message(STATUS "OpenVINO found") - message(STATUS " directory: ${OpenVINO_DIR}") - message(STATUS " version : ${OpenVINO_VERSION}") - set(ov_link_libraries openvino::runtime) - include_directories($ENV{INTEL_OPENVINO_DIR}/runtime/include) - # include_directories($ENV{OpenVINO_HOME}/runtime/include) - - list(APPEND SOURCES - src/backend/b_openvino.cpp - ) - else() - message(FATAL_ERROR "OpenVINO not found") - endif() -endif() - - -include_directories(include) - - -add_library(backend SHARED ${SOURCES}) - - -set(EXE_FILE infer) -add_executable(${EXE_FILE} app/infer.cpp) -target_link_libraries(${EXE_FILE} PRIVATE ${ov_link_libraries} ${OpenCV_LIBS} backend) diff --git a/app/infer.cpp b/app/infer.cpp deleted file mode 100644 index 17aab0c..0000000 --- a/app/infer.cpp +++ /dev/null @@ -1,37 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include "backend.hpp" - -int main(int argc, char* argv[]) { - std::string model_path = "../resource/weights/yolov5s_openvino_model/yolov5s.xml"; - std::string image_path = "../images/bus.jpg"; - det_input_t input_params; - input_params.device = "CPU"; - input_params.threshold.score = 0.5; - input_params.threshold.conf = 0.5; - input_params.threshold.nms = 0.5; - auto backend = Backend::GetBackend(BackendType::TENSORRT, input_params); - - backend->LoadModel(model_path); - - auto img = cv::imread(image_path); - - // 统计时间 - auto start_time = std::chrono::high_resolution_clock::now(); - auto batch_predictions = backend->Infer(img); - - auto end_time = std::chrono::high_resolution_clock::now(); - - std::cout << "Infer time: " << std::chrono::duration(end_time - start_time).count() << "ms" - << std::endl; - - return 0; -} \ No newline at end of file diff --git a/include/backend.hpp b/include/backend.hpp deleted file mode 100644 index b4ccda9..0000000 --- a/include/backend.hpp +++ /dev/null @@ -1,38 +0,0 @@ -#ifndef __BACKEND_HPP__ -#define __BACKEND_HPP__ -#include - -#ifdef WITH_OPENVINO -#include "backend/b_openvino.hpp" -#endif -#include "backend/interface.hpp" - -enum class BackendType { - ONNX, -#ifdef WITH_OPENVINO - OPENVINO, -#endif - TENSORRT, -}; - -class Backend { -public: - Backend() = delete; - ~Backend() = delete; - static std::unique_ptr - GetBackend(BackendType backend_type, const det_input_t &input_params) { - switch (backend_type) { -#ifdef WITH_OPENVINO - case BackendType::OPENVINO: - return std::make_unique(input_params); - break; -#endif - - default: - return nullptr; - break; - } - } -}; - -#endif // __BACKEND_HPP__ \ No newline at end of file diff --git a/include/backend/b_openvino.hpp b/include/backend/b_openvino.hpp deleted file mode 100644 index d580b52..0000000 --- a/include/backend/b_openvino.hpp +++ /dev/null @@ -1,28 +0,0 @@ -#ifndef __BACKEND__B_OPENVINO_HPP__ -#define __BACKEND__B_OPENVINO_HPP__ - -#include -#include - -#include "backend/interface.hpp" -#include "openvino/openvino.hpp" -#include "utils/extend_print.hpp" - -class OpenVINOBackend : public BaseDetectionBackend { -private: - ov::CompiledModel compiled_model; - // ov::InferRequest infer_request;// FIXME: is a member variable necessary? -public: - OpenVINOBackend(const det_input_t& input_params); - ~OpenVINOBackend(); - void LoadModel(const std::string& model_path); - det_output_t Infer(const cv::Mat& img); - std::vector GetDevice() const; - - OpenVINOBackend(const OpenVINOBackend&) = delete; - OpenVINOBackend(OpenVINOBackend&&) = delete; - OpenVINOBackend& operator=(const OpenVINOBackend&) = delete; - OpenVINOBackend& operator=(OpenVINOBackend&&) = delete; -}; - -#endif // __BACKEND__B_OPENVINO_HPP__ diff --git a/include/backend/interface.hpp b/include/backend/interface.hpp deleted file mode 100644 index 4a708e8..0000000 --- a/include/backend/interface.hpp +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef __BACKEND__INTERFACE_HPP__ -#define __BACKEND__INTERFACE_HPP__ - -#include -#include -#include -#include - -typedef struct { - std::string device; - unsigned int batch_size; - unsigned int height; - unsigned int width; - unsigned int channel; - struct { - float conf; - float score; - float nms; - } threshold; -} detection_input_t; - -typedef struct { - cv::Rect boxes; - float obj_conf; - int class_id; - float class_conf; -} detetcion_output_t; - -template -class IBackend { -protected: - _TInput input_params; - -public: - explicit IBackend(const _TInput& input_params) : input_params(input_params){}; - virtual ~IBackend(){}; - virtual void LoadModel(const std::string& model_path) = 0; - virtual _TOutput Infer(const cv::Mat& img) = 0; - virtual std::vector GetDevice() const = 0; -}; - -using det_input_t = detection_input_t; -using det_output_t = std::vector>; -class BaseDetectionBackend : public IBackend { -public: - explicit BaseDetectionBackend(const det_input_t& input_params) : IBackend(input_params){}; -}; - -#endif // __BACKEND_INTERFACE_HPP__ diff --git a/include/process.hpp b/include/process.hpp deleted file mode 100644 index 910c3bb..0000000 --- a/include/process.hpp +++ /dev/null @@ -1,8 +0,0 @@ -#ifndef __PROCESS_HPP__ -#define __PROCESS_HPP__ - -class Process { - -}; - -#endif // __PROCESS_HPP__ \ No newline at end of file diff --git a/include/utils/extend_print.hpp b/include/utils/extend_print.hpp deleted file mode 100644 index 2f5284c..0000000 --- a/include/utils/extend_print.hpp +++ /dev/null @@ -1,20 +0,0 @@ -#ifndef __UTILS__EXTEND_PRINT_HPP__ -#define __UTILS__EXTEND_PRINT_HPP__ - -#include -#include -#include - -class Utils { -public: - template - static void print_vector(const std::vector& vec, std::string split = " ") { - std::cout << "["; - for (auto i = 0; i < vec.size() - 1; i++) { - std::cout << vec[i] << split; - } - std::cout << vec[vec.size() - 1] << "]" << std::endl; - }; -}; - -#endif // __UTILS__EXTEND_PRINT_HPP__ \ No newline at end of file diff --git a/src/backend/b_openvino.cpp b/src/backend/b_openvino.cpp deleted file mode 100644 index d9377d4..0000000 --- a/src/backend/b_openvino.cpp +++ /dev/null @@ -1,138 +0,0 @@ -#include "backend/b_openvino.hpp" - -#include -#include -#include -#include -#include -#include - -#include "backend/interface.hpp" -#include "opencv2/core/mat.hpp" -#include "opencv2/imgproc.hpp" -#include "utils/extend_print.hpp" - -OpenVINOBackend::OpenVINOBackend(const det_input_t& input_params) : BaseDetectionBackend(input_params) { - auto devices = this->GetDevice(); - std::string devices_str; - for (auto&& device : devices) { - devices_str += device + ", "; - } - OPENVINO_ASSERT(std::find(devices.begin(), devices.end(), this->input_params.device) != devices.end(), - "Device name is not in available devices: " + devices_str); -} - -OpenVINOBackend::~OpenVINOBackend() {} - -void OpenVINOBackend::LoadModel(const std::string& model_path) { - ov::Core core; - std::shared_ptr model = core.read_model(model_path); - - OPENVINO_ASSERT(model->inputs().size() == 1, "Sample supports models with 1 input only"); - OPENVINO_ASSERT(model->outputs().size() == 1, "Sample supports models with 1 output only"); - // -------- Step 3. Set up input - - ov::element::Type input_type = ov::element::u8; - const ov::Layout tensor_layout{"NHWC"}; - - // -------- Step 4. Configure preprocessing -------- - - ov::preprocess::PrePostProcessor ppp(model); - - // 1) Set input tensor information: - // - input() provides information about a single model input - // - reuse precision and shape from already available `input_tensor` - // - layout of data is 'NHWC' - ppp.input().tensor().set_element_type(input_type).set_layout(tensor_layout); - // 2) Adding explicit preprocessing steps: - // - convert layout to 'NCHW' (from 'NHWC' specified above at tensor layout) - // - apply linear resize from tensor spatial dims to model spatial dims - ppp.input().preprocess().resize(ov::preprocess::ResizeAlgorithm::RESIZE_LINEAR); - // 4) Here we suppose model has 'NCHW' layout for input - ppp.input().model().set_layout("NCHW"); - // 5) Set output tensor information: - // - precision of tensor is supposed to be 'f32' - ppp.output().tensor().set_element_type(ov::element::f32); - - // 6) Apply preprocessing modifying the original 'model' - model = ppp.build(); - - // -------- Step 5. Loading a model to the device -------- - this->compiled_model = core.compile_model(model, this->input_params.device); - if (this->compiled_model.input().get_shape().size() == 4) { - auto input_shape = this->compiled_model.input().get_shape(); - this->input_params.batch_size = input_shape[0]; - this->input_params.height = input_shape[1]; - this->input_params.width = input_shape[2]; - this->input_params.channel = input_shape[3]; - } else - throw std::runtime_error("Model input shape is not BCHW"); - if (this->compiled_model.output().get_shape().size() != 3) - throw std::runtime_error("Model output shape is not BNS"); - auto output_shape = this->compiled_model.output().get_shape(); - // this->batch_predictions = det_output_t(output_shape[0], std::vector(output_shape[1])); -} - -det_output_t OpenVINOBackend::Infer(const cv::Mat& _img) { - cv::Mat img; - cv::resize(_img, - img, - cv::Size(this->compiled_model.input().get_shape()[1], this->compiled_model.input().get_shape()[2])); - - auto infer_request = compiled_model.create_infer_request(); - infer_request.set_input_tensor( - ov::Tensor(compiled_model.input().get_element_type(), compiled_model.input().get_shape(), img.data)); - infer_request.infer(); - - const ov::Tensor& output_tensor = infer_request.get_output_tensor(); - auto output_shape = output_tensor.get_shape(); - - float* detections = output_tensor.data(); - det_output_t batch_predictions; - for (int b = 0; b < output_shape[0]; b++) { - std::vector predictions; - std::vector boxes; - for (int n = 0; n < output_shape[1]; n++) { - float* detection = &detections[(b * output_shape[1] + n) * output_shape[2]]; - - if (detection[4] < this->input_params.threshold.conf) - continue; - - int max_class_id = 5; - for (int i = 5; i < output_shape[2]; i++) { - if (detection[i] > detection[max_class_id]) - max_class_id = i; - } - if (detection[max_class_id] < this->input_params.threshold.score) - continue; - - auto box = cv::Rect(cv::Point(detection[0], detection[1]), cv::Point(detection[2], detection[3])); - detetcion_output_t prediction{ - box, - detection[4], - max_class_id - 5, - detection[max_class_id], - }; - boxes.push_back(box); - - predictions.emplace_back(prediction); - } - - batch_predictions.emplace_back(predictions); - } - - return batch_predictions; // TODO: enable RVO -} - -std::vector OpenVINOBackend::GetDevice() const { - ov::Core core; - auto availableDevices = core.get_available_devices(); -#ifdef DEBUG - { - for (auto&& device : availableDevices) { - std::cout << device << std::endl; - } - } -#endif - return availableDevices; -} \ No newline at end of file diff --git a/src/empty.cpp b/src/empty.cpp deleted file mode 100644 index e69de29..0000000 diff --git a/tools/quantization.py b/tools/quantization.py deleted file mode 100644 index e69de29..0000000