Skip to content

yubin1219/text2image_manipulation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Text to Image Manipulation

Multi modal learning project

  • Using OpenAI CLIP (Contrastive Language-Image Pre-Training) [paper] [code]
  • Based on StyleCLIP (StyleCLIP: Text-Driven Manipulation of StyleGAN Imagery) [paper] [code]

Latent Optimization

  • G(w)를 CLIP의 image encoder에 넣어 임베딩 값을 구하고 CLIP의 text encoder에서 임베딩된 값과 코사인 유사도를 계산하여 loss로 추가 사용
  • G(w) 임베딩 값이 text 임베딩 값과 유사해지는 방향으로 gradient decent를 이용해 latent vector w를 update
  • text 의미에 맞게 이미지 manipulation은 잘 하지만 매번 몇 분의 optimization과정이 필요

Global Directions

  • Text prompt를 style space S의 single, global direction과 mapping하는 방법
  • Text prompt가 주어지면 CLIP을 통해 original text와 target text의 임베딩 차이인 delta t를 구한 후 기존 style과 변경된 style의 차이인 delta s에 mapping
  • 수용할 수 있는 text의 범위가 넓음

Latent Mapper

이번 프로젝트에서 개선한 Method

  • Mapper는 latent vector w를 text의 의미에 맞게 변화시키는 방향으로 Training

  • Training Process

    1. Encoder를 통해 original image의 latent vector를 얻는다.
    2. Encoder를 통해 얻은 latent vector는 text embedding vector와 summaiton 또는 concatenate되어 Mapper의 input으로 들어간다.
    3. Mapper를 통해 얻은 latent vector와 기존 latent vector w는 summation되어 StyleGAN2의 input으로 들어간다.
    4. StyleGAN2에서 생성한 이미지 G(w_hat)과 text 간의 유사도(Similarity Loss)를 CLIP model을 통해 구한다.
    5. Mapper는 G(w_hat)과 text 간의 유사도를 최소화시키는 latent vector를 뽑아내도록 학습된다.
  • Advantages

    • Mapper를 Training 시킴으로써 매번 optimization과정을 거쳐야 하는 Latent Optimization 방식의 단점을 보완하였다.
    • text embedding vector를 사용하여 기존 Mapper의 단점을 개선하였다.
    • 하나의 모델에서 multi style을 learning 하는 효율적인 방법이다.
  • 단, 학습되어 있지 않은 style은 수행하지 못한다.

Method 1. Use text embedding vector obtained from torch.nn.embedding


Method 2. Use text embedding vector obtained from CLIP text encoder


Method 3. Use Single Model for Multi Style Combination


  • 이전 method들은 하나의 모델에서 multi style을 학습하기는 하지만 그 style들이 결합된 상태는 학습하지 못한다.
  • 이 method는 하나의 모델에서 각각의 style과 그 style들이 결합된 상태까지 Training한다.
  • Inference 속도를 단축한다.

Setup

  • install CLIP
pip install ftfy regex tqdm gdown
pip install git+https://github.com/openai/CLIP.git

Usage

Inference

Guidance

  • Google drive addresses of pretrained weights and dataset download function are placed in mapper/utils.py
  • If --weights_download is set to True, the dataset and weights required in the inference process are automatically downloaded.
  • Inference argument is placed in mapper/options/test_options.py
  • --num means which late vector to use. default = 60
  • --modulesand --texts must be paired.
  • Images generated during inference are stored in the form of result_[input texts].png
  • --new_latent means whether to use the image you have. default = False
    • Enter image file path in --new_image_path. ex) "ubin.jpg"
    • "e4e_ffhq_encode.pt" and "shape_predictor_68_face_landmarks.dat" files required during the encoder process can be automatically downloaded from the code. If it's impossible, you can download it in the Google drive folder [styleclip]

1. Multi Model Combination



modules texts
celeb_female ["Elsa", "Anna", "Emma_Stone", "Anne_Hathaway", "Scarlett_Johansson"]
celeb_male ["Ironman", "Dicaprio", "Zuckerberg", "Tom_Holland"]
hair_sum or hair_cat ["curly", "wavy", "long", "bobcut" , "bangs"]
color_sum or hair_cat ["blonde", "pink", "blue", "black"]
Disney_clip ["Elsa", "Anna", "Rapunzel", "Ariel"]
hair_clip ["wavy", "long", "bangs", "bobcut"]
color_clip ["blonde", "red", "pink", "blue", "purple", "brown", "black"]

Example for testing a mapper for 3 modules combination:

cd mapper
python inference.py --latent_path "test_female.pt" --modules celeb_female --modules hair_sum --modules color_sum --texts Emma_Stone --texts wavy --texts pink

2. Single Model Learned Multi-Style Combination



texts
["Elsa" , "Emma_Watson", "wavy", "bangs", "pink", "black"]

Example for testing a mapper for single model learned multi-style:

cd mapper
python inference_single.py --latent_path "test_female.pt" --texts Emma_Watson --texts wavy --texts black

Dataset

  • CelebA-HQ images are converted into latent vectors through encoder4editing and used as a dataset.
  • Through the CLIP model, each image of the train set and test set is classified into female or male and used.
Dataset Description
train set 총 24179개의 latent vector
test set 총 2824개의 latent vector
train_female 총 15332개의 latent vector
train_male 총 8844개의 latent vector
test_female 총 1899개의 latent vector
test_male 총 925개의 latent vector

Using t-SNE for vector visualization

Each modules - celeb / hair style / color

Single model learned multi-style combination


Examples of Result

Latent Optimization

Input Image / "He is a young boy" / "He is smiling"

Latent Mapper

  • Results of Changed Hair Style

  • Results of Changed Hair Color

  • Results of Male celeb style

  • Results of Multi-Styling

Releases

No releases published

Packages

No packages published