Skip to content

Commit 9ec7935

Browse files
committed
Speedup frame to blob conversion
1 parent 57acda7 commit 9ec7935

9 files changed

Lines changed: 69 additions & 62 deletions

File tree

src/Detector/tensorrt_yolo/calibrator.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ bool Int8EntropyCalibrator::getBatch(void* bindings[], const char* names[], int
7373
}
7474
m_ImageIndex += m_BatchSize;
7575

76-
cv::Mat trtInput = blobFromDsImages(dsImages, m_InputH, m_InputW);
76+
blobFromDsImages(dsImages, m_blob, m_InputH, m_InputW);
7777

78-
NV_CUDA_CHECK(cudaMemcpy(m_DeviceInput, trtInput.ptr<float>(0), m_InputCount * sizeof(float),
78+
NV_CUDA_CHECK(cudaMemcpy(m_DeviceInput, m_blob.ptr<float>(0), m_InputCount * sizeof(float),
7979
cudaMemcpyHostToDevice));
8080
assert(!strcmp(names[0], m_InputBlobName.c_str()));
8181
bindings[0] = m_DeviceInput;

src/Detector/tensorrt_yolo/calibrator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,8 @@ class Int8EntropyCalibrator : public nvinfer1::IInt8EntropyCalibrator2
5757
void* m_DeviceInput{nullptr};
5858
std::vector<std::string> m_ImageList;
5959
std::vector<char> m_CalibrationCache;
60+
61+
cv::Mat m_blob;
6062
};
6163

6264
#endif

src/Detector/tensorrt_yolo/class_yolo_detector.hpp

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,13 @@ class YoloDectector
4545
{
4646
vec_ds_images.emplace_back(img, _vec_net_type[_config.net_type], _p_net->getInputH(), _p_net->getInputW());
4747
}
48-
cv::Mat trtInput = blobFromDsImages(vec_ds_images, _p_net->getInputH(),_p_net->getInputW());
49-
_p_net->doInference(trtInput.data, static_cast<uint32_t>(vec_ds_images.size()));
48+
blobFromDsImages(vec_ds_images, m_blob, _p_net->getInputH(),_p_net->getInputW());
49+
_p_net->doInference(m_blob.data, static_cast<uint32_t>(vec_ds_images.size()));
5050
for (size_t i = 0; i < vec_ds_images.size(); ++i)
5151
{
5252
auto curImage = vec_ds_images.at(i);
5353
auto binfo = _p_net->decodeDetections(static_cast<int>(i), curImage.getImageHeight(), curImage.getImageWidth());
54-
auto remaining = nmsAllClasses(_p_net->getNMSThresh(),
55-
binfo,
56-
_p_net->getNumClasses(),
57-
_vec_net_type[_config.net_type]);
54+
auto remaining = (_p_net->getNMSThresh() > 0) ? nmsAllClasses(_p_net->getNMSThresh(), binfo, _p_net->getNumClasses(), _vec_net_type[_config.net_type]) : binfo;
5855

5956
std::vector<tensor_rt::Result> vec_result;
6057
if (!remaining.empty())
@@ -141,6 +138,7 @@ class YoloDectector
141138
std::vector<std::string> _vec_precision{ "kINT8","kHALF","kFLOAT" };
142139
std::unique_ptr<Yolo> _p_net = nullptr;
143140
Timer _m_timer;
141+
cv::Mat m_blob;
144142
};
145143

146144

src/Detector/tensorrt_yolo/ds_image.cpp

Lines changed: 4 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,25 +32,7 @@ namespace fs = std::filesystem;
3232
namespace fs = std::experimental::filesystem;
3333
#endif
3434

35-
DsImage::DsImage() :
36-
m_Height(0),
37-
m_Width(0),
38-
m_XOffset(0),
39-
m_YOffset(0),
40-
m_ScalingFactor(0.0),
41-
m_RNG(cv::RNG(unsigned(std::time(0)))),
42-
m_ImageName()
43-
{
44-
}
45-
46-
DsImage::DsImage(const cv::Mat& mat_image_, const std::string &s_net_type_, const int& inputH, const int& inputW) :
47-
m_Height(0),
48-
m_Width(0),
49-
m_XOffset(0),
50-
m_YOffset(0),
51-
m_ScalingFactor(0.0),
52-
m_RNG(cv::RNG(unsigned(std::time(0)))),
53-
m_ImageName()
35+
DsImage::DsImage(const cv::Mat& mat_image_, const std::string &s_net_type_, const int& inputH, const int& inputW)
5436
{
5537
m_OrigImage = mat_image_;
5638
m_Height = m_OrigImage.rows;
@@ -99,16 +81,9 @@ DsImage::DsImage(const cv::Mat& mat_image_, const std::string &s_net_type_, cons
9981
// converting to RGB
10082
//cv::cvtColor(m_LetterboxImage, m_LetterboxImage, cv::COLOR_BGR2RGB);
10183
}
102-
10384
}
104-
DsImage::DsImage(const std::string& path, const std::string &s_net_type_, const int& inputH, const int& inputW) :
105-
m_Height(0),
106-
m_Width(0),
107-
m_XOffset(0),
108-
m_YOffset(0),
109-
m_ScalingFactor(0.0),
110-
m_RNG(cv::RNG(unsigned(std::time(0)))),
111-
m_ImageName()
85+
86+
DsImage::DsImage(const std::string& path, const std::string &s_net_type_, const int& inputH, const int& inputW)
11287
{
11388
m_ImageName = fs::path(path).stem().string();
11489
m_OrigImage = cv::imread(path, cv::IMREAD_UNCHANGED);
@@ -190,13 +165,11 @@ void DsImage::letterbox(const int& inputH, const int& inputW)
190165
assert(2 * m_YOffset + resizeH == inputH);
191166

192167
// resizing
193-
cv::resize(m_OrigImage, m_LetterboxImage, cv::Size(resizeW, resizeH), 0, 0, cv::INTER_CUBIC);
168+
cv::resize(m_OrigImage, m_LetterboxImage, cv::Size(resizeW, resizeH), 0, 0, cv::INTER_LINEAR);
194169
// letterboxing
195170
cv::copyMakeBorder(m_LetterboxImage, m_LetterboxImage, m_YOffset, m_YOffset, m_XOffset,
196171
m_XOffset, cv::BORDER_CONSTANT, cv::Scalar(128, 128, 128));
197172
// cv::imwrite("letter.jpg", m_LetterboxImage);
198-
// converting to RGB
199-
cv::cvtColor(m_LetterboxImage, m_LetterboxImage, cv::COLOR_BGR2RGB);
200173
}
201174

202175
void DsImage::addBBox(BBoxInfo box, const std::string& labelName)

src/Detector/tensorrt_yolo/ds_image.h

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,27 +32,28 @@ struct BBoxInfo;
3232
class DsImage
3333
{
3434
public:
35-
DsImage();
35+
DsImage() = default;
3636
DsImage(const std::string& path, const std::string &s_net_type_, const int& inputH, const int& inputW);
3737
DsImage(const cv::Mat& mat_image_, const std::string &s_net_type_, const int& inputH, const int& inputW);
3838
int getImageHeight() const { return m_Height; }
3939
int getImageWidth() const { return m_Width; }
40-
cv::Mat getLetterBoxedImage() const { return m_LetterboxImage; }
40+
const cv::Mat& getLetterBoxedImage() const { return m_LetterboxImage; }
4141
cv::Mat getOriginalImage() const { return m_OrigImage; }
4242
std::string getImageName() const { return m_ImageName; }
4343
void addBBox(BBoxInfo box, const std::string& labelName);
4444
void showImage() const;
4545
void saveImageJPEG(const std::string& dirPath) const;
4646
std::string exportJson() const;
4747
void letterbox(const int& inputH, const int& inputW);
48+
4849
private:
49-
int m_Height;
50-
int m_Width;
51-
int m_XOffset;
52-
int m_YOffset;
53-
float m_ScalingFactor;
50+
int m_Height = 0;
51+
int m_Width = 0;
52+
int m_XOffset = 0;
53+
int m_YOffset = 0;
54+
float m_ScalingFactor = 0.0f;
5455
std::string m_ImagePath;
55-
cv::RNG m_RNG;
56+
cv::RNG m_RNG { cv::RNG(unsigned(std::time(0))) };
5657
std::string m_ImageName;
5758
std::vector<BBoxInfo> m_Bboxes;
5859

src/Detector/tensorrt_yolo/plugin_factory.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,6 @@ namespace nvinfer1
114114

115115
void YoloLayer::configureWithFormat(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs, DataType type, PluginFormat format, int maxBatchSize) noexcept
116116
{
117-
118117
}
119118

120119
IPluginV2* YoloLayer::clone() const noexcept

src/Detector/tensorrt_yolo/trt_utils.cpp

Lines changed: 43 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,17 +42,52 @@ REGISTER_TENSORRT_PLUGIN(ChunkPluginCreator);
4242
REGISTER_TENSORRT_PLUGIN(HardswishPluginCreator);
4343
REGISTER_TENSORRT_PLUGIN(YoloLayerPluginCreator);
4444

45-
cv::Mat blobFromDsImages(const std::vector<DsImage>& inputImages,
46-
const int& inputH,
47-
const int& inputW)
45+
void blobFromDsImages(const std::vector<DsImage>& inputImages, cv::Mat& blob, const int& inputH, const int& inputW)
4846
{
49-
std::vector<cv::Mat> letterboxStack;
50-
letterboxStack.reserve(inputImages.size());
51-
for (uint32_t i = 0; i < inputImages.size(); ++i)
47+
cv::Size size(inputW, inputH);
48+
constexpr bool swapRB = true;
49+
constexpr int ddepth = CV_32F;
50+
constexpr int nch = 3;
51+
size_t nimages = inputImages.size();
52+
53+
int sz[] = { (int)nimages, nch, inputH, inputW };
54+
blob.create(4, sz, ddepth);
55+
cv::Mat ch[4];
56+
57+
for (size_t i = 0; i < nimages; ++i)
5258
{
53-
letterboxStack.emplace_back(inputImages[i].getLetterBoxedImage());
59+
const cv::Mat& image = inputImages[i].getLetterBoxedImage();
60+
61+
for (int j = 0; j < nch; ++j)
62+
{
63+
ch[j] = cv::Mat(size, ddepth, blob.ptr((int)i, j));
64+
}
65+
66+
if(swapRB)
67+
std::swap(ch[0], ch[2]);
68+
69+
for (int y = 0; y < image.rows; ++y)
70+
{
71+
const uchar* imPtr = image.ptr(y);
72+
float* ch0 = ch[0].ptr<float>(y);
73+
float* ch1 = ch[1].ptr<float>(y);
74+
float* ch2 = ch[2].ptr<float>(y);
75+
constexpr size_t stepSize = 32;
76+
for (int x = 0; x < image.cols; x += stepSize)
77+
{
78+
for (size_t k = 0; k < stepSize; ++k)
79+
{
80+
ch0[k] = static_cast<float>(imPtr[0 + 3 * k]);
81+
ch1[k] = static_cast<float>(imPtr[1 + 3 * k]);
82+
ch2[k] = static_cast<float>(imPtr[2 + 3 * k]);
83+
}
84+
imPtr += 3 * stepSize;
85+
ch0 += stepSize;
86+
ch1 += stepSize;
87+
ch2 += stepSize;
88+
}
89+
}
5490
}
55-
return cv::dnn::blobFromImages(letterboxStack, 1.0, cv::Size(inputW, inputH), cv::Scalar(0.0, 0.0, 0.0),true);
5691
}
5792

5893
static void leftTrim(std::string& s)

src/Detector/tensorrt_yolo/trt_utils.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,8 +131,7 @@ class Logger : public nvinfer1::ILogger
131131
//};
132132

133133
// Common helper functions
134-
cv::Mat blobFromDsImages(const std::vector<DsImage>& inputImages, const int& inputH,
135-
const int& inputW);
134+
void blobFromDsImages(const std::vector<DsImage>& inputImages, cv::Mat& blob, const int& inputH, const int& inputW);
136135
std::string trim(std::string s);
137136
std::string triml(std::string s, const char* t);
138137
std::string trimr(std::string s, const char* t);

src/Detector/tensorrt_yolo/yolo.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ class Yolo
130130
const std::string m_InputBlobName;
131131
std::vector<TensorInfo> m_OutputTensors;
132132
std::vector<std::map<std::string, std::string>> m_configBlocks;
133-
uint32_t m_InputH;
134-
uint32_t m_InputW;
135-
uint32_t m_InputC;
136-
uint64_t m_InputSize;
133+
uint32_t m_InputH = 0;
134+
uint32_t m_InputW = 0;
135+
uint32_t m_InputC = 0;
136+
uint64_t m_InputSize = 0;
137137
uint32_t _n_classes = 0;
138138
float _f_depth_multiple = 0;
139139
float _f_width_multiple = 0;

0 commit comments

Comments
 (0)