quantumos commited on
Commit
86262cf
·
verified ·
1 Parent(s): 0f880ac

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +2 -125
inference.py CHANGED
@@ -1,126 +1,3 @@
1
- import os
2
- import cv2
3
- import numpy as np
4
- import torch
5
- import torchaudio.functional
6
- import torchvision.io
7
- from PIL import Image
8
- from diffusers import AutoencoderKL, DDIMScheduler
9
- from diffusers.utils.import_utils import is_xformers_available
10
- from diffusers.utils.torch_utils import randn_tensor
11
- from insightface.app import FaceAnalysis
12
- from omegaconf import OmegaConf
13
- from transformers import CLIPVisionModelWithProjection, Wav2Vec2Model, Wav2Vec2Processor
14
-
15
- from modules import UNet2DConditionModel, UNet3DConditionModel, VKpsGuider, AudioProjection
16
- from pipelines import VExpressPipeline
17
- from pipelines.utils import draw_kps_image, save_video
18
- from pipelines.utils import retarget_kps
19
-
20
- def load_reference_net(unet_config_path, reference_net_path, dtype, device):
21
- reference_net = UNet2DConditionModel.from_config(unet_config_path).to(dtype=dtype, device=device)
22
- if os.path.exists(reference_net_path):
23
- reference_net.load_state_dict(torch.load(reference_net_path, map_location="cpu"), strict=False)
24
- print(f'Loaded weights of Reference Net from {reference_net_path}.')
25
- else:
26
- # Check for .bin file if .pth file is not found
27
- bin_path = reference_net_path.replace('.pth', '.bin')
28
- if os.path.exists(bin_path):
29
- reference_net.load_state_dict(torch.load(bin_path, map_location="cpu"), strict=False)
30
- print(f'Loaded weights of Reference Net from {bin_path}.')
31
- else:
32
- raise FileNotFoundError(f'Reference net file not found at {reference_net_path} or {bin_path}. Please provide the correct path.')
33
- return reference_net
34
-
35
- def load_denoising_unet(unet_config_path, denoising_unet_path, motion_module_path, dtype, device):
36
- inference_config_path = './inference_v2.yaml'
37
- inference_config = OmegaConf.load(inference_config_path)
38
- denoising_unet = UNet3DConditionModel.from_config_2d(
39
- unet_config_path,
40
- unet_additional_kwargs=inference_config.unet_additional_kwargs,
41
- ).to(dtype=dtype, device=device)
42
- if os.path.exists(denoising_unet_path):
43
- denoising_unet.load_state_dict(torch.load(denoising_unet_path, map_location="cpu"), strict=False)
44
- print(f'Loaded weights of Denoising U-Net from {denoising_unet_path}.')
45
- else:
46
- # Check for .bin file if .pth file is not found
47
- bin_path = denoising_unet_path.replace('.pth', '.bin')
48
- if os.path.exists(bin_path):
49
- denoising_unet.load_state_dict(torch.load(bin_path, map_location="cpu"), strict=False)
50
- print(f'Loaded weights of Denoising U-Net from {bin_path}.')
51
- else:
52
- raise FileNotFoundError(f'Denoising U-Net file not found at {denoising_unet_path} or {bin_path}. Please provide the correct path.')
53
-
54
- if os.path.exists(motion_module_path):
55
- denoising_unet.load_state_dict(torch.load(motion_module_path, map_location="cpu"), strict=False)
56
- print(f'Loaded weights of Denoising U-Net Motion Module from {motion_module_path}.')
57
- else:
58
- # Check for .bin file if .pth file is not found
59
- bin_motion_module_path = motion_module_path.replace('.pth', '.bin')
60
- if os.path.exists(bin_motion_module_path):
61
- denoising_unet.load_state_dict(torch.load(bin_motion_module_path, map_location="cpu"), strict=False)
62
- print(f'Loaded weights of Denoising U-Net Motion Module from {bin_motion_module_path}.')
63
- else:
64
- raise FileNotFoundError(f'Motion Module file not found at {motion_module_path} or {bin_motion_module_path}. Please provide the correct path.')
65
-
66
- return denoising_unet
67
-
68
- def load_v_kps_guider(v_kps_guider_path, dtype, device):
69
- v_kps_guider = VKpsGuider(320, block_out_channels=(16, 32, 96, 256)).to(dtype=dtype, device=device)
70
- if os.path.exists(v_kps_guider_path):
71
- v_kps_guider.load_state_dict(torch.load(v_kps_guider_path, map_location="cpu"))
72
- print(f'Loaded weights of V-Kps Guider from {v_kps_guider_path}.')
73
- else:
74
- # Check for .bin file if .pth file is not found
75
- bin_path = v_kps_guider_path.replace('.pth', '.bin')
76
- if os.path.exists(bin_path):
77
- v_kps_guider.load_state_dict(torch.load(bin_path, map_location="cpu"))
78
- print(f'Loaded weights of V-Kps Guider from {bin_path}.')
79
- else:
80
- raise FileNotFoundError(f'V-Kps Guider file not found at {v_kps_guider_path} or {bin_path}. Please provide the correct path.')
81
- return v_kps_guider
82
-
83
- def load_audio_projection(
84
- audio_projection_path,
85
- dtype,
86
- device,
87
- inp_dim: int,
88
- mid_dim: int,
89
- out_dim: int,
90
- inp_seq_len: int,
91
- out_seq_len: int,
92
- ):
93
- audio_projection = AudioProjection(
94
- dim=mid_dim,
95
- depth=4,
96
- dim_head=64,
97
- heads=12,
98
- num_queries=out_seq_len,
99
- embedding_dim=inp_dim,
100
- output_dim=out_dim,
101
- ff_mult=4,
102
- max_seq_len=inp_seq_len,
103
- ).to(dtype=dtype, device=device)
104
- if os.path.exists(audio_projection_path):
105
- audio_projection.load_state_dict(torch.load(audio_projection_path, map_location='cpu'))
106
- print(f'Loaded weights of Audio Projection from {audio_projection_path}.')
107
- else:
108
- # Check for .bin file if .pth file is not found
109
- bin_path = audio_projection_path.replace('.pth', '.bin')
110
- if os.path.exists(bin_path):
111
- audio_projection.load_state_dict(torch.load(bin_path, map_location='cpu'))
112
- print(f'Loaded weights of Audio Projection from {bin_path}.')
113
- else:
114
- raise FileNotFoundError(f'Audio Projection file not found at {audio_projection_path} or {bin_path}. Please provide the correct path.')
115
- return audio_projection
116
-
117
- def get_scheduler():
118
- inference_config_path = './inference_v2.yaml'
119
- inference_config = OmegaConf.load(inference_config_path)
120
- scheduler_kwargs = OmegaConf.to_container(inference_config.noise_scheduler_kwargs)
121
- scheduler = DDIMScheduler(**scheduler_kwargs)
122
- return scheduler
123
-
124
  class InferenceEngine(object):
