quantumos commited on
Commit
44ea25d
·
verified ·
1 Parent(s): ef3c14e

Update inference.py

Browse files
Files changed (1) hide show
  1. inference.py +56 -12
inference.py CHANGED
@@ -20,8 +20,17 @@ from pipelines.utils import retarget_kps
20
 
21
  def load_reference_net(unet_config_path, reference_net_path, dtype, device):
22
  reference_net = UNet2DConditionModel.from_config(unet_config_path).to(dtype=dtype, device=device)
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
  return reference_net
26
 
27
 
@@ -32,19 +41,46 @@ def load_denoising_unet(unet_config_path, denoising_unet_path, motion_module_pat
32
  unet_config_path,
33
  unet_additional_kwargs=inference_config.unet_additional_kwargs,
34
  ).to(dtype=dtype, device=device)
35
- denoising_unet.load_state_dict(torch.load(denoising_unet_path, map_location="cpu"), strict=False)
36
- print(f'Loaded weights of Denoising U-Net from {denoising_unet_path}.')
37
-
38
- denoising_unet.load_state_dict(torch.load(motion_module_path, map_location="cpu"), strict=False)
39
- print(f'Loaded weights of Denoising U-Net Motion Module from {motion_module_path}.')
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
40
 
41
  return denoising_unet
42
 
43
 
44
  def load_v_kps_guider(v_kps_guider_path, dtype, device):
45
  v_kps_guider = VKpsGuider(320, block_out_channels=(16, 32, 96, 256)).to(dtype=dtype, device=device)
46
- v_kps_guider.load_state_dict(torch.load(v_kps_guider_path, map_location="cpu"))
47
- print(f'Loaded weights of V-Kps Guider from {v_kps_guider_path}.')
 
 
 
 
 
 
 
 
 
48
  return v_kps_guider
49
 
50
 
@@ -69,11 +105,19 @@ def load_audio_projection(
69
  ff_mult=4,
70
  max_seq_len=inp_seq_len,
71
  ).to(dtype=dtype, device=device)
72
- audio_projection.load_state_dict(torch.load(audio_projection_path, map_location='cpu'))
73
- print(f'Loaded weights of Audio Projection from {audio_projection_path}.')
 
 
 
 
 
 
 
 
 
74
  return audio_projection
75
 
76
-
77
  def get_scheduler():
78
  inference_config_path = './inference_v2.yaml'
79
  inference_config = OmegaConf.load(inference_config_path)
 
20
 
21
  def load_reference_net(unet_config_path, reference_net_path, dtype, device):
22
  reference_net = UNet2DConditionModel.from_config(unet_config_path).to(dtype=dtype, device=device)
23
+ if os.path.exists(reference_net_path):
24
+ reference_net.load_state_dict(torch.load(reference_net_path, map_location="cpu"), strict=False)
25
+ print(f'Loaded weights of Reference Net from {reference_net_path}.')
26
+ else:
27
+ # Check for .bin file if .pth file is not found
28
+ bin_path = reference_net_path.replace('.pth', '.bin')
29
+ if os.path.exists(bin_path):
30
+ reference_net.load_state_dict(torch.load(bin_path, map_location="cpu"), strict=False)
31
+ print(f'Loaded weights of Reference Net from {bin_path}.')
32
+ else:
33
+ raise FileNotFoundError(f'Reference net file not found at {reference_net_path} or {bin_path}. Please provide the correct path.')
34
  return reference_net
35
 
36
 
 
41
  unet_config_path,
42
  unet_additional_kwargs=inference_config.unet_additional_kwargs,
43
  ).to(dtype=dtype, device=device)
44
+ if os.path.exists(denoising_unet_path):
45
+ denoising_unet.load_state_dict(torch.load(denoising_unet_path, map_location="cpu"), strict=False)
46
+ print(f'Loaded weights of Denoising U-Net from {denoising_unet_path}.')
47
+ else:
48
+ # Check for .bin file if .pth file is not found
49
+ bin_path = denoising_unet_path.replace('.pth', '.bin')
50
+ if os.path.exists(bin_path):
51
+ denoising_unet.load_state_dict(torch.load(bin_path, map_location="cpu"), strict=False)
52
+ print(f'Loaded weights of Denoising U-Net from {bin_path}.')
53
+ else:
54
+ raise FileNotFoundError(f'Denoising U-Net file not found at {denoising_unet_path} or {bin_path}. Please provide the correct path.')
55
+
56
+ if os.path.exists(motion_module_path):
57
+ denoising_unet.load_state_dict(torch.load(motion_module_path, map_location="cpu"), strict=False)
58
+ print(f'Loaded weights of Denoising U-Net Motion Module from {motion_module_path}.')
59
+ else:
60
+ # Check for .bin file if .pth file is not found
61
+ bin_motion_module_path = motion_module_path.replace('.pth', '.bin')
62
+ if os.path.exists(bin_motion_module_path):
63
+ denoising_unet.load_state_dict(torch.load(bin_motion_module_path, map_location="cpu"), strict=False)
64
+ print(f'Loaded weights of Denoising U-Net Motion Module from {bin_motion_module_path}.')
65
+ else:
66
+ raise FileNotFoundError(f'Motion Module file not found at {motion_module_path} or {bin_motion_module_path}. Please provide the correct path.')
67
 
68
  return denoising_unet
69
 
70
 
71
  def load_v_kps_guider(v_kps_guider_path, dtype, device):
72
  v_kps_guider = VKpsGuider(320, block_out_channels=(16, 32, 96, 256)).to(dtype=dtype, device=device)
73
+ if os.path.exists(v_kps_guider_path):
74
+ v_kps_guider.load_state_dict(torch.load(v_kps_guider_path, map_location="cpu"))
75
+ print(f'Loaded weights of V-Kps Guider from {v_kps_guider_path}.')
76
+ else:
77
+ # Check for .bin file if .pth file is not found
78
+ bin_path = v_kps_guider_path.replace('.pth', '.bin')
79
+ if os.path.exists(bin_path):
80
+ v_kps_guider.load_state_dict(torch.load(bin_path, map_location="cpu"))
81
+ print(f'Loaded weights of V-Kps Guider from {bin_path}.')
82
+ else:
83
+ raise FileNotFoundError(f'V-Kps Guider file not found at {v_kps_guider_path} or {bin_path}. Please provide the correct path.')
84
  return v_kps_guider
85
 
86
 
 
105
  ff_mult=4,
106
  max_seq_len=inp_seq_len,
107
  ).to(dtype=dtype, device=device)
108
+ if os.path.exists(audio_projection_path):
109
+ audio_projection.load_state_dict(torch.load(audio_projection_path, map_location='cpu'))
110
+ print(f'Loaded weights of Audio Projection from {audio_projection_path}.')
111
+ else:
112
+ # Check for .bin file if .pth file is not found
113
+ bin_path = audio_projection_path.replace('.pth', '.bin')
114
+ if os.path.exists(bin_path):
115
+ audio_projection.load_state_dict(torch.load(bin_path, map_location='cpu'))
116
+ print(f'Loaded weights of Audio Projection from {bin_path}.')
117
+ else:
118
+ raise FileNotFoundError(f'Audio Projection file not found at {audio_projection_path} or {bin_path}. Please provide the correct path.')
119
  return audio_projection
120
 
 
121
  def get_scheduler():
122
  inference_config_path = './inference_v2.yaml'
123
  inference_config = OmegaConf.load(inference_config_path)