• Stars
    star
    293
  • Rank 141,748 (Top 3 %)
  • Language
    Jupyter Notebook
  • License
    MIT License
  • Created almost 5 years ago
  • Updated almost 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Projecting images to latent space with StyleGAN2.

StyleGAN2: projecting images

The goal of this Google Colab notebook is to project images to latent space with StyleGAN2.

Usage

To discover how to project a real image using the original StyleGAN2 implementation, run:

To process the projection of a batch of images, using either W(1,*) (original) or W(18,*) (extended), run:

To edit latent vectors of projected images, run:

For more information about W(1,*) and W(18,*), please refer to the the original paper (section 5 on page 7):

Inverting the synthesis network $g$ is an interesting problem that has many applications. Manipulating a given image in the latent feature space requires finding a matching latent code $w$ for it first.

The following is about W(18,*):

Previous research suggests that instead of finding a common latent code $w$, the results improve if a separate $w$ is chosen for each layer of the generator. The same approach was used in an early encoder implementation.

The following is about W(1,*), which is the approach used in the original implementation:

While extending the latent space in this fashion finds a closer match to a given image, it also enables projecting arbitrary images that should have no latent representation. Instead, we concentrate on finding latent codes in the original, unextended latent space, as these correspond to images that the generator could have produced.

Data

Data consists of:

Original image of the French president

Pre-processing

There are two possible pre-processing methods:

  • either center-cropping (to 1024x1024 resolution) as sole pre-processing,

Center-cropping

  • or the same pre-processing as for the FFHQ dataset:
    1. first, an alignment based on 68 face landmarks returned by dlib,
    2. then reproduce recreate_aligned_images(), as detailed in FFHQ pre-processing code.

Face landmarks

FFHQ pre-processing

Finally, the pre-processed image can be projected to the latent space of the StyleGAN2 model trained with configuration f on the Flickr-Faces-HQ (FFHQ) dataset.

Results: influence of pre-processing

NB: results are different if the code is run twice, even if the same pre-processing is used.

With center-cropping as sole pre-processing

The result below is obtained with center-cropping as sole pre-processing, hence some issues with the projection.

Projection (with issues) as GIF

From left to right: the target image, the result obtained at the start of the projection, and the final result of the projection.

Target imageProjected image n°1/5Projected image n°5/5

From left to right: the target image, the result obtained at the start of the projection, intermediate results, and the final result.

Projection results (with issues) as PNG

The background, the hair, the ears, and the suit are relatively well reproduced, but the face is wrong, especially the neck (in the original image) is confused with the chin (in the projected images). It is possible that the face is too small relatively to the rest of the image, compared to the FFHQ training dataset, hence the poor results of the projection.

With the same pre-processing as for the FFHQ dataset

The result below is obtained with the same pre-processing as for the FFHQ dataset, which allows to avoid the projection issues mentioned above.

Projection (without issues) as GIF

From left to right: the target image, the result obtained at the start of the projection, and the final result of the projection.

Target imageProjected image n°1/5Projected image n°5/5

From left to right: the target image, the result obtained at the start of the projection, intermediate results, and the final result.

Projection results as PNG

Results: comparison with the extended projection

For the rest of the repository, the same-preprocessing as for the FFHQ dataset is used.

Shared data on Google Drive

Additional projection results are shown on the Wiki.

To make it easier to download them, they are also shared on Google Drive.

The directory structure is as follows:

stylegan2_projections/
├ aligned_images/
├ └ emmanuel-macron_01.png    # FFHQ-aligned image
├ generated_images_no_tiled/  # projections with `W(18,*)`
├ ├ emmanuel-macron_01.npy    # - latent code
├ └ emmanuel-macron_01.png    # - projected image
├ generated_images_tiled/     # projections with `W(1,*)`
├ ├ emmanuel-macron_01.npy    # - latent code
├ └ emmanuel-macron_01.png    # - projected image
├ aligned_images.tar.gz             # folder archive
├ generated_images_no_tiled.tar.gz  # folder archive
â”” generated_images_tiled.tar.gz     # folder archive

Projection results

Images below allow us to compare results obtained with the original projection W(1,*) and the extended projection W(18,*).

A projected image obtained with W(18,*) is expected to be closer to the target image, at the expense of semantics.

If image fidelity is very important, W(18,*) can be run for a higher number of iterations (default is 1000 steps), but truncation might be needed for later applications.

French politicians

