GithubHelp home page GithubHelp logo

mindslab-ai / faceshifter Goto Github PK

View Code? Open in Web Editor NEW
593.0 19.0 116.0 99.13 MB

Unofficial PyTorch Implementation for FaceShifter (https://arxiv.org/abs/1912.13457)

License: BSD 3-Clause "New" or "Revised" License

Dockerfile 1.53% Python 98.47%
pytorch pytorch-lightning face-swapping

faceshifter's Introduction

FaceShifter — Unofficial PyTorch Implementation

issueBadge starBadge repoSize lastCommit

Unofficial Implementation of FaceShifter: Towards High Fidelity And Occlusion Aware Face Swapping with Pytorch-Lightning. In the paper, there are two networks for full pipe-line, AEI-Net and HEAR-Net. We only implement the AEI-Net, which is main network for face swapping.

Take a look HifiFace, our implementation of a more recent face swapping model.

Datasets

Preparing Data

You need to download and unzip:

Preprocess Data

Preprocessing code is mainly based on Nvidia's FFHQ preprocessing code. You may modify our preprocess with multi-processing functions to finish pre-processing step much faster.

# build docker image from Dockerfile
docker build -t dlib:0.0 ./preprocess
# run docker container from image
docker run -itd --ipc host -v /PATH_TO_THIS_FOLDER/preprocess:/workspace -v /PATH_TO_THE_DATA:/DATA -v /PATH_TO_SAVE_DATASET:/RESULT --name dlib --tag dlib:0.0
# attach
docker attach dlib
# preprocess with dlib
python preprocess.py --root /DATA --output_dir /RESULT

Training

Configuration

There is yaml file in the config folder. They must be edited to match your training requirements (dataset, metadata, etc.).

  • config/train.yaml: Configs for training AEI-Net.
    • Fill in the blanks of: dataset_dir, valset_dir
    • You may want to change: batch_size for GPUs other than 32GB V100, or chkpt_dir to save checkpoints in other disk.

Using Docker

We provide a Dockerfile for easier training environment setup.

docker build -t faceshifter:0.0 .
docker run -itd --ipc host --gpus all -v /PATH_TO_THIS_FOLDER:/workspace -v /PATH_TO_DATASET:/DATA --name FS --tag faceshifter:0.0
docker attach FS

Pre-trained Arcface

During the training process, pre-trained Arcface is required. We provide our pre-trained Arcface model; you can download at this link

Command

To train the AEI-Net, run this command:

python aei_trainer.py -c <path_to_config_yaml> -g <gpus> -n <run_name>
# example command that might help you understand the arguments:
# train from scratch with name "my_runname"
python aei_trainer.py -c config/train.yaml -g 0 -n my_runname

Optionally, you can resume the training from previously saved checkpoint by adding -p <checkpoint_path> argument.

Monitoring via Tensorboard

The progress of training with loss values and validation output can be monitored with Tensorboard. By default, the logs will be stored at log, which can be modified by editing log.log_dir parameter at config yaml file.

tensorboard --log_dir log --bind_all # Scalars, Images, Hparams, Projector will be shown.

Inference

To inference the AEI-Net, run this command:

python aei_inference.py --checkpoint_path <path_to_pre_trained_file> --target_image <path_to_target_image_file> --source_image <path_to_source_image_file> --output_path <path_to_output_image_file> --gpu_num <number of gpu>
# example command that might help you understand the arguments:
# train from scratch with name "my_runname"
python aei_inference.py --checkpoint_path chkpt/my_runname/epoch=0.ckpt --target_image target.png --source_image source.png --output_path output.png --gpu_num 0

We probived colab example. You can use it with your own trained weight.

Results

Comparison with results from original paper

Figure in the original paper

Our Results

Reminds you that we only implement the AEI-Net, and the results in the original paper were generated by AEI-Net and HEAR-Net.

We will soon release the FaceShifter in our cloud API service, maum.ai

License

BSD 3-Clause License.

Implementation Author

Changho Choi @ MINDs Lab, Inc. ([email protected])

Paper Information

@article{li2019faceshifter,
  title={Faceshifter: Towards high fidelity and occlusion aware face swapping},
  author={Li, Lingzhi and Bao, Jianmin and Yang, Hao and Chen, Dong and Wen, Fang},
  journal={arXiv preprint arXiv:1912.13457},
  year={2019}
}

faceshifter's People

Contributors

m00-m00 avatar usingcolor avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

faceshifter's Issues

GPU Memory

Hi :
I‘m wondering how much GPU memory is needed for training with batch size 1. I'm training with 2080Ti 11G, It always CUDA out of memory even when batch size is 1

multi GPU training

hi, thanks for your great code.
I have a problem when using 2 GPUs.
with 1 GPU the speed of the training process is about 0.75 s/it (according to progress bar)
and with 2 GPUs it is about 1.33 s/it. and since the whole iterations are halved with 2 GPUs, consequently, one epoch would take almost the same time in both cases (1 and 2 GPUs)
would you please help me to find out what the problem is.
thanks alot

Pretrained model?

Hello! First of all thank you for your work, is incredible! 🥇

Is there any final pre-trained model available for download? or do you plan to add it in the close future? Thanks a lot! :)

