# ContraCLIP: Interpretable GAN generation driven by pairs of contrasting sentences
Authors official PyTorch implementation of the **[ContraCLIP: Interpretable GAN generation driven by pairs of contrasting sentences](https://arxiv.org/pdf/2206.02104.pdf)**. If you use this code for your research, please [**cite**](#citation) our paper.
> **ContraCLIP: Interpretable GAN generation driven by pairs of contrasting sentences**
> Christos Tzelepis, James Oldfield, Georgios Tzimiropoulos, and Ioannis Patras
> https://arxiv.org/abs/2206.02104
> ![ContraCLIP Summary](figs/summary.png)
>
> **Abstract**: This work addresses the problem of discovering non-linear interpretable paths in the latent space of pre-trained GANs in a model-agnostic manner. In the proposed method, the discovery is driven by a set of pairs of natural language sentences with contrasting semantics, named semantic dipoles, that serve as the limits of the interpretation that we require by the trainable latent paths to encode. By using the pre-trained CLIP encoder, the sentences are projected into the vision-language space, where they serve as dipoles, and where RBF-based warping functions define a set of non-linear directional paths, one for each semantic dipole, allowing in this way traversals from one semantic pole to the other. By defining an objective that discovers paths in the latent space of GANs that generate changes along the desired paths in the vision-language embedding space, we provide an intuitive way of controlling the underlying generating factors and address some of the limitations of the state-of-the-art works, namely, that a) they are typically tailored to specific GAN architectures (i.e., StyleGAN), b) they disregard the relative position of the manipulated and the original image in the image embedding and the relative position of the image and the text embeddings, and c) they lead to abrupt image manipulations and quickly arrive at regions of low density and, thus, low image quality, providing limited control of the generative factors.
| Semantic Dipole (i.e., contrasting sentences given in natural language) | Example |
| ------------------------------------------------------------ | :----------------------------------------------------------: |
| *"a picture of an **angry shaved man**." → "a picture of a **man** with a **beard crying**."*
[StyleGAN2@FFHQ] | |
| *"a picture of a person with **open eyes**." → "a picture of a person with **closed eyes**."*
[StyleGAN2@FFHQ] | |
| *"a picture of a **young person**." → "a picture of an **old person**."*
[StyleGAN2@FFHQ] | |
| *"a picture of a **man** with **hair**." → "a picture of a **bald man**."*
[ProgGAN@CelebA-HQ] | |
| *"a picture of a person with **happy** face." → "a picture of a person with **surprised** face."*
[ProgGAN@CelebA-HQ] | |
| *"a picture of a **face without makeup**." → "a picture of a **face with makeup**."*
[ProgGAN@CelebA-HQ] | |
| *"a picture of an **ugly cat**." → "a picture of a **cute cat**."*
[StyleGAN2@AFHQ-Cats] | |
| *"a picture of a **dog** with **small eyes**." → "a picture of a **dog** with **big eyes**."*
[StyleGAN2@AFHQ-Dogs] | |
## Overview
![ContraCLIP Overview](./figs/overview.svg)
The CLIP text space, warped due to semantic dipoles of contrasting pairs of sentences in natural language, provides supervision to the optimisation of non-linear interpretable paths in the latent space of a pre-trained GAN.
## Installation We recommend installing the required packages using python's native virtual environment as follows: ```bash $ python -m venv contra-clip-venv $ source contra-clip-venv/bin/activate (contra-clip-venv) $ pip install --upgrade pip (contra-clip-venv) $ pip install -r requirements.txt (contra-clip-venv) $ pip install git+https://github.com/openai/CLIP.git (contra-clip-venv) $ pip install --pre torch torchvision --extra-index-url https://download.pytorch.org/whl/nightly/cu113 ``` For using the aforementioned virtual environment in a Jupyter Notebook, you need to manually add the kernel as follows: ```bash (contra-clip-venv) $ python -m ipykernel install --user --name=contra-clip-venv ``` ## Prerequisite pre-trained models and pre-trained ContraCLIP models Download the prerequisite pre-trained models (GAN generators and various pre-trained detectors, such as ArcFace, FairFace, etc), as well as (optionally) pre-trained ContraCLIP models (by passing `-m` or `----contraclip-models`) as follows: ```bash (contra-clip-venv) $ python download.py -m ``` This will create a directory `models/pretrained` with the following sub-directories (~3.3 GiB): ``` ./models/pretrained/ ├── genforce │ ├── pggan_car256.pth │ ├── pggan_celebahq1024.pth │ ├── pggan_church256.pth │ ├── stylegan2_afhqcat512.pth │ ├── stylegan2_afhqdog512.pth │ ├── stylegan2_car512.pth │ ├── stylegan2_church256.pth │ └── stylegan2_ffhq1024.pth ├── arcface │ └── model_ir_se50.pth ├── au_detector │ └── disfa_adaptation_f0.pth ├── celeba_attributes │ └── eval_predictor.pth.tar ├── fairface │ ├── fairface_alldata_4race_20191111.pt │ └── res34_fair_align_multi_7_20190809.pt ├── hopenet │ ├── hopenet_alpha1.pkl │ ├── hopenet_alpha2.pkl │ └── hopenet_robust_alpha1.pkl └── sfd └── s3fd-619a316812.pth ``` as well as, a directory `experiments/complete/` (if not already created by the user upon an experiment's completion) for downloading the ContraCLIP pre-trained models with the following sub-directories (~160 MiB): ``` .experiments/complete/ ├── ContraCLIP_pggan_celebahq1024-Z-K9-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-attributes ├── ContraCLIP_pggan_celebahq1024-Z-K9-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-cossim-20000-attributes ├── ContraCLIP_stylegan2_afhqcat512-W+-K3-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-cats ├── ContraCLIP_stylegan2_afhqdog512-W+-K4-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-dogs ├── ContraCLIP_stylegan2_car512-W+-K3-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-cars ├── ContraCLIP_stylegan2_ffhq1024-W+-K21-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-expressions ├── ContraCLIP_stylegan2_ffhq1024-W+-K21-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-cossim-20000-expressions ├── ContraCLIP_stylegan2_ffhq1024-W+-K3-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-complex ├── ContraCLIP_stylegan2_ffhq1024-W+-K3-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-expressions3 ├── ContraCLIP_stylegan2_ffhq1024-W+-K3-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-cossim-20000-complex ├── ContraCLIP_stylegan2_ffhq1024-W+-K3-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-cossim-20000-expressions3 ├── ContraCLIP_stylegan2_ffhq1024-W+-K9-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-contrastive_0.07-20000-attributes └── ContraCLIP_stylegan2_ffhq1024-W+-K9-D64-lss_beta_0.5-eps0.1_0.2-nonlinear_css_beta_0.5-cossim-20000-attributes ``` We note that the pre-trained detectors (such as ArcFace) are used only during the evaluation stage (**no ID preserving loss is imposed during training**). ## Training For training a ContraCLIP model you need to use `train.py` (check its basic usage by running `python train.py -h`). For example, in order to train a ContraCLIP model for the corpus of contrasting sentences called "expressions3" (defined in `lib/config.py`) on the StyleGAN2 pre-trained (on FFHQ) generator (in its `W` latent space with a truncation parameter equal to `0.7`), the following command: ```bash (contra-clip-venv) $ python train.py --gan=stylegan2_ffhq1024 --truncation=0.7 --stylegan-space=W --corpus=expressions3 --num-latent-support-dipoles=128 --loss=contrastive --temperature=0.5 --beta=0.75 --min-shift-magnitude=0.1 --max-shift-magnitude=0.2 --batch-size=3 --max-iter=120000 --log-freq=10--ckp-freq=100 ``` In the example above, the batch size is set to `3` and the training will be conducted for `120000` iterations. Minimum and maximum shift magnitudes are set to `0.1` and `0.2`, respectively, and the number of support dipoles for each latent path is set to `128` (please see the [WarpedGANSpace](https://github.com/chi0tzp/WarpedGANSpace) for more details). Moreover, `contrastive` loss is being used with a temperature parameter equal to `0.5`. The `beta` parameter of the CLIP text space RBF dipoles is set to `0.75`. A set of auxiliary training scripts (for the results reported in the paper) can be found under `scripts/train/`. The training script will create a directory with the following name format: ``` ContraCLIP_