ResizeWithPadPlugin — Aspect-Ratio-Preserving Resize with Padding
Nobody has claimed this yet.
- Dominant language
- C++
- Stars
- 13.4k
- Forks
- 2.4k
- Avg merge
- 5d 3h
- Merged PRs (30d)
- 2
Description
Fature Request: ResizeWithPadPlugin — Aspect-Ratio-Preserving Resize with Padding
Summary
Add a new first-class TensorRT plugin ResizeWithPadPlugin that performs resize-with-padding (letterbox) entirely on the GPU inside the TRT graph, preserving the input aspect ratio by filling the unused canvas with a configurable constant color.
Motivation
Aspect-ratio-preserving resize is a fundamental preprocessing step for a wide range of vision models (YOLO, DETR, RT-DETR, EfficientDet, and others). Today there is no official way to do this inside a TensorRT engine:
IResizeLayerresizes but has no padding concept.IPaddingLayerpads but does not compute the scale factor needed to preserve the aspect ratio.cropAndResizePlugin/cropAndResizeDynamicPlugincrop and resize but distort the aspect ratio (see issue #4271, where a maintainer confirmed no trivial solution exists in the current API).
As a result, every deployment team rolls their own CUDA kernel outside the TRT graph, losing the opportunity for graph-level fusion, profiling, and engine portability.
Proposed Plugin: ResizeWithPadPlugin
Name follows the TensorRT OSS convention (operation-first, no model-specific branding).
Inputs
| Index | Description |
|---|---|
| 0 | Input image tensor — [N, C, H_in, W_in] or [N, H_in, W_in, C], uint8 / fp16 / fp32 |
Outputs
| Index | Description |
|---|---|
| 0 | Padded output tensor — [N, C, H_out, W_out] |
| 1 (optional) | Scale factor + pad offsets — [N, 4] as (scale, pad_top, pad_left, 0) for bbox remapping |
Attributes
| Attribute | Type | Default | Description |
|---|---|---|---|
output_h |
int | required | Target height |
output_w |
int | required | Target width |
pad_value |
float[3] | [114, 114, 114] |
Fill color (YOLO default; user-configurable) |
interpolation |
enum | kLINEAR |
kNEAREST or kLINEAR |
layout |
enum | kNCHW |
kNCHW or kNHWC |
pad_mode |
enum | kCENTER |
kCENTER or kTOP_LEFT |
swap_rb |
bool | false |
Fused BGR↔RGB channel swap |
normalize |
bool | false |
Fused division by 255.0 |
Key design decisions
- Output 1 (scale + offsets) makes the plugin self-contained for detection pipelines — downstream code can remap bounding boxes back to original image coordinates without recomputing the scale externally.
- Fused
swap_rbandnormalizeare opt-in attributes that allow a single kernel launch to replace the resize + channel-swap + normalize sequence common in all YOLO-family deployments, with no intermediate buffers. - Dynamic shapes —
H_inandW_inare dynamic; onlyH_out/W_outare fixed attributes, matching real deployment where the network input size is fixed but source images vary.
Implementation Sketch
A single CUDA kernel per batch item:
- Compute
scale = min(H_out / H_in, W_out / W_in) - Compute scaled dimensions and padding offsets
- Fill output canvas with
pad_value - Bilinear (or nearest) sample from input into the scaled region
- Optionally swap channels and normalize in the same pass
- Write scale + offsets to output 1 if requested
The kernel accesses input pixels with coalesced reads (NHWC input) or a transposed access pattern (NCHW input) and writes output in NCHW layout.
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
The proposal does not name files, tests, or an entry point. Start by locating existing TensorRT OSS plugin implementations and their handling of dynamic shapes; done means a plugin matching the listed attributes, layouts, outputs, and resize-with-padding behavior, with coverage for the specified modes.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- machine-learning
- Issue type
- Feature
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100