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 tensorrt_yolo #2327

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
2 changes: 1 addition & 1 deletion perception/tensorrt_yolo/lib/include/trt_yolo.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ class Net
std::vector<float> preprocess(
const cv::Mat & in_img, const int c, const int h, const int w) const;
// 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);
};

bool set_cuda_device(int gpu_id)
Expand Down
37 changes: 34 additions & 3 deletions perception/tensorrt_yolo/lib/src/trt_yolo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -276,15 +276,26 @@ void Net::save(const std::string & path) const
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");
}

#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
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_);
}

Expand Down Expand Up @@ -316,13 +327,25 @@ bool Net::detect(const cv::Mat & in_img, float * out_scores, float * out_boxes,

std::vector<int> Net::getInputDims() const
{
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]};
}

int Net::getMaxBatchSize() const
{
#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::getInputSize() const
Expand All @@ -333,6 +356,14 @@ int Net::getInputSize() const
return input_size;
}

int Net::getMaxDetections() const { return engine_->getBindingDimensions(1).d[1]; }
int Net::getMaxDetections() const
{
#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 yolo