Files
kijai-ComfyUI-LivePortraitKJ/liveportrait/modules/warping_network.py
T
kijai 3d195208db Squashed commit of the following:
commit b608558b9e
Merge: ad29b02 dd205ab
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 24 17:56:47 2024 +0300

    Merge branch 'develop' of https://github.com/kijai/ComfyUI-LivePortraitKJ into develop

commit ad29b02bc1
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 24 17:56:46 2024 +0300

    update workflows

commit dd205ab4a4
Author: Jukka Seppänen <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 24 17:54:47 2024 +0300

    Update readme.md

commit ba0886a905
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 24 16:09:26 2024 +0300

    fix running without insightface installed

commit 068ab2c280
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 24 03:57:59 2024 +0300

    Add MediaPipe as alternative face detector

commit 6261f4e474
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 23 22:58:33 2024 +0300

    cleanup, memory fixes

commit 46675b2016
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 23 21:13:15 2024 +0300

    update workflows, cleanup

commit 806263dd25
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 20:39:43 2024 +0300

    cleanup, fixes

commit ac89dc1e2f
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 19:19:46 2024 +0300

    fix no face frame skip

commit 27d745b53e
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 17:52:34 2024 +0300

    add other examples

commit 052762578c
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 17:45:49 2024 +0300

    Update readme.md

commit e825c51c87
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 16:21:35 2024 +0300

    separate composition to it's own node

commit 177b324fcd
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 01:02:56 2024 +0300

    Update live_portrait_pipeline.py

commit 5c03bd8439
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 22 00:57:44 2024 +0300

    MPS fallbacks

commit ef5ff7075f
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sun Jul 21 20:35:33 2024 +0300

    Update requirements.txt

commit 92fad03ee5
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sun Jul 21 20:20:46 2024 +0300

    restructure a bit for more caching

commit 4cefac79b8
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sun Jul 21 19:42:22 2024 +0300

    Add single_frame mode for webcam

commit 5e3c92d55c
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sun Jul 21 19:20:52 2024 +0300

    restructuring, video smoothing

commit cc0501a2db
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sun Jul 21 13:21:26 2024 +0300

    flag_relative_rotation_only

commit 3dc822fd2f
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sat Jul 20 20:24:15 2024 +0300

    to use GPU for pasteback

commit 697b9a78e6
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Sat Jul 20 17:39:44 2024 +0300

    Restructure nodes, skip frames with no face detect

commit 2a7bd6116f
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 10 17:14:43 2024 +0300

    Update nodes.py

commit a7d09f5d49
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 10 16:33:31 2024 +0300

    example workflow

commit 8e85d5b96d
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 10 16:06:01 2024 +0300

    some optimizations

commit 30989a9d37
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Wed Jul 10 01:17:10 2024 +0300

    Update nodes.py

commit eecf645603
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 23:02:47 2024 +0300

    rotate option for cropper

commit 1b080706df
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 22:46:51 2024 +0300

    Update nodes.py

commit 336f3f7c23
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 22:19:56 2024 +0300

    add cut method

commit f27e1cca13
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 22:11:19 2024 +0300

    remove nearest option

commit 86e91a6e9d
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 22:06:56 2024 +0300

    better error for retargeting

commit 92529f7ca8
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 21:59:32 2024 +0300

    cleanup

commit c0959056ae
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 21:49:54 2024 +0300

    eye/lip retargeting fixes

commit 2e40fe3820
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 21:23:10 2024 +0300

    Update live_portrait_pipeline.py

commit 0a5e187637
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 21:15:17 2024 +0300

    keep Cropper in memory

commit 4e19dbd6d1
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 20:19:53 2024 +0300

    Do video cropping on the cropped node too

commit 9c190804a7
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 19:02:27 2024 +0300

    big cleanup

commit d9ca40e1d6
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 15:08:17 2024 +0300

    logging

commit b68cf8788c
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 14:35:51 2024 +0300

    fix warning

commit e702b26895
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 14:31:22 2024 +0300

    Update cropper.py

commit c21705edb5
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 14:25:39 2024 +0300

    Don't draw keypoints for every frame by default

commit a284bb52b2
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 14:04:56 2024 +0300

    Bring back mismatch_method selection

commit 9884aac18a
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 14:00:43 2024 +0300

    Fix eye/lip retargeting

commit f8aada81db
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 13:00:03 2024 +0300

    face_index selection

commit 6735771664
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 11:51:38 2024 +0300

    tqdm progress bars

commit 857ddbc6d7
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 02:54:27 2024 +0300

    skip autocast if not needed for mps

commit a6edcda97d
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 01:37:05 2024 +0300

    output masks

commit ca01d706d0
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 00:57:13 2024 +0300

    custom mask support

commit 0dc9a8a695
Merge: ee7d5b4 ba6b3f5
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 00:13:07 2024 +0300

    Merge branch 'add_video_source' into develop

