/ghost-cpuonly

A new one shot face swap approach for image and video domains - version tailored to work on CPU

Primary LanguagePythonApache License 2.0Apache-2.0

[Paper] [Habr]

đź‘» GHOST: Generative High-fidelity One Shot Transfer

This is a fork of original GHOST repository tailored to work on CPU. Original code had many NVidia CUDA dependencies and needed modifications to be run on a CPU-only machine. The code is no longer GPU optimized (for example .cuda() invocations were hard-removed instead of introducing conditional code branches). Original code was using half precision floats, which are not supported by referenced libraries on CPU - code was changed to use standard float, which is less optimal from performance perspective, but does work on CPU.

Our paper "GHOST—A New Face Swap Approach for Image and Video Domains" has been published on IEEE Xplore.

Google Colab Demo

GHOST Ethics

Deepfake stands for a face swapping algorithm where the source and target can be an image or a video. Researchers have investigated sophisticated generative adversarial networks (GAN), autoencoders, and other approaches to establish precise and robust algorithms for face swapping. However, the achieved results are far from perfect in terms of human and visual evaluation. In this study, we propose a new one-shot pipeline for image-to-image and image-to-video face swap solutions - GHOST (Generative High-fidelity One Shot Transfer).

Deep fake synthesis methods have been improved a lot in quality in recent years. The research solutions were wrapped in easy-to-use API, software and different plugins for people with a little technical knowledge. As a result, almost anyone is able to make a deepfake image or video by just doing a short list of simple operations. At the same time, a lot of people with malicious intent are able to use this technology in order to produce harmful content. High distribution of such a content over the web leads to caution, disfavor and other negative feedback to deepfake synthesis or face swap research.

As a group of researchers, we are not trying to denigrate celebrities and statesmen or to demean anyone. We are computer vision researchers, we are engineers, we are activists, we are hobbyists, we are human beings. To this end, we feel that it's time to come out with a standard statement of what this technology is and isn't as far as us researchers are concerned.

  • GHOST is not for creating inappropriate content.
  • GHOST is not for changing faces without consent or with the intent of hiding its use.
  • GHOST is not for any illicit, unethical, or questionable purposes.
  • GHOST exists to experiment and discover AI techniques, for social or political commentary, for movies, and for any number of ethical and reasonable uses.

We are very troubled by the fact that GHOST can be used for unethical and disreputable things. However, we support the development of tools and techniques that can be used ethically as well as provide education and experience in AI for anyone who wants to learn it hands-on. Now and further, we take a zero-tolerance approach and total disregard to anyone using this software for any unethical purposes and will actively discourage any such uses.

Image Swap Results

Video Swap Results

Installation

NOTE: Configuration has been confirmed to work with python 3.8. Higher versions of python have troubles with getting pyTorch 1.6.0. Use venv or conda to set up the python version you need.

  1. Clone this repository
git clone https://github.com/sberbank-ai/sber-swap.git
cd sber-swap
git submodule init
git submodule update
  1. Install dependent packages
pip install -r requirements.txt

If it is not possible to install onnxruntime-gpu, try onnxruntime instead

  1. Download weights
sh download_models.sh
  1. Set environment variable
export PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python

Usage

  1. Colab Demo google colab logo or you can use jupyter notebook SberSwapInference.ipynb locally
  2. Face Swap On Video

Swap to one specific person in the video. You must set face from the target video (for example, crop from any frame).

python inference.py --source_paths {PATH_TO_IMAGE} --target_faces_paths {PATH_TO_IMAGE} --target_video {PATH_TO_VIDEO}

Swap to many person in the video. You must set multiple faces for source and the corresponding multiple faces from the target video.

python inference.py --source_paths {PATH_TO_IMAGE PATH_TO_IMAGE ...} --target_faces_paths {PATH_TO_IMAGE PATH_TO_IMAGE ...} --target_video {PATH_TO_VIDEO}
  1. Face Swap On Image

You may set the target face, and then source will be swapped on this person, or you may skip this parameter, and then source will be swapped on any person in the image.

python inference.py --target_path {PATH_TO_IMAGE} --image_to_image True

Training

We also provide the training code for face swap model as follows:

  1. Download VGGFace2 Dataset.
  2. Crop and align faces with out detection model.
python preprocess_vgg.py --path_to_dataset {PATH_TO_DATASET} --save_path {SAVE_PATH}
  1. Start training.
python train.py --run_name {YOUR_RUN_NAME}

We provide a lot of different options for the training. More info about each option you can find in train.py file. If you would like to use wandb logging of the experiments, you should login to wandb first --wandb login.

Tips

  1. For the first epochs we suggest not to use eye detection loss and scheduler if you train from scratch.
  2. In case of finetuning you can variate losses coefficients to make the output look similar to the source identity, or vice versa, to save features and attributes of target face.
  3. You can change the backbone of the attribute encoder and num_blocks of AAD ResBlk using parameters --backbone and --num_blocks.
  4. During the finetuning stage you can use our pretrain weights for generator and discriminator that are located in weights folder. We provide the weights for models with U-Net backbone and 1-3 blocks in AAD ResBlk. The main model architecture contains 2 blocks in AAD ResBlk.

Cite

If you use our model in your research, we would appreciate using the following citation

BibTeX Citation

@article{9851423,  
         author={Groshev, Alexander and Maltseva, Anastasia and Chesakov, Daniil and Kuznetsov, Andrey and Dimitrov, Denis},  
         journal={IEEE Access},   
         title={GHOST—A New Face Swap Approach for Image and Video Domains},   
         year={2022},  
         volume={10},  
         number={},  
         pages={83452-83462},  
         doi={10.1109/ACCESS.2022.3196668}
}

General Citation

A. Groshev, A. Maltseva, D. Chesakov, A. Kuznetsov and D. Dimitrov, "GHOST—A New Face Swap Approach for Image and Video Domains," in IEEE Access, vol. 10, pp. 83452-83462, 2022, doi: 10.1109/ACCESS.2022.3196668.