Git Product home page Git Product logo

3dswap's Introduction

3D-Aware Face Swapping
Official PyTorch implementation of the CVPR 2023 paper "3D-Aware Face Swapping"

Teaser image

3D-Aware Face Swapping
Yixuan Li, Chao Ma, Yichao Yan, Wenhan Zhu, Xiaokang Yang

Abstract: Face swapping is an important research topic in computer vision with wide applications in entertainment and privacy protection. Existing methods directly learn to swap 2D facial images, taking no account of the geometric information of human faces. In the presence of large pose variance between the source and the target faces, there always exist undesirable artifacts on the swapped face. In this paper, we present a novel 3D-aware face swapping method that generates high-fidelity and multi-view-consistent swapped faces from single-view source and target images. To achieve this, we take advantage of the strong geometry and texture prior of 3D human faces, where the 2D faces are projected into the latent space of a 3D generative model. By disentangling the identity and attribute features in the latent space, we succeed in swapping faces in a 3D-aware manner, being robust to pose variations while transferring fine-grained facial details. Extensive experiments demonstrate the superiority of our 3D-aware face swapping framework in terms of visual quality, identity similarity, and multi-view consistency. Project page: https://lyx0208.github.io/3dSwap

Requirements

  • Create and activate the Python environment:
    • conda create -n 3dSwap python=3.8
    • conda activate 3dSwap
    • pip install -r requirements.txt

Datasets preparation

  • We preprocess the images from the original FFHQ and CelebA-HD dataset with the data preprocessing code from EG3D, including re-cropping the images and extracting according camera poses.

    • To test on CelebA-HD dataset, please down our preprocessed data from here.

    • To test on your own images, please refer to the data preprocessing file of EG3D here.

Inference

Download our pretrained model from Baidu Disk or Goole Drive. Put model_ir_se50.pth under the "models" folder and other files under the "checkpoints" folder.

Then run:

python run_3dSwap.py

If you only want to perform the 3D GAN inversion without face swapping, run:

python run_inversion.py

Training

First, download the preprocessed FFHQ dataset from here and put it under the "datasets" folder.

To train the inversion module, run:

python -m torch.distributed.launch --nproc_per_node=4 --master_port=12345 train_inversion.py --exp_dir=inversion

To train the faceswapping module, run:

python -m torch.distributed.launch --nproc_per_node=4 --master_port=12345 train_faceswap.py --exp_dir=faceswap

Citation

@InProceedings{Li_2023_CVPR,
    author    = {Li, Yixuan and Ma, Chao and Yan, Yichao and Zhu, Wenhan and Yang, Xiaokang},
    title     = {3D-Aware Face Swapping},
    booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
    month     = {June},
    year      = {2023},
    pages     = {12705-12714}
}

Acknowledgements

3dswap's People

Contributors

lyx0208 avatar

Stargazers

Zvan avatar LeonGu avatar  avatar Pin Tang avatar Denghy avatar  avatar  avatar linxiangtan avatar  avatar Gogolian avatar Hardian Alkori avatar  avatar Adem Sreenivasula Reddy avatar Derrick avatar Liu Gongye avatar  avatar Steve Lu avatar  avatar  avatar  avatar Ciel avatar Yuki-Chai avatar  avatar  avatar  avatar Jianjin Xu avatar liheng avatar  avatar 杜双君 avatar  avatar Kaidong Zhang avatar  avatar funnyWang avatar 0xhephaistos avatar carwin avatar LiBiying avatar Broderick Stoudamire avatar Nikita avatar  avatar Phillip Chananda avatar clyang avatar Alakia avatar  avatar  avatar  avatar imomoe avatar  avatar Ho Woo Jang avatar Mayur Ingole avatar 姬忠鹏 avatar Dongjun Hwang avatar  avatar Sandalots avatar  avatar 爱可可-爱生活 avatar  avatar Qingzhe Gao avatar  avatar  avatar  avatar TaekyungKi avatar  avatar Tyler Luan avatar Oleksandr Tkachenko avatar Bruce Fan avatar Antonio Gonzalez  avatar  avatar  avatar zik avatar Snow avatar Hypochondira avatar Xirui Li avatar Guan Shanyan avatar Joyee avatar

Watchers

 avatar Chao Ma avatar Snow avatar Kostas Georgiou avatar Oleksandr Tkachenko avatar

3dswap's Issues

the training code can not predict reasonable result

I trained with the training code provided, but the network didn't seem to work properly

step 0 :
image

step 40000:
image

The last column is y_hat, but it seems to change into y_recon with training and stay there

the same result in ffhq dataset
image

what may be the problem?

the training code can not work properly

I use 4x3090 to run the training code, but I encountered an issue where the program became unresponsive during the importing process.
image
I use the debugger and find the program stuck in "from models.psp import pSp" => "from models.encoders import psp_encoders"=>"from models.stylegan2.model import EqualLinear"

