派生自 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
/**
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 __PLUGIN_LAYER_H__
#define __PLUGIN_LAYER_H__
 
#include <cassert>
#include <cstring>
#include <cudnn.h>
#include <iostream>
#include <memory>
#include <vector>
#include "NvInfer.h"
 
#define NV_CUDA_CHECK(status)                                                                      \
    {                                                                                              \
        if (status != 0)                                                                           \
        {                                                                                          \
            std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \
                      << " at line " << __LINE__ << std::endl;                                     \
            abort();                                                                               \
        }                                                                                          \
    }
 
// Forward declaration of cuda kernels
cudaError_t cudaYoloLayerV3(const void* input, void* output, const uint32_t& batchSize,
    const uint32_t& n_grid_h_, const uint32_t& n_grid_w_,
    const uint32_t& numOutputClasses, const uint32_t& numBBoxes,
    uint64_t outputSize, cudaStream_t stream);
 
//class PluginFactory : public nvinfer1::IPluginFactory
//{
//
//public:
//    PluginFactory();
//    nvinfer1::IPlugin* createPlugin(const char* layerName, const void* serialData,
//                                    size_t serialLength);
//    bool isPlugin(const char* name);
//    void destroy();
//
//private:
//    static const int m_MaxLeakyLayers = 72;
//    static const int m_ReorgStride = 2;
//    static constexpr float m_LeakyNegSlope = 0.1f;
//    static const int m_NumBoxes = 5;
//    static const int m_NumCoords = 4;
//    static const int m_NumClasses = 80;
//    static const int m_MaxYoloLayers = 3;
//    int m_LeakyReLUCount = 0;
//    int m_YoloLayerCount = 0;
//    nvinfer1::plugin::RegionParameters m_RegionParameters{m_NumBoxes, m_NumCoords, m_NumClasses,
//                                                          nullptr};
//
//    struct INvPluginDeleter
//    {
//        void operator()(nvinfer1::plugin::INvPlugin* ptr)
//        {
//            if (ptr)
//            {
//                ptr->destroy();
//            }
//        }
//    };
//    struct IPluginDeleter
//    {
//        void operator()(nvinfer1::IPlugin* ptr)
//        {
//            if (ptr)
//            {
//                ptr->terminate();
//            }
//        }
//    };
//    typedef std::unique_ptr<nvinfer1::plugin::INvPlugin, INvPluginDeleter> unique_ptr_INvPlugin;
//    typedef std::unique_ptr<nvinfer1::IPlugin, IPluginDeleter> unique_ptr_IPlugin;
//
//    unique_ptr_INvPlugin m_ReorgLayer;
//    unique_ptr_INvPlugin m_RegionLayer;
//    unique_ptr_INvPlugin m_LeakyReLULayers[m_MaxLeakyLayers];
//    unique_ptr_IPlugin m_YoloLayers[m_MaxYoloLayers];
//};
namespace nvinfer1
{
    template <typename T>
    void wr(char*& buffer, const T& val)
    {
        *reinterpret_cast<T*>(buffer) = val;
        buffer += sizeof(T);
    }
 
    template <typename T>
    void re(const char*& buffer, T& val)
    {
        val = *reinterpret_cast<const T*>(buffer);
        buffer += sizeof(T);
    }
 
    class YoloLayer : public IPluginV2
    {
    public:
        explicit YoloLayer();
        YoloLayer(const void* data, size_t length);
        YoloLayer(const uint32_t& numBoxes, const uint32_t& numClasses, const uint32_t& grid_h_, const uint32_t &grid_w_);
        int getNbOutputs() const noexcept override;
        nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims* inputs,
            int nbInputDims)noexcept override;
        /*void configure(const nvinfer1::Dims* inputDims, int nbInputs, const nvinfer1::Dims* outputDims,
                       int nbOutputs, int maxBatchSize)noexcept override;*/
 
                       /*void configure(const nvinfer1::Dims* inputDims, int nbInputs,
                           const nvinfer1::Dims* outputDims, int nbOutputs, int maxBatchSize)noexcept override;*/
 
        int initialize()noexcept override;
        void terminate()noexcept override;
        size_t getWorkspaceSize(int maxBatchSize) const noexcept override;
        
        int enqueue(int batchSize, const void* const* inputs, void* * outputs, void* workspace,
            cudaStream_t stream) noexcept override;
 
        size_t getSerializationSize() const noexcept  override;
        void serialize(void* buffer) const noexcept  override;
        
        const char* getPluginType() const noexcept  override
        {
            return "YOLO_TRT";
        }
        bool supportsFormat(DataType type, PluginFormat format) const noexcept override;
        void configureWithFormat(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs, DataType type, PluginFormat format, int maxBatchSize) noexcept override;
 
        const char* getPluginVersion() const noexcept  override
        {
            return "1.0";
        }
 
        void setPluginNamespace(const char* pluginNamespace) noexcept  override
        {
            _s_plugin_namespace = pluginNamespace;
        }
        const char* getPluginNamespace() const  noexcept override
        {
            return _s_plugin_namespace.c_str();
        }
        void destroy() noexcept  override
        {
            delete this;
        }
        IPluginV2* clone() const noexcept override;
    private:
    
        std::string _s_plugin_namespace;
        uint32_t m_NumBoxes;
        uint32_t m_NumClasses;
        uint32_t m_GridSize;
        uint64_t m_OutputSize;
        uint32_t _n_grid_h;
        uint32_t _n_grid_w;
    };
 
 
 
    class YoloLayerPluginCreator : public IPluginCreator
    {
    public:
        YoloLayerPluginCreator();
 
        ~YoloLayerPluginCreator() override = default;
 
        const char* getPluginName() const noexcept  override;
 
        const char* getPluginVersion() const  noexcept override;
 
        const PluginFieldCollection* getFieldNames() noexcept  override;
 
        IPluginV2* createPlugin(const char* name, const PluginFieldCollection* fc)  noexcept override;
 
        IPluginV2* deserializePlugin(const char* name, const void* serialData, size_t serialLength) noexcept  override;
 
        void setPluginNamespace(const char* libNamespace) noexcept  override;
 
        const char* getPluginNamespace() const noexcept  override;
 
    private:
        std::string mNamespace;
        static PluginFieldCollection mFC;
        static std::vector<PluginField> mPluginAttributes;
    };
}
#endif // __PLUGIN_LAYER_H__