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

--input

-i

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:

  1. HWC → CHW: Single image, channels-last to channels-first

  2. CHW → HWC: Reverse of above

  3. NHWC → NCHW: Batched images with layout change

CV-CUDA Operators Used

Operator

Purpose

cvcuda.reformat()

Convert between different tensor layouts

cvcuda.stack()

Add batch dimension (used alongside reformat)

Common Utilities Used

See Also