Skip to content

Commit

Permalink
chore: save engine file in the same directory as onnx file (tier4#519)
Browse files Browse the repository at this point in the history
Signed-off-by: wep21 <border_goldenmarket@yahoo.co.jp>
  • Loading branch information
wep21 authored and boyali committed Oct 19, 2022
1 parent 72eb821 commit b49e07c
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 29 deletions.
4 changes: 2 additions & 2 deletions perception/traffic_light_classifier/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ cmake_minimum_required(VERSION 3.5)
project(traffic_light_classifier)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
endif()
if(CMAKE_COMPILER_IS_GNUCXX OR CMAKE_CXX_COMPILER_ID MATCHES "Clang")
add_compile_options(-Wall -Wextra -Wpedantic -Wno-deprecated-declarations -Werror)
Expand Down Expand Up @@ -133,7 +133,7 @@ if(TRT_AVAIL AND CUDA_AVAIL AND CUDNN_AVAIL)
${CUDA_LIBRARIES}
${CUBLAS_LIBRARIES}
${CUDNN_LIBRARY}
${Boost_LIBRARIES}
stdc++fs
)

ament_auto_add_library(traffic_light_classifier_nodelet SHARED
Expand Down
4 changes: 1 addition & 3 deletions perception/traffic_light_classifier/src/cnn_classifier.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,7 @@ CNNClassifier::CNNClassifier(rclcpp::Node * node_ptr) : node_ptr_(node_ptr)

readLabelfile(label_file_path, labels_);

std::string cache_dir =
ament_index_cpp::get_package_share_directory("traffic_light_classifier") + "/data";
trt_ = std::make_shared<Tn::TrtCommon>(model_file_path, cache_dir, precision);
trt_ = std::make_shared<Tn::TrtCommon>(model_file_path, precision);
trt_->setup();
}

Expand Down
25 changes: 16 additions & 9 deletions perception/traffic_light_classifier/utils/trt_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@

#include <trt_common.hpp>

#if (defined(_MSC_VER) or (defined(__GNUC__) and (7 <= __GNUC_MAJOR__)))
#include <filesystem>
namespace fs = ::std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = ::std::experimental::filesystem;
#endif

#include <functional>
#include <string>

Expand All @@ -29,9 +37,8 @@ void check_error(const ::cudaError_t e, decltype(__FILE__) f, decltype(__LINE__)
}
}

