Skip to content

Commit f5d869b

Browse files
committed
Fixed bug with resizing image to input
1 parent 41e6de0 commit f5d869b

4 files changed

Lines changed: 39 additions & 114 deletions

File tree

data/settings_yolov8.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ class_names = C:/work/home/mtracker/Multitarget-tracker/data/coco/coco.names
3434
#-----------------------------
3535
confidence_threshold = 0.5
3636

37-
max_crop_ratio = 0
37+
max_crop_ratio = 1.5
3838
max_batch = 1
3939
gpu_id = 0
4040

src/Detector/tensorrt_yolo/YoloONNX.cpp

Lines changed: 37 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -241,37 +241,6 @@ bool YoloONNX::ConstructNetwork(YoloONNXUniquePtr<nvinfer1::IBuilder>& builder,
241241
return res;
242242
}
243243

244-
//!
245-
//! \brief Runs the TensorRT inference engine for this sample
246-
//!
247-
//! \details This function is the main execution function of the sample. It allocates the buffer,
248-
//! sets inputs and executes the engine.
249-
//!
250-
bool YoloONNX::Detect(cv::Mat frame, std::vector<tensor_rt::Result>& bboxes)
251-
{
252-
// Read the input data into the managed buffers
253-
assert(m_params.inputTensorNames.size() == 1);
254-
255-
if (!ProcessInputAspectRatio(frame))
256-
return false;
257-
258-
// Memcpy from host input buffers to device input buffers
259-
m_buffers->copyInputToDevice();
260-
261-
bool status = m_context->executeV2(m_buffers->getDeviceBindings().data());
262-
if (!status)
263-
return false;
264-
265-
// Memcpy from device output buffers to host output buffers
266-
m_buffers->copyOutputToHost();
267-
268-
// Post-process detections and verify results
269-
if (!VerifyOutputAspectRatio(0, bboxes, frame.size()))
270-
return false;
271-
272-
return true;
273-
}
274-
275244
///
276245
/// \brief YoloONNX::Detect
277246
/// \param frames
@@ -283,25 +252,9 @@ bool YoloONNX::Detect(const std::vector<cv::Mat>& frames, std::vector<tensor_rt:
283252
// Read the input data into the managed buffers
284253
assert(m_params.inputTensorNames.size() == 1);
285254

286-
#if 1
287255
if (!ProcessInputAspectRatio(frames))
288256
return false;
289-
#else
290-
std::vector<DsImage> vec_ds_images;
291-
cv::Size netSize = GetInputSize();
292-
for (const auto& img : frames)
293-
{
294-
vec_ds_images.emplace_back(img, m_params.m_netType, netSize.height, netSize.width);
295-
}
296-
int sz[] = { m_params.explicitBatchSize, 3, netSize.height, netSize.width };
297-
float* hostInputBuffer = nullptr;
298-
if (m_params.inputTensorNames[0].empty())
299-
hostInputBuffer = static_cast<float*>(m_buffers->getHostBuffer(0));
300-
else
301-
hostInputBuffer = static_cast<float*>(m_buffers->getHostBuffer(m_params.inputTensorNames[0]));
302-
cv::Mat blob(4, sz, CV_32F, hostInputBuffer);
303-
blobFromDsImages(vec_ds_images, blob, netSize.height, netSize.width);
304-
#endif
257+
305258
// Memcpy from host input buffers to device input buffers
306259
m_buffers->copyInputToDevice();
307260

@@ -352,7 +305,7 @@ size_t YoloONNX::GetNumClasses() const
352305
//!
353306
//! \brief Reads the input and mean data, preprocesses, and stores the result in a managed buffer
354307
//!
355-
bool YoloONNX::ProcessInputAspectRatio(const cv::Mat& mSampleImage)
308+
bool YoloONNX::ProcessInputAspectRatio(const std::vector<cv::Mat>& sampleImages)
356309
{
357310
const int inputB = m_inputDims.d[0];
358311
const int inputC = m_inputDims.d[1];
@@ -373,72 +326,59 @@ bool YoloONNX::ProcessInputAspectRatio(const cv::Mat& mSampleImage)
373326
}
374327
}
375328

376-
auto scaleSize = cv::Size(inputW, inputH);
377-
cv::resize(mSampleImage, m_resized, scaleSize, 0, 0, cv::INTER_LINEAR);
378-
379-
// Each element in batch share the same image matrix
380-
for (int b = 0; b < inputB; ++b)
381-
{
382-
cv::split(m_resized, m_inputChannels[b]);
383-
std::swap(m_inputChannels[b][0], m_inputChannels[b][2]);
384-
}
329+
#if 0
385330

386-
int volBatch = inputC * inputH * inputW;
387-
int volChannel = inputH * inputW;
331+
// resize the DsImage with scale
332+
const float imgHeight = static_cast<float>(sampleImages[0].rows);
333+
const float imgWidth = static_cast<float>(sampleImages[0].cols);
334+
float dim = std::max(imgHeight, imgWidth);
335+
int resizeH = ((imgHeight / dim) * inputH);
336+
int resizeW = ((imgWidth / dim) * inputW);
337+
float scalingFactor = static_cast<float>(resizeH) / static_cast<float>(imgHeight);
388338

389-
constexpr float to1 = 1.f / 255.0f;
339+
// Additional checks for images with non even dims
340+
if ((inputW - resizeW) % 2)
341+
resizeW--;
342+
if ((inputH - resizeH) % 2)
343+
resizeH--;
344+
assert((inputW - resizeW) % 2 == 0);
345+
assert((inputH - resizeH) % 2 == 0);
390346

