We provide a PyTorch implementation of GANs projection using multilayer latent codes. Choosing a single latent layer for GAN inversion leads to a dilemma between obtaining a faithful reconstruction
of the input image and being able to perform downstream edits (1st and 2nd row).
In contrast, our proposed method automatically selects the latent space tailored for each region to balance the reconstruction
quality and editability (3rd row).
Spatially-Adaptive Multilayer Selection for GAN Inversion and Editing
Gaurav Parmar, Yijun Li, Jingwan Lu, Richard Zhang, Jun-Yan Zhu, Krishna Kumar Singh
CMU, Adobe Research
CVPR 2022
Below we show image inversion and editing results obtained using the proposed method. Please see the project website for more results.
Clone this repo:
git clone --recurse-submodules https://github.com/adobe-research/sam_inversion
cd sam_inversion
See environment.yml for a full list of library dependencies. The following commands can be used to install all the dependencies in a new conda environment.
conda env create -f environment.yml
conda activate inversion
An example command for inverting an image for a given target image is shown below. The --image_category
should be one of {"cars", "faces", "cats"}. The --sweep_threshold
will perform inversion for a range of different threshold values. See file for other optional flags.
python src/sam_inv_optimization.py \
--image_category "cars" --image_path test_images/cars/b.png \
--output_path "output/cars/" --sweep_thresholds --generate_edits
To perform SAM Inversion on a custom dataset, we need to train a corresponding invertibility network. First, perform a single layer inversion using all candidate latent spaces as shown in the command below for all images in the training set.
for latent_name in "W+" "F4" "F6" "F8" "F10"; do
python src/single_latent_inv.py \
--image_category "cats" --image_folder_path datasets/custom_images/train \
--num_opt_steps 501 --output_path "output/custom_ds/train/${latent_name}" --target_H 256 --target_W 256 \
--latent_name ${latent_name}
done
Next, repeat the above for the validation and test splits. Finally, train the invertibility network as shown in the example command below.
python src/train_invertibility.py \
--dataset_folder_train output/custom_ds/train \
--dataset_folder_val output/custom_ds/val \
--output_folder output/invertibility/custom_ds \
--gpu-ids "0" --batch-size 16 --lr 0.0001
If you find this code useful for your research, please cite our paper.
@inproceedings{
parmar2022sam,
title={Spatially-Adaptive Multilayer Selection for GAN Inversion and Editing},
author={Gaurav Parmar, Yijun Li, Jingwan Lu, Richard Zhang, Jun-Yan Zhu, Krishna Kumar Singh},
booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
year={2022}
}
Please check out our past GANs inversion projects:
iGAN (ECCV 2016), GANPaint (SIGGRAPH 2019), GANSeeing (ICCV 2019), pix2latent (ECCV 2020)
Our work is built partly based on the following repos:
- e4e - Encoder used for the
W+
inversions. - StyleGAN - The generative model used for the inversion.
- Deeplab3-xception - Used for the base architectore of the invertibility prediction network.
- HRNet, Detectron - Used for segmenting images (except faces).
- Face Parsing - Used for segmenting face images.