forked from dongfangduoshou123/YoloV3-TensorRT
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoloPlugin.cpp
More file actions
272 lines (232 loc) · 7.56 KB
/
yoloPlugin.cpp
File metadata and controls
272 lines (232 loc) · 7.56 KB
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
/*
created by wzq 2019 10.24
*/
/*
* Copyright (c) 2019, NVIDIA CORPORATION. All rights reserved.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "utils.h"
#include "kernel.h"
#include "yoloPlugin.h"
#include <assert.h>
#include <iostream>
using namespace nvinfer1;
using nvinfer1::plugin::Yolo;
using nvinfer1::plugin::YoloPluginCreator;
namespace
{
const char* Yolo_PLUGIN_VERSION{"1"};
const char* Yolo_PLUGIN_NAME{"Yolo_TRT"};
// Write values into buffer
template <typename T>
void write(char*& buffer, const T& val)
{
*reinterpret_cast<T*>(buffer) = val;
buffer += sizeof(T);
}
// Read values from buffer
template <typename T>
T read(const char*& buffer)
{
T val = *reinterpret_cast<const T*>(buffer);
buffer += sizeof(T);
return val;
}
} // namespace
PluginFieldCollection YoloPluginCreator::mFC{};
std::vector<PluginField> YoloPluginCreator::mPluginAttributes;
Yolo::Yolo(int numclass, int stride, int gridesize, int numanchors)
{
numanchors_ = numanchors;
numclass_ = numclass;
stride_ = stride;
gridesize_ = gridesize;
}
Yolo::Yolo(const void* buffer, size_t length)
{
const char *d = reinterpret_cast<const char*>(buffer), *a = d;
numanchors_ = read<int>(d);
numclass_ = read<int>(d);
stride_ = read<int>(d);
gridesize_ = read<int>(d);
assert(d == a + length);
}
int Yolo::getNbOutputs() const
{
return 1;
}
Dims Yolo::getOutputDimensions(int index, const Dims* inputs, int nbInputDims)
{
assert(nbInputDims == 1);
assert(inputs[0].nbDims == 3);
return inputs[0];
}
int Yolo::initialize()
{
return 0;
}
void Yolo::terminate()
{
}
size_t Yolo::getWorkspaceSize(int maxBatchSize) const
{
return 0;
}
int Yolo::enqueue(int batchSize, const void* const* inputs, void** outputs, void* workspace, cudaStream_t stream)
{
CHECK(cudaYoloLayerV3(inputs[0],outputs[0],batchSize, gridesize_,numclass_, numanchors_, gridesize_*gridesize_*numanchors_*(5 + numclass_),stream));
return 0;
}
size_t Yolo::getSerializationSize() const
{
return sizeof(numanchors_) + sizeof(numclass_) + sizeof(stride_) + sizeof(gridesize_);
}
//jie shou zou bo de cheng guo.
void Yolo::serialize(void* buffer) const
{
std::cout << "jinle\n";
*reinterpret_cast<int*>(buffer) = numanchors_;
buffer += sizeof(int);
std::cout << "buffer:" << buffer << std::endl;
*reinterpret_cast<int*>(buffer) = numclass_;
buffer += sizeof(int);
std::cout << "buffer:" << buffer << std::endl;
*reinterpret_cast<int*>(buffer) = stride_;
buffer += sizeof(int);
std::cout << "buffer:" << buffer << std::endl;
*reinterpret_cast<int*>(buffer) = gridesize_;
buffer += sizeof(int);
std::cout << "buffer:" << buffer << std::endl;
}
bool Yolo::supportsFormat(DataType type, PluginFormat format) const
{
return (type == DataType::kFLOAT && format == PluginFormat::kNCHW);
}
// Set plugin namespace
void Yolo::setPluginNamespace(const char* pluginNamespace)
{
mPluginNamespace = pluginNamespace;
}
const char* Yolo::getPluginNamespace() const
{
return mPluginNamespace;
}
// Return the DataType of the plugin output at the requested index
DataType Yolo::getOutputDataType(int index, const nvinfer1::DataType* inputTypes, int nbInputs) const
{
assert(index == 0);
return DataType::kFLOAT;
}
// Return true if output tensor is broadcast across a batch.
bool Yolo::isOutputBroadcastAcrossBatch(int outputIndex, const bool* inputIsBroadcasted, int nbInputs) const
{
return false;
}
// Return true if plugin can use input that is broadcast across batch without replication.
bool Yolo::canBroadcastInputAcrossBatch(int inputIndex) const
{
return false;
}
// Configure the layer with input and output data types.
void Yolo::configurePlugin(const Dims* inputDims, int nbInputs, const Dims* outputDims, int nbOutputs,
const DataType* inputTypes, const DataType* outputTypes, const bool* inputIsBroadcast,
const bool* outputIsBroadcast, PluginFormat floatFormat, int maxBatchSize)
{
assert(*inputTypes == DataType::kFLOAT && floatFormat == PluginFormat::kNCHW);
assert(nbInputs == 1);
assert(inputDims != nullptr);
}
// Attach the plugin object to an execution context and grant the plugin the access to some context resource.
void Yolo::attachToContext(cudnnContext* cudnnContext, cublasContext* cublasContext, IGpuAllocator* gpuAllocator)
{
}
// Detach the plugin object from its execution context.
void Yolo::detachFromContext() {}
const char* Yolo::getPluginType() const
{
return Yolo_PLUGIN_NAME;
}
const char* Yolo::getPluginVersion() const
{
return Yolo_PLUGIN_VERSION;
}
void Yolo::destroy()
{
delete this;
}
// Clone the plugin
IPluginV2Ext* Yolo::clone() const
{
// Create a new instance
IPluginV2Ext* plugin = new Yolo(numclass_,stride_,gridesize_,numanchors_);
// Set the namespace
plugin->setPluginNamespace(mPluginNamespace);
return plugin;
}
YoloPluginCreator::YoloPluginCreator()
{
mPluginAttributes.emplace_back(PluginField("numclass", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("stride", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("gridesize", nullptr, PluginFieldType::kINT32, 1));
mPluginAttributes.emplace_back(PluginField("numanchors", nullptr, PluginFieldType::kINT32, 1));
mFC.nbFields = mPluginAttributes.size();
mFC.fields = mPluginAttributes.data();
}
const char* YoloPluginCreator::getPluginName() const
{
return Yolo_PLUGIN_NAME;
}
const char* YoloPluginCreator::getPluginVersion() const
{
return Yolo_PLUGIN_VERSION;
}
const PluginFieldCollection* YoloPluginCreator::getFieldNames()
{
return &mFC;
}
IPluginV2Ext* YoloPluginCreator::createPlugin(const char* name, const PluginFieldCollection* fc)
{
assert(!strcmp(name, getPluginName()));
const PluginField* fields = fc->fields;
for (int i = 0; i < fc->nbFields; ++i)
{
const char* attrName = fields[i].name;
if (!strcmp(attrName, "numclass"))
{
assert(fields[i].type == PluginFieldType::kINT32);
numclass_ = *(static_cast<const int*>(fields[i].data));
}
else if (!strcmp(attrName, "stride"))
{
assert(fields[i].type == PluginFieldType::kINT32);
stride_ = *(static_cast<const int*>(fields[i].data));
}else if(!strcmp(attrName, "gridesize")){
assert(fields[i].type == PluginFieldType::kINT32);
gridesize_ = *(static_cast<const int*>(fields[i].data));
}else if(!strcmp(attrName, "numanchors")){
assert(fields[i].type == PluginFieldType::kINT32);
numanchors_ = *(static_cast<const int*>(fields[i].data));
}
}
Yolo* obj = new Yolo(numclass_, stride_, gridesize_, numanchors_);
obj->setPluginNamespace(mNamespace.c_str());
return obj;
}
IPluginV2Ext* YoloPluginCreator::deserializePlugin(const char* name, const void* serialData, size_t serialLength)
{
Yolo* obj = new Yolo(serialData, serialLength);
obj->setPluginNamespace(mNamespace.c_str());
return obj;
}
REGISTER_TENSORRT_PLUGIN(YoloPluginCreator);