MCPcopy Create free account
hub / github.com/cyrusbehr/tensorrt-cpp-api / runInference

Method runInference

src/engine.h:556–681  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

554
555template <typename T>
556bool Engine<T>::runInference(const std::vector<std::vector<cv::cuda::GpuMat>> &inputs,
557 std::vector<std::vector<std::vector<T>>> &featureVectors) {
558 // First we do some error checking
559 if (inputs.empty() || inputs[0].empty()) {
560 std::cout << "===== Error =====" << std::endl;
561 std::cout << "Provided input vector is empty!" << std::endl;
562 return false;
563 }
564
565 const auto numInputs = m_inputDims.size();
566 if (inputs.size() != numInputs) {
567 std::cout << "===== Error =====" << std::endl;
568 std::cout << "Incorrect number of inputs provided!" << std::endl;
569 return false;
570 }
571
572 // Ensure the batch size does not exceed the max
573 if (inputs[0].size() > static_cast<size_t>(m_options.maxBatchSize)) {
574 std::cout << "===== Error =====" << std::endl;
575 std::cout << "The batch size is larger than the model expects!" << std::endl;
576 std::cout << "Model max batch size: " << m_options.maxBatchSize << std::endl;
577 std::cout << "Batch size provided to call to runInference: " << inputs[0].size() << std::endl;
578 return false;
579 }
580
581 // Ensure that if the model has a fixed batch size that is greater than 1, the
582 // input has the correct length
583 if (m_inputBatchSize != -1 && inputs[0].size() != static_cast<size_t>(m_inputBatchSize)) {
584 std::cout << "===== Error =====" << std::endl;
585 std::cout << "The batch size is different from what the model expects!" << std::endl;
586 std::cout << "Model batch size: " << m_inputBatchSize << std::endl;
587 std::cout << "Batch size provided to call to runInference: " << inputs[0].size() << std::endl;
588 return false;
589 }
590
591 const auto batchSize = static_cast<int32_t>(inputs[0].size());
592 // Make sure the same batch size was provided for all inputs
593 for (size_t i = 1; i < inputs.size(); ++i) {
594 if (inputs[i].size() != static_cast<size_t>(batchSize)) {
595 std::cout << "===== Error =====" << std::endl;
596 std::cout << "The batch size needs to be constant for all inputs!" << std::endl;
597 return false;
598 }
599 }
600
601 // Create the cuda stream that will be used for inference
602 cudaStream_t inferenceCudaStream;
603 Util::checkCudaErrorCode(cudaStreamCreate(&inferenceCudaStream));
604
605 std::vector<cv::cuda::GpuMat> preprocessedInputs;
606
607 // Preprocess all the inputs
608 for (size_t i = 0; i < numInputs; ++i) {
609 const auto &batchInput = inputs[i];
610 const auto &dims = m_inputDims[i];
611
612 auto &input = batchInput[0];
613 if (input.channels() != dims.d[0] || input.rows != dims.d[1] || input.cols != dims.d[2]) {

Callers 1

mainFunction · 0.80

Calls 1

checkCudaErrorCodeFunction · 0.85

Tested by

no test coverage detected