派生自 Algorithm/baseDetector

Scheaven
2021-09-08 9b1532d86c2cf48a63017f3460897d8d14b98b60
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
#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_LeakyReLUCount;
        return m_LeakyReLULayers[m_LeakyReLUCount - 1].get();
    }
    else if (std::string(layerName).find("reorg") != std::string::npos)
    {
        assert(m_ReorgLayer == nullptr);
        return m_ReorgLayer.get();
    }
    else if (std::string(layerName).find("region") != std::string::npos)
    {
        assert(m_RegionLayer == nullptr);
        return m_RegionLayer.get();
    }
    else if (std::string(layerName).find("yolo") != std::string::npos)
    {
        assert(m_SLayerCount >= 0 && m_SLayerCount < m_MaxSLayers);
        assert(m_SLayers[m_SLayerCount] == nullptr);
        m_SLayers[m_SLayerCount]
            = unique_ptr_IPlugin(new SLayerV3(serialData, serialLength));
        ++m_SLayerCount;
        return m_SLayers[m_SLayerCount - 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_MaxSLayers; ++i)
    {
        m_SLayers[i].reset();
    }
 
    m_LeakyReLUCount = 0;
    m_SLayerCount = 0;
}
 
/******* Layer V3 *******/
/*****************************/
SLayerV3::SLayerV3(const void* data, size_t length)
{
    const char *d = static_cast<const char*>(data), *a = d;
    read(d, m_NumBoxes);
    read(d, m_NumClasses);
    read(d,_n_grid_h);
    read(d,_n_grid_w);
    read(d, m_OutputSize);
    assert(d = a + length);
}
 
SLayerV3::SLayerV3(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 SLayerV3::getNbOutputs() const { return 1; }
 
nvinfer1::Dims SLayerV3::getOutputDimensions(int index, const nvinfer1::Dims* inputs,
                                                int nbInputDims)
{
    assert(index == 0);
    assert(nbInputDims == 1);
    return inputs[0];
}
 
void SLayerV3::configure(const nvinfer1::Dims* inputDims, int nbInputs,
                            const nvinfer1::Dims* outputDims, int nbOutputs, int maxBatchSize)
{
    assert(nbInputs == 1);
    assert(inputDims != nullptr);
}
 
int SLayerV3::initialize() { return 0; }
 
void SLayerV3::terminate() {}
 
size_t SLayerV3::getWorkspaceSize(int maxBatchSize) const { return 0; }
 
int SLayerV3::enqueue(int batchSize, const void* const* inputs, void** outputs, void* workspace,
                         cudaStream_t stream)
{
    NV_CUDA_CHECK(cudaSLayerV3(inputs[0], outputs[0], batchSize,_n_grid_h,_n_grid_w, m_NumClasses,
                                  m_NumBoxes, m_OutputSize, stream));
    return 0;
}
 
size_t SLayerV3::getSerializationSize()
{
    return sizeof(m_NumBoxes) + sizeof(m_NumClasses) + sizeof(_n_grid_w)+sizeof(_n_grid_h) + sizeof(m_OutputSize);
}
 
void SLayerV3::serialize(void* buffer)
{
    char *d = static_cast<char*>(buffer), *a = d;
    write(d, m_NumBoxes);
    write(d, m_NumClasses);
    write(d,_n_grid_h);
    write(d,_n_grid_w);
    write(d, m_OutputSize);
    assert(d == a + getSerializationSize());
}