-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
222 lines (173 loc) · 5.16 KB
/
test.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# ReWaS
# Copyright (c) 2024-present NAVER Cloud Corp.
# CC BY-NC-SA 4.0 (https://creativecommons.org/licenses/by-nc-sa/4.0/)
import os
from audioldm.pipeline import rewas_generation, build_control_model
import json
import argparse
import pandas as pd
from random import shuffle
from omegaconf import OmegaConf
from tqdm import tqdm
import warnings
warnings.filterwarnings("ignore")
import torch
import torch.nn as nn
from torch.utils.data.distributed import DistributedSampler
from utils import seed_everything
from encoder.encoder_utils import patch_config, get_pretrained
from encoder.phi import Phi
def main(args):
seed = args.seed
seed_everything(seed)
assert os.path.isfile(args.ckpt_path), "check checkpoints in ckpt_path!"
control_type = args.control_type
save_path = args.save_path
os.makedirs(save_path, exist_ok=True)
audioldm_control = build_control_model(
ckpt_path = args.ckpt_path,
control_type = args.control_type,
config = args.config,
model_name = args.model_name)
cfg_path = f'./configs/cfg-{args.synchformer_exp}.yaml'
synchformer_cfg = OmegaConf.load(cfg_path)
synchformer_cfg = patch_config(synchformer_cfg)
video_encoder = get_pretrained(args.synchformer_exp, 0)
phi = Phi()
resume_params = torch.load(args.phi_ckpt_path)
resume_new = {k.replace("module.",""): v for k, v in resume_params.items()}
phi.load_state_dict(resume_new)
phi.eval()
phi = nn.DataParallel(phi, device_ids=[i for i in range(torch.cuda.device_count())])
print(f'Generate data list: {args.testlist}')
with open(args.testlist, 'rb') as f:
datalist = list(map(json.loads, f))
for x in tqdm(datalist):
prompt = x['prompt']
videopath = x['video_name']
waveform = rewas_generation(
audioldm_control,
prompt,
videopath,
args.control_type,
args.synchformer_exp,
synchformer_cfg,
video_encoder,
phi,
args.file_path,
seed,
duration=args.duration,
guidance_scale=args.guidance_scale,
ddim_steps=args.ddim_steps,
n_candidate_gen_per_text=args.n_candidate_gen_per_cond,
batchsize=args.batchsize,
save_path=save_path,
re_encode=args.re_encode,
local_rank=0
)
if args.re_encode:
os.rmdir('.cache/')
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
"--testlist",
type=str,
default="test_samples.json",
)
parser.add_argument(
"--datadir",
type=str,
default="/path/to/video",
)
parser.add_argument(
"-f",
"--file_path",
type=str,
default=None,
)
parser.add_argument(
"-s",
"--save_path",
type=str,
help="The path to save model output",
default="./results",
)
parser.add_argument(
"--model_name",
type=str,
help="The checkpoint you gonna use",
default="audioldm-m-full",
)
parser.add_argument(
"-ckpt",
"--ckpt_path",
type=str,
help="The path to the pretrained .ckpt model",
default="ckpts/audioldm_m_rewas_vggsound.ckpt",
)
parser.add_argument(
"--phi_ckpt_path",
type=str,
help="The path to the pretrained .ckpt video encoder",
default="ckpts/phi_vggsound.ckpt",
)
parser.add_argument(
"--synchformer_exp",
type=str,
help="The name of experiment of synchformer",
default="24-01-04T16-39-21",
)
parser.add_argument(
"-b",
"--batchsize",
type=int,
default=1,
help="Generate how many samples at the same time",
)
parser.add_argument(
"--ddim_steps",
type=int,
default=200,
help="The sampling step for DDIM",
)
parser.add_argument(
"-gs",
"--guidance_scale",
type=float,
default=3,
help="Guidance scale (Large => better quality and relavancy to text; Small => better diversity)",
)
parser.add_argument(
"-dur",
"--duration",
type=float,
default=5.0,
help="The duration of the samples",
)
parser.add_argument(
"-n",
"--n_candidate_gen_per_cond",
type=int,
default=1,
help="The number of generated sample per condition. A Larger value usually lead to better quality with heavier computation",
)
parser.add_argument(
"--seed",
type=int,
default=42,
help="Change this value (any integer number) will lead to a different generation result.",
)
parser.add_argument(
"--config",
type=str,
default="configs/audioldm_m_rewas.yaml",
)
parser.add_argument(
"--control_type",
type=str,
default="energy_video",
choices=["energy_audio", "energy_video"]
)
parser.add_argument('--re_encode', action='store_true')
args = parser.parse_args()
main(args)