派生自 Algorithm/baseDetector

sunty
2022-03-21 d0a24896f95b4e060011852f80048ebfb0bf5f55
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
 
/**
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.
*
*/
 
#ifndef __TRT_UTILS_H__
#define __TRT_UTILS_H__
 
/* OpenCV headers */
//#include <opencv/cv.h>
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/dnn/dnn.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
 
#include "mish.h"
#include "chunk.h"
#include "hardswish.h"
#include <set>
#include <math.h>
#include <algorithm> 
#include "NvInfer.h"
 
#include "ds_image.h"
#include "plugin_factory.h"
//#include "logging.h"
 
 
 
class DsImage;
struct BBox
{
    float x1, y1, x2, y2;
};
 
struct BBoxInfo
{
    BBox box;
    int label;
    int classId; // For coco benchmarking
    float prob;
};
 
class Logger : public nvinfer1::ILogger
{
public:
    Logger(Severity severity = Severity::kWARNING)
    {
 
    }
 
    ~Logger()
    {
 
    }
    nvinfer1::ILogger& getTRTLogger()
    {
        return *this;
    }
 
    void log(nvinfer1::ILogger::Severity severity, const char* msg)  noexcept override
    {
        // suppress info-level messages
        if (severity == Severity::kINFO) return;
 
        switch (severity)
        {
        case Severity::kINTERNAL_ERROR: std::cerr << "INTERNAL_ERROR: " << msg << std::endl; break;
        case Severity::kERROR: std::cerr << "ERROR: " << msg << std::endl; break;
        case Severity::kWARNING: std::cerr << "WARNING: " << msg << std::endl; break;
        case Severity::kINFO: std::cerr << "INFO: " << msg << std::endl; break;
        case Severity::kVERBOSE: break;
      //  default: std::cerr <<"UNKNOW:"<< msg << std::endl;break;
        }
    }
};
 
//class YoloTinyMaxpoolPaddingFormula : public nvinfer1::IOutputDimensionsFormula
//{
//
//private:
//    std::set<std::string> m_SamePaddingLayers;
//
//    nvinfer1::DimsHW compute(nvinfer1::DimsHW inputDims, nvinfer1::DimsHW kernelSize,
//                             nvinfer1::DimsHW stride, nvinfer1::DimsHW padding,
//                             nvinfer1::DimsHW dilation, const char* layerName) const override
//    {
//     //   assert(inputDims.d[0] == inputDims.d[1]);
//        assert(kernelSize.d[0] == kernelSize.d[1]);
//        assert(stride.d[0] == stride.d[1]);
//        assert(padding.d[0] == padding.d[1]);
//
//        int output_h, output_w;
//        // Only layer maxpool_12 makes use of same padding
//        if (m_SamePaddingLayers.find(layerName) != m_SamePaddingLayers.end())
//        {
//            output_h = (inputDims.d[0] + 2 * padding.d[0]) / stride.d[0];
//            output_w = (inputDims.d[1] + 2 * padding.d[1]) / stride.d[1];
//        }
//        // Valid Padding
//        else
//        {
//            output_h = (inputDims.d[0] - kernelSize.d[0]) / stride.d[0] + 1;
//            output_w = (inputDims.d[1] - kernelSize.d[1]) / stride.d[1] + 1;
//        }
//        return nvinfer1::DimsHW{output_h, output_w};
//    }
//
//public:
//    void addSamePaddingLayer(std::string input) { m_SamePaddingLayers.insert(input); }
//};
 
// Common helper functions
cv::Mat blobFromDsImages(const std::vector<DsImage>& inputImages, const int& inputH,
                         const int& inputW);
std::string trim(std::string s);
std::string triml(std::string s, const char* t);
std::string trimr(std::string s, const char* t);
float clamp(const float val, const float minVal, const float maxVal);
bool fileExists(const std::string fileName, bool verbose = true);
BBox convertBBoxNetRes(const float& bx, const float& by, const float& bw, const float& bh,
                       const uint32_t& stride, const uint32_t& netW, const uint32_t& netH);
void convertBBoxImgRes(const float scalingFactor,
    const float xOffset,
    const float yOffset,
    BBox& bbox);
void printPredictions(const BBoxInfo& info, const std::string& className);
std::vector<std::string> loadListFromTextFile(const std::string filename);
std::vector<std::string> loadImageList(const std::string filename, const std::string prefix);
std::vector<BBoxInfo> diou_nms(const float numThresh, std::vector<BBoxInfo> binfo);
std::vector<BBoxInfo> nmsAllClasses(const float nmsThresh, std::vector<BBoxInfo>& binfo,
                                    const uint32_t numClasses, const std::string &model_type);
std::vector<BBoxInfo> nonMaximumSuppression(const float nmsThresh, std::vector<BBoxInfo> binfo);
nvinfer1::ICudaEngine* loadTRTEngine(const std::string planFilePath,/* PluginFactory* pluginFactory,*/
                                     Logger& logger);
std::vector<float> loadWeights(const std::string weightsFilePath, const std::string& networkType);
std::string dimsToString(const nvinfer1::Dims d);
void displayDimType(const nvinfer1::Dims d);
int getNumChannels(nvinfer1::ITensor* t);
uint64_t get3DTensorVolume(nvinfer1::Dims inputDims);
 
// Helper functions to create yolo engine
nvinfer1::ILayer* netAddMaxpool(int layerIdx, std::map<std::string, std::string>& block,
                                nvinfer1::ITensor* input, nvinfer1::INetworkDefinition* network);