Training with only CelebHQ dataset

Thanks for the great work!

When I try to train the AEI-Net with 30k images from celebHQ dataset using 6 P40 32G GPUs, I got the training curve as below:
Screenshot from 2020-11-23 11-40-54
Screenshot from 2020-11-23 11-41-05
Screenshot from 2020-11-23 11-41-17
Screenshot from 2020-11-23 11-41-25
Screenshot from 2020-11-23 11-41-34
Screenshot from 2020-11-23 11-41-41

All the other setting are set by default and the generated swap faces are also weird:
00001
00002
output2

Should I continue training or any sugguestions? Thanks in advance!

CUDA out of memory.

Hi, can someone tell me what can I do to fix this issues?
This is what I get when running it on local machine or google colab.

2020-11-05 11:56:07.097272: I tensorflow/stream_executor/platform/default/dso_loader.cc:48] Successfully opened dynamic library libcudart.so.10.1
GPU available: True, used: True
TPU available: False, using: 0 TPU cores
CUDA_VISIBLE_DEVICES: [0]
/usr/local/lib/python3.6/dist-packages/pytorch_lightning/utilities/distributed.py:37: UserWarning: WORLD_SIZE environment variable (2) is not equal to the computed world size (1). Ignored.
  warnings.warn(*args, **kwargs)
initializing ddp: GLOBAL_RANK: 0, MEMBER: 1/1
----------------------------------------------------------------------------------------------------
distributed_backend=ddp
All DDP processes registered. Starting ddp with 1 processes
----------------------------------------------------------------------------------------------------
/usr/local/lib/python3.6/dist-packages/pytorch_lightning/utilities/distributed.py:37: UserWarning: Could not log computational graph since the `model.example_input_array` attribute is not set or `input_array` was not given
  warnings.warn(*args, **kwargs)

  | Name     | Type                        | Params
---------------------------------------------------------
0 | G        | ADDGenerator                | 372 M 
1 | E        | MultilevelAttributesEncoder | 67 M  
2 | D        | MultiscaleDiscriminator     | 8 M   
3 | Z        | ResNet                      | 43 M  
4 | Loss_GAN | GANLoss                     | 0     
5 | Loss_E_G | AEI_Loss                    | 0     
Validation sanity check: 0it [00:00, ?it/s]/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:2494: UserWarning: Default upsampling behavior when mode=bilinear is changed to align_corners=False since 0.4.0. Please specify align_corners=True if the old behavior is desired. See the documentation of nn.Upsample for details.
  "See the documentation of nn.Upsample for details.".format(mode))
