Home

Awesome

waifu2x on iOS

Introduction

This is a Core ML implementation of waifu2x. The target of this project is to run waifu2x models right on iOS devices even without network. For macOS version please refer to waifu2x-mac.

Video support based on Metal Performance Shaders is also included in this repo. Models are loaded directly from Core ML models (see CoreML-MPS). It is meant to be run on macOS with a powerful discerete GPU through Mac Catalyst. Running it on iOS devices will significantly drop battery life and cause thermal issues. Most likely it will crash immediately.

The author is not responsible of any damage to your device.

Requirements (for shared library)

Usage

After cloning this repo, remember to update submodules:

git submodule update --init

Then open waifu2x-ios.xcworkspace (not waifu2x-ios.xcodeproj).

Click Video Test on the top to pick video files. Output path will be printed in the console (starts with Output path:).

Image format

Images with RGB color space works fine. Others should be converted to RGB before processing otherwise output image will be broken. Alpha channel is scaled using bicubic interpolation. Generally it runs on GPU. It automatically falls back to CPU if image is too large for Metal to process, which is extremely slow. (A bad idea)

Video format

The built-in video decoder on iOS and macOS is very limited. If your video doesn't work, you can convert to a supported format using ffmpeg:

ffmpeg -i <INPUT VIDEO> -c:v libx264 -preset ultrafast -pix_fmt yuv420p -c:a aac -f mp4 <OUTPUT VIDEO>.mp4

About models

This repository includes all the models converted from waifu2x-caffe. If you want to dig into Core ML, it is recommended that you should convert them by yourself.

You can convert pre-trained models to Core ML format and then import them to XCode. The pre-trained model can be obtained from waifu2x-caffe.

You can use the same method described in MobileNet-CoreML. You should not specify any input and output layer in python script.

A working model should have input and output like the following example:

Benchmark on images

Environment

Results

All of the tests are running denoise level 2 with scale 2x model on anime-style images from Pixiv.

Test1

Image resolution: 600*849

DeviceTime(s)
iPhone6s6.8
iPhone84.0
iPhone11Pro2.0
iPad2.9
PC2.1

Test2

Image resolution: 3000*3328

DeviceTime(s)
iPhone6s129.2
iPhone873.5
iPhone11Pro18.8
iPad49.2
PC37.5

Evolution

Device: iPad Image resolution: 3000*3328

MilestoneTime(s)RAM usage(GB)
Before using upconv models141.71.86
After using upconv models63.61.28
After adding pipeline on output56.81.28
After adding pipeline on prediction49.20.38
Pure MPSCNN implementation*29.61.06

*: With crop size of 384 and double command buffers.

Performance on video

About 1.78 frames per second while scaling 1080p -> 2160p on 5700XT GPU.

Runs out of memory and crashes immediately with the same video on iOS with 4GB memory.

Demo