RAI 1.5 ESRGAN Inference (C++)
A C++ command-line tool for running ESRGAN (Enhanced Super-Resolution Generative Adversarial Networks) inference using ONNX Runtime with support for both CPU and AMD NPU (Neural Processing Unit) execution via VitisAI Execution Provider.
Overview
This tool performs image super-resolution using pre-trained ESRGAN models, taking low-resolution images as input and producing high-resolution upscaled outputs. It supports execution on both CPU and AMD Ryzen AI NPUs for accelerated inference.
The tool has two versions (RAI 1.4) and 1.5 (RAI 1.5)
The two versions differ in CMakeLists.txt, config files, and vai_ep options that are passed. These instructions are for the RAI 1.5.
Required Files
- ONNX Model: ESRGAN model file (
esrgan_fp32_qdq.onnx
) - Config JSON: VitisAI configuration file (
vaip_config.json
) - esrgan_cache: Precompiled cache to speed up model loading (
esrgan_cahce
) - Runtime DLLs: Ryzen AI runtime libraries (automatically copied from Ryzen AI SDK during build)
- XCLBIN Files: NPU binary files (automatically copied from Ryzen AI SDK during build)
- OpenCV Dependencies: OpenCV DLLs (included in
opencv/build/x64/vc16/bin/
)
Building the Project
The build process automatically copies all required dependencies to the output directory, making the executable completely self-contained for distribution.
Prerequisites for Building
It is recommend to use the Developer Command Prompt for Visual Studio 2022.
The path to RYZEN_AI_INSTALLATION_PATH
needs to be set properly for the code to run properly. This can be done in one of two ways:
- activating the conda environment for Ryzen AI 1.5
conda activate ryzenai-1.5
- setting the environment variable manually in the command prompt:
set RYZEN_AI_INSTALLATION_PATH=C:\Program Files\RyzenAI\1.5.0
The build script can be built using the compile.bat file, or manually using CMake.
Option 1: Using the Build Script (Recommended)
- Run the build script:
compile.bat
Option 2: Manual Build with CMAKE
Generate build files with CMake:
cmake -DCMAKE_CONFIGURATION_TYPES=Release -A x64 -T host=x64 -B build -S . -G "Visual Studio 17 2022"
Build the project:
cmake --build .\build --config Release
Source Files
src/
βββ main.cpp # Main application logic
βββ npu_util.cpp # NPU utility functions
βββ npu_util.h # NPU utility headers
βββ cxxopts.hpp # Command-line argument parsing (header-only library)
Project Root
./
βββ bird_input.png # Sample input image
βββ CMakeLists.txt # Build configuration
βββ compile.bat # Build script for Windows
βββ vaip_config.json # VitisAI EP configuration
βββ esrgan_fp32_qdq.onnx # ONNX model file
βββ esrgan_cache/ # VitisAI compilation cache (generated after first run)
βββ opencv/ # Local OpenCV installation
β βββ build/ # Pre-built OpenCV binaries and headers
βββ src/ # Source code directory
Usage
Command-Line Syntax
esrgan_inference.exe [OPTIONS]
Required Arguments
-m, --model <file>
: ONNX model filename (relative to executable directory)-c, --config <file>
: JSON configuration filename (relative to executable directory)
Optional Arguments
-i, --input_image <file>
: Input image file (default:..\..\bird_input.png
)-o, --output_image <file>
: Output image file (default:output_image.png
)-k, --cache_key <string>
: Cache key for VitisAI EP (default: empty)-d, --cache_dir <string>
: Cache directory for VitisAI EP (default: empty)-x, --xclbin <file>
: XCLBIN filename for NPU (default: auto-selected)-h, --help
: Show help message
Example Usage
esrgan_inference.exe ^
-m esrgan_fp32_qdq.onnx ^
-c vaip_config.json ^
-d . ^
-k esrgan_cache
-i ..\..\bird_input.png ^
-o output_image.png ^
This example demonstrates:
- Using model and config files from the build directory (automatically copied)
- Relative paths for input images (from project root)
- Using the existing supplied VitisAI cache directory for faster subsequent runs
Note: The esrgan_cache/
directory is created automatically during the first NPU inference run if the enable_cache_file_io_in_mem
is set to 0
in the vai_ep_options
If this option isn't set, the cache will be stored in memory only and the model will be recompiled every time the application is run, which can be slow.
The esrgan_cache
directory contains compiled model artifacts that significantly speed up subsequent runs. You can point to this existing cache using the -d
and -k
parameters.