PTI Save

Official Implementation for "Pivotal Tuning for Latent-based editing of Real Images" (ACM TOG 2022)

Project README

PTI: Pivotal Tuning for Latent-based editing of Real Images (ACM TOG 2022)

Inference Notebook:

Pivotal Tuning Inversion (PTI) enables employing off-the-shelf latent based semantic editing techniques on real images using StyleGAN. PTI excels in identity preserving edits, portrayed through recognizable figures — Serena Williams and Robert Downey Jr. (top), and in handling faces which are clearly out-of-domain, e.g., due to heavy makeup (bottom).


Official Implementation of our PTI paper + code for evaluation metrics. PTI introduces an optimization mechanizem for solving the StyleGAN inversion task. Providing near-perfect reconstruction results while maintaining the high editing abilitis of the native StyleGAN latent space W. For more details, see

Recent Updates

2021.07.01: Fixed files download phase in the inference notebook. Which might caused the notebook not to run smoothly.

2021.06.29: Added support for CPU. In order to run PTI on CPU please change device parameter under configs/ to "cpu" instead of "cuda".

2021.06.25 : Adding mohawk edit using StyleCLIP+PTI in inference notebook. Updating documentation in inference notebook due to Google Drive rate limit reached. Currently, Google Drive does not allow to download the pretrined models using Colab automatically. Manual intervention might be needed.

Getting Started


  • Linux or macOS
  • NVIDIA GPU + CUDA CuDNN (Not mandatory bur recommended)
  • Python 3


  • Dependencies:
    1. lpips
    2. wandb
    3. pytorch
    4. torchvision
    5. matplotlib
    6. dlib
  • All dependencies can be installed using pip install and the package name

Pretrained Models

Please download the pretrained models from the following links.

Auxiliary Models

We provide various auxiliary models needed for PTI inversion task.
This includes the StyleGAN generator and pre-trained models used for loss computation.

Path Description
FFHQ StyleGAN StyleGAN2-ada model trained on FFHQ with 1024x1024 output resolution.
Dlib alignment Dlib alignment used for images preproccessing.
FFHQ e4e encoder Pretrained e4e encoder. Used for StyleCLIP editing.

Note: The StyleGAN model is used directly from the official stylegan2-ada-pytorch implementation. For StyleCLIP pretrained mappers, please see StyleCLIP's official routes

By default, we assume that all auxiliary models are downloaded and saved to the directory pretrained_models. However, you may use your own paths by changing the necessary values in configs/


Preparing your Data

In order to invert a real image and edit it you should first align and crop it to the correct size. To do so you should perform One of the following steps:

  1. Run notebooks/align_data.ipynb and change the "images_path" variable to the raw images path
  2. Run utils/ and change the "images_path" variable to the raw images path

Weights And Biases

The project supports Weights And Biases framework for experiment tracking. For the inversion task it enables visualization of the losses progression and the generator intermediate results during the initial inversion and the Pivotal Tuning(PT) procedure.

The log frequency can be adjusted using the parameters defined at configs/ under the "Logs" subsection.

There is no no need to have an account. However, in order to use the features provided by Weights and Biases you first have to register on their site.

Running PTI

The main training script is scripts/ The script receives aligned and cropped images from paths configured in the "Input info" subscetion in configs/ Results are saved to directories found at "Dirs for output files" under configs/ This includes inversion latent codes and tuned generators. The hyperparametrs for the inversion task can be found at configs/ They are intilized to the default values used in the paper.


By default, we assume that all auxiliary edit directions are downloaded and saved to the directory editings. However, you may use your own paths by changing the necessary values in configs/ under "Edit directions" subsection.

Example of editing code can be found at scripts/

Inference Notebooks

To help visualize the results of PTI we provide a Jupyter notebook found in notebooks/inference_playground.ipynb.
The notebook will download the pretrained models and run inference on a sample image found online or on images of your choosing. It is recommended to run this in Google Colab.

The notebook demonstrates how to:

  • Invert an image using PTI
  • Visualise the inversion and use the PTI output
  • Edit the image after PTI using InterfaceGAN and StyleCLIP
  • Compare to other inversion methods


Currently the repository supports qualitative evaluation for reconstruction of: PTI, SG2 (W Space), e4e, SG2Plus (W+ Space). As well as editing using InterfaceGAN and GANSpace for the same inversion methods. To run the evaluation please see evaluation/ Examples of the evaluation scripts are:

Reconsturction comparison between different methods. The images order is: Original image, W+ inversion, e4e inversion, W inversion, PTI inversion

InterfaceGAN pose edit comparison between different methods. The images order is: Original, W+, e4e, W, PTI

Image per edit or several edits without comparison

Coming Soon - Quantitative evaluation and StyleCLIP qualitative evaluation

Repository structure

Path Description
├  configs Folder containing configs defining Hyperparameters, paths and logging
├  criteria Folder containing various loss and regularization criterias for the optimization
├  dnnlib Folder containing internal utils for StyleGAN2-ada
├  docs Folder containing the latent space edit directions
├  editings Folder containing images displayed in the README
├  environment Folder containing Anaconda environment used in our experiments
├  licenses Folder containing licenses of the open source projects used in this repository
├  models Folder containing models used in different editing techniques and first phase inversion
├  notebooks Folder with jupyter notebooks to demonstrate the usage of PTI end-to-end
├  scripts Folder with running scripts for inversion, editing and metric computations
├  torch_utils Folder containing internal utils for StyleGAN2-ada
├  training Folder containing the core training logic of PTI
├  utils Folder with various utility functions


StyleGAN2-ada model and implementation: Copyright © 2021, NVIDIA Corporation.
Nvidia Source Code License

LPIPS model and implementation:
Copyright (c) 2020, Sou Uchida
License (BSD 2-Clause)

e4e model and implementation: Copyright (c) 2021 omertov
License (MIT)

StyleCLIP model and implementation: Copyright (c) 2021 orpatashnik
License (MIT)

InterfaceGAN implementation: Copyright (c) 2020 genforce
License (MIT)

GANSpace implementation: Copyright (c) 2020 harkish
License (Apache License 2.0)


This repository structure is based on encoder4editing and ReStyle repositories


For any inquiry please contact us at our email addresses: [email protected] or [email protected]


If you use this code for your research, please cite:

  title={Pivotal Tuning for Latent-based Editing of Real Images},
  author={Roich, Daniel and Mokady, Ron and Bermano, Amit H and Cohen-Or, Daniel},
  publisher = {Association for Computing Machinery},
  journal={ACM Trans. Graph.},
Open Source Agenda is not affiliated with "PTI" Project. README Source: danielroich/PTI
Open Issues
Last Commit
2 months ago

Open Source Agenda Badge

Open Source Agenda Rating