派生自 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
 
 
#include "plugin_factory.h"
#include "trt_utils.h"
 
//PluginFactory::PluginFactory() : m_ReorgLayer{nullptr}, m_RegionLayer{nullptr}
//{
//    for (int i = 0; i < m_MaxLeakyLayers; ++i) m_LeakyReLULayers[i] = nullptr;
//}
//
//nvinfer1::IPlugin* PluginFactory::createPlugin(const char* layerName, const void* serialData,
//                                               size_t serialLength)
//{
//    assert(isPlugin(layerName));
//    if (std::string(layerName).find("leaky") != std::string::npos)
//    {
//        assert(m_LeakyReLUCount >= 0 && m_LeakyReLUCount <= m_MaxLeakyLayers);
//        assert(m_LeakyReLULayers[m_LeakyReLUCount] == nullptr);
//        /*m_LeakyReLULayers[m_LeakyReLUCount]
//            = unique_ptr_INvPlugin(nvinfer1::plugin::createPReLUPlugin(serialData, serialLength));*/
//        ++m_LeakyReLUCount;
//        return m_LeakyReLULayers[m_LeakyReLUCount - 1].get();
//    }
//    else if (std::string(layerName).find("reorg") != std::string::npos)
//    {
//        assert(m_ReorgLayer == nullptr);
//        /*m_ReorgLayer = unique_ptr_INvPlugin(
//            nvinfer1::plugin::createYOLOReorgPlugin(serialData, serialLength));*/
//        return m_ReorgLayer.get();
//    }
//    else if (std::string(layerName).find("region") != std::string::npos)
//    {
//        assert(m_RegionLayer == nullptr);
//        /*m_RegionLayer = unique_ptr_INvPlugin(
//             nvinfer1::plugin::createYOLORegionPlugin(serialData, serialLength));*/
//        return m_RegionLayer.get();
//    }
//    else if (std::string(layerName).find("yolo") != std::string::npos)
//    {
//        assert(m_YoloLayerCount >= 0 && m_YoloLayerCount < m_MaxYoloLayers);
//        assert(m_YoloLayers[m_YoloLayerCount] == nullptr);
//        m_YoloLayers[m_YoloLayerCount]
//            = unique_ptr_IPlugin(new YoloLayerV3(serialData, serialLength));
//        ++m_YoloLayerCount;
//        return m_YoloLayers[m_YoloLayerCount - 1].get();
//    }
//    else
//    {
//        std::cerr << "ERROR: Unrecognised layer : " << layerName << std::endl;
//        assert(0);
//        return nullptr;
//    }
//}
//
//bool PluginFactory::isPlugin(const char* name)
//{
//    return ((std::string(name).find("leaky") != std::string::npos)
//            || (std::string(name).find("reorg") != std::string::npos)
//            || (std::string(name).find("region") != std::string::npos)
//            || (std::string(name).find("yolo") != std::string::npos));
//}
//
//void PluginFactory::destroy()
//{
//    m_ReorgLayer.reset();
//    m_RegionLayer.reset();
//
//    for (int i = 0; i < m_MaxLeakyLayers; ++i)
//    {
//        m_LeakyReLULayers[i].reset();
//    }
//
//    for (int i = 0; i < m_MaxYoloLayers; ++i)
//    {
//        m_YoloLayers[i].reset();
//    }
//
//    m_LeakyReLUCount = 0;
//    m_YoloLayerCount = 0;
//}
 
/******* Yolo Layer V3 *******/
/*****************************/
namespace nvinfer1
{
    YoloLayer::YoloLayer()
    {}
 
    YoloLayer::YoloLayer(const void* data, size_t length)
    {
        const char *d = static_cast<const char*>(data), *a = d;
        re(d, m_NumBoxes);
        re(d, m_NumClasses);
        re(d, _n_grid_h);
        re(d, _n_grid_w);
        re(d, m_OutputSize);
        assert(d = a + length);
    }
    void YoloLayer::serialize(void* buffer)const noexcept
    {
        char *d = static_cast<char*>(buffer), *a = d;
        wr(d, m_NumBoxes);
        wr(d, m_NumClasses);
        wr(d, _n_grid_h);
        wr(d, _n_grid_w);
        wr(d, m_OutputSize);
        assert(d == a + getSerializationSize());
    }
 
