Skip to content

Commit

Permalink
learning: WIP do depth anything directly
Browse files Browse the repository at this point in the history
  • Loading branch information
marcojob committed Nov 8, 2024
1 parent bfffda7 commit 422e526
Show file tree
Hide file tree
Showing 9 changed files with 217 additions and 238 deletions.
17 changes: 11 additions & 6 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ endif()
set(TENSORRT_LIB_DIR /usr/lib/x86_64-linux-gnu CACHE PATH "Path to TensorRT libraries")
find_library(NVINFER nvinfer PATHS ${TENSORRT_LIB_DIR})
find_library(NVINFER_PLUGIN nvinfer_plugin PATHS ${TENSORRT_LIB_DIR})
find_library(NVONNXPARSER nvonnxparser PATHS ${TENSORRT_LIB_DIR})

message(STATUS "TensorRT NVINFER library found at: ${NVINFER}")
message(STATUS "TensorRT NVINFER_PLUGIN library found at: ${NVINFER_PLUGIN}")

message(STATUS "TensorRT NVONNXPARSER library found at: ${NVONNXPARSER}")

# Check if TensorRT libraries are found
if(NOT NVINFER OR NOT NVINFER_PLUGIN)
message(FATAL_ERROR "TensorRT libraries not found. Set TENSORRT_LIB_DIR correctly.")
if(NOT NVINFER OR NOT NVINFER_PLUGIN OR NOT NVONNXPARSER)
message(FATAL_ERROR "TensorRT libraries not found. Ensure TENSORRT_LIB_DIR is set correctly.")
endif()

## Build the USB camera library
Expand Down Expand Up @@ -82,6 +84,7 @@ target_link_libraries(${PROJECT_NAME}
${swscale_LIBRARIES}
${NVINFER}
${NVINFER_PLUGIN}
${NVONNXPARSER}
)

# Define catkin package
Expand All @@ -100,6 +103,7 @@ target_link_libraries(${PROJECT_NAME}_node
${swscale_LIBRARIES}
${NVINFER}
${NVINFER_PLUGIN}
${NVONNXPARSER}
)
set_target_properties(${PROJECT_NAME}_node PROPERTIES LINK_FLAGS "-Wl,--no-as-needed")

Expand All @@ -113,16 +117,17 @@ if(BUILD_TESTING)
find_package(GTest REQUIRED)
include_directories(${GTEST_INCLUDE_DIRS})

catkin_add_gtest(test_learning_interface test/test_learning_interface.cpp)
target_link_libraries(test_learning_interface
catkin_add_gtest(test_depth_anything_v2 test/test_depth_anything_v2.cpp)
target_link_libraries(test_depth_anything_v2
${PROJECT_NAME}
${catkin_LIBRARIES}
GTest::gtest_main
${NVINFER}
${NVINFER_PLUGIN}
${NVONNXPARSER}
${CUDA_LIBRARIES}
)
set_target_properties(test_learning_interface PROPERTIES LINK_FLAGS "-Wl,--no-as-needed")
set_target_properties(test_depth_anything_v2 PROPERTIES LINK_FLAGS "-Wl,--no-as-needed")
endif()

# Installation rules
Expand Down
46 changes: 23 additions & 23 deletions ci/pr_run_tests.sh
Original file line number Diff line number Diff line change
@@ -1,45 +1,45 @@
#!/bin/bash
source /opt/ros/noetic/setup.bash

# Check if the plan file exists before generating it
echo $LD_LIBRARY_PATH
if [ ! -f "test/resources/raft-small.plan" ]; then
echo "Plan file not found. Generating plan file..."
if /usr/src/tensorrt/bin/trtexec --buildOnly --onnx="test/resources/raft-small.onnx" --saveEngine="test/resources/raft-small.plan" --plugins="/usr/lib/x86_64-linux-gnu/libnvinfer_plugin.so"
then
echo "Plan file generation successful"
# Set paths for the model and plan files
MODEL_PATH="test/resources/depth_anything_v2_vitb.onnx"
MODEL_URL="https://github.com/fabio-sim/Depth-Anything-ONNX/releases/download/v2.0.0/depth_anything_v2_vitb.onnx"

# Step 1: Check if the ONNX model file exists
if [ ! -f "$MODEL_PATH" ]; then
echo "ONNX model file not found. Downloading..."
if wget -O "$MODEL_PATH" "$MODEL_URL"; then
echo "Model downloaded successfully."
else
echo "Plan file generation failed"
echo "Model download failed."
exit 1
fi
else
echo "Plan file already exists. Skipping generation."
echo "ONNX model file already exists. Skipping download."
fi

# Build the project and run tests
rm -rf build
mkdir -p build
cd build
if cmake .. -DBUILD_TESTING=ON
then
echo "CMake successfull"
if make test_learning_interface
then
echo "Make successfull"

if cmake .. -DBUILD_TESTING=ON; then
echo "CMake successful."
if make test_depth_anything_v2; then
echo "Make successful."
else
echo "Make failed"
echo "Make failed."
exit 1
fi
else
echo "CMake failed"
echo "CMake failed."
exit 1
fi

if ./devel/lib/usb_cam/test_learning_interface
then
echo "Tests successful"
# Run the test executable
if ./devel/lib/usb_cam/test_depth_anything_v2; then
echo "Tests successful."
else
echo "Tests failed"
echo "Tests failed."
exit 1
fi


23 changes: 23 additions & 0 deletions include/usb_cam/learning/depth_anything_v2.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#ifndef DEPTH_ANYTHING_HPP_
#define DEPTH_ANYTHING_HPP_

#include "interface.hpp"
#include <opencv2/opencv.hpp>
#include <vector>

class DepthAnythingV2 : public LearningInterface {
public:
DepthAnythingV2(std::string model_path) {
_model_path = model_path;
}

void get_output(uint8_t* output_buffer) override {
// TODO
}

void publish() override {
// TODO
}
};

#endif // DEPTH_ANYTHING_HPP_
67 changes: 26 additions & 41 deletions include/usb_cam/learning/interface.hpp
Original file line number Diff line number Diff line change
@@ -1,73 +1,58 @@
#ifndef LEARNING_INTERFACE_HPP_
#define LEARNING_INTERFACE_HPP_

#include <cassert>
#include <cuda_runtime.h>
#include <fstream>
#include <iostream>
#include <NvInfer.h>
#include <NvInferPlugin.h>
#include <NvInferRuntime.h>
#include <NvInferRuntimeCommon.h>
#include <NvOnnxParser.h>
#include <sstream>
#include <fstream>
#include <string>
#include <vector>
#include <tuple>
#include <algorithm>
#include <opencv2/opencv.hpp>
#include <NvInfer.h>

class LearningInterface {
public:
LearningInterface() : _model_path("") {
// Instantiate the logger and initialize plugins
if (!initLibNvInferPlugins(static_cast<void*>(&_logger), "")) {
std::cerr << "Error: Failed to initialize TensorRT plugins." << std::endl;
throw std::runtime_error("Failed to initialize TensorRT plugins.");
}
}
LearningInterface() : _model_path("") {}

void set_input(cv::Mat input_image);

virtual void set_input(const uint8_t* input_buffer, size_t height, size_t width) = 0;
virtual void get_output(uint8_t* output_buffer) = 0;
virtual void publish() = 0;

void load_model();
bool run_inference(size_t batch_size);

virtual ~LearningInterface() {
// Release allocated CUDA memory
if (_buffers[0]) cudaFree(_buffers[0]);
if (_buffers[1]) cudaFree(_buffers[1]);

delete[] _input_buffer;
delete[] _output_buffer;
}
void predict();

float* get_input_buffer() { return _input_buffer; }
nvinfer1::ICudaEngine* get_engine() { return _engine; }
nvinfer1::IExecutionContext* get_context() { return _context; }
nvinfer1::IRuntime* get_runtime() { return _runtime; }

~LearningInterface();

protected:
float* _input_buffer = nullptr;
float* _output_buffer = nullptr;
nvinfer1::ICudaEngine* _engine = nullptr;
nvinfer1::IExecutionContext* _context = nullptr;
nvinfer1::IRuntime* _runtime = nullptr;
size_t input_height;
size_t input_width;
size_t output_height;
size_t output_width;
cudaStream_t _stream;
float* _input_data = nullptr;
float* _output_data = nullptr;
nvinfer1::ICudaEngine* _engine;
nvinfer1::IExecutionContext* _context;
nvinfer1::INetworkDefinition* _network;
nvinfer1::IRuntime* _runtime;
std::string _model_path;

private:
void* _buffers[2] = { nullptr, nullptr };

// TODO: static?
class Logger : public nvinfer1::ILogger {
public:
void log(Severity severity, const char* msg) noexcept override {
if (severity <= Severity::kWARNING) { // Limit logging to warnings and errors
// Only output logs with severity greater than warning
if (severity <= Severity::kWARNING) {
std::cout << msg << std::endl;
}
}
};
Logger _logger;
} _logger;

bool _save_engine(const std::string& onnx_path);
void _build(std::string onnx_path);
};

#endif // LEARNING_INTERFACE_HPP_
43 changes: 0 additions & 43 deletions include/usb_cam/learning/raft.hpp

This file was deleted.

Loading

0 comments on commit 422e526

Please sign in to comment.