Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tensorrt): update tensorrt code of traffic_light_ssd_fine_detector #2326

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Net
void save(const std::string & path);

// Infer using pre-allocated GPU buffers {data, scores, boxes}
void infer(std::vector<void *> & buffers, const int batch_size);
void infer([[maybe_unused]] std::vector<void *> & buffers, const int batch_size);

// Get (c, h, w) size of the fixed input
std::vector<int> getInputSize();
Expand Down
45 changes: 40 additions & 5 deletions perception/traffic_light_ssd_fine_detector/lib/src/trt_ssd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -164,35 +164,70 @@ void Net::save(const std::string & path)
file.write(reinterpret_cast<const char *>(plan_->data()), plan_->size());
}

void Net::infer(std::vector<void *> & buffers, const int batch_size)
void Net::infer([[maybe_unused]] std::vector<void *> & buffers, const int batch_size)
{
if (!context_) {
throw std::runtime_error("Fail to create context");
}
auto input_dims = engine_->getBindingDimensions(0);

#if (NV_TENSORRT_MAJOR * 10000) + (NV_TENSORRT_MINOR * 100) + NV_TENSOR_PATCH >= 80500
const auto input_dims = engine_->getTensorShape(engine_->getIOTensorName(0));
context_->setInputShape(
engine_->getIOTensorName(0),
nvinfer1::Dims4(batch_size, input_dims.d[1], input_dims.d[2], input_dims.d[3]));
context_->enqueueV3(stream_);
#else
// Deprecated since 8.5
const auto input_dims = engine_->getBindingDimensions(0);
context_->setBindingDimensions(
0, nvinfer1::Dims4(batch_size, input_dims.d[1], input_dims.d[2], input_dims.d[3]));
context_->enqueueV2(buffers.data(), stream_, nullptr);
#endif
cudaStreamSynchronize(stream_);
}

std::vector<int> Net::getInputSize()
{
auto dims = engine_->getBindingDimensions(0);
#if (NV_TENSORRT_MAJOR * 10000) + (NV_TENSORRT_MINOR * 100) + NV_TENSOR_PATCH >= 80500
const auto dims = engine_->getTensorShape(engine_->getIOTensorName(0));
#else
// Deprecated since 8.5
const auto dims = engine_->getBindingDimensions(0);
#endif
return {dims.d[1], dims.d[2], dims.d[3]};
}

std::vector<int> Net::getOutputScoreSize()
{
auto dims = engine_->getBindingDimensions(1);
#if (NV_TENSORRT_MAJOR * 10000) + (NV_TENSORRT_MINOR * 100) + NV_TENSOR_PATCH >= 80500
const auto dims = engine_->getTensorShape(engine_->getIOTensorName(1));
#else
// Deprecated since 8.5
const auto dims = engine_->getBindingDimensions(1);
#endif
return {dims.d[1], dims.d[2]};
}

int Net::getMaxBatchSize()
{
#if (NV_TENSORRT_MAJOR * 10000) + (NV_TENSORRT_MINOR * 100) + NV_TENSOR_PATCH >= 80500
return engine_
->getProfileShape(engine_->getIOTensorName(0), 0, nvinfer1::OptProfileSelector::kMAX)
.d[0];
#else
// Deprecated since 8.5
return engine_->getProfileDimensions(0, 0, nvinfer1::OptProfileSelector::kMAX).d[0];
#endif
}

int Net::getMaxDetections() { return engine_->getBindingDimensions(1).d[1]; }
int Net::getMaxDetections()
{
#if (NV_TENSORRT_MAJOR * 10000) + (NV_TENSORRT_MINOR * 100) + NV_TENSOR_PATCH >= 80500
return engine_->getTensorShape(engine_->getIOTensorName(1)).d[1];
#else
// Deprecated since 8.5
return engine_->getBindingDimensions(1).d[1];
#endif
}

} // namespace ssd