commit ba6b3f5f68
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 23:09:56 2024 +0200

    bring KJ edits

commit ee7d5b4241
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Tue Jul 9 00:08:10 2024 +0300

    revert this for compatibility

commit 03df9f35cd
Merge: ec6b5c8 8509d9a
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 8 23:55:22 2024 +0300

    Merge branch 'add_video_source' into develop

commit ec6b5c8c85
Merge: 6f9dba7 e724da1
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 8 23:52:58 2024 +0300

    calc_combined_eye_ratio

commit 8509d9a551
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 22:52:48 2024 +0200

    remove unused imports

commit e724da1161
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 21:38:27 2024 +0200

    fix relative mode

    use R_d_0 instead of source

commit 68d0ddf72a
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 21:22:50 2024 +0200

    remove reference frame attempt

    also use batches for driving when either retargetting is enabled

commit 6f9dba7777
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 8 20:50:44 2024 +0300

    fixes

commit 811ca557fb
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 8 20:31:00 2024 +0300

    more

commit 6d790bdcc3
Merge: ef8b426 eb5fddf
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 8 20:30:35 2024 +0300

    Merge branch 'add_video_source' into develop

commit ef8b4263b4
Author: kijai <40791699+kijai@users.noreply.github.com>
Date:   Mon Jul 8 20:21:45 2024 +0300

    separating functions to nodes

commit eb5fddf4de
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 19:10:53 2024 +0200

    fix issues from merge

commit 9c7db3c59a
Merge: bf3410c 1f28e12
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 19:09:02 2024 +0200

    Merge branch 'main' into add_video_source

commit bf3410cd0d
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 19:04:42 2024 +0200

    trying reference frame

commit 24c65627db
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 19:03:28 2024 +0200

    local updates before merging main

commit 72bb6910e9
Author: Mel Massadian <mel@melmassadian.com>
Date:   Mon Jul 8 16:48:15 2024 +0200

    initial

    too much diff due to formatting
2024-07-24 18:01:19 +03:00

82 lines
3.0 KiB
Python

# coding: utf-8
"""
Warping field estimator(W) defined in the paper, which generates a warping field using the implicit
keypoint representations x_s and x_d, and employs this flow field to warp the source feature volume f_s.
"""
from torch import nn
import torch.nn.functional as F
from .util import SameBlock2d
from .dense_motion import DenseMotionNetwork
class WarpingNetwork(nn.Module):
def __init__(
self,
num_kp,
block_expansion,
max_features,
num_down_blocks,
reshape_channel,
estimate_occlusion_map=False,
dense_motion_params=None,
**kwargs
):
super(WarpingNetwork, self).__init__()
self.upscale = kwargs.get('upscale', 1)
self.flag_use_occlusion_map = kwargs.get('flag_use_occlusion_map', True)
if dense_motion_params is not None:
self.dense_motion_network = DenseMotionNetwork(
num_kp=num_kp,
feature_channel=reshape_channel,
estimate_occlusion_map=estimate_occlusion_map,
**dense_motion_params
)
else:
self.dense_motion_network = None
self.third = SameBlock2d(max_features, block_expansion * (2 ** num_down_blocks), kernel_size=(3, 3), padding=(1, 1), lrelu=True)
self.fourth = nn.Conv2d(in_channels=block_expansion * (2 ** num_down_blocks), out_channels=block_expansion * (2 ** num_down_blocks), kernel_size=1, stride=1)
self.estimate_occlusion_map = estimate_occlusion_map
def deform_input(self, inp, deformation):
try:
return F.grid_sample(inp, deformation, align_corners=False)
except NotImplementedError:
out_device = inp.device # Store input device
return F.grid_sample(inp.to('cpu'), deformation.to('cpu'), align_corners=False).to(out_device)
def forward(self, feature_3d, kp_driving, kp_source):
if self.dense_motion_network is not None:
# Feature warper, Transforming feature representation according to deformation and occlusion
dense_motion = self.dense_motion_network(
feature=feature_3d, kp_driving=kp_driving, kp_source=kp_source
)
if 'occlusion_map' in dense_motion:
occlusion_map = dense_motion['occlusion_map'] # Bx1x64x64
else:
occlusion_map = None
deformation = dense_motion['deformation'] # Bx16x64x64x3
out = self.deform_input(feature_3d, deformation) # Bx32x16x64x64
bs, c, d, h, w = out.shape # Bx32x16x64x64
out = out.view(bs, c * d, h, w) # -> Bx512x64x64
out = self.third(out) # -> Bx256x64x64
out = self.fourth(out) # -> Bx256x64x64
if self.flag_use_occlusion_map and (occlusion_map is not None):
out = out * occlusion_map
ret_dct = {
'occlusion_map': occlusion_map,
'deformation': deformation,
'out': out,
}
return ret_dct