chore: import upstream snapshot with attribution
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
Docker Image CI / build-ubuntu2004 (push) Has been cancelled
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
#
|
||||
# SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
|
||||
add_plugin_source(
|
||||
cropAndResizePlugin.cpp
|
||||
cropAndResizePlugin.h
|
||||
)
|
||||
|
||||
@@ -0,0 +1,138 @@
|
||||
#
|
||||
# SPDX-FileCopyrightText: Copyright (c) 2022-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
# 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.
|
||||
#
|
||||
---
|
||||
name: CropAndResizeDynamic
|
||||
versions:
|
||||
"1": # Legacy Ver 1
|
||||
interface: "IPluginV2DynamicExt"
|
||||
inputs:
|
||||
- feature_maps
|
||||
- rois
|
||||
outputs:
|
||||
- pfmap
|
||||
input_dims:
|
||||
input1: 4
|
||||
input2: 4
|
||||
input_dim_constraints:
|
||||
- "feature_maps_0 == rois_0"
|
||||
- "rois_2 == 4"
|
||||
- "rois_3 == 1"
|
||||
output_dims:
|
||||
pfmap: "feature_maps_0, rois_1, feature_maps_1, crop_width_0, crop_height_0"
|
||||
attributes:
|
||||
- crop_height
|
||||
- crop_width
|
||||
attribute_types:
|
||||
crop_height: int32
|
||||
crop_width: int32
|
||||
attribute_length:
|
||||
crop_height: 1
|
||||
crop_width: 1
|
||||
attribute_options:
|
||||
crop_height:
|
||||
min: "0"
|
||||
max: "=pinf"
|
||||
crop_width:
|
||||
min: "0"
|
||||
max: "=pinf"
|
||||
attributes_required:
|
||||
- crop_height
|
||||
- crop_width
|
||||
golden_io_path: "plugin/CropAndResizeDynamic_PluginGoldenIO.json"
|
||||
abs_tol: 1e-6
|
||||
rel_tol: 1e-6
|
||||
fp16_atol: 1e-3
|
||||
fp16_rtol: 1e-3
|
||||
configs:
|
||||
config_base:
|
||||
input_types:
|
||||
feature_maps: "float32"
|
||||
rois: "float32"
|
||||
attribute_options:
|
||||
crop_height:
|
||||
value: 7
|
||||
crop_width:
|
||||
value: 7
|
||||
config_larger:
|
||||
input_types:
|
||||
feature_maps: "float32"
|
||||
rois: "float32"
|
||||
attribute_options:
|
||||
crop_height:
|
||||
value: 14
|
||||
crop_width:
|
||||
value: 14
|
||||
"2": # New Ver 2 (using IPluginV3 interface)
|
||||
interface: "IPluginV3"
|
||||
inputs:
|
||||
- feature_maps
|
||||
- rois
|
||||
outputs:
|
||||
- pfmap
|
||||
input_dims:
|
||||
input1: 4
|
||||
input2: 4
|
||||
input_dim_constraints:
|
||||
- "feature_maps_0 == rois_0"
|
||||
- "rois_2 == 4"
|
||||
- "rois_3 == 1"
|
||||
output_dims:
|
||||
pfmap: "feature_maps_0, rois_1, feature_maps_1, crop_width_0, crop_height_0"
|
||||
attributes:
|
||||
- crop_height
|
||||
- crop_width
|
||||
attribute_types:
|
||||
crop_height: int32
|
||||
crop_width: int32
|
||||
attribute_length:
|
||||
crop_height: 1
|
||||
crop_width: 1
|
||||
attribute_options:
|
||||
crop_height:
|
||||
min: "0"
|
||||
max: "=pinf"
|
||||
crop_width:
|
||||
min: "0"
|
||||
max: "=pinf"
|
||||
attributes_required:
|
||||
- crop_height
|
||||
- crop_width
|
||||
golden_io_path: "plugin/CropAndResizeDynamic_PluginGoldenIO.json"
|
||||
abs_tol: 1e-6
|
||||
rel_tol: 1e-6
|
||||
fp16_atol: 1e-3
|
||||
fp16_rtol: 1e-3
|
||||
configs:
|
||||
config_base:
|
||||
input_types:
|
||||
feature_maps: "float32"
|
||||
rois: "float32"
|
||||
attribute_options:
|
||||
crop_height:
|
||||
value: 7
|
||||
crop_width:
|
||||
value: 7
|
||||
config_larger:
|
||||
input_types:
|
||||
feature_maps: "float32"
|
||||
rois: "float32"
|
||||
attribute_options:
|
||||
crop_height:
|
||||
value: 14
|
||||
crop_width:
|
||||
value: 14
|
||||
...
|
||||
@@ -0,0 +1,98 @@
|
||||
# cropAndResize Plugin
|
||||
|
||||
**Table Of Contents**
|
||||
- [Description](#description)
|
||||
* [Structure](#structure)
|
||||
- [Parameters](#parameters)
|
||||
- [Additional resources](#additional-resources)
|
||||
- [License](#license)
|
||||
- [Changelog](#changelog)
|
||||
- [Known issues](#known-issues)
|
||||
|
||||
## Description
|
||||
|
||||
> NOTE: The `IPluginV2Ext` and `IPluginV2DynamicExt` versions of this plugin are deprecated since TensorRT 9.0 and 10.11 respectively. The `IPluginV3`-based `CropAndResizeDynamic` (version: 2) is recommended instead. See [Plugin versions](#plugin-versions) for more details.
|
||||
|
||||
The `cropAndResizePlugin` performs object detection for the Faster R-CNN model. This plugin is included in TensorRT.
|
||||
|
||||
`cropAndResizePlugin` implements the TensorFlow style of ROIPooling(a.k.a. CropAndResize). It crops multiple region of interests(ROIs) from the input image with given ROI coordinates and then (bilinearly) resizes the cropped patches to a target spatial(width and height) size.
|
||||
|
||||
Note this implementation is different from the original Caffe implement of ROIPooling. Also, this implementation doesn't fuse the ROIPooling layer and the Proposal layer into a single layer as the `nvFasterRCNN` plugin does.
|
||||
|
||||
This plugin is optimized for the above steps and it allows you to do Faster R-CNN inference in TensorRT.
|
||||
|
||||
### Structure
|
||||
|
||||
The `cropAndResizePlugin` takes two inputs; `feature_maps` and `rois`.
|
||||
|
||||
`feature_maps`
|
||||
Input featuremaps where the patches are going to be cropped from. feature_maps has shape `[N, C, H, W]` where `N` is the batch size, `C` is the channel number, `H` is the height, `W` is the width. The feature map contains the features that we want to crop patches from and do classification after that to determine the class of each patch(potential object) and the bounding boxes deltas for that object.
|
||||
|
||||
|
||||
`rois`
|
||||
Coordinates of region of interest bounding boxes, normalized to the range of (0, 1). `rois` has shape `[N, B, 4, 1]`, where `N` is the batch size, `B` is the maximum number of region of interest bounding boxes per image(featuremap), and `4` represents and region of interest bounding box coordinates `[y_1, x_1, y_2, x_2]`. Here, `x_1` and `y_1` are the coordinates of bounding box at the top-left corner, and `x_2` and `y_2` are the coordinates of bounding box at the bottom-right corner.
|
||||
|
||||
|
||||
The `cropAndResizePlugin` generates the following one outputs:
|
||||
|
||||
`pfmap`
|
||||
ROI pooled feature map corresponding to the region of interest. `pfmap` has shape `[N, B, C, crop_height, crop_width]` where `N` is the batch size, `B` is the maximum number of region of interest bounding boxes, `C` is the number of channels in the feature map, `crop_height` is the height of ROI pooled feature map, and `crop_width` is the width of ROI pooled feature map.
|
||||
|
||||
|
||||
The ROI pooling step uses the inferred region of interest bounding boxes information to extract its corresponding regions on feature map, and does POI pooling to get uniformly shaped features from different shaped region of interest bounding boxes.
|
||||
|
||||
## Parameters
|
||||
|
||||
`cropAndResizePlugin` has plugin creator class `cropAndResizePluginCreator` and plugin class `CropAndResizePlugin`.
|
||||
|
||||
The parameters are defined below and consists of the following attributes:
|
||||
|
||||
| Type | Parameter | Description
|
||||
|----------|--------------------------|--------------------------------------------------------
|
||||
|`int` |`crop_width` |The height of the output in pixels after ROI pooling on the feature map.
|
||||
|`int` |`crop_height` |The width of the output in pixels after ROI pooling on the feature map.
|
||||
|
||||
## Plugin versions
|
||||
|
||||
It is recommended to use the `IPluginV3`-based `CropAndResizeDynamic` (version: 2) instead of the previous versions.
|
||||
The below table summarizes the plugin metadata, their differences,and their deprecation status. All versions share the same I/O and attributes.
|
||||
|
||||
| Plugin Name | Version | Parent Interface Class | Deprecated | Supports Dynamic Shapes |
|
||||
|-------------|---------|------------------------|------------|-------------------------|
|
||||
| CropAndResize | 1 | IPluginV2Ext | Yes (TensorRT 9.0) | No |
|
||||
| CropAndResizeDynamic | 1 | IPluginV2DynamicExt | Yes (TensorRT 10.11) | Yes |
|
||||
| CropAndResizeDynamic | 2 | IPluginV3 | No (Recommended) | Yes |
|
||||
|
||||
## Additional resources
|
||||
|
||||
The following resources provide a deeper understanding of the `cropAndResizePlugin` plugin:
|
||||
|
||||
**Networks:**
|
||||
- [Faster R-CNN](https://arxiv.org/abs/1506.01497)
|
||||
|
||||
**Documentation:**
|
||||
- [Original ROI Pooling Definition from Fast R-CNN](https://arxiv.org/abs/1504.08083)
|
||||
- [CropAndResize Op in TensorFlow](https://www.tensorflow.org/api_docs/cc/class/tensorflow/ops/crop-and-resize)
|
||||
- [tf.image.crop_and_resize API](https://www.tensorflow.org/api_docs/python/tf/image/crop_and_resize)
|
||||
|
||||
## License
|
||||
|
||||
For terms and conditions for use, reproduction, and distribution, see the [TensorRT Software License Agreement](https://docs.nvidia.com/deeplearning/sdk/tensorrt-sla/index.html)
|
||||
documentation.
|
||||
|
||||
|
||||
## Changelog
|
||||
|
||||
April 2025
|
||||
Migrated the CropAndResizeDynamic plugin to use IPluginV3 interface (Version "2"). Legacy implementation (Version "1") is deprecated, and is maintained for backward compatibility.
|
||||
|
||||
June 2023
|
||||
Add deprecation note for `IPluginV2Ext` version of the plugin.
|
||||
|
||||
May 2019
|
||||
This is the first release of this `README.md` file.
|
||||
|
||||
|
||||
## Known issues
|
||||
|
||||
There are no known issues in this plugin.
|
||||
@@ -0,0 +1,397 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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 "NvInfer.h"
|
||||
|
||||
#include "common/templates.h"
|
||||
#include "cropAndResizePlugin.h"
|
||||
#include <cstring>
|
||||
#include <vector>
|
||||
|
||||
namespace nvinfer1::plugin
|
||||
{
|
||||
// Plugin-specific constants
|
||||
namespace
|
||||
{
|
||||
char const* const kCROP_AND_RESIZE_DYNAMIC_PLUGIN_VERSION{"2"};
|
||||
char const* const kCROP_AND_RESIZE_DYNAMIC_PLUGIN_NAME{"CropAndResizeDynamic"};
|
||||
} // namespace
|
||||
|
||||
CropAndResizeDynamicPlugin::CropAndResizeDynamicPlugin(int32_t cropWidth, int32_t cropHeight)
|
||||
: mCropWidth(cropWidth)
|
||||
, mCropHeight(cropHeight)
|
||||
{
|
||||
}
|
||||
|
||||
CropAndResizeDynamicPlugin::CropAndResizeDynamicPlugin(
|
||||
int32_t cropWidth, int32_t cropHeight, int32_t depth, int32_t inputWidth, int32_t inputHeight, int32_t maxBoxNum)
|
||||
: mCropWidth(cropWidth)
|
||||
, mCropHeight(cropHeight)
|
||||
, mDepth(depth)
|
||||
, mInputWidth(inputWidth)
|
||||
, mInputHeight(inputHeight)
|
||||
, mNumBoxes(maxBoxNum)
|
||||
{
|
||||
}
|
||||
|
||||
CropAndResizeDynamicPlugin::~CropAndResizeDynamicPlugin() noexcept {}
|
||||
|
||||
char const* CropAndResizeDynamicPlugin::getPluginName() const noexcept
|
||||
{
|
||||
return kCROP_AND_RESIZE_DYNAMIC_PLUGIN_NAME;
|
||||
}
|
||||
|
||||
char const* CropAndResizeDynamicPlugin::getPluginVersion() const noexcept
|
||||
{
|
||||
return kCROP_AND_RESIZE_DYNAMIC_PLUGIN_VERSION;
|
||||
}
|
||||
|
||||
int32_t CropAndResizeDynamicPlugin::getNbOutputs() const noexcept
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
int32_t CropAndResizeDynamicPlugin::getOutputShapes(DimsExprs const* inputs, int32_t nbInputs,
|
||||
DimsExprs const* shapeInputs, int32_t nbShapeInputs, DimsExprs* outputs, int32_t nbOutputs,
|
||||
IExprBuilder& exprBuilder) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(outputs != nullptr);
|
||||
PLUGIN_VALIDATE(nbOutputs == 1);
|
||||
PLUGIN_VALIDATE(nbInputs == 2);
|
||||
PLUGIN_VALIDATE(inputs != nullptr);
|
||||
PLUGIN_VALIDATE(inputs[0].nbDims == 4);
|
||||
|
||||
// 5D output (N, R, C, H, W)
|
||||
outputs[0].nbDims = 5;
|
||||
outputs[0].d[0] = inputs[0].d[0]; // N(batch)
|
||||
outputs[0].d[1] = inputs[1].d[1]; // R(MaxBoxNum)
|
||||
outputs[0].d[2] = inputs[0].d[1]; // C(channel)
|
||||
outputs[0].d[3] = exprBuilder.constant(mCropHeight);
|
||||
outputs[0].d[4] = exprBuilder.constant(mCropWidth);
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return STATUS_FAILURE;
|
||||
}
|
||||
|
||||
int32_t CropAndResizeDynamicPlugin::enqueue(PluginTensorDesc const* inputDesc, PluginTensorDesc const* /* outputDesc */,
|
||||
void const* const* inputs, void* const* outputs, void* /* workspace */, cudaStream_t stream) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(inputDesc != nullptr && inputs != nullptr && outputs != nullptr);
|
||||
|
||||
// Our plugin outputs only one tensor
|
||||
void* output = outputs[0];
|
||||
|
||||
int32_t batchSize = inputDesc[0].dims.d[0];
|
||||
int32_t status = cropAndResizeInference(stream, mDepth * mInputHeight * mInputWidth * batchSize, inputs[0],
|
||||
inputs[1], batchSize, mInputHeight, mInputWidth, mNumBoxes, mCropHeight, mCropWidth, mDepth, output);
|
||||
return status;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return STATUS_FAILURE;
|
||||
}
|
||||
|
||||
size_t CropAndResizeDynamicPlugin::getWorkspaceSize(DynamicPluginTensorDesc const* inputs, int32_t nbInputs,
|
||||
DynamicPluginTensorDesc const* outputs, int32_t nbOutputs) const noexcept
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
IPluginV3* CropAndResizeDynamicPlugin::clone() noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
auto plugin = std::make_unique<CropAndResizeDynamicPlugin>(
|
||||
mCropWidth, mCropHeight, mDepth, mInputWidth, mInputHeight, mNumBoxes);
|
||||
plugin->setPluginNamespace(mNamespace.c_str());
|
||||
return plugin.release();
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
IPluginCapability* CropAndResizeDynamicPlugin::getCapabilityInterface(PluginCapabilityType type) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
if (type == PluginCapabilityType::kBUILD)
|
||||
{
|
||||
return static_cast<IPluginV3OneBuild*>(this);
|
||||
}
|
||||
if (type == PluginCapabilityType::kRUNTIME)
|
||||
{
|
||||
return static_cast<IPluginV3OneRuntime*>(this);
|
||||
}
|
||||
PLUGIN_ASSERT(type == PluginCapabilityType::kCORE);
|
||||
return static_cast<IPluginV3OneCore*>(this);
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
PluginFieldCollection const* CropAndResizeDynamicPlugin::getFieldsToSerialize() noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
mDataToSerialize.clear();
|
||||
mDataToSerialize.emplace_back(PluginField("crop_width", &mCropWidth, PluginFieldType::kINT32, 1));
|
||||
mDataToSerialize.emplace_back(PluginField("crop_height", &mCropHeight, PluginFieldType::kINT32, 1));
|
||||
|
||||
mFCToSerialize.nbFields = mDataToSerialize.size();
|
||||
mFCToSerialize.fields = mDataToSerialize.data();
|
||||
return &mFCToSerialize;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
int32_t CropAndResizeDynamicPlugin::getOutputDataTypes(
|
||||
DataType* outputTypes, int32_t nbOutputs, DataType const* inputTypes, int32_t nbInputs) const noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(inputTypes != nullptr);
|
||||
PLUGIN_VALIDATE(nbInputs == 2);
|
||||
// One output
|
||||
PLUGIN_VALIDATE(nbOutputs == 1);
|
||||
PLUGIN_VALIDATE(outputTypes != nullptr);
|
||||
outputTypes[0] = DataType::kFLOAT;
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return STATUS_FAILURE;
|
||||
}
|
||||
|
||||
int32_t CropAndResizeDynamicPlugin::onShapeChange(
|
||||
PluginTensorDesc const* inputs, int32_t nbInputs, PluginTensorDesc const* outputs, int32_t nbOutputs) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(nbInputs == 2);
|
||||
PLUGIN_VALIDATE(nbOutputs == 1);
|
||||
PLUGIN_VALIDATE(inputs != nullptr);
|
||||
|
||||
// Re-validate dimensions and update internal state if needed
|
||||
// Here we can update mDepth, mInputHeight, mInputWidth, mNumBoxes if they change
|
||||
mDepth = inputs[0].dims.d[1];
|
||||
mInputHeight = inputs[0].dims.d[2];
|
||||
mInputWidth = inputs[0].dims.d[3];
|
||||
mNumBoxes = inputs[1].dims.d[1];
|
||||
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return STATUS_FAILURE;
|
||||
}
|
||||
|
||||
IPluginV3* CropAndResizeDynamicPlugin::attachToContext(IPluginResourceContext* context) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
return clone(); // Simple clone is sufficient since no context resources are needed
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
bool CropAndResizeDynamicPlugin::supportsFormatCombination(
|
||||
int32_t pos, DynamicPluginTensorDesc const* inOut, int32_t nbInputs, int32_t nbOutputs) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
// 2 inputs, 1 outputs, so 3 input/output in total
|
||||
PLUGIN_VALIDATE(0 <= pos && pos < 3);
|
||||
PLUGIN_VALIDATE(inOut != nullptr);
|
||||
auto const* in = inOut;
|
||||
auto const* out = inOut + nbInputs;
|
||||
bool const consistentFloatPrecision = (in[0].desc.type == in[pos].desc.type);
|
||||
switch (pos)
|
||||
{
|
||||
case 0:
|
||||
return (in[0].desc.type == DataType::kFLOAT && in[0].desc.format == PluginFormat::kLINEAR
|
||||
&& consistentFloatPrecision);
|
||||
case 1:
|
||||
return (in[1].desc.type == DataType::kFLOAT && in[1].desc.format == PluginFormat::kLINEAR
|
||||
&& consistentFloatPrecision);
|
||||
case 2:
|
||||
return (out[0].desc.type == DataType::kFLOAT && out[0].desc.format == PluginFormat::kLINEAR
|
||||
&& consistentFloatPrecision);
|
||||
}
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
void CropAndResizeDynamicPlugin::setPluginNamespace(char const* libNamespace) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(libNamespace != nullptr);
|
||||
mNamespace = libNamespace;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
}
|
||||
|
||||
int32_t CropAndResizeDynamicPlugin::configurePlugin(
|
||||
DynamicPluginTensorDesc const* in, int32_t nbInputs, DynamicPluginTensorDesc const* out, int32_t nbOutputs) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
// Validate input/output counts and update internal state based on input dimensions
|
||||
PLUGIN_VALIDATE(nbInputs == 2);
|
||||
PLUGIN_VALIDATE(nbOutputs == 1);
|
||||
mDepth = in[0].desc.dims.d[1];
|
||||
mInputHeight = in[0].desc.dims.d[2];
|
||||
mInputWidth = in[0].desc.dims.d[3];
|
||||
mNumBoxes = in[1].desc.dims.d[1];
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
return STATUS_FAILURE;
|
||||
}
|
||||
return STATUS_SUCCESS;
|
||||
}
|
||||
|
||||
char const* CropAndResizeDynamicPlugin::getPluginNamespace() const noexcept
|
||||
{
|
||||
return mNamespace.c_str();
|
||||
}
|
||||
|
||||
CropAndResizeDynamicPluginCreator::CropAndResizeDynamicPluginCreator()
|
||||
{
|
||||
mPluginAttributes.clear();
|
||||
mPluginAttributes.emplace_back(PluginField("crop_width", nullptr, PluginFieldType::kINT32, 1));
|
||||
mPluginAttributes.emplace_back(PluginField("crop_height", nullptr, PluginFieldType::kINT32, 1));
|
||||
mFC.nbFields = mPluginAttributes.size();
|
||||
mFC.fields = mPluginAttributes.data();
|
||||
}
|
||||
|
||||
char const* CropAndResizeDynamicPluginCreator::getPluginName() const noexcept
|
||||
{
|
||||
return kCROP_AND_RESIZE_DYNAMIC_PLUGIN_NAME;
|
||||
}
|
||||
|
||||
char const* CropAndResizeDynamicPluginCreator::getPluginVersion() const noexcept
|
||||
{
|
||||
return kCROP_AND_RESIZE_DYNAMIC_PLUGIN_VERSION;
|
||||
}
|
||||
|
||||
PluginFieldCollection const* CropAndResizeDynamicPluginCreator::getFieldNames() noexcept
|
||||
{
|
||||
return &mFC;
|
||||
}
|
||||
|
||||
IPluginV3* CropAndResizeDynamicPluginCreator::createPlugin(
|
||||
char const* name, PluginFieldCollection const* fc, TensorRTPhase phase) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(fc != nullptr);
|
||||
PLUGIN_VALIDATE(fc->fields != nullptr);
|
||||
|
||||
validateRequiredAttributesExist({"crop_width", "crop_height"}, fc);
|
||||
|
||||
int32_t cropWidth = 0;
|
||||
int32_t cropHeight = 0;
|
||||
|
||||
for (int32_t i = 0; i < fc->nbFields; ++i)
|
||||
{
|
||||
PluginField const& field = fc->fields[i];
|
||||
PLUGIN_VALIDATE(field.type == PluginFieldType::kINT32);
|
||||
|
||||
std::string_view const fieldName{field.name};
|
||||
auto const value = *static_cast<int32_t const*>(field.data);
|
||||
|
||||
if (fieldName == "crop_width")
|
||||
{
|
||||
cropWidth = value;
|
||||
}
|
||||
else if (fieldName == "crop_height")
|
||||
{
|
||||
cropHeight = value;
|
||||
}
|
||||
}
|
||||
|
||||
PLUGIN_VALIDATE(cropWidth > 0);
|
||||
PLUGIN_VALIDATE(cropHeight > 0);
|
||||
|
||||
auto plugin = std::make_unique<CropAndResizeDynamicPlugin>(cropWidth, cropHeight);
|
||||
plugin->setPluginNamespace(mNamespace.c_str());
|
||||
return plugin.release();
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
void CropAndResizeDynamicPluginCreator::setPluginNamespace(char const* pluginNamespace) noexcept
|
||||
{
|
||||
try
|
||||
{
|
||||
PLUGIN_VALIDATE(pluginNamespace != nullptr);
|
||||
mNamespace = pluginNamespace;
|
||||
}
|
||||
catch (std::exception const& e)
|
||||
{
|
||||
caughtError(e);
|
||||
}
|
||||
}
|
||||
|
||||
char const* CropAndResizeDynamicPluginCreator::getPluginNamespace() const noexcept
|
||||
{
|
||||
return mNamespace.c_str();
|
||||
}
|
||||
|
||||
} // namespace nvinfer1::plugin
|
||||
@@ -0,0 +1,139 @@
|
||||
/*
|
||||
* SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
|
||||
#ifndef TRT_CROP_AND_RESIZE_PLUGIN_H
|
||||
#define TRT_CROP_AND_RESIZE_PLUGIN_H
|
||||
|
||||
#include "NvInferRuntime.h"
|
||||
#include "common/kernels/kernel.h"
|
||||
#include "common/plugin.h"
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
namespace nvinfer1
|
||||
{
|
||||
namespace plugin
|
||||
{
|
||||
|
||||
//!
|
||||
//! \brief CropAndResizeDynamicPlugin implements a TensorRT plugin that performs
|
||||
//! crop and resize operations on input tensors based on provided boxes.
|
||||
//!
|
||||
class CropAndResizeDynamicPlugin : public IPluginV3,
|
||||
public IPluginV3OneCore,
|
||||
public IPluginV3OneBuild,
|
||||
public IPluginV3OneRuntime
|
||||
{
|
||||
public:
|
||||
//!
|
||||
//! \brief Constructs a CropAndResizeDynamicPlugin with specified crop dimensions.
|
||||
//!
|
||||
//! \param cropWidth Width of the output crop
|
||||
//! \param cropHeight Height of the output crop
|
||||
//!
|
||||
CropAndResizeDynamicPlugin(int32_t cropWidth, int32_t cropHeight);
|
||||
|
||||
//!
|
||||
//! \brief Constructs a CropAndResizeDynamicPlugin with full parameters.
|
||||
//!
|
||||
//! \param cropWidth Width of the output crop
|
||||
//! \param cropHeight Height of the output crop
|
||||
//! \param depth Depth (channels) of the input tensor
|
||||
//! \param inputWidth Width of the input tensor
|
||||
//! \param inputHeight Height of the input tensor
|
||||
//! \param maxBoxNum Maximum number of boxes
|
||||
//!
|
||||
CropAndResizeDynamicPlugin(int32_t cropWidth, int32_t cropHeight, int32_t depth, int32_t inputWidth,
|
||||
int32_t inputHeight, int32_t maxBoxNum);
|
||||
|
||||
// It doesn't make sense to make CropAndResizeDynamicPlugin without arguments, so we delete default constructor.
|
||||
CropAndResizeDynamicPlugin() noexcept = delete;
|
||||
|
||||
~CropAndResizeDynamicPlugin() noexcept override;
|
||||
|
||||
// IPluginV3 methods
|
||||
IPluginCapability* getCapabilityInterface(PluginCapabilityType type) noexcept override;
|
||||
IPluginV3* clone() noexcept override;
|
||||
char const* getPluginName() const noexcept override;
|
||||
char const* getPluginVersion() const noexcept override;
|
||||
char const* getPluginNamespace() const noexcept override;
|
||||
void setPluginNamespace(char const* libNamespace) noexcept;
|
||||
|
||||
// IPluginV3OneCore methods
|
||||
int32_t getNbOutputs() const noexcept override;
|
||||
int32_t getOutputDataTypes(
|
||||
DataType* outputTypes, int32_t nbOutputs, DataType const* inputTypes, int32_t nbInputs) const noexcept override;
|
||||
|
||||
// IPluginV3OneBuild methods
|
||||
int32_t getOutputShapes(DimsExprs const* inputs, int32_t nbInputs, DimsExprs const* shapeInputs,
|
||||
int32_t nbShapeInputs, DimsExprs* outputs, int32_t nbOutputs, IExprBuilder& exprBuilder) noexcept override;
|
||||
bool supportsFormatCombination(
|
||||
int32_t pos, DynamicPluginTensorDesc const* inOut, int32_t nbInputs, int32_t nbOutputs) noexcept override;
|
||||
int32_t configurePlugin(DynamicPluginTensorDesc const* in, int32_t nbInputs, DynamicPluginTensorDesc const* out,
|
||||
int32_t nbOutputs) noexcept override;
|
||||
int32_t onShapeChange(PluginTensorDesc const* inputs, int32_t nbInputs, PluginTensorDesc const* outputs,
|
||||
int32_t nbOutputs) noexcept override;
|
||||
size_t getWorkspaceSize(DynamicPluginTensorDesc const* inputs, int32_t nbInputs,
|
||||
DynamicPluginTensorDesc const* outputs, int32_t nbOutputs) const noexcept override;
|
||||
|
||||
// IPluginV3OneRuntime methods
|
||||
int32_t enqueue(PluginTensorDesc const* inputDesc, PluginTensorDesc const* outputDesc, void const* const* inputs,
|
||||
void* const* outputs, void* workspace, cudaStream_t stream) noexcept override;
|
||||
IPluginV3* attachToContext(IPluginResourceContext* context) noexcept override;
|
||||
PluginFieldCollection const* getFieldsToSerialize() noexcept override;
|
||||
|
||||
private:
|
||||
int32_t mCropWidth;
|
||||
int32_t mCropHeight;
|
||||
int32_t mDepth;
|
||||
int32_t mInputWidth;
|
||||
int32_t mInputHeight;
|
||||
int32_t mNumBoxes;
|
||||
std::string mNamespace;
|
||||
|
||||
PluginFieldCollection mFCToSerialize;
|
||||
std::vector<PluginField> mDataToSerialize;
|
||||
};
|
||||
|
||||
//!
|
||||
//! \brief Creator class for CropAndResizeDynamicPlugin
|
||||
//!
|
||||
class CropAndResizeDynamicPluginCreator : public IPluginCreatorV3One
|
||||
{
|
||||
public:
|
||||
CropAndResizeDynamicPluginCreator();
|
||||
~CropAndResizeDynamicPluginCreator() override = default;
|
||||
|
||||
// IPluginCreatorV3One methods
|
||||
char const* getPluginName() const noexcept override;
|
||||
char const* getPluginVersion() const noexcept override;
|
||||
PluginFieldCollection const* getFieldNames() noexcept override;
|
||||
void setPluginNamespace(char const* pluginNamespace) noexcept;
|
||||
char const* getPluginNamespace() const noexcept override;
|
||||
IPluginV3* createPlugin(char const* name, PluginFieldCollection const* fc, TensorRTPhase phase) noexcept override;
|
||||
|
||||
private:
|
||||
PluginFieldCollection mFC;
|
||||
std::vector<PluginField> mPluginAttributes;
|
||||
std::string mNamespace;
|
||||
};
|
||||
|
||||
} // namespace plugin
|
||||
|
||||
} // namespace nvinfer1
|
||||
|
||||
#endif // TRT_CROP_AND_RESIZE_PLUGIN_H
|
||||
Reference in New Issue
Block a user