    bool YoloLayer::supportsFormat(DataType type, PluginFormat format) const noexcept
    {
        return (type == DataType::kFLOAT && format == PluginFormat::kLINEAR);
    }
 
    void YoloLayer::configureWithFormat(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs, DataType type, PluginFormat format, int maxBatchSize) noexcept
    {
 
    }
 
    IPluginV2* YoloLayer::clone() const noexcept
    {
        YoloLayer *p = new YoloLayer(m_NumBoxes,m_NumClasses,_n_grid_h,_n_grid_w);
        p->setPluginNamespace(_s_plugin_namespace.c_str());
        return p;
    }
 
    YoloLayer::YoloLayer(const uint32_t& numBoxes, const uint32_t& numClasses, const uint32_t& grid_h_, const uint32_t &grid_w_) :
        m_NumBoxes(numBoxes),
        m_NumClasses(numClasses),
        _n_grid_h(grid_h_),
        _n_grid_w(grid_w_)
    {
        assert(m_NumBoxes > 0);
        assert(m_NumClasses > 0);
        assert(_n_grid_h > 0);
        assert(_n_grid_w > 0);
        m_OutputSize = _n_grid_h * _n_grid_w * (m_NumBoxes * (4 + 1 + m_NumClasses));
    }
 
    int YoloLayer::getNbOutputs() const noexcept { return 1; }
 
    nvinfer1::Dims YoloLayer::getOutputDimensions(int index, const nvinfer1::Dims* inputs,
        int nbInputDims) noexcept
    {
        assert(index == 0);
        assert(nbInputDims == 1);
        return inputs[0];
    }
 
    //void YoloLayerV3::configure(const nvinfer1::Dims* inputDims, int nbInputs,
    //                            const nvinfer1::Dims* outputDims, int nbOutputs, int maxBatchSize) noexcept
    //{
    //    assert(nbInputs == 1);
    //    assert(inputDims != nullptr);
    //}
 
    int YoloLayer::initialize() noexcept { return 0; }
 
    void YoloLayer::terminate() noexcept {}
 
    size_t YoloLayer::getWorkspaceSize(int maxBatchSize) const noexcept
    {
        return 0;
    }
 
    int YoloLayer::enqueue(int batchSize,
        const void* const* inputs,
        void* * outputs,
        void* workspace,
        cudaStream_t stream) noexcept
    {
        NV_CUDA_CHECK(cudaYoloLayerV3(inputs[0], outputs[0], batchSize, _n_grid_h, _n_grid_w, m_NumClasses,
            m_NumBoxes, m_OutputSize, stream));
        return 0;
    }
 
    size_t YoloLayer::getSerializationSize()const noexcept
    {
        return sizeof(m_NumBoxes) + sizeof(m_NumClasses) + sizeof(_n_grid_w) + sizeof(_n_grid_h) + sizeof(m_OutputSize);
    }
 
 
 
 
    PluginFieldCollection YoloLayerPluginCreator::mFC{};
    std::vector<PluginField> YoloLayerPluginCreator::mPluginAttributes;
 
    YoloLayerPluginCreator::YoloLayerPluginCreator()
    {
        mPluginAttributes.clear();
 
        mFC.nbFields = mPluginAttributes.size();
        mFC.fields = mPluginAttributes.data();
    }
 
    const char* YoloLayerPluginCreator::getPluginName() const noexcept
    {
        return "YOLO_TRT";
    }
 
    const char* YoloLayerPluginCreator::getPluginVersion() const noexcept
    {
        return "1.0";
    }
 
    const PluginFieldCollection* YoloLayerPluginCreator::getFieldNames()noexcept
    {
        return &mFC;
    }
 
    IPluginV2* YoloLayerPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc)noexcept
    {
        YoloLayer* obj = new YoloLayer();
        obj->setPluginNamespace(mNamespace.c_str());
        return obj;
    }
 
    IPluginV2* YoloLayerPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength)noexcept
    {
        // This object will be deleted when the network is destroyed, which will
        // call MishPlugin::destroy()
        YoloLayer* obj = new YoloLayer(serialData, serialLength);
        obj->setPluginNamespace(mNamespace.c_str());
        return obj;
    }
 
 
    void YoloLayerPluginCreator::setPluginNamespace(const char* libNamespace)noexcept
    {
        mNamespace = libNamespace;
    }
 
    const char* YoloLayerPluginCreator::getPluginNamespace() const noexcept
    {
        return mNamespace.c_str();
    }
 
 
}