# Fast Fake: Easy-to-Train Face Swap Model
## 3 Methodology
### Loss Functions  
This chapter on the loss functions presents the critical elements in deepfake model optimization, which are intended to ensure the stability of the training process and the efficiency of image generation. Several cost functions were introduced in our model, necessary for learning complex many-to-many relationships in deepfake generation. **Identity Loss**: The first one pf these functions is the so-called identity loss, which plays a fundamental role in ensuring the accuracy of the identity of the generated faces. It uses a pre-trained face recognition model, like ArcFace [31], which generates high-dimensional representations (embeddings) of faces. During training, the embeddings of the source face \(\mathrm{v}_{\mathrm{X_{t}}}\) and the generated output image \(\mathrm{v}_{\mathrm{Y}}\) are compared. Minimizing the cosine distance between these embeddings is one of the training goals, ensuring that the generated images retain vital features of the source identity. \[\mathrm{L}_{\mathrm{Id}}=\ 1-\frac{\mathrm{v}_{\mathrm{X_{t}}}\cdot\mathrm{v}_{ \mathrm{Y}}}{||\mathrm{v}_{\mathrm{X_{t}}}||_{2}||\mathrm{v}_{\mathrm{Y}}||_{ 2}} \tag{1}\]  
**Reconstruction Loss**: The second function, reconstruction loss, focuses on calculating the mean squared error (MSE) between regions that are not part of the face of the target image \(\mathrm{X_{t}}\) and the generated output \(\mathrm{Y}\). A face segmenter such as BiSeNet is used to determine these areas, which allows for a more accurate representation of the context in which the face is located. \[\mathrm{L}_{\mathrm{Rec}}=||\mathrm{NotFace}_{\mathrm{mask}}(\mathrm{X_{t}} )\times(\mathrm{X_{t}}-\mathrm{Y})||_{2} \tag{2}\]  
**Feature Loss**: The following function, feature loss, is calculated as the mean squared error between the logits extracted by the BiSeNet segmenter network. The mean square error between the features of the \(\mathrm{X_{t}}\) target image blurred with box blur and the generated output is calculated, which helps to simulate the location of facial details more accurately. \[\mathrm{L}_{\mathrm{Feat}}=||\mathrm{Blur}(\mathrm{Feat}(\mathrm{X_{t}}), \mathrm{kernel})-\mathrm{Blur}(\mathrm{Feat}(\mathrm{Y}),\mathrm{kernel})|| _{2} \tag{3}\]  
**Adversarial Loss**: The essential cost function in GAN training is adversarial loss. It includes classifying images presented to the discriminator as true or false. This decision is a crucial element of the training process that pushes the generator to create more convincing images while also training the discriminator to refine their authenticity assessment. The overall function of the generator can be described as follows:  
\[\mathrm{L}=\lambda_{\mathrm{Id}}\mathrm{L}_{\mathrm{Id}}+\lambda_{\mathrm{ Rec}}\mathrm{L}_{\mathrm{Rec}}+\lambda_{\mathrm{Feat}}\mathrm{L}_{\mathrm{Feat}}+ \lambda_{\mathrm{Adv}}\mathrm{L}_{\mathrm{Adv}} \tag{4}\]where \(\lambda_{1d}=5,\ \lambda_{Rec}=100,\ \lambda_{Feat}=40,\ \text{and}\ \lambda_{Adv}=1\). The rationale behind selecting the \(\lambda\)s parameters in the loss function is a critical aspect of the model's design, ensuring a balanced contribution of each component to the overall training objective. The \(\lambda\) values guide the model to prioritize certain aspects of the image generation process, directly impacting the quality and realism of the generated deepfakes. Identity Loss (\(\lambda_{1d}\)): The choice of \(\lambda_{1d}=5\) for our model was empirically determined through testing to find a sweet spot where the model sufficiently modifies the face to reflect the source identity without causing excessive alterations beyond the facial region, such as unnatural hair color changes or adding hair in inappropriate areas. This parameter's tuning is delicate; too low a value would result in minimal-to-no changes in the early training phases, whereas too high a value could introduce artefacts which detract from the realism of the face swap.