All Projects → minyoungg → Pix2latent

minyoungg / Pix2latent

Licence: apache-2.0
Code for: Transforming and Projecting Images into Class-conditional Generative Networks

Programming Languages

python
139335 projects - #7 most used programming language

Projects that are alternatives of or similar to Pix2latent

Awesome Gan For Medical Imaging
Awesome GAN for Medical Imaging
Stars: ✭ 1,814 (+1186.52%)
Mutual labels:  gan
Alphagan Matting
This project is an unofficial implementation of AlphaGAN: Generative adversarial networks for natural image matting published at the BMVC 2018
Stars: ✭ 138 (-2.13%)
Mutual labels:  gan
Tsit
[ECCV 2020 Spotlight] A Simple and Versatile Framework for Image-to-Image Translation
Stars: ✭ 141 (+0%)
Mutual labels:  gan
Electra
中文 预训练 ELECTRA 模型: 基于对抗学习 pretrain Chinese Model
Stars: ✭ 132 (-6.38%)
Mutual labels:  gan
Oneshottranslation
Pytorch implementation of "One-Shot Unsupervised Cross Domain Translation" NIPS 2018
Stars: ✭ 135 (-4.26%)
Mutual labels:  gan
Unetgan
Official Implementation of the paper "A U-Net Based Discriminator for Generative Adversarial Networks" (CVPR 2020)
Stars: ✭ 139 (-1.42%)
Mutual labels:  gan
Cyclegan
Software that can generate photos from paintings, turn horses into zebras, perform style transfer, and more.
Stars: ✭ 10,933 (+7653.9%)
Mutual labels:  gan
Starnet
StarNet
Stars: ✭ 141 (+0%)
Mutual labels:  gan
Infogan Pytorch
implement infoGAN using pytorch
Stars: ✭ 135 (-4.26%)
Mutual labels:  gan
Focal Frequency Loss
Focal Frequency Loss for Generative Models
Stars: ✭ 141 (+0%)
Mutual labels:  gan
Gandissect
Pytorch-based tools for visualizing and understanding the neurons of a GAN. https://gandissect.csail.mit.edu/
Stars: ✭ 1,700 (+1105.67%)
Mutual labels:  gan
Deep Learning With Python
Example projects I completed to understand Deep Learning techniques with Tensorflow. Please note that I do no longer maintain this repository.
Stars: ✭ 134 (-4.96%)
Mutual labels:  gan
Nice Gan Pytorch
Official PyTorch implementation of NICE-GAN: Reusing Discriminators for Encoding: Towards Unsupervised Image-to-Image Translation
Stars: ✭ 140 (-0.71%)
Mutual labels:  gan
Ganimation
GANimation: Anatomically-aware Facial Animation from a Single Image (ECCV'18 Oral) [PyTorch]
Stars: ✭ 1,730 (+1126.95%)
Mutual labels:  gan
Data science blogs
A repository to keep track of all the code that I end up writing for my blog posts.
Stars: ✭ 139 (-1.42%)
Mutual labels:  gan
Reconstructing faces from voices
An example of the paper "reconstructing faces from voices"
Stars: ✭ 127 (-9.93%)
Mutual labels:  gan
Generative adversarial networks 101
Keras implementations of Generative Adversarial Networks. GANs, DCGAN, CGAN, CCGAN, WGAN and LSGAN models with MNIST and CIFAR-10 datasets.
Stars: ✭ 138 (-2.13%)
Mutual labels:  gan
Semantic image inpainting
Semantic Image Inpainting
Stars: ✭ 140 (-0.71%)
Mutual labels:  gan
Glcic Pytorch
A High-Quality PyTorch Implementation of "Globally and Locally Consistent Image Completion".
Stars: ✭ 141 (+0%)
Mutual labels:  gan
Human Video Generation
Human Video Generation Paper List
Stars: ✭ 139 (-1.42%)
Mutual labels:  gan

pix2latent: framework for inverting images into generative models

Framework for inverting images. Codebase used in:

Transforming and Projecting Images into Class-conditional Generative Networks
project page | paper
Minyoung Huh   Richard Zhang   Jun-Yan Zhu   Sylvain Paris   Aaron Hertzmann
MIT CSAIL   Adobe Research
ECCV 2020 (oral)

@inproceedings{huh2020ganprojection,
    title = {Transforming and Projecting Images to Class-conditional Generative Networks}
    author = {Minyoung Huh and Richard Zhang, Jun-Yan Zhu and Sylvain Paris and Aaron Hertzmann},
    booktitle = {ECCV},
    year = {2020}
}

NOTE [8/25/20] The codebase has been renamed from GAN-Transform-and-Project to pix2latent, and also refactored to make it easier to use and extend to any generative model beyond BigGAN. To access the original codebase refer to the legacy branch.

Example results

All results below are without fine-tuning.

BigGAN (z-space) - ImageNet (256x256)

StyleGAN2 (z-space) - LSUN Cars (384x512)

StyleGAN2 (z-space) - FFHQ (1024x1024)

Prerequisites

The code was developed on

  • Ubuntu 18.04
  • Python 3.7
  • PyTorch 1.4.0

Getting Started

  • Install PyTorch
    Install the correct PyTorch version for your machine

  • Install the python dependencies
    Install the remaining dependencies via

    pip install -r requirements.txt
    
  • Install pix2latent

    git clone https://github.com/minyoungg/pix2latent
    cd pix2latent
    pip install .
    

Examples

We provide several demo codes in ./examples/ for both BigGAN and StyleGAN2. Note that the codebase has been tuned and developed on BigGAN.

> cd examples
> python invert_biggan_adam.py --num_samples 4

Using the make_video flag will save the optimization trajectory as a video.

> python invert_biggan_adam.py --make_video --num_samples 4

(slow) To optimize with CMA-ES or BasinCMA, we use PyCMA. Note that the PyCMA version of CMA-ES has a predefined number of samples to jointly evaluate (18 for BigGAN) and (22 for StyleGAN2).

> python invert_biggan_cma.py 
> python invert_biggan_basincma.py 

(fast) Alternatively CMA-ES in Nevergrad provides sample parallelization so you can set your own number of samples. Although this runs faster, we have observed the performance to be slightly worse. (warning: performance depends on num_samples).

> python invert_biggan_nevergrad.py --ng_method CMA --num_samples 4
> python invert_biggan_hybrid_nevergrad.py --ng_method CMA --num_samples 4

Same applies to StyleGAN2. See ./examples/ for extensive list of examples.

Template pseudocode

import torch, torch.nn as nn
import pix2latent.VariableManger
from pix2latent.optimizer import GradientOptimizer

# load your favorite model
class Generator(nn.Module):
    ...
    
    def forward(self, z):
        ...
        return im

model = Generator() 

# define your loss objective .. or use the predefined loss functions in pix2latent.loss_functions
loss_fn = lambda out, target: (target - out).abs().mean()

# tell the optimizer what the input-output relationship is
vm = VariableManager()
vm.register(variable_name='z', shape=(128,), var_type='input')
vm.register(variable_name='target', shape(3, 256, 256), var_type='output')

# setup optimizer
opt = GradientOptimizer(model, vm, loss_fn)

# optimize
vars, out, loss = opt.optimize(num_samples=1, grad_steps=500)

detailed usage

pix2latent

Command Description
pix2latent.loss_function predefined loss functions
pix2latent.distribution distribution functions used to initialize variables

pix2latent.VariableManger

class variable for managing variables. variable manager instance is initialized by

var_man = VariableManager()
Method Description
var_man.register(...) registers variable. this variable is created when initialize is called
var_man.unregister(...) removes a variable that is already registered
var_man.edit_variable(...) edits existing variable
var_man.initialize(...) initializes variable from defined specification

pix2latent.optimizer

Command Description
pix2latent.optimizer.GradientOptimizer gradient-based optimizer. defaults to optimizer defined in pix2latent.VariableManager
pix2latent.optimizer.CMAOptimizer uses CMA optimization to search over latent variables z
pix2latent.optimizer.BasinCMAOptimizer uses BasinCMA optimization. a combination of CMA and gradient-based optimization
pix2latent.optimizer.NevergradOptimizer uses Nevergrad library for optimization. supports most gradient-free optimization method implemented in Nevergrad
pix2latent.optimizer.HybridNevergradOptimizer uses hybrid optimization by alternating gradient and gradient-free optimization provided by Nevergrad

pix2latent.transform

Command Description
pix2latent.SpatialTransform spatial transformation function, used to optimize for image scale and position
pix2latent.TransformBasinCMAOptimizer BasinCMA-like optimization method used to search for image transformation

pix2latent.util

Command Description
pix2latent.util.image utility for image pre and post processing
pix2latent.util.video utility for video (e.g. saving videos)
pix2latent.util.misc miscellaneous functions
pix2latent.util.function_hooks function hooks that can be attached to variables in the optimization loop. (e.g. Clamp, Perturb)

pix2latent.model

Command Description
pix2latent.model.BigGAN BigGAN model wrapper. Uses implementation by huggingface using the official weights
pix2latent.model.StyleGAN2 StyleGAN2 model wrapper. Uses PyTorch implementation by rosinality using the official weights

pix2latent.edit

Command Description
pix2latent.edit.BigGANLatentEditor BigGAN editor. Simple interface to edit class and latent variables using oversimplified version of GANSpace
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].