Skip to content

Commit 2e4289c

Browse files
committed
Fix bug with batch size = 1
1 parent 20da8d3 commit 2e4289c

3 files changed

Lines changed: 33 additions & 20 deletions

File tree

example/main.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ const char* keys =
3737

3838
int main(int argc, char** argv)
3939
{
40-
Help();
41-
4240
cv::CommandLineParser parser(argc, argv, keys);
4341

42+
Help();
43+
parser.printMessage();
44+
4445
bool useOCL = parser.get<int>("gpu") != 0;
4546
cv::ocl::setUseOpenCL(useOCL);
4647
std::cout << (cv::ocl::useOpenCL() ? "OpenCL is enabled" : "OpenCL not used") << std::endl;

src/Detector/YoloDarknetDetector.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ void YoloDarknetDetector::Detect(const cv::UMat& colorFrame)
101101
else
102102
{
103103
std::vector<cv::Rect> crops = GetCrops(m_maxCropRatio, m_netSize, colorMat.size());
104+
std::cout << "Image on " << crops.size() << " crops with size " << crops.front().size() << ", input size " << m_netSize << ", batch " << m_batchSize << ", frame " << colorMat.size() << std::endl;
104105
regions_t tmpRegions;
105106
if (m_batchSize > 1)
106107
{
@@ -306,7 +307,9 @@ void YoloDarknetDetector::Detect(const std::vector<cv::UMat>& frames, std::vecto
306307
{
307308
if (frames.size() == 1)
308309
{
309-
Detect(frames[0].getMat(cv::ACCESS_READ), regions[0]);
310+
Detect(frames[0]);
311+
regions[0] = m_regions;
312+
310313
}
311314
else
312315
{

src/Detector/YoloTensorRTDetector.cpp

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ void YoloTensorRTDetector::Detect(const cv::UMat& colorFrame)
143143
else
144144
{
145145
std::vector<cv::Rect> crops = GetCrops(m_maxCropRatio, m_detector->get_input_size(), colorMat.size());
146+
std::cout << "Image on " << crops.size() << " crops with size " << crops.front().size() << ", input size " << m_detector->get_input_size() << ", batch " << m_batchSize << ", frame " << colorMat.size() << std::endl;
146147
regions_t tmpRegions;
147148
std::vector<cv::Mat> batch;
148149
batch.reserve(m_batchSize);
@@ -189,22 +190,30 @@ void YoloTensorRTDetector::Detect(const cv::UMat& colorFrame)
189190
///
190191
void YoloTensorRTDetector::Detect(const std::vector<cv::UMat>& frames, std::vector<regions_t>& regions)
191192
{
192-
std::vector<cv::Mat> batch;
193-
for (const auto& frame : frames)
194-
{
195-
batch.emplace_back(frame.getMat(cv::ACCESS_READ));
196-
}
193+
if (frames.size() == 1)
194+
{
195+
Detect(frames.front());
196+
regions[0].assign(std::begin(m_regions), std::end(m_regions));
197+
}
198+
else
199+
{
200+
std::vector<cv::Mat> batch;
201+
for (const auto& frame : frames)
202+
{
203+
batch.emplace_back(frame.getMat(cv::ACCESS_READ));
204+
}
197205

198-
std::vector<tensor_rt::BatchResult> detects;
199-
m_detector->detect(batch, detects);
200-
for (size_t i = 0; i < detects.size(); ++i)
201-
{
202-
const tensor_rt::BatchResult& dets = detects[i];
203-
for (const tensor_rt::Result& bbox : dets)
204-
{
205-
if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(bbox.id)) != std::end(m_classesWhiteList))
206-
regions[i].emplace_back(bbox.rect, T2T(bbox.id), bbox.prob);
207-
}
208-
}
209-
m_regions.assign(std::begin(regions.back()), std::end(regions.back()));
206+
std::vector<tensor_rt::BatchResult> detects;
207+
m_detector->detect(batch, detects);
208+
for (size_t i = 0; i < detects.size(); ++i)
209+
{
210+
const tensor_rt::BatchResult& dets = detects[i];
211+
for (const tensor_rt::Result& bbox : dets)
212+
{
213+
if (m_classesWhiteList.empty() || m_classesWhiteList.find(T2T(bbox.id)) != std::end(m_classesWhiteList))
214+
regions[i].emplace_back(bbox.rect, T2T(bbox.id), bbox.prob);
215+
}
216+
}
217+
m_regions.assign(std::begin(regions.back()), std::end(regions.back()));
218+
}
210219
}

0 commit comments

Comments
 (0)