somanchiu / ReSwapper

ReSwapper aims to reproduce the implementation of inswapper. This repository provides code for training, inference, and includes pretrained weights.
GNU Affero General Public License v3.0
12 stars 2 forks source link
deep-learning deepfake faceswap image-to-image img2img pytorch

ReSwapper

ReSwapper aims to reproduce the implementation of inswapper. This repository provides code for training, inference, and includes pretrained weights.

Here is the comparesion of the output of Inswapper and Reswapper. Target Source Inswapper Output Reswapper Output
(Step 429500)
image image image image
image image image image
image image image image

Installation

git clone https://github.com/somanchiu/ReSwapper.git
cd ReSwapper
python -m venv venv

venv\scripts\activate

pip install -r requirements.txt

pip install torch torchvision --force --index-url https://download.pytorch.org/whl/cu121
pip install onnxruntime-gpu --force --extra-index-url https://aiinfra.pkgs.visualstudio.com/PublicPackages/_packaging/onnxruntime-cuda-12/pypi/simple/

The details of inswapper

Model architecture

The inswapper model architecture can be visualized in Netron. You can compare with ReSwapper implementation to see architectural similarities

We can also use the following Python code to get more details:

model = onnx.load('test.onnx')
printable_graph=onnx.helper.printable_graph(model.graph)

Model inputs

Model output

Model inswapper_128 not only changes facial features, but also body shape.

Target Source Inswapper Output Reswapper Output
(Step 429500)
image image image image

Loss Functions

There is no information released from insightface. It is an important part of the training. However, there are a lot of articles and papers that can be referenced. By reading a substantial number of articles and papers on face swapping, ID fidelity, and style transfer, you'll frequently encounter the following keywords:

Training

0. Pretrained weights (Optional)

If you don't want to train the model from scratch, you can download the pretrained weights and pass model_path into the train function in train.py.

1. Dataset Preparation

Download FFHQ to use as target and source images. For the swaped face images, we can use the inswapper output.

2. Model Training

Optimizer: Adam

Learning rate: 0.0001

Modify the code in train.py if needed. Then, execute:

python train.py

The model will be saved as "reswapper-\<total steps>.pth". You can also save the model as ONNX using the create_onnx_model function. The ONNX model can then be used with the original INSwapper class.

Notes

Inference

python swap.py

Pretrained Model

To Do