the env is : torch 1.9.0+cu111 + torchaudio 0.9.0 + torchvision 0.10.0+cu111 + cuda 11.3

What could be the problem? Thank you!

How to test swapping on your own data?

Hello! Do you plan to make it possible to test face swap on your own faces like in other face swap projects and not just on the dataset? It would be nice and more interesting to be able to change faces in your own images. Best regards.

subprocess.CalledProcessError

D:\anaconda3\envs\3dSwap\lib\site-packages\torch\utils\cpp_extension.py:304: UserWarning: Error checking compiler version for cl: [WinError 2] The system cannot find the file specified
warnings.warn(f'Error checking compiler version for {compiler}: {error}')
INFO: Could not find files for the given pattern(s).
Traceback (most recent call last):
File "run_3dSwap.py", line 2, in
from models.faceswap_coach import FaceSwapCoach
File "F:\New folder\3dSwap\models\faceswap_coach.py", line 1, in
from models.encoders.psp_encoders import GradualStyleEncoder
File "F:\New folder\3dSwap\models\encoders\psp_encoders.py", line 8, in
from models.stylegan2.model import EqualLinear
File "F:\New folder\3dSwap\models\stylegan2\model.py", line 7, in
from models.stylegan2.op import FusedLeakyReLU, fused_leaky_relu, upfirdn2d
File "F:\New folder\3dSwap\models\stylegan2\op_init_.py", line 1, in
from .fused_act import FusedLeakyReLU, fused_leaky_relu
File "F:\New folder\3dSwap\models\stylegan2\op\fused_act.py", line 9, in
fused = load(
File "D:\anaconda3\envs\3dSwap\lib\site-packages\torch\utils\cpp_extension.py", line 1079, in load
return _jit_compile(
File "D:\anaconda3\envs\3dSwap\lib\site-packages\torch\utils\cpp_extension.py", line 1292, in _jit_compile
_write_ninja_file_and_build_library(
File "D:\anaconda3\envs\3dSwap\lib\site-packages\torch\utils\cpp_extension.py", line 1391, in _write_ninja_file_and_build_library
_write_ninja_file_to_build_library(
File "D:\anaconda3\envs\3dSwap\lib\site-packages\torch\utils\cpp_extension.py", line 1823, in _write_ninja_file_to_build_library
_write_ninja_file(
File "D:\anaconda3\envs\3dSwap\lib\site-packages\torch\utils\cpp_extension.py", line 1947, in _write_ninja_file
cl_paths = subprocess.check_output(['where',
File "D:\anaconda3\envs\3dSwap\lib\subprocess.py", line 415, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "D:\anaconda3\envs\3dSwap\lib\subprocess.py", line 516, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['where', 'cl']' returned non-zero exit status 1.

  1. set up env following requirements, torch installed from here
  2. download models and checkpoints and put them into their folders
  3. ran python run_3dSwap.py

请问出现了这个问题_pickle.UnpicklingError: invalid load key, '\xad'.,应该如何解决呀?有点紧急,非常感谢!

(3dSwap) root@autodl-container-7dda1180fa-17262490:~/3dSwap# python run_3dSwap.py
Setting up [LPIPS] perceptual loss: trunk [alex], v[0.1], spatial [off]
Loading model from: /root/miniconda3/envs/3dSwap/lib/python3.8/site-packages/lpips/weights/v0.1/alex.pth
Traceback (most recent call last):
File "run_3dSwap.py", line 23, in
run_3dSwap(args)
File "run_3dSwap.py", line 10, in run_3dSwap
coach = FaceSwapCoach()
File "/root/3dSwap/models/faceswap_coach.py", line 95, in init
self.encoder = self.load_encoder()
File "/root/3dSwap/models/faceswap_coach.py", line 102, in load_encoder
encoder_ckpt = torch.load('checkpoints/encoder.pt')
File "/root/miniconda3/envs/3dSwap/lib/python3.8/site-packages/torch/serialization.py", line 593, in load
return _legacy_load(opened_file, map_location, pickle_module, **pickle_load_args)
File "/root/miniconda3/envs/3dSwap/lib/python3.8/site-packages/torch/serialization.py", line 762, in _legacy_load
magic_number = pickle_module.load(f, **pickle_load_args)
_pickle.UnpicklingError: invalid load key, '\xad'.

Cannot download from Baidu and Code is unlicensed

Hello! I am trying to run your code, but I am unable to download the preprocessed datasets from baidu.com. Is it possible for you to upload the data to Google Drive, as you did with the pretrained models?

Additionally, there is no license associated with the repository. Would you mind adding a license, preferably a permissive one such as MIT or BSD?

how to make the demo's Effect

can i please ask a question a bit off topic,how can make the output result like the demo video's effect, i mean like a video rather than a picture

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.