Reformat
Overview
The Reformat sample demonstrates tensor layout transformations using CV-CUDA.
Usage
Basic Usage
Run layout conversion demonstrations:
python3 reformat.py -i input.jpg
Command-Line Arguments
Argument |
Short Form |
Default |
Description |
|---|---|---|---|
|
|
tabby_tiger_cat.jpg |
Input image file path |
Implementation
Layout Conversions
# 1. Perform a reformat operation on the image to CHW
chw_image: cvcuda.Tensor = cvcuda.reformat(input_image, "CHW")
assert chw_image.shape == (channels, height, width)
# 2. Perform a reformat operation on the CHW image back to HWC
hwc_image: cvcuda.Tensor = cvcuda.reformat(chw_image, "HWC")
assert hwc_image.shape == (height, width, channels)
# 3. Add a batch dimension to the image and reformat to NCHW
nhwc_image: cvcuda.Tensor = cvcuda.stack([input_image])
nchw_image: cvcuda.Tensor = cvcuda.reformat(nhwc_image, "NCHW")
assert nchw_image.shape == (1, channels, height, width)
The sample demonstrates three key conversions:
HWC → CHW: Single image, channels-last to channels-first
CHW → HWC: Reverse of above
NHWC → NCHW: Batched images with layout change
CV-CUDA Operators Used
Operator |
Purpose |
|---|---|
Convert between different tensor layouts |
|
Add batch dimension (used alongside reformat) |
Common Utilities Used
read_image() - Load image (returns HWC)
See Also
Classification Sample - Uses reformat for model input
Stack Operator - Adding batch dimensions
Common Utilities - Helper functions