start working at inference.h with numcpp
This commit is contained in:
parent
d5564e27a1
commit
fe408c07a7
2 changed files with 113 additions and 19 deletions
|
|
@ -1,46 +1,54 @@
|
|||
cmake_minimum_required(VERSION 3.15)
|
||||
project(NCNNTest LANGUAGES CXX)
|
||||
|
||||
# Find required system packages
|
||||
# Find system packages
|
||||
find_package(X11 REQUIRED)
|
||||
|
||||
# Clone ncnn if missing
|
||||
if(NOT EXISTS "${CMAKE_SOURCE_DIR}/thirdparty/ncnn")
|
||||
# Set thirdparty directory
|
||||
set(THIRDPARTY_DIR ${CMAKE_SOURCE_DIR}/thirdparty)
|
||||
|
||||
# Clone minimal dependencies
|
||||
if(NOT EXISTS "${THIRDPARTY_DIR}/ncnn")
|
||||
find_package(Git REQUIRED)
|
||||
message(STATUS "Cloning ncnn...")
|
||||
execute_process(
|
||||
COMMAND ${GIT_EXECUTABLE} clone --depth 1
|
||||
https://github.com/Tencent/ncnn.git
|
||||
WORKING_DIRECTORY "${CMAKE_SOURCE_DIR}/thirdparty"
|
||||
RESULT_VARIABLE GIT_CLONE_RESULT
|
||||
WORKING_DIRECTORY ${THIRDPARTY_DIR}
|
||||
)
|
||||
endif()
|
||||
|
||||
if(NOT EXISTS "${THIRDPARTY_DIR}/NumCpp")
|
||||
find_package(Git REQUIRED)
|
||||
execute_process(
|
||||
COMMAND ${GIT_EXECUTABLE} clone --depth 1
|
||||
https://github.com/dpilger26/NumCpp.git
|
||||
WORKING_DIRECTORY ${THIRDPARTY_DIR}
|
||||
)
|
||||
if(NOT GIT_CLONE_RESULT EQUAL 0)
|
||||
message(FATAL_ERROR "Failed to clone ncnn repository")
|
||||
endif()
|
||||
endif()
|
||||
|
||||
# Configure ncnn
|
||||
set(NCNN_OPENMP OFF CACHE BOOL "Disable OpenMP")
|
||||
set(NCNN_VULKAN OFF CACHE BOOL "Disable Vulkan")
|
||||
set(NCNN_SYSTEM_GLSLANG OFF CACHE BOOL "Disable GLSLANG")
|
||||
add_subdirectory(thirdparty/ncnn)
|
||||
|
||||
# Main executable (add your capture.cpp here)
|
||||
# Configure NumCpp without Boost
|
||||
add_library(numcpp INTERFACE)
|
||||
target_compile_definitions(numcpp INTERFACE NUMCPP_NO_USE_BOOST)
|
||||
target_include_directories(numcpp INTERFACE
|
||||
${THIRDPARTY_DIR}/NumCpp/include
|
||||
)
|
||||
|
||||
# Main executable
|
||||
add_executable(ncnn_test
|
||||
main.cpp
|
||||
capture.cpp
|
||||
)
|
||||
|
||||
# Include directories
|
||||
target_include_directories(ncnn_test PRIVATE
|
||||
${CMAKE_CURRENT_SOURCE_DIR} # For stb_image.h
|
||||
${X11_INCLUDE_DIR} # X11 headers
|
||||
)
|
||||
|
||||
# Link libraries
|
||||
# Link dependencies
|
||||
target_link_libraries(ncnn_test PRIVATE
|
||||
ncnn
|
||||
${X11_LIBRARIES} # X11 libraries
|
||||
${X11_LIBRARIES}
|
||||
numcpp
|
||||
)
|
||||
|
||||
# C++ standard
|
||||
|
|
|
|||
86
inference.h
Normal file
86
inference.h
Normal file
|
|
@ -0,0 +1,86 @@
|
|||
#ifndef INFERENCE_H
|
||||
#define INFERENCE_H
|
||||
|
||||
#include <ncnn/net.h>
|
||||
#include <NumCpp.hpp>
|
||||
#include <string>
|
||||
#include <algorithm>
|
||||
|
||||
namespace nc = NumCpp;
|
||||
|
||||
struct InferenceResult {
|
||||
nc::NdArray<float> logits;
|
||||
nc::NdArray<int64_t> token_ids;
|
||||
};
|
||||
|
||||
|
||||
nc::NdArray<int64_t> argmax(const nc::NdArray<float>& array) {
|
||||
nc::NdArray<int64_t> result(array.numRows(), 1);
|
||||
|
||||
for(nc::uint32 row = 0; row < array.numRows(); ++row) {
|
||||
auto begin = array.cbegin(row);
|
||||
auto end = array.cend(row);
|
||||
result[row] = std::distance(begin, std::max_element(begin, end));
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
|
||||
|
||||
ncnn::Mat preprocess(const ncnn::Mat& input) {
|
||||
// Resize and normalize
|
||||
ncnn::Mat processed;
|
||||
ncnn::Mat::from_pixels_resize(input, ncnn::Mat::PIXEL_RGB,
|
||||
input.w, input.h, 224, 224);
|
||||
const float mean[3] = {0.5f, 0.5f, 0.5f};
|
||||
const float norm[3] = {1/255.0f, 1/255.0f, 1/255.0f};
|
||||
processed.substract_mean_normalize(mean, norm);
|
||||
return processed;
|
||||
}
|
||||
|
||||
nc::NdArray<int64_t> create_sequence_input() {
|
||||
// NumCpp equivalent of np.full((300, 1), 0, dtype=np.int64)
|
||||
return nc::zeros<int64_t>(nc::Shape(300, 1));
|
||||
}
|
||||
|
||||
InferenceResult run_inference(const ncnn::Mat& input,
|
||||
const std::string& model_path = "model") {
|
||||
static ncnn::Net net;
|
||||
net.opt.num_threads = 16;
|
||||
net.opt.use_vulkan_compute = false;
|
||||
|
||||
// Load model
|
||||
if(net.load_param((model_path + "/ncnn_model.param").c_str()) ||
|
||||
net.load_model((model_path + "/ncnn_model.bin").c_str())) {
|
||||
throw std::runtime_error("Failed to load model");
|
||||
}
|
||||
|
||||
ncnn::Extractor ex = net.create_extractor();
|
||||
|
||||
// Convert NumCpp array to ncnn mat
|
||||
auto sequence_nc = create_sequence_input();
|
||||
ncnn::Mat sequence_mat(sequence_nc.numCols(), sequence_nc.numRows());
|
||||
std::memcpy(sequence_mat.data, sequence_nc.data(),
|
||||
sequence_nc.size() * sizeof(int64_t));
|
||||
|
||||
// Run inference
|
||||
ex.input("in0", input);
|
||||
ex.input("in1", sequence_mat);
|
||||
|
||||
ncnn::Mat out_mat;
|
||||
if(ex.extract("out0", out_mat) != 0) {
|
||||
throw std::runtime_error("Inference failed");
|
||||
}
|
||||
|
||||
// Convert ncnn mat to NumCpp array
|
||||
nc::NdArray<float> logits(out_mat.h, out_mat.w);
|
||||
std::memcpy(logits.data(), out_mat.data, logits.size() * sizeof(float));
|
||||
|
||||
// Argmax using NumCpp
|
||||
auto token_ids = nc::argmax(logits, nc::Axis::ROW);
|
||||
|
||||
return {logits, token_ids};
|
||||
}
|
||||
|
||||
#endif // INFERENCE_H
|
||||
Loading…
Reference in a new issue