391-
int d_batch_pos = 0;
392-
for (int b = 0; b < inputB; ++b)
393-
{
394-
int d_c_pos = d_batch_pos;
395-
for (int c = 0; c < inputC; ++c)
396-
{
397-
m_inputChannels[b][c].convertTo(cv::Mat(inputH, inputW, CV_32FC1, &hostInputBuffer[d_c_pos]), CV_32FC1, to1, 0);
398-
d_c_pos += volChannel;
399-
}
400-
d_batch_pos += volBatch;
401-
}
347+
float xOffset = (inputW - resizeW) / 2;
348+
float yOffset = (inputH - resizeH) / 2;
402349

403-
return true;
404-
}
350+
assert(2 * xOffset + resizeW == inputW);
351+
assert(2 * yOffset + resizeH == inputH);
405352

406-
//!
407-
//! \brief Reads the input and mean data, preprocesses, and stores the result in a managed buffer
408-
//!
409-
bool YoloONNX::ProcessInputAspectRatio(const std::vector<cv::Mat>& mSampleImage)
410-
{
411-
const int inputB = m_inputDims.d[0];
412-
const int inputC = m_inputDims.d[1];
413-
const int inputH = m_inputDims.d[2];
414-
const int inputW = m_inputDims.d[3];
353+
cv::Size scaleSize(inputW, inputH);
354+
cv::Rect roiRect(xOffset, yOffset, resizeW, resizeH);
415355

416-
float* hostInputBuffer = nullptr;
417-
if (m_params.inputTensorNames[0].empty())
418-
hostInputBuffer = static_cast<float*>(m_buffers->getHostBuffer(0));
419-
else
420-
hostInputBuffer = static_cast<float*>(m_buffers->getHostBuffer(m_params.inputTensorNames[0]));
356+
if (m_resizedBatch.size() < sampleImages.size())
357+
m_resizedBatch.resize(sampleImages.size());
421358

422-
if (static_cast<int>(m_inputChannels.size()) < inputB)
359+
// Each element in batch share the same image matrix
360+
for (int b = 0; b < inputB; ++b)
423361
{
424-
for (int b = 0; b < inputB; ++b)
425-
{
426-
m_inputChannels.push_back(std::vector<cv::Mat> {static_cast<size_t>(inputC)});
427-
}
362+
if (m_resizedBatch[b].size() != scaleSize)
363+
m_resizedBatch[b] = cv::Mat(scaleSize, sampleImages[b].type(), cv::Scalar::all(128));
364+
cv::resize(sampleImages[b], cv::Mat(m_resizedBatch[b], roiRect), roiRect.size(), 0, 0, cv::INTER_LINEAR);
365+
cv::split(m_resizedBatch[b], m_inputChannels[b]);
366+
std::swap(m_inputChannels[b][0], m_inputChannels[b][2]);
428367
}
429-
368+
#else
430369
auto scaleSize = cv::Size(inputW, inputH);
431370

432-
if (m_resizedBatch.size() < mSampleImage.size())
433-
m_resizedBatch.resize(mSampleImage.size());
371+
if (m_resizedBatch.size() < sampleImages.size())
372+
m_resizedBatch.resize(sampleImages.size());
434373

435374
// Each element in batch share the same image matrix
436375
for (int b = 0; b < inputB; ++b)
437376
{
438-
cv::resize(mSampleImage[b], m_resizedBatch[b], scaleSize, 0, 0, cv::INTER_LINEAR);
377+
cv::resize(sampleImages[b], m_resizedBatch[b], scaleSize, 0, 0, cv::INTER_LINEAR);
439378
cv::split(m_resizedBatch[b], m_inputChannels[b]);
440379
std::swap(m_inputChannels[b][0], m_inputChannels[b][2]);
441380
}
381+
#endif
442382

443383
int volBatch = inputC * inputH * inputW;
444384
int volChannel = inputH * inputW;

src/Detector/tensorrt_yolo/YoloONNX.hpp

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,6 @@ class YoloONNX
6363
//!
6464
bool Init(const SampleYoloParams& params);
6565

66-
//!
67-
//! \brief Runs the TensorRT inference engine for this sample
68-
//!
69-
bool Detect(cv::Mat frame, std::vector<tensor_rt::Result>& bboxes);
70-
7166
//!
7267
//! \brief Runs the TensorRT inference engine for this sample
7368
//!
@@ -110,8 +105,7 @@ class YoloONNX
110105
//!
111106
//! \brief Reads the input and mean data, preprocesses, and stores the result in a managed buffer
112107
//!
113-
bool ProcessInputAspectRatio(const cv::Mat &mSampleImage);
114-
bool ProcessInputAspectRatio(const std::vector<cv::Mat>& mSampleImage);
108+
bool ProcessInputAspectRatio(const std::vector<cv::Mat>& sampleImages);
115109

116110
//!
117111
//! \brief Filters output detections and verify results

src/Detector/tensorrt_yolo/class_detector.cpp

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,16 +163,7 @@ namespace tensor_rt
163163
if (vec_batch_result.capacity() < mat_image.size())
164164
vec_batch_result.reserve(mat_image.size());
165165

166-
#if 1
167166
m_detector->Detect(mat_image, vec_batch_result);
168-
#else
169-
for (const cv::Mat& frame : mat_image)
170-
{
171-
std::vector<tensor_rt::Result> bboxes;
172-
m_detector.Detect(frame, bboxes);
173-
vec_batch_result.emplace_back(bboxes);
174-
}
175-
#endif
176167
}
177168

178169
cv::Size GetInputSize() const override

0 commit comments

Comments
 (0)