All Projects → yandex-research → Navigan

yandex-research / Navigan

Navigating the GAN Parameter Space for Semantic Image Editing

Projects that are alternatives of or similar to Navigan

Timeseries fastai
fastai V2 implementation of Timeseries classification papers.
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Htmresearch
Experimental algorithms. Unsupported.
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Sec
Seed, Expand, Constrain: Three Principles for Weakly-Supervised Image Segmentation
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Pythondemo
虾神的Python示例代码库
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Neural Style Painting
Implementing of the "A Neural Algorithm of Artistic Style"
Stars: ✭ 219 (-0.9%)
Mutual labels:  jupyter-notebook
Team Learning Rs
主要存储Datawhale组队学习中“推荐系统”方向的资料。
Stars: ✭ 216 (-2.26%)
Mutual labels:  jupyter-notebook
Fauxtograph
Tools for using a variational auto-encoder for latent image encoding and generation.
Stars: ✭ 220 (-0.45%)
Mutual labels:  jupyter-notebook
Ipython Notebooks
A collection of IPython notebooks covering various topics.
Stars: ✭ 2,543 (+1050.68%)
Mutual labels:  jupyter-notebook
Scikit Geometry
Scientific Python Geometric Algorithms Library
Stars: ✭ 220 (-0.45%)
Mutual labels:  jupyter-notebook
Data Augmentation For Wearable Sensor Data
A sample code of data augmentation methods for wearable sensor data (time-series data)
Stars: ✭ 222 (+0.45%)
Mutual labels:  jupyter-notebook
Paperboy
A web frontend for scheduling Jupyter notebook reports
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Tensorflow Tutorial
A tutorial on TensorFlow
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Melusine
Melusine is a high-level library for emails classification and feature extraction "dédiée aux courriels français".
Stars: ✭ 222 (+0.45%)
Mutual labels:  jupyter-notebook
Interpret Text
A library that incorporates state-of-the-art explainers for text-based machine learning models and visualizes the result with a built-in dashboard.
Stars: ✭ 220 (-0.45%)
Mutual labels:  jupyter-notebook
Vqa demo
Visual Question Answering Demo on pretrained model
Stars: ✭ 222 (+0.45%)
Mutual labels:  jupyter-notebook
Interview Prepartion Data Science
Stars: ✭ 220 (-0.45%)
Mutual labels:  jupyter-notebook
Sklearn pycon2014
Repository containing files for my PyCon 2014 scikit-learn tutorial.
Stars: ✭ 221 (+0%)
Mutual labels:  jupyter-notebook
Deep Vector Quantization
VQVAEs, GumbelSoftmaxes and friends
Stars: ✭ 222 (+0.45%)
Mutual labels:  jupyter-notebook
Ownphotos
Self hosted alternative to Google Photos
Stars: ✭ 2,587 (+1070.59%)
Mutual labels:  jupyter-notebook
Natural Language Processing With Tensorflow
Natural Language Processing with TensorFlow, published by Packt
Stars: ✭ 222 (+0.45%)
Mutual labels:  jupyter-notebook

Navigating the GAN Parameter Space for Semantic Image Editing

Authors official implementation of the Navigating the GAN Parameter Space for Semantic Image Editing by Anton Cherepkov, Andrey Voynov, and Artem Babenko.

Main steps of our approach:

  • First: we form a low-dimensional subspace in the parameters space of a pretrained GAN;
  • Second: we solve an optimization problem to discover interpretable controls in this subspace.

Typical Visual Effects

FFHQ

original
An image
nose length
An image
eyes size
An image
eyes direction
An image
brows up
An image
vampire
An image

LSUN-Car

An image
Wheels size

LSUN-Church

An image
Add conic structures

LSUN-Horse

An image
Thickness

Real Images Domain

An image
An image
An image
An image

An image An image An image

Pix2PixHD

The proposed method is also applicable to pixel-to-pixel models. Here we present some of the effects discovered for the label-to-streetview model.

Alt Text
Add curb

Alt Text
Road darkening

check high-res videos here: curb1, curb2, darkening1, darkening2


Training

There are two options to form the low-dimensional parameters subspace: LPIPS-Hessian-based and SVD-based. The first one is recommended.

LPIPS-Hessian-based

Once you want to use the LPIPS-Hessian, first run its computation:

  • Calculating hessian's eigenvectors
python hessian_power_iteration.py \
    --out result \                             #  script output
    --gan_weights stylegan2-car-config-f.pt \  #  model weigths
    --resolution 512 \                         #  model resolution
    --gan_conv_layer_index 3 \                 #  target convolutional layer index starting from 0
    --num_samples 512 \                        #  z-samples count to use for hessian computation
    --num_eigenvectors 64 \                    #  number of leading eigenvectors to calculate

Second, run the interpretable directions search:

  • Interpretable directions in the hessian's eigenvectors subspace
