[PYTHON] I tried to implement StarGAN (1)

I tried to implement StarGAN on Linux

This time, I tried to implement StarGAN. Basically, we will implement it based on the code published on github. On this page, we will explain and implement a light paper. I would like to do it next time when I apply it using my own dataset.

--About StarGAN Super easy --Implementation on Linux

It's easy, but I'll explain it according to the above two items.

Super easy about StarGAN

Paper: https://arxiv.org/abs/1711.09020

** StarGAN ** is a type of Generative Adversarial Netwok (GAN) that enables conversion between different domains ** CycleGAN ** and GAN with multi-class classification learning ** AC-GAN It is a combination of **.

** CycleGAN ** can only do domain conversion between two domains. In order to achieve domain conversion between two or more k domains, k (k-1) Generators must be trained. This is practically possible, but annoying. コメント 2020-05-22 234300.png This image is from the paper, but if k = 4, it means that 4 * 3 = 12 Generators are needed.

To solve such a problem, StarGAN has introduced an algorithm that converts multiple domains with one Generator. コメント 2020-05-22 234328.png If you have 5 domains, they will look like the image above. It has a star shape. That's why it's StarGAN. Somehow you can tickle your heart.

There was a polite explanation other articles, so I will share it. (I can't explain any more)

Implementation on Linux

Public code https://github.com/yunjey/StarGAN

Implementation environment

Implemented on public dataset

First, clone git to any directory. Then change to the StarGAN / directory. This time, we will download the CelebA dataset and RaFD dataset that were also used in the paper.

$ git clone https://github.com/yunjey/StarGAN.git
$ cd StarGAN/
$ bash download.sh celeba

The download is complete.

  .
  .
  .
  inflating: ./data/celeba/images/072137.jpg  
  inflating: ./data/celeba/images/027742.jpg  
  inflating: ./data/celeba/images/188764.jpg  
  inflating: ./data/celeba/list_attr_celeba.txt 

This dataset called CelebA dataset is here Is published.

This dataset contains 202,599 celebrity color facial images in 178 x 218 pixels. In addition to this, 40 types of attributes are assigned to each image. For example, Black_Hair, Blonde_Hair, Brown_Hair, Male, Young, etc. For more information, please refer to here.

コメント 2020-05-22 215619.png

The download directory will contain an image like the one above and a txt file called list_attr_celeba.txt with the attributes for each image. The contents here are

[Image folder name] 1 1 -1 1 -1 1 1 ... 1 -1

It has become. There are 1 or -1 for the number of attributes (40). When it is 1, it means that it is an attribute, and when it is -1, it means that it is not that attribute. Keep in mind that this needs to be generated when training with your own dataset.


** Learning **

First, let's learn StarGAN using CelebA dataset.

$ python main.py --mode train --dataset CelebA --image_size 128 --c_dim 5 \
               --sample_dir stargan_celeba/samples --log_dir stargan_celeba/logs \
               --model_save_dir stargan_celeba/models --result_dir stargan_celeba/results \
               --selected_attrs Black_Hair Blond_Hair Brown_Hair Male Young

Describes the command. --mode: mode --dataset: Dataset to use --image_size: image size --c_dim: number of classes of attribute --sample_dir: Folder where the sample is saved --log_dir: Folder to save logs --model_save_dir: Folder to save the model --result_dir: Folder to save results --selected_attrs: attribute you want to learn It's like that.

test

Next, we will test.

$ python main.py --mode test --dataset CelebA --image_size 128 --c_dim 5 \
               --sample_dir stargan_celeba/samples --log_dir stargan_celeba/logs \
               --model_save_dir stargan_celeba/models --result_dir stargan_celeba/results \
               --selected_attrs Black_Hair Blond_Hair Brown_Hair Male Young

It's easy to do.


Next, I wanted to download RaFD data as well, but I needed to apply. Perhaps even if you can apply, you will not be able to post it on this page either.

For those who find it difficult to learn, we will download the learned network.

$ bash download.sh pretrained-celeba-128x128

The downloaded model is saved in ./stargan_celeba_128/models. Next, use the following command to convert the image using the trained network.

$ python main.py --mode test --dataset CelebA --image_size 128 --c_dim 5 \
                 --selected_attrs Black_Hair Blond_Hair Brown_Hair Male Young \
                 --model_save_dir='stargan_celeba_128/models' \
                 --result_dir='stargan_celeba_128/results'

It's super easy.

The converted image is saved in ./stargan_celeba_128/results. This completes a simple implementation on a public dataset. Next time, I will apply it to the dataset I prepared.


Reference material

Paper: https://arxiv.org/pdf/1703.10593.pdf Github:https://github.com/yunjey/StarGAN CelebA:http://mmlab.ie.cuhk.edu.hk/projects/CelebA.html 7 Change! Change your face with AI: https://qiita.com/uyuutosa/items/5dd9b2b80a9aaca67050

Recommended Posts

I tried to implement StarGAN (1)
I tried to implement PCANet
I tried to implement Deep VQE
I tried to implement adversarial validation
I tried to implement hierarchical clustering
I tried to implement PLSA in Python
I tried to implement PLSA in Python 2
I tried to debug.
I tried to implement ADALINE in Python
I tried to implement PPO in Python
I tried to implement CVAE with PyTorch
I tried to paste
I tried to implement TOPIC MODEL in Python
I tried to implement selection sort in python
I tried to implement the traveling salesman problem
I tried to learn PredNet
I tried to organize SVM.
I tried to reintroduce Linux
I tried to introduce Pylint
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement and learn DCGAN with PyTorch
I tried to implement Minesweeper on terminal with python
I tried to implement a pseudo pachislot in Python
I tried to implement a recommendation system (content-based filtering)
I tried to implement Dragon Quest poker in Python
I tried to implement an artificial perceptron with python
I tried to implement time series prediction with GBDT
I tried to implement GA (genetic algorithm) in Python
I tried to implement Grad-CAM with keras and tensorflow
I tried to implement SSD with PyTorch now (Dataset)
I tried to implement automatic proof of sequence calculation
I tried to create Quip API
I tried to touch Python (installation)
I tried to explain Pytorch dataset
I tried Watson Speech to Text
I tried to touch Tesla's API
I tried to organize about MCMC.
I tried to move the ball
I tried to estimate the interval.
I tried to implement a basic Recurrent Neural Network model
I tried to implement anomaly detection by sparse structure learning
I tried to implement a one-dimensional cellular automaton in Python
I tried to implement breakout (deception avoidance type) with Quantx
[Django] I tried to implement access control by class inheritance.
I tried to implement ListNet of rank learning with Chainer
I tried to implement the mail sending function in Python
I tried to implement Harry Potter sort hat with CNN
I tried to implement Perceptron Part 1 [Deep Learning from scratch]
I tried to implement blackjack of card game in Python
I tried to implement SSD with PyTorch now (model edition)
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried using Azure Speech to Text.
I tried to create a linebot (preparation)
I tried to visualize AutoEncoder with TensorFlow
I tried scraping
I tried to recognize the wake word
I tried to get started with Hy
I tried PyQ
[Python] I tried to implement stable sorting, so make a note