125
  def __init__(self, args):
126
  self.init_params(args)
@@ -129,7 +6,7 @@ class InferenceEngine(object):
129
  self.set_vexpress_pipeline()
130
  self.set_face_analysis_app()
131
 
132
- def init_params(self, args):
133
  for key, value in args.items():
134
  setattr(self, key, value)
135
  print("Image width: ", self.image_width)
@@ -183,7 +60,7 @@ class InferenceEngine(object):
183
  audio_encoder=self.audio_encoder,
184
  audio_processor=self.audio_processor,
185
  scheduler=self.scheduler,
186
- audio_projection=self.audio_projection,
187
  ).to(dtype=self.dtype, device=self.device)
188
 
189
  def set_face_analysis_app(self):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  class InferenceEngine(object):
2
  def __init__(self, args):
3
  self.init_params(args)
 
6
  self.set_vexpress_pipeline()
7
  self.set_face_analysis_app()
8
 
9
+ def init_params(self, args):
10
  for key, value in args.items():
11
  setattr(self, key, value)
12
  print("Image width: ", self.image_width)
 
60
  audio_encoder=self.audio_encoder,
61
  audio_processor=self.audio_processor,
62
  scheduler=self.scheduler,
63
+ audio_projection=self.audio_projection,
64
  ).to(dtype=self.dtype, device=self.device)
65
 
66
  def set_face_analysis_app(self):