Skip to content
tkdyun.xyz

AnimeGAN for Jetson

PyTorch → ONNX → TensorRT FP16 on Jetson Orin Nano

Generative AI

Year
2025
Role
Solo • Intern · ALLAI
Stack
AnimeGANv2 · TensorRT · PyCUDA · Docker · JetPack
View on GitHub

Overview

AnimeGAN transforms real-world photos into artistic images mimicking animation styles such as Shinkai Makoto's. Running generative models like AnimeGANv2 on edge devices requires optimization. Converting the PyTorch model to TensorRT with FP16 precision maximizes performance on the Jetson Orin Nano (8 GB).

Preview

AnimeGAN result
AnimeGAN result 1AnimeGAN result 3

1 — Environment Setup

Built on JetPack 5.1.2 using a Jetson-optimized Docker image from NVIDIA NGC.

docker build -t ur-image:v1 .
sudo docker run -it --rm --net=host \
  --runtime nvidia \
  -e DISPLAY=$DISPLAY \
  -v /tmp/.X11-unix/:/tmp/.X11-unix \
  -v ~/work:/workspace \
  ur-image:v1

Dockerfile is based on nvcr.io/nvidia/l4t-jetpack:r35.4.1 with PyTorch 2.1.0 for aarch64.

2 — PyTorch → ONNX

python3 onnx_convert.py \
  --model ./weights/paprika.pt \
  --output ./weights/model.onnx

Dynamic axes are defined to support variable batch sizes and image resolutions:

x = torch.randn(1, 3, 256, 256, requires_grad=True).to(device)
torch.onnx.export(
    net, x, args.output,
    export_params=True,
    input_names=['input'], output_names=['output'],
    dynamic_axes={
        'input' : {0: 'batch_size', 2: 'height', 3: 'width'},
        'output': {0: 'batch_size', 2: 'height', 3: 'width'}
    }
)

3 — TensorRT Engine

trtexec --onnx=model.onnx \
        --saveEngine=model.engine \
        --minShapes=input:1x3x64x64 \
        --optShapes=input:1x3x256x256 \
        --maxShapes=input:1x3x512x512 \
        --fp16

--fp16 uses 16-bit precision, drastically reducing memory footprint and increasing inference speed with minimal impact on output quality.

4 — TensorRT Inference

  1. Deserialize the .engine file into the TensorRT runtime
  2. Allocate PyCUDA memory buffers on GPU for input and output
  3. Preprocess: normalize (RGB, dataset mean/std), copy host → device
  4. Execute context asynchronously
  5. Copy result device → host, un-normalize, save as image
python3 engine_test.py

The FP16 TensorRT engine makes AnimeGANv2 significantly more lightweight and faster, suitable for real-time edge deployment within the memory constraints of the Orin Nano.

Search the site

Choose a destination, or type to search.