Skip to content

Commit

Permalink
changes
Browse files Browse the repository at this point in the history
  • Loading branch information
harisreedhar committed Feb 27, 2025
1 parent 3b97f6e commit b1e3d79
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 3 deletions.
2 changes: 2 additions & 0 deletions face_swapper/src/dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from torch.utils.data import Dataset
from torchvision import io, transforms

from .helper import warp_tensor
from .types import Batch


Expand Down Expand Up @@ -35,6 +36,7 @@ def compose_transforms() -> transforms:
transforms.ColorJitter(brightness = 0.2, contrast = 0.2, saturation = 0.2, hue = 0.1),
transforms.RandomAffine(4, translate = (0.01, 0.01), scale = (0.98, 1.02), shear = (1, 1)),
transforms.ToTensor(),
lambda temp: warp_tensor(temp.unsqueeze(0), '__vgg_face_hq__to__arcface_128_v2__').squeeze(0),
transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))
])

Expand Down
26 changes: 24 additions & 2 deletions face_swapper/src/helper.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,32 @@
import torch
from torch import Tensor, nn

from .types import EmbedderModule, Embedding, Padding
from .types import AlignmentMatrices, EmbedderModule, Embedding, Padding

ALIGNMENT_MATRICES: AlignmentMatrices =\
{
'__vgg_face_hq__to__arcface_128_v2__': torch.tensor(
[
[ 1.01305414, -0.00140513, -0.00585911 ],
[ 0.00140513, 1.01305414, 0.11169602 ]
], dtype = torch.float32),
'__arcface_128_v2__to__arcface_112_v2__': torch.tensor(
[
[ 8.75000016e-01, -1.07193451e-08, 3.80446920e-10 ],
[ 1.07193451e-08, 8.75000016e-01, -1.25000007e-01 ]
], dtype = torch.float32)
}


def warp_tensor(input_tensor : Tensor, alignment_matrix : str) -> Tensor:
matrix = ALIGNMENT_MATRICES.get(alignment_matrix).repeat(input_tensor.shape[0], 1, 1)
grid = nn.functional.affine_grid(matrix.to(input_tensor.device), list(input_tensor.shape))
output_tensor = nn.functional.grid_sample(input_tensor, grid, align_corners = False, padding_mode = 'reflection')
return output_tensor


def calc_embedding(embedder : EmbedderModule, input_tensor : Tensor, padding : Padding) -> Embedding:
crop_tensor = input_tensor[:, :, 15: 241, 15: 241]
crop_tensor = warp_tensor(input_tensor, '__arcface_128_v2__to__arcface_112_v2__')
crop_tensor = nn.functional.interpolate(crop_tensor, size = (112, 112), mode = 'area')
crop_tensor[:, :, :padding[0], :] = 0
crop_tensor[:, :, 112 - padding[1]:, :] = 0
Expand Down
4 changes: 3 additions & 1 deletion face_swapper/src/types.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Any, Tuple, TypeAlias
from typing import Any, Dict, Tuple, TypeAlias

from torch import Tensor
from torch.nn import Module
Expand All @@ -17,3 +17,5 @@
MotionExtractorModule : TypeAlias = Module

OptimizerConfig : TypeAlias = Any

AlignmentMatrices : TypeAlias = Dict[str, Tensor]

0 comments on commit b1e3d79

Please sign in to comment.