From top to bottom: aligned target image, projection with W(1,*), projection with W(18,*).

Aligned target imageAligned target imageAligned target image

W1 projected imageW1 projected imageW1 projected image

W18 projected imageW18 projected imageW18 projected image

From top to bottom: aligned target image, projection with W(1,*), projection with W(18,*).

Aligned target imageAligned target imageAligned target image

W1 projected imageW1 projected imageW1 projected image

W18 projected imageW18 projected imageW18 projected image

Art

From top to bottom: aligned target image, projection with W(1,*), projection with W(18,*).

Aligned target imageAligned target imageAligned target image

W1 projected imageW1 projected imageW1 projected image

W18 projected imageW18 projected imageW18 projected image

Applications

In the following, we assume that real images have been projected, so that we have access to their latent codes, of shape (1, 512) or (18, 512) depending on the projection method.

There are three main applications:

  1. morphing (linear interpolation),
  2. style transfer (crossover),
  3. expression transfer (adding a vector and a scaled difference vector).

Shared data on Google Drive

Results corresponding to each application are:

The directory structure is as follows:

stylegan2_editing/
├ expression/                   # expression transfer
| ├ no_tiled/                   # - `W(18,*)`
| | └ expression_01_age.jpg     # face n°1 ; age
| â”” tiled/                      # - `W(1,*)`
|   â”” expression_01_age.jpg
├ morphing/                     # morphing
| ├ no_tiled/                   # - `W(18,*)`
| | └ morphing_07_01.jpg        # face n°7 to face n°1
| â”” tiled/                      # - `W(1,*)`
|   â”” morphing_07_01.jpg
├ style_mixing/                 # style transfer
| ├ no_tiled/                   # - `W(18,*)`
| | â”” style_mixing_42-07-10-29-41_42-07-22-39.jpg
| â”” tiled/                      # - `W(1,*)`
|   â”” style_mixing_42-07-10-29-41_42-07-22-39.jpg
├ video_style_mixing/           # style transfer
| ├ no_tiled/                   # - `W(18,*)`
| | â”” video_style_mixing_000.000.jpg
| ├ tiled/                      # - `W(1,*)`
| | â”” video_style_mixing_000.000.jpg
| ├ no_tiled_small.mp4          # with 2 reference faces
| ├ no_ tiled.mp4               # with 4 reference faces
| ├ tiled_small.mp4
| â”” tiled.mp4
├ expression_transfer.tar.gz    # folder archive
├ morphing.tar.gz               # folder archive
├ style_mixing.tar.gz           # folder archive
â”” video_style_mixing.tar.gz     # folder archive

1. Morphing

Morphing consists in a linear interpolation between two latent vectors (two faces).

Results are shown on the Wiki.

With the original projection W(1,*)

Morphing Morphing

Morphing Morphing

With the extended projection W(18,*)

Morphing Morphing

Morphing Morphing

2. Style transfer

Style transfer consists in a crossover of latent vectors at the layer level (cf. this piece of code).

There are 18 layers for the generator. The latent vector of the reference face is used for the first 7 layers. The latent vector of the face whose style has to be copied is used for the remaining 11 layers.

Results are shown on the Wiki.

With the original projection W(1,*)

Thanks to morphing of the faces whose style is copied, style transfer can be watched as a video.

Style Transfer

With the extended projection W(18,*)

Thanks to morphing of the faces whose style is copied, style transfer can be watched as a video.

Style Transfer

3. Expression transfer

Expression transfer consists in the addition of:

  • a latent vector (a face),
  • a scaled difference vector (an expression).

Expressions were defined, learnt, and shared on Github by a Chinese speaker:

  1. age
  2. angle_horizontal
  3. angle_pitch
  4. beauty
  5. emotion_angry
  6. emotion_disgust
  7. emotion_easy
  8. emotion_fear
  9. emotion_happy
  10. emotion_sad
  11. emotion_surprise
  12. eyes_open
  13. face_shape
  14. gender
  15. glasses
  16. height
  17. race_black
  18. race_white
  19. race_yellow
  20. smile
  21. width

Results are shown on the Wiki.

With the original projection W(1,*)

  • Age: Expression Transfer

  • Smile: Expression Transfer

  • Age: Expression Transfer

  • Smile: Expression Transfer

With the extended projection W(18,*)

  • Age: Expression Transfer

  • Smile: Expression Transfer

  • Age: Expression Transfer

  • Smile: Expression Transfer

References

More Repositories

1

