alexjc / neural-enhance
- воскресенье, 30 октября 2016 г. в 03:13:15
Python
Super Resolution for images using deep learning.
Example #1 — Old Station: view comparison in 24-bit HD, original photo CC-BY-SA @siv-athens.
As seen on TV! What if you could increase the resolution of your photos using technology from CSI laboratories? Thanks to deep learning and #NeuralEnhance
, it's now possible to train a neural network to zoom in to your images at 2x or even 4x. You'll get even better results by increasing the number of neurons or training with a dataset similar to your low resolution image.
The catch? The neural network is hallucinating details based on its training from example images. It's not reconstructing your photo exactly as it would have been if it was HD. That's only possible in Holywood — but using deep learning as "Creative AI" works and its just as cool! Here's how you can get started...
The main script is called enhance.py
, which you can run with Python 3.4+ once it's setup as below. The --device
argument that lets you specify which GPU or CPU to use. For the samples above, here are the performance results:
The default is to use --device=cpu
, if you have NVIDIA card setup with CUDA already try --device=gpu0
. On the CPU, you can also set environment variable to OMP_NUM_THREADS=4
, which is most useful when running the script multiple times in parallel.
# Run the super-resolution script for one image.
python3 enhance.py example.png
# Also process multiple files with a single run.
python3 enhance.py file1.jpg file2.jpg
# Display output images that were given `_ne4x.png` suffix.
open *_ne4x.png
Pre-trained models are provided in the GitHub releases. Training your own is a delicate process that may require you to pick parameters based on your image dataset.
# Remove the model file as don't want to reload the data to fine-tune it.
rm -f ne4x*.pkl.bz2
# Pre-train the model using perceptual loss from paper [1] below.
python3.4 enhance.py --train "data/*.jpg" --scales=2 --epochs=50 \
--perceptual-layer=conv2_2 --smoothness-weight=1e7 --adversary-weight=0.0 \
--generator-blocks=4 --generator-filters=64
# Train the model using an adversarial setup based on [4] below.
python3.4 enhance.py --train "data/*.jpg" --scales=2 --epochs=250 \
--perceptual-layer=conv5_2 --smoothness-weight=2e4 --adversary-weight=2e5 \
--generator-start=5 --discriminator-start=0 --adversarial-start=5 \
--discriminator-size=64
Example #2 — Bank Lobby: view comparison in 24-bit HD, original photo CC-BY-SA @benarent.
(work in progress)
This project requires Python 3.4+ and you'll also need numpy
and scipy
(numerical computing libraries) as well as python3-dev
installed system-wide. If you want more detailed instructions, follow these:
Afterward fetching the repository, you can run the following commands from your terminal to setup a local environment:
# Create a local environment for Python 3.x to install dependencies here.
python3 -m venv pyvenv --system-site-packages
# If you're using bash, make this the active version of Python.
source pyvenv/bin/activate
# Setup the required dependencies simply using the PIP module.
python3 -m pip install --ignore-installed -r requirements.txt
After this, you should have pillow
, theano
and lasagne
installed in your virtual environment. You'll also need to download this pre-trained neural network (VGG19, 80Mb) and put it in the same folder as the script to run. To de-install everything, you can just delete the #/pyvenv/
folder.
Example #3 — Specialized super-resolution for faces, trained on HD examples of celebrity faces only. The quality is significantly higher when narrowing the domain from "photos" in general.
This code uses a combination of techniques from the following papers, as well as some minor improvements yet to be documented (watch this repository for updates):
Special thanks for their help and support in various ways:
There's a Python extension compiler called Cython, and it's missing or inproperly installed. Try getting it directly from the system package manager rather than PIP.
FIX: sudo apt-get install cython3
This happens when you're running without a GPU, and the CPU libraries were not found (e.g. libblas
). The neural network expressions cannot be evaluated by Theano and it's raising an exception.
FIX: sudo apt-get install libblas-dev libopenblas-dev
You need to install Lasagne and Theano directly from the versions specified in requirements.txt
, rather than from the PIP versions. These alternatives are older and don't have the required features.
FIX: python3 -m pip install -r requirements.txt
It seems your terminal is misconfigured and not compatible with the way Python treats locales. You may need to change this in your .bash_rc
or other startup script. Alternatively, this command will fix it once for this shell instance.
FIX: export LC_ALL=en_US.UTF-8
Example #4 — Street View: view comparison in 24-bit HD, original photo CC-BY-SA @cyalex.