TrtCommon::TrtCommon(std::string model_path, std::string cache_dir, std::string precision)
TrtCommon::TrtCommon(std::string model_path, std::string precision)
: model_file_path_(model_path),
cache_dir_(cache_dir),
precision_(precision),
input_name_("input_0"),
output_name_("output_0"),
Expand All @@ -42,20 +49,20 @@ TrtCommon::TrtCommon(std::string model_path, std::string cache_dir, std::string

void TrtCommon::setup()
{
const boost::filesystem::path path(model_file_path_);
const fs::path path(model_file_path_);
std::string extension = path.extension().string();

if (boost::filesystem::exists(path)) {
if (fs::exists(path)) {
if (extension == ".engine") {
loadEngine(model_file_path_);
} else if (extension == ".onnx") {
std::string cache_engine_path = cache_dir_ + "/" + path.stem().string() + ".engine";
const boost::filesystem::path cache_path(cache_engine_path);
if (boost::filesystem::exists(cache_path)) {
loadEngine(cache_engine_path);
fs::path cache_engine_path{model_file_path_};
cache_engine_path.replace_extension("engine");
if (fs::exists(cache_engine_path)) {
loadEngine(cache_engine_path.string());
} else {
logger_.log(nvinfer1::ILogger::Severity::kINFO, "start build engine");
buildEngineFromOnnx(model_file_path_, cache_engine_path);
buildEngineFromOnnx(model_file_path_, cache_engine_path.string());
logger_.log(nvinfer1::ILogger::Severity::kINFO, "end build engine");
}
} else {
Expand Down
4 changes: 1 addition & 3 deletions perception/traffic_light_classifier/utils/trt_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>

#include <boost/filesystem.hpp>

#include <./cudnn.h>
#include <NvInfer.h>
#include <NvOnnxParser.h>
Expand Down Expand Up @@ -110,7 +108,7 @@ Tn::UniquePtr<T> make_unique()
class TrtCommon
{
public:
TrtCommon(std::string model_path, std::string cache_dir, std::string precision);
TrtCommon(std::string model_path, std::string precision);
~TrtCommon() {}

bool loadEngine(std::string engine_file_path);
Expand Down
5 changes: 2 additions & 3 deletions perception/traffic_light_ssd_fine_detector/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.5)

if(NOT CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 14)
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_CXX_EXTENSIONS OFF)
endif()
Expand Down Expand Up @@ -123,8 +123,6 @@ if(TRT_AVAIL AND CUDA_AVAIL AND CUDNN_AVAIL)
${CUDA_INCLUDE_DIRS}
)

set(CMAKE_CXX_FLAGS "-O2 -Wall -Wunused-variable ${CMAKE_CXX_FLAGS} -fpic -std=c++11 -pthread")

### ssd ###
ament_auto_add_library(ssd SHARED
lib/src/trt_ssd.cpp
Expand All @@ -149,6 +147,7 @@ if(TRT_AVAIL AND CUDA_AVAIL AND CUDNN_AVAIL)

target_link_libraries(traffic_light_ssd_fine_detector_nodelet
${OpenCV_LIB}
stdc++fs
ssd
)

Expand Down
23 changes: 14 additions & 9 deletions perception/traffic_light_ssd_fine_detector/src/nodelet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,16 @@

#include "traffic_light_ssd_fine_detector/nodelet.hpp"

#include <ament_index_cpp/get_package_share_directory.hpp>
#include <cuda_utils.hpp>

#if (defined(_MSC_VER) or (defined(__GNUC__) and (7 <= __GNUC_MAJOR__)))
#include <filesystem>
namespace fs = ::std::filesystem;
#else
#include <experimental/filesystem>
namespace fs = ::std::experimental::filesystem;
#endif

#include <memory>
#include <string>
#include <utility>
Expand All @@ -36,25 +43,23 @@ TrafficLightSSDFineDetectorNodelet::TrafficLightSSDFineDetectorNodelet(
using std::placeholders::_1;
using std::placeholders::_2;

std::string package_path =
ament_index_cpp::get_package_share_directory("traffic_light_ssd_fine_detector");
std::string data_path = package_path + "/data/";
std::string engine_path = package_path + "/data/mb2-ssd-lite.engine";
std::ifstream fs(engine_path);
std::vector<std::string> labels;
const int max_batch_size = this->declare_parameter("max_batch_size", 8);
const std::string onnx_file = this->declare_parameter<std::string>("onnx_file");
const std::string label_file = this->declare_parameter<std::string>("label_file");
const std::string mode = this->declare_parameter("mode", "FP32");

fs::path engine_path{onnx_file};
engine_path.replace_extension("engine");

if (readLabelFile(label_file, labels)) {
if (!getTlrIdFromLabel(labels, tlr_id_)) {
RCLCPP_ERROR(this->get_logger(), "Could not find tlr id");
}
}

if (fs.is_open()) {
RCLCPP_INFO(this->get_logger(), "Found %s", engine_path.c_str());
if (fs::exists(engine_path)) {
RCLCPP_INFO(this->get_logger(), "Found %s", engine_path.string().c_str());
net_ptr_.reset(new ssd::Net(engine_path, false));
if (max_batch_size != net_ptr_->getMaxBatchSize()) {
RCLCPP_INFO(
Expand All @@ -69,7 +74,7 @@ TrafficLightSSDFineDetectorNodelet::TrafficLightSSDFineDetectorNodelet(
} else {
RCLCPP_INFO(
this->get_logger(), "Could not find %s, try making TensorRT engine from onnx",
engine_path.c_str());
engine_path.string().c_str());
net_ptr_.reset(new ssd::Net(onnx_file, mode, max_batch_size));
net_ptr_->save(engine_path);
}
Expand Down

0 comments on commit b49e07c

Please sign in to comment.