stable-diffusion-colab

Colab notebook for Stable Diffusion Hyper-SDXL.
Jupyter Notebook
319
star
2

steam-market

Find arbitrages on the Steam Market.
Python
158
star
3

finetune-detr

Fine-tune Facebook's DETR (DEtection TRansformer) on Colaboratory.
Jupyter Notebook
136
star
4

SimSwap-colab

Colab notebooks to apply SimSwap to images, animated GIF, and videos.
Jupyter Notebook
86
star
5

download-steam-reviews

Download Steam reviews for any game. Available on PyPI.
Python
60
star
6

hidden-gems

Ranking of Steam games which favors "hidden gems". Featured in PC Gamer.
Python
42
star
7

deep-learning-resources

Books, courses, videos and blogs, mostly about Deep Learning.
36
star
8

steam-stylegan2

Train a StyleGAN2 model on Colaboratory to generate Steam banners.
Jupyter Notebook
32
star
9

steam-stylegan2-ada

Train a StyleGAN2-ADA model on Colaboratory to generate Steam banners.
Jupyter Notebook
32
star
10

steamspypi

SteamSpyPI: an API for SteamSpy. Available on PyPI.
Python
29
star
11

steam-portal

Portal to my Github repositories to analyze Steam data.
21
star
12

glide-text2im-colab

Colab notebook for openai/glide-text2im.
Jupyter Notebook
21
star
13

steam-reviews

Statistics of Steam reviews.
Python
18
star
14

steam-api

Analysis of the Steam store (attributes, categories, genres).
Python
12
star
15

google-colab-transfer

Transfer data between Colab and Drive. Available on PyPI.
Python
12
star
16

steam-image-search

Search for images on Steam using natural language queries.
Jupyter Notebook
11
star
17

epic-games-achievements

Achievements of games on the Epic Games store (EGS).
Python
10
star
18

playing-with-simpleitk

Playing with SimpleITK and nnU-Net to process data from the CHAOS challenge on Google Colab.
Jupyter Notebook
9
star
19

steam-reviews-to-sales

Study the relationship between review numbers and sales for Steam games
Python
9
star
20

steampi

SteamPI: a simple API for Steam. Available on PyPI.
Python
9
star
21

steam-stylegan2-ada-pytorch

Train a StyleGAN2-ADA (PyTorch) model on Colab to generate Steam banners.
Jupyter Notebook
8
star
22

steampy-buy-orders

Automatically set buy orders on the Steam Market with steampy.
Python
8
star
23

google-colab

Train a DCGAN model on Colaboratory to generate Steam banners.
Jupyter Notebook
8
star
24

egs-15DaysofGames

Datamining of 15 Days of (free) Games at the Epic Games Store (EGS).
Python
7
star
25

steam-trade-finder

Find Steam trades for a given appID.
Python
7
star
26

gamedatacrunch

gdc: an API for GameDataCrunch. Available on PyPI.
Python
6
star
27

steam-labs-recommender

Tweak the Interactive Recommender of Steam Labs.
Python
6
star
28

SensCritique-Takeout

SensCritique Takeout. Available on PyPI.
Python
6
star
29

steam-CLIP

Retrieve Steam games with similar store banners, with OpenAI's CLIP.
Jupyter Notebook
6
star
30

steam-stylegan

Train a StyleGAN model on Colaboratory to generate Steam banners.
Jupyter Notebook
5
star
31

puissance4

AI for the game "Connect Four". Available on PyPI.
Python
5
star
32

humble-monthly

Analysis of Humble Monthly Bundles.
Python
5
star
33

steam-api-data

Data consisting of Steam app details.
Batchfile
5
star
34

steam-cut

Count reviews for Steam and non-Steam purchases. Featured in Ars Technica.
Python
5
star
35

stable-diffusion-safety-checker

Python package to apply the Safety Checker from Stable Diffusion.
Python
5
star
36

sample-steam-reviews-with-gpt-2

Sample Steam reviews with the GPT-2 language model.
Jupyter Notebook
4
star
37

egs-datamining

Datamining of Epic Games Store (EGS).
Python
4
star
38

steam-filtered-image-data

Build the Steam-OneFace dataset.
Jupyter Notebook
4
star
39

heroku-clip

Deploy an API built with Streamlit on Heroku, in order to classify images.
Python
4
star
40

recent-sales

Evaluation of sales and revenue generated by Steam games during release week.
Python
4
star
41

sample-steam-descriptions

