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

Method getBatch

src/engine.cpp:63–107  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

61}
62
63bool Int8EntropyCalibrator2::getBatch(void **bindings, const char **names, int32_t nbBindings) noexcept {
64 // This method will read a batch of images into GPU memory, and place the
65 // pointer to the GPU memory in the bindings variable.
66
67 if (m_imgIdx + m_batchSize > static_cast<int>(m_imgPaths.size())) {
68 // There are not enough images left to satisfy an entire batch
69 return false;
70 }
71
72 // Read the calibration images into memory for the current batch
73 std::vector<cv::cuda::GpuMat> inputImgs;
74 for (int i = m_imgIdx; i < m_imgIdx + m_batchSize; i++) {
75 std::cout << "Reading image " << i << ": " << m_imgPaths[i] << std::endl;
76 auto cpuImg = cv::imread(m_imgPaths[i]);
77 if (cpuImg.empty()) {
78 std::cout << "Fatal error: Unable to read image at path: " << m_imgPaths[i] << std::endl;
79 return false;
80 }
81
82 cv::cuda::GpuMat gpuImg;
83 gpuImg.upload(cpuImg);
84 cv::cuda::cvtColor(gpuImg, gpuImg, cv::COLOR_BGR2RGB);
85
86 // TODO: Define any preprocessing code here, such as resizing
87 auto resized = Engine<float>::resizeKeepAspectRatioPadRightBottom(gpuImg, m_inputH, m_inputW);
88
89 inputImgs.emplace_back(std::move(resized));
90 }
91
92 // Convert the batch from NHWC to NCHW
93 // ALso apply normalization, scaling, and mean subtraction
94 auto mfloat = Engine<float>::blobFromGpuMats(inputImgs, m_subVals, m_divVals, m_normalize);
95 auto *dataPointer = mfloat.ptr<void>();
96
97 // Copy the GPU buffer to member variable so that it persists
98 checkCudaErrorCode(cudaMemcpyAsync(m_deviceInput, dataPointer, m_inputCount * sizeof(float), cudaMemcpyDeviceToDevice));
99
100 m_imgIdx += m_batchSize;
101 if (std::string(names[0]) != m_inputBlobName) {
102 std::cout << "Error: Incorrect input name provided!" << std::endl;
103 return false;
104 }
105 bindings[0] = m_deviceInput;
106 return true;
107}
108
109void const *Int8EntropyCalibrator2::readCalibrationCache(size_t &length) noexcept {
110 std::cout << "Searching for calibration cache: " << m_calibTableName << std::endl;

Callers

nothing calls this directly

Calls 1

checkCudaErrorCodeFunction · 0.85

Tested by

no test coverage detected