neural-enhance README

The project's README from github.com/alexjc/neural-enhance. As one markdown file: README.md.

Neural Enhance

.. image:: docs/OldStation_example.gif

Example #1 — Old Station: view comparison <http://enhance.nucl.ai/w/0f5177f4-9ce6-11e6-992c-c86000be451f/view>_ in 24-bit HD, original photo <https://flic.kr/p/oYhbBv>_ CC-BY-SA @siv-athens.


As seen on TV! <https://www.youtube.com/watch?v=LhF_56SxrGk>_ 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 Hollywood — but using deep learning as "Creative AI" works and it is just as cool! Here's how you can get started...

  1. Examples & Usage <#1-examples--usage>_
  2. Installation <#2-installation--setup>_
  3. Background & Research <#3-background--research>_
  4. Troubleshooting <#4-troubleshooting-problems>_
  5. Frequent Questions <#5-frequent-questions>_

|Python Version| |License Type| |Project Stars|

.. image:: docs/EnhanceCSI_example.png :target: http://enhance.nucl.ai/w/8581db92-9d61-11e6-990b-c86000be451f/view

  1. Examples & Usage

The main script is called enhance.py, which you can run with Python 3.4+ once it's setup <#2-installation--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.

1.a) Enhancing Images

A list of example command lines you can use with the pre-trained models provided in the GitHub releases:

.. code:: bash

# Run the super-resolution script to repair JPEG artefacts, zoom factor 1:1.
python3 enhance.py --type=photo --model=repair --zoom=1 broken.jpg

# Process multiple good quality images with a single run, zoom factor 2:1.
python3 enhance.py --type=photo --zoom=2 file1.jpg file2.jpg

# Display output images that were given `_ne?x.png` suffix.
open *_ne?x.png

Here's a list of currently supported models, image types, and zoom levels in one table.

================== ===================== ==================== ===================== ==================== FEATURES --model=default --model=repair --model=denoise --model=deblur ================== ===================== ==================== ===================== ==================== --type=photo 2x 1x … …
================== ===================== ==================== ===================== ====================

1.b) Training Super-Resolution

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.

.. code:: bash

# Remove the model file as don't want to reload the data to fine-tune it.
rm -f ne?x*.pkl.bz2

# Pre-train the model using perceptual loss from paper [1] below.
python3.4 enhance.py --train "data/*.jpg" --model custom --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" --model custom --scales=2 --epochs=250 \
         --perceptual-layer=conv5_2 --smoothness-weight=2e4 --adversary-weight=1e3 \
         --generator-start=5 --discriminator-start=0 --adversarial-start=5 \
         --discriminator-size=64

# The newly trained model is output into this file...
ls ne?x-custom-*.pkl.bz2

.. image:: docs/BankLobby_example.gif

Example #2 — Bank Lobby: view comparison <http://enhance.nucl.ai/w/38d10880-9ce6-11e6-becb-c86000be451f/view>_ in 24-bit HD, original photo <https://flic.kr/p/6a8cwm>_ CC-BY-SA @benarent.

  1. Installation & Setup

The easiest way to get up-and-running is to install Docker <https://www.docker.com/>. Then, you should be able to download and run the pre-built image using the docker command line tool. Find out more about the alexjc/neural-enhance image on its Docker Hub <https://hub.docker.com/r/alexjc/neural-enhance/> page.

Here's the simplest way you can call the script using docker, assuming you're familiar with using -v argument to mount folders you can use this directly to specify files to enhance:

.. code:: bash

# Download the Docker image and show the help text to make sure it works.
docker run --rm -v `pwd`:/ne/input -it alexjc/neural-enhance --help

Single Image — In practice, we suggest you setup an alias called enhance to automatically expose the folder containing your specified image, so the script can read it and store results where you can access them. This is how you can do it in your terminal console on OSX or Linux:

.. code:: bash

# Setup the alias. Put this in your .bashrc or .zshrc file so it's available at startup.
alias enhance='function ne() { docker run --rm -v "$(pwd)/`dirname ${@:$#}`":/ne/input -it alexjc/neural-enhance ${@:1:$#-1} "input/`basename ${@:$#}`"; }; ne'

# Now run any of the examples above using this alias, without the `.py` extension.
enhance --zoom=1 --model=repair images/broken.jpg

Multiple Images — To enhance multiple images in a row (faster) from a folder or wildcard specification, make sure to quote the argument to the alias command:

.. code:: bash

# Process multiple images, make sure to quote the argument!
enhance --zoom=2 "images/*.jpg"

If you want to run on your NVIDIA GPU, you can instead change the alias to use the image alexjc/neural-enhance:gpu which comes with CUDA and CUDNN pre-installed. Then run it within nvidia-docker <https://github.com/NVIDIA/nvidia-docker>_ and it should use your physical hardware!

2.b) Manual Installation [developers]

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:

  1. Linux Installation of Lasagne <https://github.com/Lasagne/Lasagne/wiki/From-Zero-to-Lasagne-on-Ubuntu-14.04>_ (intermediate)
  2. Mac OSX Installation of Lasagne <http://deeplearning.net/software/theano/install.html#mac-os>_ (advanced)
  3. Windows Installation of Lasagne <https://github.com/Lasagne/Lasagne/wiki/From-Zero-to-Lasagne-on-Windows-7-%2864-bit%29>_ (expert)