Sample Steam descriptions with the GPT-2 language model.
Jupyter Notebook
4
star
42

add-free-licenses

Automatically add free licenses on Steam.
Python
4
star
43

match-steam-banners

Retrieve Steam games with similar store banners, with MobileNet v3.
Python
4
star
44

regression

Gaussian Process Regression vs. Relevance Vector Machine.
Python
4
star
45

steam-points-shop

Find games which have been removed from the Steam Points Shop.
Python
4
star
46

steam-store-snapshots

Data snapshots of app lists on Steam.
Python
3
star
47

epic-games-gift-tracker

Track the upcoming gift wrappings on the Epic Games store (EGS).
Python
3
star
48

sample-steam-reviews

Sample Steam reviews with different approaches.
Jupyter Notebook
3
star
49

steam-tag-mapping

Mapping of Steam tags.
Python
3
star
50

steam-reviews-data

Data available to compute statistics of Steam reviews.
3
star
51

steam-next-fest

Play demos for the Steam Next Fest.
Python
3
star
52

Steam-Bayesian-Average

Bayesian average of games, developers, and publishers.
Python
3
star
53

shap-e-colab

Colab notebook to run OpenAI's Shape-E.
Jupyter Notebook
2
star
54

epic-games-tracker

Track the numbers of players and ratings on the Epic Games store (EGS).
Python
2
star
55

download-steam-banners-data

Data consisting of Steam banners.
2
star
56

steam-popular-appids

Find a list of popular Steam appIDs.
Python
2
star
57

steam-descriptions

Retrieve semantically similar Steam games.
Python
2
star
58

Classification-binaire

Modèles de classification binaire.
Python
2
star
59

epic-games-promotion-tracker

Track the upcoming promotional offers on the Epic Games store (EGS).
Python
2
star
60

match-steam-tags

Match Steam tags with appIDs. Available on PyPI.
Python
2
star
61

steam-lightweight-gan

Train a "lightweight GAN" on Colaboratory to generate Steam banners.
Jupyter Notebook
2
star
62

steam-DINOv2

Retrieve Steam games with similar store banners, with Facebook's DINOv2.
Jupyter Notebook
2
star
63

dungleon

A study of the hidden rules of Dungleon.
Python
2
star
64

heroku-flask-api

Deploy an API built with Flask on Heroku, in order to match game banners.
Python
1
star
65

woctezuma.github.io

JavaScript
1
star
66

download-steam-screenshots-data

Data consisting of Steam screenshots.
1
star
67

steam-groups

Analysis of the libraries of members of Steam groups.
Python
1
star
68

metacouncil-goty

MetaCouncil's PC Games of the Year Awards 2018-20, Decade Awards 201X.
Python
1
star
69

steam-DINO

Retrieve Steam games with similar store banners, with Facebook's DINO.
Jupyter Notebook
1
star
70

steam-hype

Find the most followed upcoming Steam games.
Python
1
star
71

steam-svelte-autocomplete

Auto-complete Steam game names with Svelte.
JavaScript
1
star
72

steam-era-goty

SteamEra Games of the Year Awards 2017.
Python
1
star
73

steam-face-detection

Detect faces on Steam store/library banners.
Jupyter Notebook
1
star
74

steam-BEiT

Retrieve Steam games with similar store banners, with Microsoft's Bidirectional Encoder representation from Image Transformers (BEiT).
Jupyter Notebook
1
star
75

download-steam-screenshots

Retrieve Steam games with similar store screenshots, with MobileNet v1.
Python
1
star
76

download-steam-banners

Retrieve Steam games with similar store banners, with MobileNet v1.
Python
1
star
77

epic-games-ratings

Bayesian average ratings of games on the Epic Games store (EGS).
Python
1
star
78

openai-retro-contest

My entry for OpenAI Retro Contest.
Python
1
star
79

geforce-leak

Fetch data from the Geforce leak.
Python
1
star
80

heroku-levenshtein-api

Deploy an API built with Flask on Heroku, in order to match game names.
Python
1
star
81

steam-curator-release-dates

Query Steam API to reveal hidden release dates for a Curator list.
Python
1
star
82

steam-palette-extractor

Find the dominant / most common colors in game banners on the Steam store.
Python
1
star
83

3b1b-wordle-solver

Solve Wordle and Dungleon puzzles with information theory.
Python
1
star
84

bundle-split

Fetch bundle content, then split cost based on prices at keyshop resellers.
Python
1
star