waifu2x

repository·master·Indexed 12 days ago

https://github.com/nagadomi/waifu2x

An image super-resolution tool using Deep Convolutional Neural Networks designed for anime-style art and photos. It features a command-line interface, a web application, and tools for training custom models, supporting noise reduction and 2x upscaling.

Tokens
1.9K
Snippets
9
Records
12
Agent score
96%

What's inside waifu2x

  1. Use Test-Time Augmentation (TTA) for improved quality

    master

    TTA is an ensemble technique supported by waifu2x that improves PSNR (approximately +0.1 for photo models and +0.4 for art models).

    Trade-off: Using TTA is significantly slower—roughly 8x slower than the non-TTA method. To enable TTA in the photo benchmark command, set the -tta flag to 1.

  2. Use the waifu2x Command Line Tool

    master

    The waifu2x.lua script is the primary entry point for image processing. Use th waifu2x.lua to run commands.

    Common Operations

    Noise Reduction

    Use the -m noise mode. Specify noise levels with -noise_level (0, 1, 2, or 3).

    th waifu2x.lua -m noise -noise_level 1 -i input_image.png -o output_image.png

    2x Upscaling

    Use the -m scale mode.

    th waifu2x.lua -m scale -i input_image.png -o output_image.png

    Noise Reduction + 2x Upscaling

    Use the -m noise_scale mode.

    th waifu2x.lua -m noise_scale -noise_level 1 -i input_image.png -o output_image.png

    Using Photo Models

    To use models optimized for photos instead of anime-style art, add the -model_dir models/photo flag:

    th waifu2x.lua -model_dir models/photo -m scale -i input_image.png -o output_image.png

    Performance and Troubleshooting

    • cuDNN Acceleration: If you have the cuDNN library installed, use -force_cudnn 1 for significantly faster processing.
    • Out of Memory (OOM): If you encounter GPU memory errors, reduce the processing chunk size using -crop_size (e.g., -crop_size 128).
  3. Add a translation file to webgen

    master

    To include new translations in the generated web pages, follow these steps:

    1. Add your translation file to the ./locales directory.
    2. Run the generation script to rebuild the pages: ./gen.rb.
    # 1. Add file to ./locales
    # 2. Run generator
    ./gen.rb
  4. Train a Custom waifu2x Model

    master

    To train your own models, you must first prepare your data and then run the training script.

    1. Data Preparation

    Generate a list of high-resolution, noise-free images:

    find /path/to/image/dir -name "*.png" > data/image_list.txt
    th convert_data.lua

    2. Training

    Use th train.lua with the appropriate method and model directory.

    Train Noise Reduction (Level 1)

    th train.lua -model_dir models/my_model -method noise -noise_level 1 -test images/miku_noisy.png

    Train 2x Upscaling

    th train.lua -model upconv_7 -model_dir models/my_model -method scale -scale 2 -test images/miku_small.png

    Train Noise Reduction + 2x Upscaling Fusion

    th train.lua -model upconv_7 -model_dir models/my_model -method noise_scale -scale 2 -noise_level 1 -test images/miku_small.png

    Note: If cuDNN is available, you can use -backend cudnn during training.

  5. Run waifu2x via Docker

    master

    The project provides a Docker image that requires nvidia-docker.

    Run Web Server in Docker

    docker run --gpus all -p 8812:8812 waifu2x th web.lua

    Run CLI Command in Docker

    Mount a host directory to /images to provide input and receive output:

    docker run --gpus all -v `pwd`/images:/images waifu2x th waifu2x.lua -force_cudnn 1 -m scale -scale 2 -i /images/miku_small.png -o /images/output.png

    Performance Optimization (JIT Caching)

    Running without JIT caching is slow in Docker. To fix this, mount a host volume to the CUDA_CACHE_PATH:

    docker run --gpus all -v $PWD/ComputeCache:/root/.nv/ComputeCache waifu2x th waifu2x.lua --help
  6. Install waifu2x on Ubuntu 16.04

    master

    To set up the waifu2x command line environment on Ubuntu 16.04, follow these steps to install CUDA, system dependencies, Torch7, and the waifu2x repository.

    1. Install CUDA

    Download and install CUDA using dpkg:

    sudo dpkg -i cuda-repo-ubuntu1404_7.5-18_amd64.deb
    sudo apt-get update
    sudo apt-get install cuda

    2. Install System Dependencies

    Install the required development libraries:

    sudo apt-get install libsnappy-dev
    sudo apt-get install libgraphicsmagick1-dev
    sudo apt-get install libssl1.0-dev

    Note: waifu2x requires little-cms2 linked graphicsmagick.

    3. Install Torch7

    Follow the official Torch getting started guide. Ensure compatibility with your CUDA version (refer to repository issues for CUDA 8.x, 9.x, and 10.x).

    4. Clone and Install waifu2x

    Clone the repository and run the provided installation script for Lua modules:

    git clone --depth 1 https://github.com/nagadomi/waifu2x.git
    cd waifu2x
    ./install_lua_modules.sh
  7. Batch Conversion with waifu2x

    master

    You can process multiple images at once by providing a list of files via the -l flag. The output filename supports format specifiers:

    • %s: Replaced by the basename of the source filename.
    • %d: Replaced by a sequence number (e.g., %06d for 000001).

    Example workflow:

    1. Generate a list of images:
      find /path/to/imagedir -name "*.png" -o -name "*.jpg" > image_list.txt
    2. Run batch conversion:
      th waifu2x.lua -m scale -l ./image_list.txt -o /path/to/outputdir/prefix_%d.png
    find /path/to/imagedir -name "*.png" -o -name "*.jpg" > image_list.txt
    th waifu2x.lua -m scale -l ./image_list.txt -o /path/to/outputdir/prefix_%d.png
  8. Compare vgg_7/art and upconv_7/art models

    master

    This document provides benchmark comparisons between two specific model variants: vgg_7/art and upconv_7/art. These benchmarks evaluate performance based on PSNR (Peak Signal-to-Noise Ratio) and processing time across different scenarios.

    Key Takeaways from Benchmarks:

    • Performance (PSNR): The upconv_7/art model generally achieves higher PSNR values compared to vgg_7/art across noise-free and noisy datasets.
    • Speed (Time): The upconv_7/art model is significantly faster, typically processing images in approximately half the time required by vgg_7/art.

    Benchmark Scenarios:

    1. 2x Upscaling (Noise Free): Evaluates pure upscaling on 84 anime-style PNG arts.
    2. 2x Upscaling + Noise Reduction (Level 1, JPEG Quality 80): Evaluates performance when handling mild compression artifacts.
    3. 2x Upscaling + Noise Reduction (Level 2, JPEG Quality 50/45): Evaluates performance under heavier noise and compression conditions.

    Note: These benchmark results are marked as outdated and may not reflect the most recent model performance.

  9. Run photo model benchmarks

    master

    Use the tools/benchmark.lua script to benchmark photo models against datasets like BSD100 or Urban100. This allows you to measure PSNR (Peak Signal-to-Noise Ratio) and execution time for different models (e.g., vgg_7/photo, upconv_7/photo, resnet_14l/photo).

    th tools/benchmark.lua -dir <dataset_dir> -model1_dir <model_dir> -method scale -filter Catrom -color y -range_bug 1 -tta <0|1> -force_cudnn 1