Afterward fetching the repository, you can run the following commands from your terminal to setup a local environment:

.. code:: bash

# 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 <https://github.com/alexjc/neural-doodle/releases/download/v0.0/vgg19_conv.pkl.bz2>_ (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.

.. image:: docs/Faces_example.png

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.

  1. Background & Research

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):

  1. Perceptual Losses for Real-Time Style Transfer and Super-Resolution <http://arxiv.org/abs/1603.08155>_
  2. Real-Time Super-Resolution Using Efficient Sub-Pixel Convolution <https://arxiv.org/abs/1609.05158>_
  3. Deeply-Recursive Convolutional Network for Image Super-Resolution <https://arxiv.org/abs/1511.04491>_
  4. Photo-Realistic Super-Resolution Using a Generative Adversarial Network <https://arxiv.org/abs/1609.04802>_

Special thanks for their help and support in various ways:

  1. Troubleshooting Problems

Can't install or Unable to find pgen, not compiling formal grammar.

There's a Python extension compiler called Cython, and it's missing or improperly installed. Try getting it directly from the system package manager rather than PIP.

FIX: sudo apt-get install cython3

NotImplementedError: AbstractConv2d theano optimization failed.

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

TypeError: max_pool_2d() got an unexpected keyword argument 'mode'

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

ValueError: unknown locale: UTF-8

It seems your terminal is misconfigured and not compatible with the way Python treats locales. You may need to change this in your .bashrc or other startup script. Alternatively, this command will fix it once for this shell instance.

FIX: export LC_ALL=en_US.UTF-8

.. image:: docs/StreetView_example.gif

Example #4 — Street View: view comparison <http://enhance.nucl.ai/w/3b3c8054-9d00-11e6-9558-c86000be451f/view>_ in 24-bit HD, original photo <https://flic.kr/p/gnxcXH>_ CC-BY-SA @cyalex.


|Python Version| |License Type| |Project Stars|

.. |Python Version| image:: http://aigamedev.github.io/scikit-neuralnetwork/badge_python.svg :target: https://www.python.org/

.. |License Type| image:: https://img.shields.io/badge/license-AGPL-blue.svg :target: https://github.com/alexjc/neural-enhance/blob/master/LICENSE

.. |Project Stars| image:: https://img.shields.io/github/stars/alexjc/neural-enhance.svg?style=flat :target: https://github.com/alexjc/neural-enhance/stargazers

neural-enhance (alexjc/neural-enhance)

Searching Database…

11.9K ·GitHub
archived
这是什么?Neural Enhance 使用深度学习(一种经过感知/对抗损失训练的卷积神经网络)来放大照片、修复 JPEG 伪影、降噪和去模糊。
这是一个库还是独立脚本?它是一个单独的命令行 Python 脚本 enhance.py,你可以直接运行,也可以通过预构建的 Docker 镜像运行,它不是一个可以导入的库。
怎样才能让它运行起来?推荐的方式是拉取 Docker 镜像并运行,例如 'docker run --rm -v $(pwd)/input -it alexjc/neural-enhance --help';也可以创建一个 Python 3.x 虚拟环境,运行 'pip install -r requirements.txt',并手动下载预训练的 VGG19 模型。
需要 CUDA,还是只用 CPU 就行?CPU 开箱即可使用,加上 --device=cpu 参数即可,但处理一张 1080p 图片需要 20 到 60 秒;如果你有 NVIDIA 显卡并已配置好 CUDA/CUDNN,可以使用 --device=gpu0 参数,或通过 nvidia-docker 使用 alexjc/neural-enhance:gpu 镜像,大约 5 秒即可完成。
这个项目还在维护吗?不再维护。该仓库已归档,内容停留在 2016 年前后的超分辨率研究水平(基于 Theano/Lasagne),其故障排除部分记录的安装问题也不会再获得更新或修复。
它能在 Windows 和 macOS 上运行,还是只支持 Linux?通过 Docker 镜像,三个平台都可以运行;如果要手动安装,则分别提供了 Linux、Mac OSX 和 Windows 上安装其 Lasagne 依赖的分步指南。
开箱即用支持哪些模型和图像类型?预训练模型涵盖 1 倍和 2 倍放大的照片超分辨率,以及专用的修复、降噪和去模糊模型,可通过 --model 和 --type 参数选择。
我能训练自己的超分辨率模型吗?可以,enhance.py 支持 --train 模式,你只需指定一个图片文件夹,并调整感知层、平滑度权重和对抗权重等参数,即可微调模型或从零开始构建自定义模型。
为什么缩小训练领域(比如只用人脸)会有帮助?README 显示,在高清名人人脸等专用数据集上训练,得到的结果质量明显高于通用照片模型。
这个项目基于哪些研究?它结合了四篇论文中的技术:用于风格迁移和超分辨率的感知损失、高效的亚像素卷积、深度递归卷积网络,以及基于 GAN 的照片级真实超分辨率。
Creator
alexjc
Size
3.3 MB
Stars
11.9K
Updated
CommitNov 30
Age
10y
Commits · 90 Days