GPU Image Decoding for AI

Decode JPEG 2000 and HTJ2K batches into PyTorch tensors or NumPy arrays. Apple Metal and NVIDIA CUDA acceleration are available for supported inputs, alongside Rust integrations for Burn and MPSGraph.

Batch decoding

Frames prepares and groups encoded images, executes supported codec routes, and returns the decoded rectangles and source indices. Sampling, labels and model policy remain with the application.

Group compatible outputs without padding

Owned inputs can request full, region, reduced or region-plus-reduced decoding. The codec groups compatible dimensions, layouts and native sample types while retaining source order metadata.

Native integer NCHW or NHWC

j2k-ml returns rank-four Burn tensors as U8, U16 or I16. Float conversion and normalization remain ordinary tensor operations.

Direct or staged handoff

PyTorch supports direct MPS and CUDA output for supported inputs. Burn supports direct CUDA output; its staged Metal and CUDA routes copy through host memory.

Framework integrations

Rust / Burn

j2k-ml batch adapter

Staged Metal, direct CUDA, staged CUDA and CPU routes create Burn tensors while preserving codec group and failure information.

Apple

j2k-mpsgraph resident batches

Supported decoded batches can be handed to Apple MPSGraph without leaving the GPU. This integration adds no codec formats beyond J2K’s support boundary.

Python bindings

j2k-python PyTorch and NumPy

Use the Rust codecs from Python with NumPy and PyTorch output. Supported builds decode directly into PyTorch MPS tensors on Apple GPUs or CUDA tensors on NVIDIA GPUs. Region and reduced-resolution decoding, reusable batches and selected wavelet bands are also available.

Run a complete batch example

The examples make the dataset, batching and device-transfer ownership visible. Explicit accelerator routes return errors for unsupported inputs rather than falling back silently.

Portable BurnCPU codec
$ cargo add j2k-ml --features cpu
$ cargo run -p j2k-ml --example training_batcher --features cpu

Dataset → DataLoader → Batcher with source-index realignment and tensor normalization.

Direct CUDABurn-owned destination
$ cargo run -p j2k-ml --example cuda_direct --features cuda

Decode into the final Burn CUDA allocation without decoded-pixel host readback.