Epoch 0:   0% 0/5000250 [00:00<?, ?it/s] Traceback (most recent call last):
  File "aei_trainer.py", line 62, in <module>
    main(args)
  File "aei_trainer.py", line 40, in main
    trainer.fit(model)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/states.py", line 48, in wrapped_fn
    result = fn(self, *args, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/trainer.py", line 1058, in fit
    results = self.accelerator_backend.spawn_ddp_children(model)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/accelerators/ddp_backend.py", line 123, in spawn_ddp_children
    results = self.ddp_train(local_rank, mp_queue=None, model=model, is_master=True)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/accelerators/ddp_backend.py", line 224, in ddp_train
    results = self.trainer.run_pretrain_routine(model)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/trainer.py", line 1239, in run_pretrain_routine
    self.train()
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/training_loop.py", line 394, in train
    self.run_training_epoch()
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/training_loop.py", line 491, in run_training_epoch
    batch_output = self.run_training_batch(batch, batch_idx)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/training_loop.py", line 844, in run_training_batch
    self.hiddens
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/training_loop.py", line 1015, in optimizer_closure
    hiddens)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/trainer/training_loop.py", line 1197, in training_forward
    output = self.model(*args)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/usr/local/lib/python3.6/dist-packages/pytorch_lightning/overrides/data_parallel.py", line 170, in forward
    output = self.module.training_step(*inputs[0], **kwargs[0])
  File "/content/faceshifter/aei_net.py", line 54, in training_step
    output, z_id, output_z_id, feature_map, output_feature_map = self(target_img, source_img)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/content/faceshifter/aei_net.py", line 44, in forward
    output = self.G(z_id, feature_map)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/content/faceshifter/model/AEINet.py", line 132, in forward
    x = self.model["layer_7"](x, z_att[7], z_id)
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/content/faceshifter/model/AEINet.py", line 98, in forward
    x1 = self.activation(self.add1(h_in, z_att, z_id))
  File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 541, in __call__
    result = self.forward(*input, **kwargs)
  File "/content/faceshifter/model/AEINet.py", line 72, in forward
    h_out = (1-m)*a + m*i
RuntimeError: CUDA out of memory. Tried to allocate 256.00 MiB (GPU 0; 14.73 GiB total capacity; 12.56 GiB already allocated; 45.88 MiB free; 1.17 GiB cached)
Epoch 0:   0%|          | 0/5000250 [00:36<?, ?it/s]

colab example?

You can provide one for colab , I would appreciate it.

The implementation of ID Loss seems different from the original paper

At first, thank you for your nice work, it helped me a lot!
But when I was reading your code, I found a problem: in the original paper, id loss is calculated by the cos similarity, while in this implementation, it's calculated by bmm or inner product.
Would like to know the reason why change cosine loss to bmm, and the influence on the final result. much appreciate it if you would reply.

Training Iterations

Hello again, thanks for sharing code. I am trying to train. I have 200k dataset but tqdm shows process finish in 125 hours. Because 1 epoch need 2 millions 206k iterations. How may i change this?

Suggestion of training epochs?

Thanks for the implementation! Well done!
I am about to use your code to train my own model. I am curious how long it took your model to be trained? How many epochs did you use?

Thanks!

Coefficients of loss

Thank you for sharing such an excellent project. I would like to ask what are the coefficients of loss? I tried the coefficients in the author's paper, but the effect is not very satisfactory. Could you please share your coefficient below? Thank you very much! ! !

where is val set

i have aproblem that what is the valset_dir?there is only one data file after process is that output_dir

A question about train step

Excuse me, my training data set has only 1000 pictures, And I found len(AEI_Dataset) = len(self.files) * 5 = 5000 my batch size = 20. So train step should be 250,but why train step is 125000250 。

help with training

Thanks for the awesome code! I am training my own model right now and have a few questions:

  • currently I am using 100k (out of around 1.8m) images from CelebAMask-HQ, ffhq and vggface to train the model. did you use the full set to train your model?
  • I didn't see large improvement for most losses anymore (160k steps trained, 4gpus x 12images/batch); is this normal? should I just continue training for more steps?
    image
    image
    image
    image
    image
  • I also checked the validation results, and the reconstruction is not good.
    image
    image
    image
  • I noticed shuffle for the training dataloader is not set to True, did you use the same setting?

Thanks!

Killed Error on Training

Hello, thanks for sharing codes. I tried to train with batch size 2, 4, 8 and 16 seperately on v100 16GB but i got an error "Validation sanity check: 0it [00:00, ?it/s]Killed". Do you know what is the problem?

Should affine be set as False in your ADD layers?

Hi, Thanks for your great implementation!

In the original paper, batch normalization is conducted without affine parameters (they are replaced with attributes and identities modulation parameters).

So in this way, we should explicitly set the flag of affine of BN in the ADD layers as false as follows,

self.BNorm = nn.BatchNorm2d(h_inchannel, affine=False)

Thanks for your response.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.