Skip to content

Conversation

IasonTheodorou
Copy link
Member

No description provided.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR represents a significant refactoring of a Segment Anything Model (SAM) ONNX inference codebase, transitioning from a standalone C++ application to a ROS package with comprehensive testing infrastructure.

Key changes include:

  • Migration from standalone executable to ROS catkin package structure
  • Complete reorganization of include directories from inc/ to include/
  • Addition of comprehensive unit and integration tests using Google Test
  • Refactoring of core functionality with improved error handling and code organization

Reviewed Changes

Copilot reviewed 16 out of 17 changed files in this pull request and generated 7 comments.

Show a summary per file
File Description
test/test_utils.cpp New comprehensive unit tests for Utils class covering image preprocessing and coordinate scaling
test/sam_test.cpp New integration tests for SAM inference pipeline with model file validation
src/utils.cpp Refactored preprocessing and postprocessing with improved error handling and cleaner code structure
src/segmentation.cpp New segmentation wrapper providing unified interface for SAM encoder/decoder operations
src/sam_inference.cpp Major refactoring with consistent naming conventions and improved session management
src/main.cpp Simplified main function using new segmentation interface
include/*.h New header files replacing old inc/ structure with improved organization
CMakeLists.txt Complete restructure for ROS catkin package with testing support
package.xml New ROS package manifest

Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

std::vector<SEG::DL_RESULT> resSam;
params_encoder.rectConfidenceThreshold = 0.1;
params_encoder.iouThreshold = 0.5;
params_encoder.modelPath = "/home/amigo//Documents/repos/sam_onnx_ros/build/SAM_encoder.onnx";
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded absolute path with double slash '//' makes the code non-portable and fragile. Consider using relative paths or environment variables.

Copilot uses AI. Check for mistakes.


params_decoder = params_encoder;
params_decoder.modelType = SEG::SAM_SEGMENT_DECODER;
params_decoder.modelPath = "/home/amigo/Documents/repos/sam_onnx_ros/build/SAM_mask_decoder.onnx";
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded absolute path makes the code non-portable. Consider using relative paths or environment variables for better maintainability.

Copilot uses AI. Check for mistakes.

src/main.cpp Outdated
SEG::DL_RESULT res;
std::tie(samSegmentors, params_encoder, params_decoder, res, resSam) = Initializer();
std::filesystem::path current_path = std::filesystem::current_path();
std::filesystem::path imgs_path = "/home/amigo/Documents/repos/hero_sam/sam_inference/build/images"; // current_path / <- you could use
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded absolute path makes the code non-portable. The commented suggestion to use current_path / would be more maintainable.

Suggested change
std::filesystem::path imgs_path = "/home/amigo/Documents/repos/hero_sam/sam_inference/build/images"; // current_path / <- you could use
std::filesystem::path imgs_path = current_path / "build/images"; // Use current_path for portability

Copilot uses AI. Check for mistakes.

# -------------- ONNXRuntime ------------------#
set(ONNXRUNTIME_VERSION 1.21.0)
set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../onnxruntime-linux-x64-gpu-1.21.1")
set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../hero_sam.bak/onnxruntime-linux-x64-gpu-1.21.1")
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded relative path to a '.bak' directory is fragile and non-portable. Consider using find_package or environment variables to locate ONNX Runtime.

Suggested change
set(ONNXRUNTIME_ROOT "${CMAKE_CURRENT_SOURCE_DIR}/../hero_sam.bak/onnxruntime-linux-x64-gpu-1.21.1")
# Allow user to specify ONNXRUNTIME_ROOT via CMake variable or environment variable
if(NOT DEFINED ONNXRUNTIME_ROOT)
if(DEFINED ENV{ONNXRUNTIME_ROOT})
set(ONNXRUNTIME_ROOT $ENV{ONNXRUNTIME_ROOT})
else()
message(FATAL_ERROR "ONNXRUNTIME_ROOT is not set. Please set the ONNXRUNTIME_ROOT environment variable or pass -DONNXRUNTIME_ROOT=... to cmake.")
endif()
endif()

Copilot uses AI. Check for mistakes.

CMakeLists.txt Outdated
Comment on lines 87 to 88
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded absolute paths with tilde expansion and '.bak' directories are not portable and will fail on other systems. Consider using relative paths or making model paths configurable.

Copilot uses AI. Check for mistakes.

GTEST_SKIP() << "Models not found in build dir";
}

auto masks = SegmentAnything(samSegmentors, params_encoder, params_decoder, testImage_realistic);
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function call doesn't match the signature. SegmentAnything expects 6 parameters but only 4 are provided. Missing resSam and res parameters.

Copilot uses AI. Check for mistakes.

Comment on lines +40 to +43
void SegmentAnything(std::vector<std::unique_ptr<SAM>> &samSegmentors,
const SEG::DL_INIT_PARAM &params_encoder,
const SEG::DL_INIT_PARAM &params_decoder, const cv::Mat &img, std::vector<SEG::DL_RESULT> &resSam,
SEG::DL_RESULT &res) {
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Function returns void but the test expects it to return a vector of masks. Consider returning the results or updating the test to match the actual function signature.

Copilot uses AI. Check for mistakes.

… and segment anything interfaces changed) and added one more test to check the image dimensions W,H
Copy link
Member

@MatthijsBurgh MatthijsBurgh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix missing newline at EOF

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 18 out of 19 changed files in this pull request and generated 2 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.

CMakeLists.txt Outdated
Comment on lines 87 to 88
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hard-coded paths containing user-specific directories and .bak folders make the build system non-portable. These should use relative paths or be configurable through CMake variables.

Suggested change
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)
set(SAM_MODEL_DIR "${CMAKE_CURRENT_SOURCE_DIR}/../hero_sam.bak/sam_inference/model" CACHE PATH "Directory containing SAM .onnx model files")
configure_file(${SAM_MODEL_DIR}/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
configure_file(${SAM_MODEL_DIR}/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)

Copilot uses AI. Check for mistakes.

}
return RET_OK;

_outputNodeNames.size();
Copy link

Copilot AI Sep 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line appears to be a leftover statement that has no effect. It should either be removed or used in a meaningful way (e.g., assigned to a variable or used in a function call).

Suggested change
_outputNodeNames.size();

Copilot uses AI. Check for mistakes.

Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

Copilot reviewed 18 out of 19 changed files in this pull request and generated 3 comments.


Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.


params_decoder = params_encoder;
params_decoder.modelType = SEG::SAM_SEGMENT_DECODER;
params_decoder.modelPath = "/home/amigo/Documents/repos/sam_onnx_ros/build/SAM_mask_decoder.onnx";
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This hardcoded absolute path is not portable and will fail on other systems. Consider using a relative path, environment variable, or ROS parameter to make the code more maintainable.

Copilot uses AI. Check for mistakes.

Comment on lines +29 to +30
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_mask_decoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_mask_decoder.onnx COPYONLY)
configure_file(~/Documents/repos/hero_sam.bak/sam_inference/model/SAM_encoder.onnx ${CMAKE_CURRENT_BINARY_DIR}/SAM_encoder.onnx COPYONLY)
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These hardcoded paths with tilde expansion are not portable and will fail on other systems. Consider using environment variables or making these paths configurable through CMake options.

Copilot uses AI. Check for mistakes.

SegmentAnything(samSegmentors, params_encoder, params_decoder, testImage_realistic, resSam, res);

// We only check that a vector is returned. (You can strengthen this to EXPECT_FALSE(masks.empty()).)
EXPECT_TRUE(res.masks.size() >= 0) << "Masks should be a valid output vector";
Copy link

Copilot AI Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This assertion will always pass since vector size() returns size_t which is always >= 0. Change to 'EXPECT_FALSE(res.masks.empty())' or 'EXPECT_GT(res.masks.size(), 0)' to test for meaningful output.

Suggested change
EXPECT_TRUE(res.masks.size() >= 0) << "Masks should be a valid output vector";
EXPECT_FALSE(res.masks.empty()) << "Masks should be a valid output vector";

Copilot uses AI. Check for mistakes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants