From f42cb41436cdcc5afece8b71651b17dd2d49493d Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Mon, 8 Jul 2024 18:13:48 +0900 Subject: [PATCH 01/15] fix: add rle compress Signed-off-by: badai-nguyen --- .../tensorrt_yolox/tensorrt_yolox_node.hpp | 5 +++ .../src/tensorrt_yolox_node.cpp | 31 +++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp index 7f622b2dc867e..bec2033c3a211 100644 --- a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp +++ b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp @@ -34,6 +34,7 @@ #else #include #endif +#include #include #include @@ -70,6 +71,7 @@ class TrtYoloXNode : public rclcpp::Node public: explicit TrtYoloXNode(const rclcpp::NodeOptions & node_options); + std::vector> rle_compress(const cv::Mat & mask); private: void onConnect(); @@ -81,6 +83,9 @@ class TrtYoloXNode : public rclcpp::Node int mapRoiLabel2SegLabel(const int32_t roi_label_index); image_transport::Publisher image_pub_; image_transport::Publisher mask_pub_; + // rclcpp::Publisher::SharedPtr mask_pub_; + rclcpp::Publisher::SharedPtr str_pub_; + image_transport::Publisher color_mask_pub_; rclcpp::Publisher::SharedPtr objects_pub_; diff --git a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp index f3c7b2552edb6..1a09f8105db80 100644 --- a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp +++ b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp @@ -103,6 +103,7 @@ TrtYoloXNode::TrtYoloXNode(const rclcpp::NodeOptions & node_options) mask_pub_ = image_transport::create_publisher(this, "~/out/mask"); color_mask_pub_ = image_transport::create_publisher(this, "~/out/color_mask"); image_pub_ = image_transport::create_publisher(this, "~/out/image"); + str_pub_ = this->create_publisher("~/out/mask_string", 10); if (declare_parameter("build_only", false)) { RCLCPP_INFO(this->get_logger(), "TensorRT engine file is built and exit."); @@ -126,6 +127,25 @@ void TrtYoloXNode::onConnect() } } +std::vector> TrtYoloXNode::rle_compress(const cv::Mat & image) +{ + std::vector> compressed_data; + const int rows = image.rows; + const int cols = image.cols; + compressed_data.emplace_back(image.at(0, 0), 0); + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + uint8_t current_value = image.at(i, j); + if (compressed_data.back().first == current_value) { + ++compressed_data.back().second; + } else { + compressed_data.emplace_back(current_value, 1); + } + } + } + return compressed_data; +} + void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) { stop_watch_ptr_->toc("processing_time", true); @@ -183,6 +203,17 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) cv_bridge::CvImage(std_msgs::msg::Header(), sensor_msgs::image_encodings::MONO8, mask) .toImageMsg(); out_mask_msg->header = msg->header; + + std::vector> compressed_data = TrtYoloXNode::rle_compress(mask); + int step = sizeof(uint8_t) + sizeof(int); + out_mask_msg->data.resize(static_cast(compressed_data.size()) * step); + for (size_t i = 0; i < compressed_data.size(); ++i) { + std::memcpy(&compressed_data.at(i).first, &out_mask_msg->data[i * step], sizeof(uint8_t)); + std::memcpy( + &compressed_data.at(i).second, &out_mask_msg->data[i * step + sizeof(uint8_t)], + sizeof(int)); + } + out_mask_msg->step = step; mask_pub_.publish(out_mask_msg); } image_pub_.publish(in_image_ptr->toImageMsg()); From 10bfceb35f8a9c0b4a4671f3b314236de4982835 Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Tue, 9 Jul 2024 09:09:18 +0900 Subject: [PATCH 02/15] fix: rle compress Signed-off-by: badai-nguyen --- .../autoware/tensorrt_yolox/tensorrt_yolox_node.hpp | 4 +--- .../autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp | 8 +++----- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp index bec2033c3a211..13924a11c37dc 100644 --- a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp +++ b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp @@ -34,13 +34,13 @@ #else #include #endif -#include #include #include #include #include #include +#include #include namespace autoware::tensorrt_yolox @@ -83,8 +83,6 @@ class TrtYoloXNode : public rclcpp::Node int mapRoiLabel2SegLabel(const int32_t roi_label_index); image_transport::Publisher image_pub_; image_transport::Publisher mask_pub_; - // rclcpp::Publisher::SharedPtr mask_pub_; - rclcpp::Publisher::SharedPtr str_pub_; image_transport::Publisher color_mask_pub_; diff --git a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp index 1a09f8105db80..03ac0a77ff101 100644 --- a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp +++ b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp @@ -103,7 +103,6 @@ TrtYoloXNode::TrtYoloXNode(const rclcpp::NodeOptions & node_options) mask_pub_ = image_transport::create_publisher(this, "~/out/mask"); color_mask_pub_ = image_transport::create_publisher(this, "~/out/color_mask"); image_pub_ = image_transport::create_publisher(this, "~/out/image"); - str_pub_ = this->create_publisher("~/out/mask_string", 10); if (declare_parameter("build_only", false)) { RCLCPP_INFO(this->get_logger(), "TensorRT engine file is built and exit."); @@ -197,7 +196,7 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) overlapSegmentByRoi(yolox_object, mask, width, height); } } - // TODO(badai-nguyen): consider to change to 4bits data transfer + // Compress mask to RLE format if (trt_yolox_->getMultitaskNum() > 0) { sensor_msgs::msg::Image::SharedPtr out_mask_msg = cv_bridge::CvImage(std_msgs::msg::Header(), sensor_msgs::image_encodings::MONO8, mask) @@ -208,12 +207,11 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) int step = sizeof(uint8_t) + sizeof(int); out_mask_msg->data.resize(static_cast(compressed_data.size()) * step); for (size_t i = 0; i < compressed_data.size(); ++i) { - std::memcpy(&compressed_data.at(i).first, &out_mask_msg->data[i * step], sizeof(uint8_t)); + std::memcpy(&out_mask_msg->data[i * step], &compressed_data.at(i).first, sizeof(uint8_t)); std::memcpy( - &compressed_data.at(i).second, &out_mask_msg->data[i * step + sizeof(uint8_t)], + &out_mask_msg->data[i * step + sizeof(uint8_t)], &compressed_data.at(i).second, sizeof(int)); } - out_mask_msg->step = step; mask_pub_.publish(out_mask_msg); } image_pub_.publish(in_image_ptr->toImageMsg()); From c22d0527607677f9416d6d890da1b89b3801c885 Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Fri, 26 Jul 2024 16:11:22 +0900 Subject: [PATCH 03/15] fix: move rle into utils Signed-off-by: badai-nguyen --- .../autoware_tensorrt_yolox/CMakeLists.txt | 1 + .../tensorrt_yolox/tensorrt_yolox_node.hpp | 1 - .../include/autoware/tensorrt_yolox/utils.hpp | 25 ++++++++ .../src/tensorrt_yolox_node.cpp | 23 +------ .../autoware_tensorrt_yolox/src/utils.cpp | 64 +++++++++++++++++++ 5 files changed, 92 insertions(+), 22 deletions(-) create mode 100644 perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp create mode 100644 perception/autoware_tensorrt_yolox/src/utils.cpp diff --git a/perception/autoware_tensorrt_yolox/CMakeLists.txt b/perception/autoware_tensorrt_yolox/CMakeLists.txt index 1f54326f2b33a..564ac54dbc563 100644 --- a/perception/autoware_tensorrt_yolox/CMakeLists.txt +++ b/perception/autoware_tensorrt_yolox/CMakeLists.txt @@ -143,6 +143,7 @@ rclcpp_components_register_node(yolox_single_image_inference_node ) ament_auto_add_library(${PROJECT_NAME}_node SHARED + src/utils.cpp src/tensorrt_yolox_node.cpp ) diff --git a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp index 13924a11c37dc..2e317139f27c0 100644 --- a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp +++ b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/tensorrt_yolox_node.hpp @@ -71,7 +71,6 @@ class TrtYoloXNode : public rclcpp::Node public: explicit TrtYoloXNode(const rclcpp::NodeOptions & node_options); - std::vector> rle_compress(const cv::Mat & mask); private: void onConnect(); diff --git a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp new file mode 100644 index 0000000000000..28c6f1a8dbc85 --- /dev/null +++ b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp @@ -0,0 +1,25 @@ +// Copyright 2024 TIER IV, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#ifndef AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ +#define AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ +#include + +namespace autoware::tensorrt_yolox +{ +std::vector> runLengthEncoder(const cv::Mat & mask); +cv::Mat runLengthDecoder(const std::vector & rle_data, const int rows, const int cols); +} // namespace autoware::tensorrt_yolox + +#endif // AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ diff --git a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp index 03ac0a77ff101..ee620ad10446e 100644 --- a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp +++ b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp @@ -14,6 +14,7 @@ #include "autoware/tensorrt_yolox/tensorrt_yolox_node.hpp" +#include "autoware/tensorrt_yolox/utils.hpp" #include "object_recognition_utils/object_classification.hpp" #include @@ -126,25 +127,6 @@ void TrtYoloXNode::onConnect() } } -std::vector> TrtYoloXNode::rle_compress(const cv::Mat & image) -{ - std::vector> compressed_data; - const int rows = image.rows; - const int cols = image.cols; - compressed_data.emplace_back(image.at(0, 0), 0); - for (int i = 0; i < rows; ++i) { - for (int j = 0; j < cols; ++j) { - uint8_t current_value = image.at(i, j); - if (compressed_data.back().first == current_value) { - ++compressed_data.back().second; - } else { - compressed_data.emplace_back(current_value, 1); - } - } - } - return compressed_data; -} - void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) { stop_watch_ptr_->toc("processing_time", true); @@ -196,14 +178,13 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) overlapSegmentByRoi(yolox_object, mask, width, height); } } - // Compress mask to RLE format if (trt_yolox_->getMultitaskNum() > 0) { sensor_msgs::msg::Image::SharedPtr out_mask_msg = cv_bridge::CvImage(std_msgs::msg::Header(), sensor_msgs::image_encodings::MONO8, mask) .toImageMsg(); out_mask_msg->header = msg->header; - std::vector> compressed_data = TrtYoloXNode::rle_compress(mask); + std::vector> compressed_data = runLengthEncoder(mask); int step = sizeof(uint8_t) + sizeof(int); out_mask_msg->data.resize(static_cast(compressed_data.size()) * step); for (size_t i = 0; i < compressed_data.size(); ++i) { diff --git a/perception/autoware_tensorrt_yolox/src/utils.cpp b/perception/autoware_tensorrt_yolox/src/utils.cpp new file mode 100644 index 0000000000000..e93729f5a0c28 --- /dev/null +++ b/perception/autoware_tensorrt_yolox/src/utils.cpp @@ -0,0 +1,64 @@ +// Copyright 2024 Tier IV, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "autoware/tensorrt_yolox/utils.hpp" + +namespace autoware::tensorrt_yolox +{ + +std::vector> runLengthEncoder(const cv::Mat & image) +{ + std::vector> compressed_data; + const int rows = image.rows; + const int cols = image.cols; + compressed_data.emplace_back(image.at(0, 0), 0); + for (int i = 0; i < rows; ++i) { + for (int j = 0; j < cols; ++j) { + uint8_t current_value = image.at(i, j); + if (compressed_data.back().first == current_value) { + ++compressed_data.back().second; + } else { + compressed_data.emplace_back(current_value, 1); + } + } + } + return compressed_data; +} + +cv::Mat runLengthDecoder(const std::vector & rle_data, const int rows, const int cols) +{ + cv::Mat mask(rows, cols, CV_8UC1, cv::Scalar(0)); + int idx = 0; + int step = sizeof(uint8_t) + sizeof(int); + int nb_pixels = 0; + for (size_t i = 0; i < rle_data.size(); i += step) { + uint8_t value; + int length; + std::memcpy(&value, &rle_data[i], sizeof(uint8_t)); + std::memcpy(&length, &rle_data[i + sizeof(uint8_t)], sizeof(int)); + nb_pixels += length; + for (int j = 0; j < length; ++j) { + int row_idx = static_cast(idx / cols); + int col_idx = static_cast(idx % cols); + mask.at(row_idx, col_idx) = value; + idx++; + if (idx > rows * cols) { + break; + } + } + } + return mask; +} + +} // namespace autoware::tensorrt_yolox From a853f8f16ffb0df36ee808f432af0a05ae90fe0b Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Fri, 26 Jul 2024 20:04:43 +0900 Subject: [PATCH 04/15] chore: pre-commit Signed-off-by: badai-nguyen --- .../include/autoware/tensorrt_yolox/utils.hpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp index 28c6f1a8dbc85..8b86a798e798b 100644 --- a/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp +++ b/perception/autoware_tensorrt_yolox/include/autoware/tensorrt_yolox/utils.hpp @@ -16,6 +16,9 @@ #define AUTOWARE__TENSORRT_YOLOX__UTILS_HPP_ #include +#include +#include + namespace autoware::tensorrt_yolox { std::vector> runLengthEncoder(const cv::Mat & mask); From f7facd5ccd29d9b761752c3d42986cbf3ac448f4 Mon Sep 17 00:00:00 2001 From: badai nguyen <94814556+badai-nguyen@users.noreply.github.com> Date: Mon, 29 Jul 2024 17:15:19 +0900 Subject: [PATCH 05/15] Update perception/autoware_tensorrt_yolox/src/utils.cpp Co-authored-by: Yukihiro Saito --- perception/autoware_tensorrt_yolox/src/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perception/autoware_tensorrt_yolox/src/utils.cpp b/perception/autoware_tensorrt_yolox/src/utils.cpp index e93729f5a0c28..3877c53b1e815 100644 --- a/perception/autoware_tensorrt_yolox/src/utils.cpp +++ b/perception/autoware_tensorrt_yolox/src/utils.cpp @@ -1,4 +1,4 @@ -// Copyright 2024 Tier IV, Inc. +// Copyright 2024 TIER IV, Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. From 12307ea62af1a182458bf2dfba812c76796e5d02 Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Wed, 7 Aug 2024 09:19:02 +0900 Subject: [PATCH 06/15] fix: remove unused variable Signed-off-by: badai-nguyen --- perception/autoware_tensorrt_yolox/src/utils.cpp | 2 -- 1 file changed, 2 deletions(-) diff --git a/perception/autoware_tensorrt_yolox/src/utils.cpp b/perception/autoware_tensorrt_yolox/src/utils.cpp index 3877c53b1e815..2fadf09bfd4fb 100644 --- a/perception/autoware_tensorrt_yolox/src/utils.cpp +++ b/perception/autoware_tensorrt_yolox/src/utils.cpp @@ -41,13 +41,11 @@ cv::Mat runLengthDecoder(const std::vector & rle_data, const int rows, cv::Mat mask(rows, cols, CV_8UC1, cv::Scalar(0)); int idx = 0; int step = sizeof(uint8_t) + sizeof(int); - int nb_pixels = 0; for (size_t i = 0; i < rle_data.size(); i += step) { uint8_t value; int length; std::memcpy(&value, &rle_data[i], sizeof(uint8_t)); std::memcpy(&length, &rle_data[i + sizeof(uint8_t)], sizeof(int)); - nb_pixels += length; for (int j = 0; j < length; ++j) { int row_idx = static_cast(idx / cols); int col_idx = static_cast(idx % cols); From bd3f0b0a8526d967d357801daa2416e160203eec Mon Sep 17 00:00:00 2001 From: badai nguyen <94814556+badai-nguyen@users.noreply.github.com> Date: Wed, 7 Aug 2024 09:44:46 +0900 Subject: [PATCH 07/15] Update perception/autoware_tensorrt_yolox/src/utils.cpp Co-authored-by: Manato Hirabayashi <3022416+manato@users.noreply.github.com> --- perception/autoware_tensorrt_yolox/src/utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perception/autoware_tensorrt_yolox/src/utils.cpp b/perception/autoware_tensorrt_yolox/src/utils.cpp index 2fadf09bfd4fb..d15d5db2196b8 100644 --- a/perception/autoware_tensorrt_yolox/src/utils.cpp +++ b/perception/autoware_tensorrt_yolox/src/utils.cpp @@ -45,7 +45,7 @@ cv::Mat runLengthDecoder(const std::vector & rle_data, const int rows, uint8_t value; int length; std::memcpy(&value, &rle_data[i], sizeof(uint8_t)); - std::memcpy(&length, &rle_data[i + sizeof(uint8_t)], sizeof(int)); + std::memcpy(&length, &rle_data[i + 1], sizeof(int)); // under the condition that we know rle_data[i] only consume 1 element of the vector for (int j = 0; j < length; ++j) { int row_idx = static_cast(idx / cols); int col_idx = static_cast(idx % cols); From e581b4d0334098e7f744f4ecd9039f1af143d084 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 7 Aug 2024 00:46:48 +0000 Subject: [PATCH 08/15] style(pre-commit): autofix --- perception/autoware_tensorrt_yolox/src/utils.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/perception/autoware_tensorrt_yolox/src/utils.cpp b/perception/autoware_tensorrt_yolox/src/utils.cpp index d15d5db2196b8..8d2b4a4a0527f 100644 --- a/perception/autoware_tensorrt_yolox/src/utils.cpp +++ b/perception/autoware_tensorrt_yolox/src/utils.cpp @@ -45,7 +45,10 @@ cv::Mat runLengthDecoder(const std::vector & rle_data, const int rows, uint8_t value; int length; std::memcpy(&value, &rle_data[i], sizeof(uint8_t)); - std::memcpy(&length, &rle_data[i + 1], sizeof(int)); // under the condition that we know rle_data[i] only consume 1 element of the vector + std::memcpy( + &length, &rle_data[i + 1], + sizeof( + int)); // under the condition that we know rle_data[i] only consume 1 element of the vector for (int j = 0; j < length; ++j) { int row_idx = static_cast(idx / cols); int col_idx = static_cast(idx % cols); From ce85d8b89d04eacb94590332cb2db240f1a955ed Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Wed, 7 Aug 2024 10:57:14 +0900 Subject: [PATCH 09/15] feat: add unit test for utils Signed-off-by: badai-nguyen --- .../autoware_tensorrt_yolox/CMakeLists.txt | 5 ++ .../autoware_tensorrt_yolox/package.xml | 1 + .../test/test_utils.cpp | 75 +++++++++++++++++++ 3 files changed, 81 insertions(+) create mode 100644 perception/autoware_tensorrt_yolox/test/test_utils.cpp diff --git a/perception/autoware_tensorrt_yolox/CMakeLists.txt b/perception/autoware_tensorrt_yolox/CMakeLists.txt index 564ac54dbc563..aa8c945a9e594 100644 --- a/perception/autoware_tensorrt_yolox/CMakeLists.txt +++ b/perception/autoware_tensorrt_yolox/CMakeLists.txt @@ -167,6 +167,11 @@ rclcpp_components_register_node(${PROJECT_NAME}_node if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() + + find_package(ament_cmake_gtest REQUIRED) + ament_auto_add_gtest(test_utils + test/test_utils.cpp + ) endif() ament_auto_package(INSTALL_TO_SHARE diff --git a/perception/autoware_tensorrt_yolox/package.xml b/perception/autoware_tensorrt_yolox/package.xml index a7921eb55df52..ee961e1523ec8 100644 --- a/perception/autoware_tensorrt_yolox/package.xml +++ b/perception/autoware_tensorrt_yolox/package.xml @@ -32,6 +32,7 @@ image_transport_decompressor + ament_cmake_gtest ament_lint_auto autoware_lint_common diff --git a/perception/autoware_tensorrt_yolox/test/test_utils.cpp b/perception/autoware_tensorrt_yolox/test/test_utils.cpp new file mode 100644 index 0000000000000..911875fbea050 --- /dev/null +++ b/perception/autoware_tensorrt_yolox/test/test_utils.cpp @@ -0,0 +1,75 @@ +// Copyright 2024 TIER IV, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +#include "autoware/tensorrt_yolox/utils.hpp" +#include +#include + +using autoware::tensorrt_yolox::runLengthEncoder; +using autoware::tensorrt_yolox::runLengthDecoder; + +// Test case 1: Test if the decoded image is the same as the original image +TEST(UtilsTest, runLengthEncoderDecoderTest) +{ + int height = 10; + int width = 20; + uint8_t number_cls = 16; + // Create an image as below + // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 4 4 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 6 6 6 6 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 7 7 7 7 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 + // 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 + // 9 9 9 9 9 9 9 9 9 0 0 0 0 0 0 0 0 0 0 0 + cv::Mat image = cv::Mat::zeros(10, 20, CV_8UC1); + for (int i = 0; i < height; ++i) { + for (int j = 0; j < width; ++j) { + if (j < i){ + image.at(i, j) = i % number_cls; + } + else{ + image.at(i, j) = 0; + } + } + } + // Compress the image + std::vector> compressed_data = runLengthEncoder(image); + int step = sizeof(uint8_t) + sizeof(int); + std::vector data(compressed_data.size() * step); + for (size_t i = 0; i < compressed_data.size(); ++i) { + std::memcpy(&data[i * step], &compressed_data.at(i).first, sizeof(uint8_t)); + std::memcpy( + &data[i * step + sizeof(uint8_t)], &compressed_data.at(i).second, + sizeof(int)); + } + // Decompress the image + cv::Mat decoded_image = runLengthDecoder(data, height, width); + // Compare the original image and the decoded image + ASSERT_EQ(image.rows, decoded_image.rows); + ASSERT_EQ(image.cols, decoded_image.cols); + bool image_eq = true; + for (int i = 0; i < image.rows; ++i) { + for (int j = 0; j < image.cols; ++j) { + if (image.at(i, j) != decoded_image.at(i, j)){ + image_eq = false; + break; + } + } + } + EXPECT_EQ(image_eq, true); +} \ No newline at end of file From 1dc4020c8accc696d3876eb5f0d5ca186ff65bff Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 7 Aug 2024 01:59:48 +0000 Subject: [PATCH 10/15] style(pre-commit): autofix --- .../autoware_tensorrt_yolox/test/test_utils.cpp | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/perception/autoware_tensorrt_yolox/test/test_utils.cpp b/perception/autoware_tensorrt_yolox/test/test_utils.cpp index 911875fbea050..0421fc01e6d70 100644 --- a/perception/autoware_tensorrt_yolox/test/test_utils.cpp +++ b/perception/autoware_tensorrt_yolox/test/test_utils.cpp @@ -13,11 +13,13 @@ // limitations under the License. #include "autoware/tensorrt_yolox/utils.hpp" + #include + #include -using autoware::tensorrt_yolox::runLengthEncoder; using autoware::tensorrt_yolox::runLengthDecoder; +using autoware::tensorrt_yolox::runLengthEncoder; // Test case 1: Test if the decoded image is the same as the original image TEST(UtilsTest, runLengthEncoderDecoderTest) @@ -39,10 +41,9 @@ TEST(UtilsTest, runLengthEncoderDecoderTest) cv::Mat image = cv::Mat::zeros(10, 20, CV_8UC1); for (int i = 0; i < height; ++i) { for (int j = 0; j < width; ++j) { - if (j < i){ + if (j < i) { image.at(i, j) = i % number_cls; - } - else{ + } else { image.at(i, j) = 0; } } @@ -53,9 +54,7 @@ TEST(UtilsTest, runLengthEncoderDecoderTest) std::vector data(compressed_data.size() * step); for (size_t i = 0; i < compressed_data.size(); ++i) { std::memcpy(&data[i * step], &compressed_data.at(i).first, sizeof(uint8_t)); - std::memcpy( - &data[i * step + sizeof(uint8_t)], &compressed_data.at(i).second, - sizeof(int)); + std::memcpy(&data[i * step + sizeof(uint8_t)], &compressed_data.at(i).second, sizeof(int)); } // Decompress the image cv::Mat decoded_image = runLengthDecoder(data, height, width); @@ -65,11 +64,11 @@ TEST(UtilsTest, runLengthEncoderDecoderTest) bool image_eq = true; for (int i = 0; i < image.rows; ++i) { for (int j = 0; j < image.cols; ++j) { - if (image.at(i, j) != decoded_image.at(i, j)){ + if (image.at(i, j) != decoded_image.at(i, j)) { image_eq = false; break; } } } EXPECT_EQ(image_eq, true); -} \ No newline at end of file +} From 44c6175299946e46f0894a97f5adb5acb8021732 Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Wed, 7 Aug 2024 11:15:20 +0900 Subject: [PATCH 11/15] fix: unit test Signed-off-by: badai-nguyen --- perception/autoware_tensorrt_yolox/test/test_utils.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/perception/autoware_tensorrt_yolox/test/test_utils.cpp b/perception/autoware_tensorrt_yolox/test/test_utils.cpp index 0421fc01e6d70..7f61ed9141c1e 100644 --- a/perception/autoware_tensorrt_yolox/test/test_utils.cpp +++ b/perception/autoware_tensorrt_yolox/test/test_utils.cpp @@ -72,3 +72,9 @@ TEST(UtilsTest, runLengthEncoderDecoderTest) } EXPECT_EQ(image_eq, true); } + +int main(int argc, char ** argv) +{ + testing::InitGoogleTest(&argc, argv); + return RUN_ALL_TESTS(); +} From 3220095709b0fd653c8bda7c39d5eaa9fe6efa64 Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Wed, 7 Aug 2024 16:54:15 +0900 Subject: [PATCH 12/15] chore: change to explicit index Signed-off-by: badai-nguyen --- perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp index ee620ad10446e..cd21dc9f9bd67 100644 --- a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp +++ b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp @@ -190,7 +190,7 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) for (size_t i = 0; i < compressed_data.size(); ++i) { std::memcpy(&out_mask_msg->data[i * step], &compressed_data.at(i).first, sizeof(uint8_t)); std::memcpy( - &out_mask_msg->data[i * step + sizeof(uint8_t)], &compressed_data.at(i).second, + &out_mask_msg->data[i * step + 1], &compressed_data.at(i).second, sizeof(int)); } mask_pub_.publish(out_mask_msg); From 191e2e393ddd7b1ca4eae577815ecfae7cd84391 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 7 Aug 2024 07:56:31 +0000 Subject: [PATCH 13/15] style(pre-commit): autofix --- .../autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp index cd21dc9f9bd67..7f398ca005d67 100644 --- a/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp +++ b/perception/autoware_tensorrt_yolox/src/tensorrt_yolox_node.cpp @@ -189,9 +189,7 @@ void TrtYoloXNode::onImage(const sensor_msgs::msg::Image::ConstSharedPtr msg) out_mask_msg->data.resize(static_cast(compressed_data.size()) * step); for (size_t i = 0; i < compressed_data.size(); ++i) { std::memcpy(&out_mask_msg->data[i * step], &compressed_data.at(i).first, sizeof(uint8_t)); - std::memcpy( - &out_mask_msg->data[i * step + 1], &compressed_data.at(i).second, - sizeof(int)); + std::memcpy(&out_mask_msg->data[i * step + 1], &compressed_data.at(i).second, sizeof(int)); } mask_pub_.publish(out_mask_msg); } From a69a75abfd6f96c3d47ccff4ffda7995c86ac49a Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Wed, 7 Aug 2024 17:58:31 +0900 Subject: [PATCH 14/15] fix: cuda cmake Signed-off-by: badai-nguyen --- perception/autoware_tensorrt_yolox/CMakeLists.txt | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/perception/autoware_tensorrt_yolox/CMakeLists.txt b/perception/autoware_tensorrt_yolox/CMakeLists.txt index aa8c945a9e594..a6eaf28933160 100644 --- a/perception/autoware_tensorrt_yolox/CMakeLists.txt +++ b/perception/autoware_tensorrt_yolox/CMakeLists.txt @@ -168,10 +168,11 @@ if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() - find_package(ament_cmake_gtest REQUIRED) - ament_auto_add_gtest(test_utils - test/test_utils.cpp - ) + if(TRT_AVAIL AND CUDA_AVAIL AND CUDNN_AVAIL) + ament_auto_add_gtest(test_utils + test/test_utils.cpp + ) + endif() endif() ament_auto_package(INSTALL_TO_SHARE From 1293d0dbcfee2fab757a96854ad2e855b73c3a79 Mon Sep 17 00:00:00 2001 From: badai-nguyen Date: Thu, 8 Aug 2024 21:13:10 +0900 Subject: [PATCH 15/15] fix: separate unit test into different PR Signed-off-by: badai-nguyen --- .../autoware_tensorrt_yolox/CMakeLists.txt | 6 -- .../autoware_tensorrt_yolox/package.xml | 1 - .../test/test_utils.cpp | 80 ------------------- 3 files changed, 87 deletions(-) delete mode 100644 perception/autoware_tensorrt_yolox/test/test_utils.cpp diff --git a/perception/autoware_tensorrt_yolox/CMakeLists.txt b/perception/autoware_tensorrt_yolox/CMakeLists.txt index a6eaf28933160..564ac54dbc563 100644 --- a/perception/autoware_tensorrt_yolox/CMakeLists.txt +++ b/perception/autoware_tensorrt_yolox/CMakeLists.txt @@ -167,12 +167,6 @@ rclcpp_components_register_node(${PROJECT_NAME}_node if(BUILD_TESTING) find_package(ament_lint_auto REQUIRED) ament_lint_auto_find_test_dependencies() - - if(TRT_AVAIL AND CUDA_AVAIL AND CUDNN_AVAIL) - ament_auto_add_gtest(test_utils - test/test_utils.cpp - ) - endif() endif() ament_auto_package(INSTALL_TO_SHARE diff --git a/perception/autoware_tensorrt_yolox/package.xml b/perception/autoware_tensorrt_yolox/package.xml index b627b455ec7df..9205b0c83cbea 100644 --- a/perception/autoware_tensorrt_yolox/package.xml +++ b/perception/autoware_tensorrt_yolox/package.xml @@ -32,7 +32,6 @@ autoware_image_transport_decompressor - ament_cmake_gtest ament_lint_auto autoware_lint_common diff --git a/perception/autoware_tensorrt_yolox/test/test_utils.cpp b/perception/autoware_tensorrt_yolox/test/test_utils.cpp deleted file mode 100644 index 7f61ed9141c1e..0000000000000 --- a/perception/autoware_tensorrt_yolox/test/test_utils.cpp +++ /dev/null @@ -1,80 +0,0 @@ -// Copyright 2024 TIER IV, Inc. -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. - -#include "autoware/tensorrt_yolox/utils.hpp" - -#include - -#include - -using autoware::tensorrt_yolox::runLengthDecoder; -using autoware::tensorrt_yolox::runLengthEncoder; - -// Test case 1: Test if the decoded image is the same as the original image -TEST(UtilsTest, runLengthEncoderDecoderTest) -{ - int height = 10; - int width = 20; - uint8_t number_cls = 16; - // Create an image as below - // 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 2 2 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 3 3 3 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 4 4 4 4 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 5 5 5 5 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 6 6 6 6 6 6 0 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 7 7 7 7 7 7 7 0 0 0 0 0 0 0 0 0 0 0 0 0 - // 8 8 8 8 8 8 8 8 0 0 0 0 0 0 0 0 0 0 0 0 - // 9 9 9 9 9 9 9 9 9 0 0 0 0 0 0 0 0 0 0 0 - cv::Mat image = cv::Mat::zeros(10, 20, CV_8UC1); - for (int i = 0; i < height; ++i) { - for (int j = 0; j < width; ++j) { - if (j < i) { - image.at(i, j) = i % number_cls; - } else { - image.at(i, j) = 0; - } - } - } - // Compress the image - std::vector> compressed_data = runLengthEncoder(image); - int step = sizeof(uint8_t) + sizeof(int); - std::vector data(compressed_data.size() * step); - for (size_t i = 0; i < compressed_data.size(); ++i) { - std::memcpy(&data[i * step], &compressed_data.at(i).first, sizeof(uint8_t)); - std::memcpy(&data[i * step + sizeof(uint8_t)], &compressed_data.at(i).second, sizeof(int)); - } - // Decompress the image - cv::Mat decoded_image = runLengthDecoder(data, height, width); - // Compare the original image and the decoded image - ASSERT_EQ(image.rows, decoded_image.rows); - ASSERT_EQ(image.cols, decoded_image.cols); - bool image_eq = true; - for (int i = 0; i < image.rows; ++i) { - for (int j = 0; j < image.cols; ++j) { - if (image.at(i, j) != decoded_image.at(i, j)) { - image_eq = false; - break; - } - } - } - EXPECT_EQ(image_eq, true); -} - -int main(int argc, char ** argv) -{ - testing::InitGoogleTest(&argc, argv); - return RUN_ALL_TESTS(); -}