Skip to content

Commit

Permalink
ci: Add script to test raft, failing
Browse files Browse the repository at this point in the history
  • Loading branch information
marcojob committed Nov 7, 2024
1 parent dc38d01 commit ec8253e
Show file tree
Hide file tree
Showing 9 changed files with 130 additions and 227 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:

strategy:
matrix:
ci_script: [pr_compile]
ci_script: [pr_compile, pr_run_tests]

steps:
- name: Checkout
Expand Down
29 changes: 13 additions & 16 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -103,22 +103,19 @@ target_include_directories(${PROJECT_NAME}_node PUBLIC

# Testing
if(BUILD_TESTING)
if($ENV{ROS_VERSION} EQUAL 2)
find_package(ament_lint_auto REQUIRED)
ament_lint_auto_find_test_dependencies()
find_package(ament_cmake_gtest)

# Unit tests
ament_add_gtest(test_usb_cam_utils test/test_usb_cam_utils.cpp)
target_link_libraries(test_usb_cam_utils ${PROJECT_NAME})

ament_add_gtest(test_pixel_formats test/test_pixel_formats.cpp)
target_link_libraries(test_pixel_formats ${PROJECT_NAME})

# Integration tests (commented out as per TODO)
# ament_add_gtest(test_usb_cam_lib test/test_usb_cam_lib.cpp)
# target_link_libraries(test_usb_cam_lib ${PROJECT_NAME})
endif()
# Find GTest package
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
${PROJECT_NAME}
${catkin_LIBRARIES}
GTest::gtest_main
${NVINFER}
${NVINFER_PLUGIN}
${CUDA_LIBRARIES}
)
endif()

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

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"
else
echo "Make failed"
exit 1
fi
else
echo "CMake failed"
exit 1
fi

if ./devel/lib/usb_cam/test_learning_interface
then
echo "Tests successful"
else
echo "Tests failed"
exit 1
fi


12 changes: 8 additions & 4 deletions include/usb_cam/learning/interface.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ class LearningInterface {

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


virtual ~LearningInterface() {
// if (_context) _context->destroy();
Expand All @@ -36,19 +35,24 @@ class LearningInterface {
delete[] _output_buffer;
}

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; }

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;
std::string _model_path;

private:
nvinfer1::ICudaEngine* _engine = nullptr;
nvinfer1::IExecutionContext* _context = nullptr;
nvinfer1::IRuntime* _runtime = nullptr;
void* _buffers[2] = { nullptr, nullptr };
};

Expand Down
32 changes: 17 additions & 15 deletions src/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,23 +30,25 @@ void LearningInterface::load_model() {
// Allocate buffers for input and output
size_t input_size;
size_t output_size;
// for (int io = 0; io < _engine->getNbIOTensors(); io++) {
// const char* name = _engine->getIOTensorName(io);
// std::cout << io << ": " << name;
// const nvinfer1::Dims dims = _engine->getTensorShape(name);
for (int io = 0; io < _engine->getNbBindings(); io++) {
const char* name = _engine->getBindingName(io);
std::cout << io << ": " << name;
const nvinfer1::Dims dims = _engine->getBindingDimensions(io);

// size_t total_dims = 1;
// for (int d = 0; d < dims.nbDims; d++) {
// total_dims *= dims.d[d];
// }
size_t total_dims = 1;
for (int d = 0; d < dims.nbDims; d++) {
total_dims *= dims.d[d];
}

// std::cout << " size: " << total_dims << std::endl;
// if (io == 0) {
// input_size = total_dims * sizeof(float);
// } else if (io == 1) {
// output_size = total_dims * sizeof(float);
// }
// }
std::cout << " size: " << total_dims << std::endl;

// Check if it's an input or output binding
if (_engine->bindingIsInput(io)) {
input_size = total_dims * sizeof(float);
} else {
output_size = total_dims * sizeof(float);
}
}

// Allocate device buffers
cudaMalloc(&_buffers[0], input_size);
Expand Down
61 changes: 61 additions & 0 deletions test/test_learning_interface.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include <gtest/gtest.h>
#include "usb_cam/learning/raft.hpp"
#include <opencv2/opencv.hpp>
#include <cuda_runtime.h>
#include <fstream>

// Define a fixture for Raft tests
class RaftTest : public ::testing::Test {
protected:
// Initialize variables for the test
std::string model_path = "path/to/model.onnx"; // Change to a valid model path
size_t input_height = 224;
size_t input_width = 224;

Raft* raft;

void SetUp() override {
// Instantiate the Raft model with the test parameters
raft = new Raft(model_path, input_height, input_width);
raft->load_model();
}

void TearDown() override {
delete raft;
}
};

TEST_F(RaftTest, TestModelLoad) {
// Test that the model loads successfully
ASSERT_NE(raft->get_engine(), nullptr);
ASSERT_NE(raft->get_context(), nullptr);
ASSERT_NE(raft->get_runtime(), nullptr);
}

TEST_F(RaftTest, TestSetInput) {
// Create a dummy input buffer
std::vector<uint8_t> input_data(input_height * input_width, 128);

// Set input and check if it is copied to the device
raft->set_input(input_data.data(), input_height, input_width);

// Allocate host memory to copy back the data from GPU for verification
std::vector<float> host_input(input_height * input_width);
cudaMemcpy(host_input.data(), raft->get_input_buffer(), input_height * input_width * sizeof(float), cudaMemcpyDeviceToHost);

// Verify the data (simple check to see if values are scaled correctly)
for (size_t i = 0; i < host_input.size(); ++i) {
ASSERT_NEAR(host_input[i], 128.0f / 255.0f, 1e-5);
}
}

TEST_F(RaftTest, TestRunInference) {
// Dummy batch size
size_t batch_size = 1;

// Run inference
bool success = raft->run_inference(batch_size);

// Check if inference ran successfully
ASSERT_TRUE(success);
}
49 changes: 0 additions & 49 deletions test/test_pixel_formats.cpp

This file was deleted.

59 changes: 0 additions & 59 deletions test/test_usb_cam_lib.cpp

This file was deleted.

Loading

0 comments on commit ec8253e

Please sign in to comment.