Stack

Overview

The Stack sample demonstrates how to combine multiple images into a batched tensor using CV-CUDA’s stack operator.

Usage

Basic Usage

python3 stack.py -i input.jpg

Command-Line Arguments

Argument

Short Form

Default

Description

--input

-i

tabby_tiger_cat.jpg

Input image file path

Implementation

Stacking Images

# 1. Generate all the images
# We will simply use the same image for all to do the stack operation
# in the real world, you would have different images/data to stack
images: list[cvcuda.Tensor] = [input_image] * 3

# 2. Perform a stack operation on the images
stacked_images: cvcuda.Tensor = cvcuda.stack(images)
assert stacked_images.shape == (3, height, width, channels)

Understanding Stack

Combines multiple tensors along a new batch dimension. For example, three HWC images of shape (480, 640, 3) become a single NHWC batch of shape (3, 480, 640, 3). All input tensors must have the same shape, data type, and layout.

Common Use Cases

Batch Inference:

images = [read_image(p) for p in image_paths]
batch = cvcuda.stack(images)
predictions = model(batch)

Batch Operations:

batch = cvcuda.stack(images)
resized_batch = cvcuda.resize(batch, (batch.shape[0], 224, 224, 3))
blurred_batch = cvcuda.gaussian(resized_batch, (5, 5), (1.0, 1.0))

CV-CUDA Operators Used

Operator

Purpose

cvcuda.stack()

Combine multiple tensors into batch

Common Utilities Used

See Also