-
Notifications
You must be signed in to change notification settings - Fork 3
/
trt-yolo-app.cpp
191 lines (171 loc) · 6.8 KB
/
trt-yolo-app.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/**
MIT License
Copyright (c) 2018 NVIDIA CORPORATION. All rights reserved.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*
*/
#include "ds_image.h"
#include "trt_utils.h"
#include "yolo.h"
#include "yolo_config_parser.h"
#include "yolov2.h"
#include "yolov3.h"
#include <experimental/filesystem>
#include <fstream>
#include <string>
#include <chrono>
int main(int argc, char** argv)
{
// Flag set in the command line overrides the value in the flagfile
gflags::SetUsageMessage(
"Usage : trt-yolo-app --flagfile=</path/to/config_file.txt> --<flag>=value ...");
// parse config params
yoloConfigParserInit(argc, argv);
NetworkInfo yoloInfo = getYoloNetworkInfo();
InferParams yoloInferParams = getYoloInferParams();
uint64_t seed = getSeed();
std::string networkType = getNetworkType();
std::string precision = getPrecision();
std::string testImages = getTestImages();
std::string testImagesPath = getTestImagesPath();
bool decode = getDecode();
bool doBenchmark = getDoBenchmark();
bool viewDetections = getViewDetections();
bool saveDetections = getSaveDetections();
std::string saveDetectionsPath = getSaveDetectionsPath();
uint32_t batchSize = getBatchSize();
bool shuffleTestSet = getShuffleTestSet();
srand(unsigned(seed));
std::unique_ptr<Yolo> inferNet{nullptr};
if ((networkType == "yolov2") || (networkType == "yolov2-tiny"))
{
inferNet = std::unique_ptr<Yolo>{new YoloV2(batchSize, yoloInfo, yoloInferParams)};
}
else if ((networkType == "yolov3") || (networkType == "yolov3-tiny"))
{
inferNet = std::unique_ptr<Yolo>{new YoloV3(batchSize, yoloInfo, yoloInferParams)};
}
else
{
assert(false && "Unrecognised network_type. Network Type has to be one among the following : yolov2, yolov2-tiny, yolov3 and yolov3-tiny");
}
if (testImages.empty())
{
std::cout << "Enter a valid file path for test_images config param" << std::endl;
return -1;
}
std::vector<std::string> imageList = loadImageList(testImages, testImagesPath);
std::cout << "Total number of images used for inference : " << imageList.size() << std::endl;
if (shuffleTestSet)
{
std::random_shuffle(imageList.begin(), imageList.end(), [](int i) { return rand() % i; });
}
std::vector<DsImage> dsImages;
const int barWidth = 70;
std::chrono::duration<double> inferElapsed;
std::ofstream fout;
bool written = false;
if (doBenchmark)
{
size_t extIndex = testImages.find_last_of(".txt");
fout.open(testImages.substr(0, extIndex - 3) + "_" + networkType + "_" + precision
+ "_results.json");
fout << "[";
}
// Batched inference loop
for (uint32_t loopIdx = 0; loopIdx < imageList.size(); loopIdx += batchSize)
{
// Load a new batch
dsImages.clear();
for (uint32_t imageIdx = loopIdx;
imageIdx < std::min((loopIdx + batchSize), static_cast<uint32_t>(imageList.size()));
++imageIdx)
{
dsImages.emplace_back(imageList.at(imageIdx), inferNet->getInputH(),
inferNet->getInputW());
}
cv::Mat trtInput = blobFromDsImages(dsImages, inferNet->getInputH(), inferNet->getInputW());
auto start = std::chrono::high_resolution_clock::now();
inferNet->doInference(trtInput.data, dsImages.size());
auto finish = std::chrono::high_resolution_clock::now();
inferElapsed = finish - start;
if (decode)
{
for (uint32_t imageIdx = 0; imageIdx < dsImages.size(); ++imageIdx)
{
auto curImage = dsImages.at(imageIdx);
auto binfo = inferNet->decodeDetections(imageIdx, curImage.getImageHeight(),
curImage.getImageWidth());
auto remaining
= nmsAllClasses(inferNet->getNMSThresh(), binfo, inferNet->getNumClasses());
for (auto b : remaining)
{
if (inferNet->isPrintPredictions())
{
printPredictions(b, inferNet->getClassName(b.label));
}
curImage.addBBox(b, inferNet->getClassName(b.label));
}
if (saveDetections)
{
curImage.saveImageJPEG(saveDetectionsPath);
}
if (viewDetections)
{
curImage.showImage();
}
if (doBenchmark)
{
std::string jsonString = curImage.exportJson();
if (jsonString == "") continue;
if (written)
fout << "," << jsonString;
else
fout << jsonString;
written = true;
}
}
}
std::cout << "[";
int progress = ((loopIdx + dsImages.size()) * 100) / imageList.size();
progress = progress > 100 ? 100 : progress;
int pos = (barWidth * progress) / 100;
for (int i = 0; i < pos; ++i)
{
std::cout << "=";
}
if (pos < barWidth) std::cout << ">";
for (int i = pos; i < barWidth; ++i)
{
std::cout << " ";
}
std::cout << "] " << progress << " %\r";
std::cout.flush();
}
if (doBenchmark)
{
fout << std::endl << "]";
fout.close();
std::cout << std::endl;
}
std::cout << std::endl
<< "Network Type : " << inferNet->getNetworkType() << " Precision : " << precision
<< " Batch Size : " << batchSize
<< " Inference time per image : " << inferElapsed.count() / imageList.size() << " ms"
<< std::endl;
return 0;
}