nvinfer1::ILayer* netAddConvLinear(int layerIdx, std::map<std::string, std::string>& block,
                                   std::vector<float>& weights,
                                   std::vector<nvinfer1::Weights>& trtWeights, int& weightPtr,
                                   int& inputChannels, nvinfer1::ITensor* input,
                                   nvinfer1::INetworkDefinition* network);
 
nvinfer1::ILayer* net_conv_bn_mish(int layerIdx,
    std::map<std::string, std::string>& block,
    std::vector<float>& weights,
    std::vector<nvinfer1::Weights>& trtWeights,
    int& weightPtr,
    int& inputChannels,
    nvinfer1::ITensor* input,
    nvinfer1::INetworkDefinition* network);
 
nvinfer1::ILayer* netAddConvBNLeaky(int layerIdx, std::map<std::string, std::string>& block,
                                    std::vector<float>& weights,
                                    std::vector<nvinfer1::Weights>& trtWeights, int& weightPtr,
                                    int& inputChannels, nvinfer1::ITensor* input,
                                    nvinfer1::INetworkDefinition* network);
nvinfer1::ILayer* netAddUpsample(int layerIdx, std::map<std::string, std::string>& block,
                                 std::vector<float>& weights,
                                 std::vector<nvinfer1::Weights>& trtWeights, int& inputChannels,
                                 nvinfer1::ITensor* input, nvinfer1::INetworkDefinition* network);
void printLayerInfo(std::string layerIndex, std::string layerName, std::string layerInput,
                    std::string layerOutput, std::string weightPtr);
 
nvinfer1::ILayer * layer_split(const int n_layer_index_,
    nvinfer1::ITensor *input_,
    nvinfer1::INetworkDefinition* network);
 
std::vector<int> parse_int_list(const std::string s_args_);
 
nvinfer1::ILayer* layer_focus(std::vector<nvinfer1::Weights> &trtWeights_,
    std::string s_model_name_,
    std::map<std::string, std::vector<float>>& map_wts_,
    nvinfer1::ITensor* input,
    const int out_channels_,
    const int kernel_size_,
    std::vector<nvinfer1::Weights>& trtWeights,
    nvinfer1::INetworkDefinition* network);
 
nvinfer1::ILayer * layer_conv_bn_act(std::vector<nvinfer1::Weights> &trtWeights_,
    const std::string s_layer_name_,
    std::map<std::string, std::vector<float>> &vec_wts_,//conv-bn
    nvinfer1::ITensor* input_,
    nvinfer1::INetworkDefinition* network_,
    const int n_filters_,
    const int n_kernel_size_ = 3,
    const int n_stride_ = 1,
    const int group_ =1,
    const bool b_padding_ = true,
    const bool b_bn_ = true,
    const std::string s_act_ = "silu");
 
nvinfer1::ILayer * layer_act(nvinfer1::ITensor* input_,
    nvinfer1::INetworkDefinition* network_,
    const std::string s_act_ = "silu");
 
nvinfer1::ILayer * C3(std::vector<nvinfer1::Weights> &trtWeights_,
    std::string s_model_name_,
    std::map<std::string, std::vector<float>> &map_wts_,
    nvinfer1::INetworkDefinition* network_,
    nvinfer1::ITensor* input_,
    const int c2_,
    const int n_depth_ = 1,
    const bool b_short_cut_ = true,
    const int group_ = 1,
    const float e_ = 0.5);
 
nvinfer1::ILayer * layer_bottleneck_csp(std::vector<nvinfer1::Weights> &trtWeights_,
    std::string s_model_name_,
    std::map<std::string, std::vector<float>> &map_wts_,
    nvinfer1::INetworkDefinition* network_,
    nvinfer1::ITensor* input_,
    const int c2_,
    const int n_depth_ = 1,
    const bool b_short_cut_ = true,
    const int group_ = 1,
    const float e_ = 0.5);
 
nvinfer1::ILayer * layer_spp(std::vector<nvinfer1::Weights> &trtWeights_,
    std::string s_model_name_,
    std::map<std::string, std::vector<float>> &map_wts_,
    nvinfer1::INetworkDefinition* network_,
    nvinfer1::ITensor* input_,
    const int c2_,
    const std::vector<int> &vec_args_);
 
nvinfer1::ILayer * layer_sppf(std::vector<nvinfer1::Weights> &trtWeights_,
    std::string s_model_name_,
    std::map<std::string, std::vector<float>> &map_wts_,
    nvinfer1::INetworkDefinition* network_,
    nvinfer1::ITensor* input_,
    const int c2_,
    int k_);
 
nvinfer1::ILayer *layer_upsample(std::string s_model_name_,
    std::map<std::string, std::vector<float>> &map_wts_,
    nvinfer1::INetworkDefinition* network_,
    nvinfer1::ITensor* input_,
    const int n_scale_);
 
nvinfer1::ILayer * layer_conv(std::vector<nvinfer1::Weights> &trtWeights_,
    const std::string s_layer_name_,
    std::map<std::string, std::vector<float>>&vec_wts_,//conv-bn
    nvinfer1::ITensor* input_,
    nvinfer1::INetworkDefinition* network_,
    const int n_filters_,
    const int n_kernel_size_,
    const int n_stride_ = 1,
    const bool b_bias_ = false,
    const int group_ = 1,
    const bool b_padding_ = true);
std::vector<int> dims2chw(const nvinfer1::Dims d);
 
 
 
#endif