NVIDIA DeepStream SDK API Reference

6.4 Release
yoloPlugins.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019-2021, NVIDIA CORPORATION. All rights reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
17  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20  * DEALINGS IN THE SOFTWARE.
21  */
22 
23 #ifndef __YOLO_PLUGINS__
24 #define __YOLO_PLUGINS__
25 
26 #include <cassert>
27 #include <cstring>
28 #include <cuda_runtime_api.h>
29 #include <iostream>
30 #include <memory>
31 
32 #include "NvInferPlugin.h"
33 
34 #define CHECK(status) \
35  { \
36  if (status != 0) \
37  { \
38  std::cout << "Cuda failure: " << cudaGetErrorString(status) << " in file " << __FILE__ \
39  << " at line " << __LINE__ << std::endl; \
40  abort(); \
41  } \
42  }
43 
44 namespace
45 {
46 const char* YOLOV3LAYER_PLUGIN_VERSION {"1"};
47 const char* YOLOV3LAYER_PLUGIN_NAME {"YoloLayerV3_TRT"};
48 } // namespace
49 
50 class YoloLayerV3 : public nvinfer1::IPluginV2
51 {
52 public:
53  YoloLayerV3 (const void* data, size_t length);
54  YoloLayerV3 (const uint& numBoxes, const uint& numClasses, const uint& gridSize);
55  const char* getPluginType () const noexcept override { return YOLOV3LAYER_PLUGIN_NAME; }
56  const char* getPluginVersion () const noexcept override { return YOLOV3LAYER_PLUGIN_VERSION; }
57  int getNbOutputs () const noexcept override { return 1; }
58 
59  nvinfer1::Dims getOutputDimensions (
60  int index, const nvinfer1::Dims* inputs,
61  int nbInputDims) noexcept override;
62 
63  bool supportsFormat (
64  nvinfer1::DataType type, nvinfer1::PluginFormat format) const noexcept override;
65 
66  void configureWithFormat (
67  const nvinfer1::Dims* inputDims, int nbInputs,
68  const nvinfer1::Dims* outputDims, int nbOutputs,
69  nvinfer1::DataType type, nvinfer1::PluginFormat format, int maxBatchSize) noexcept override;
70 
71  int initialize () noexcept override { return 0; }
72  void terminate () noexcept override {}
73  size_t getWorkspaceSize (int maxBatchSize) const noexcept override { return 0; }
74  int32_t enqueue (
75  int32_t batchSize, void const* const* inputs, void* const* outputs,
76  void* workspace, cudaStream_t stream) noexcept override;
77  size_t getSerializationSize() const noexcept override;
78  void serialize (void* buffer) const noexcept override;
79  void destroy () noexcept override { delete this; }
80  nvinfer1::IPluginV2* clone() const noexcept override;
81 
82  void setPluginNamespace (const char* pluginNamespace) noexcept override {
83  m_Namespace = pluginNamespace;
84  }
85  virtual const char* getPluginNamespace () const noexcept override {
86  return m_Namespace.c_str();
87  }
88 
89 private:
90  uint m_NumBoxes {0};
91  uint m_NumClasses {0};
92  uint m_GridSize {0};
93  uint64_t m_OutputSize {0};
94  std::string m_Namespace {""};
95 };
96 
97 class YoloLayerV3PluginCreator : public nvinfer1::IPluginCreator
98 {
99 public:
102 
103  const char* getPluginName () const noexcept override { return YOLOV3LAYER_PLUGIN_NAME; }
104  const char* getPluginVersion () const noexcept override { return YOLOV3LAYER_PLUGIN_VERSION; }
105 
106  const nvinfer1::PluginFieldCollection* getFieldNames() noexcept override {
107  std::cerr<< "YoloLayerV3PluginCreator::getFieldNames is not implemented" << std::endl;
108  return nullptr;
109  }
110 
111  nvinfer1::IPluginV2* createPlugin (
112  const char* name, const nvinfer1::PluginFieldCollection* fc) noexcept override
113  {
114  std::cerr<< "YoloLayerV3PluginCreator::getFieldNames is not implemented.\n";
115  return nullptr;
116  }
117 
118  nvinfer1::IPluginV2* deserializePlugin (
119  const char* name, const void* serialData, size_t serialLength) noexcept override
120  {
121  std::cout << "Deserialize yoloLayerV3 plugin: " << name << std::endl;
122  return new YoloLayerV3(serialData, serialLength);
123  }
124 
125  void setPluginNamespace(const char* libNamespace) noexcept override {
126  m_Namespace = libNamespace;
127  }
128  const char* getPluginNamespace() const noexcept override {
129  return m_Namespace.c_str();
130  }
131 
132 private:
133  std::string m_Namespace {""};
134 };
135 
136 #endif // __YOLO_PLUGINS__
YoloLayerV3::initialize
int initialize() noexcept override
Definition: yoloPlugins.h:71
YoloLayerV3::clone
nvinfer1::IPluginV2 * clone() const noexcept override
YoloLayerV3::getPluginNamespace
virtual const char * getPluginNamespace() const noexcept override
Definition: yoloPlugins.h:85
YoloLayerV3::getWorkspaceSize
size_t getWorkspaceSize(int maxBatchSize) const noexcept override
Definition: yoloPlugins.h:73
ds3d::DataType
DataType
Definition: idatatype.h:77
YoloLayerV3::getPluginVersion
const char * getPluginVersion() const noexcept override
Definition: yoloPlugins.h:56
YoloLayerV3PluginCreator::getFieldNames
const nvinfer1::PluginFieldCollection * getFieldNames() noexcept override
Definition: yoloPlugins.h:106
YoloLayerV3::supportsFormat
bool supportsFormat(nvinfer1::DataType type, nvinfer1::PluginFormat format) const noexcept override
YoloLayerV3PluginCreator::getPluginName
const char * getPluginName() const noexcept override
Definition: yoloPlugins.h:103
YoloLayerV3::YoloLayerV3
YoloLayerV3(const void *data, size_t length)
inputDims
NvDsInferDimsCHW inputDims
Holds the input dimensions for the model.
Definition: nvdsinfer_context.h:264
YoloLayerV3PluginCreator::deserializePlugin
nvinfer1::IPluginV2 * deserializePlugin(const char *name, const void *serialData, size_t serialLength) noexcept override
Definition: yoloPlugins.h:118
YoloLayerV3::destroy
void destroy() noexcept override
Definition: yoloPlugins.h:79
YoloLayerV3::configureWithFormat
void configureWithFormat(const nvinfer1::Dims *inputDims, int nbInputs, const nvinfer1::Dims *outputDims, int nbOutputs, nvinfer1::DataType type, nvinfer1::PluginFormat format, int maxBatchSize) noexcept override
YoloLayerV3
Definition: yoloPlugins.h:50
YoloLayerV3::getNbOutputs
int getNbOutputs() const noexcept override
Definition: yoloPlugins.h:57
cudaStream_t
struct CUstream_st * cudaStream_t
Forward declaration of cudaStream_t.
Definition: nvbufsurftransform.h:29
YoloLayerV3::terminate
void terminate() noexcept override
Definition: yoloPlugins.h:72
YoloLayerV3PluginCreator::setPluginNamespace
void setPluginNamespace(const char *libNamespace) noexcept override
Definition: yoloPlugins.h:125
YoloLayerV3::getPluginType
const char * getPluginType() const noexcept override
Definition: yoloPlugins.h:55
YoloLayerV3PluginCreator::~YoloLayerV3PluginCreator
~YoloLayerV3PluginCreator()
Definition: yoloPlugins.h:101
YoloLayerV3::getOutputDimensions
nvinfer1::Dims getOutputDimensions(int index, const nvinfer1::Dims *inputs, int nbInputDims) noexcept override
YoloLayerV3::setPluginNamespace
void setPluginNamespace(const char *pluginNamespace) noexcept override
Definition: yoloPlugins.h:82
YoloLayerV3PluginCreator::createPlugin
nvinfer1::IPluginV2 * createPlugin(const char *name, const nvinfer1::PluginFieldCollection *fc) noexcept override
Definition: yoloPlugins.h:111
YoloLayerV3::getSerializationSize
size_t getSerializationSize() const noexcept override
YoloLayerV3::serialize
void serialize(void *buffer) const noexcept override
YoloLayerV3PluginCreator::getPluginVersion
const char * getPluginVersion() const noexcept override
Definition: yoloPlugins.h:104
YoloLayerV3::enqueue
int32_t enqueue(int32_t batchSize, void const *const *inputs, void *const *outputs, void *workspace, cudaStream_t stream) noexcept override
YoloLayerV3PluginCreator::getPluginNamespace
const char * getPluginNamespace() const noexcept override
Definition: yoloPlugins.h:128
YoloLayerV3PluginCreator::YoloLayerV3PluginCreator
YoloLayerV3PluginCreator()
Definition: yoloPlugins.h:100
YoloLayerV3PluginCreator
Definition: yoloPlugins.h:97