python run_train.py \
    --out results \                           #  script out
    --gan_type StyleGAN2 \                    #  currently only StyleGAN2 is available
    --gan_weights stylegan2-car-config-f.pt \
    --resolution 512 \
    --shift_predictor_size 256 \              # resize to 256 before shift prediction [memory saving-option]
    --deformator_target weight_fixedbasis \
    --basis_vectors_path eigenvectors_layer3_stylegan2-car-config-f.pt \  # first step results
    --deformator_conv_layer_index 3 \         # should be the same as on the first step
    --directions_count 64 \
    --shift_scale 60 \
    --min_shift 15 \

SVD-based

The second option is to run the search over the SVD-based basis:

python run_train.py \
    --out results \
    --gan_type StyleGAN2 \
    --gan_weights stylegan2-car-config-f.pt \
    --resolution 512 \
    --shift_predictor_size 256 \
    --deformator_target weight_svd \
    --deformator_conv_layer_index 3 \  #  target convolutional layer index starting from 0
    --directions_count 64 \
    --shift_scale 3500 \
    --shift_weight 0.0025 \
    --min_shift 300 \

Though we successfully use the same shift_scale for different layers, its manual per-layer tuning can slightly improve performance.


Evaluation

Here we present the code to visualize controls discovered by the previous steps for:

  • SVD;
  • SVD with optimization (optimization-based);
  • Hessian (spectrum-based);
  • Hessian with optimization (hybrid)

First, import the required modules and load the generator:

from inference import GeneratorWithWeightDeformator
from loading import load_generator

G = load_generator(
    args={'resolution': 512, 'gan_type': 'StyleGAN2'},
    G_weights='stylegan2-car-config-f.pt'
)

Second, modify the GAN parameters using one of the methods below.

SVD-based
G = GeneratorWithWeightDeformator(
    generator=G,
    deformator_type='svd',
    layer_ix=3,
)
Optimization in the SVD basis
G = GeneratorWithWeightDeformator(
    generator=G,
    deformator_type='svd_rectification',
    layer_ix=3,
    checkpoint_path='_svd_based_train_/checkpoint.pt',
)
Hessian's eigenvectors
G = GeneratorWithWeightDeformator(
    generator=G,
    deformator_type='hessian',
    layer_ix=3,
    eigenvectors_path='eigenvectors_layer3_stylegan2-car-config-f.pt'
)
Optimization in the Hessian eigenvectors basis
G = GeneratorWithWeightDeformator(
    generator=G,
    deformator_type='hessian_rectification',
    layer_ix=3,
    checkpoint_path='_hessian_based_train_/checkpoint.pt',
    eigenvectors_path='eigenvectors_layer3_stylegan2-car-config-f.pt'
)

Now you can apply modified parameters for every element in the batch in the following manner:

# Generate some samples
zs = torch.randn((4, 512)).cuda()

# Specify deformation index and shift
direction = 0
shift = 100.0
G.deformate(direction, shift)

# Simply call the generator
imgs_deformated = G(zs)

Saving into a file

You can save the discovered parameters shifts (including layer_ix and data) into a file. In order to do this:

  1. Modify the GAN parameters in the manner described above;
  2. Call G.save_deformation(path, direction_ix).

Loading from file

First, import the required modules and load the generator:

from inference import GeneratorWithFixedWeightDeformation
from loading import load_generator

G = load_generator(
    args={'resolution': 512, 'gan_type': 'StyleGAN2'},
    G_weights='stylegan2-car-config-f.pt'
)

Second, modify the GAN:

G = GeneratorWithFixedWeightDeformation(generator=G, deformation_path='deformation.pt')

Now you can apply modified parameters for every element in the batch in the following manner:

# Generate some samples
zs = torch.randn((4, 512)).cuda()

# Deformate; G.scale is a recommended scale
G.deformate(0.5 * G.scale)

# Simply call the generator
imgs_deformated = G(zs)

Pretrained directions

Annotated generators directions and gif examples sources:
FFHQ: https://www.dropbox.com/s/7m838ewhzgcb3v5/ffhq_weights_deformations.tar
Car: https://www.dropbox.com/s/rojdcfvnsdue10o/car_weights_deformations.tar
Horse: https://www.dropbox.com/s/ir1lg5v2yd4cmkx/horse_weights_deformations.tar
Church: https://www.dropbox.com/s/do9yt3bggmggehm/church_weights_deformations.tar

StyleGAN2 weights: https://www.dropbox.com/s/d0aas2fyc9e62g5/stylegan2_weights.tar
generators weights are the original models weights converted to pytorch (see credits)

You can find loading and deformation example at example.ipynb


Credits

Our code is based on the Unsupervised Discovery of Interpretable Directions in the GAN Latent Space official implementation
https://github.com/anvoynov/GANLatentDiscovery
Generator model is implemented over the StyleGAN2-pytorch:
https://github.com/rosinality/stylegan2-pytorch
Generators weights were converted from the original StyleGAN2:
https://github.com/NVlabs/stylegan2

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].