-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsample.py
More file actions
193 lines (164 loc) · 6.07 KB
/
sample.py
File metadata and controls
193 lines (164 loc) · 6.07 KB
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
import torch
from torch.utils.data import DataLoader
from networks import get_model
from utils import seed_all, size_of_model
from datasets import get_dataset
from inference import unproject, t, sample, condiff, joint_ode
import torchvision.io as tvio
from tqdm import tqdm
from omegaconf import OmegaConf
from datetime import datetime
from pathlib import Path
from functools import partial
from einops import rearrange
from PIL import Image
import numpy as np
import threading
def main():
# === load config ===
torch.set_float32_matmul_precision("high")
cli_config = OmegaConf.from_cli()
if hasattr(cli_config, "yaml"):
yaml = Path(cli_config.yaml)
del cli_config.yaml
else:
yaml = Path("config/video-flow.yaml")
cfg = OmegaConf.load(yaml)
# set to True to disallow adding unknown fields to the config
OmegaConf.set_struct(cfg, True)
cfg = OmegaConf.merge(cfg, cli_config)
# === checkpoint code ===
ws_path = Path(cfg.log.exp_path) / (
datetime.now().strftime("%Y_%m_%d_%H_%M_%S")
if not cfg.log.comment
else cfg.log.comment
)
ws_path.mkdir(parents=True, exist_ok=True)
# print the config to the console
print(OmegaConf.to_yaml(cfg))
# === setup ===
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
seed_all(cfg.seed)
n_samples = cfg.inference.n_samples
batchsize = cfg.inference.batchsize
assert n_samples % batchsize == 0
inf_noise_level = cfg.inference.noise_level
n_inf_frames = cfg.inference.n_frames
solve_backward = cfg.inference.backward
if solve_backward:
assert (
cfg.branch.name == "video_biflow"
), "Only video biflow supports backward sampling"
# === load data ===
dataset_test = get_dataset(cfg.data)
dataloader_test = DataLoader(
dataset_test,
batch_size=batchsize,
num_workers=4,
pin_memory=True,
)
# === load model ===
model = get_model(cfg.model)
model = model.to(device)
model.requires_grad_(False)
model = model.eval()
print(f"The size of model is {size_of_model(model)}")
# seed is set for the rest of the code
seed_all(cfg.seed)
# === inference function ===
sample_func = partial(sample, solver=cfg.inference.solver)
sample_func = torch.compile(sample_func)
# === training loop ===
data_test_iterator = iter(dataloader_test)
# generate n_samples samples, but each time only with batchsize samples at most
stats_final_list = []
threads = []
for batch_idx in (
pbar := tqdm(
range(0, n_samples, batchsize),
"Inference",
dynamic_ncols=True,
)
):
# inference for checkpointing
xs_list = []
stats_list = []
# some ode's and timepoints
t_01 = t(batchsize, device)
# sample first frame
data_test = next(data_test_iterator)
x0 = data_test[:, 1]
x0 = x0.to(device)
xs_list.append(x0)
for i in range(n_inf_frames - 1):
x0 = xs_list[-1]
if cfg.branch.name == "video_flow":
initial_value = x0
# not used, just for matching the random seeds
_ = torch.randn_like(x0, device=device)
predictor = model
elif cfg.branch.name == "video_biflow":
initial_value = x0 + torch.randn_like(
x0, device=device
) * inf_noise_level
if solve_backward:
start = (0, 0)
end = (1, inf_noise_level)
else:
start = (0, inf_noise_level)
end = (1, 0)
predictor = joint_ode(model, start, end)
elif cfg.branch.name == "condiff":
initial_value = torch.randn_like(x0, device=device)
predictor = condiff(model, x0)
stats, xs = sample_func(
predictor, initial_value, t_01 if not solve_backward else 1 - t_01
)
stats_list.append(stats["n_f_evals"].to(xs))
x1 = xs[:, 1, ...]
x1 = x1.clamp(-1.0, 1.0)
xs_list.append(x1)
xs = torch.stack(xs_list, dim=1) # B x T x C x H x W
xs = unproject(xs)
stats = torch.stack(stats_list, dim=-1) # B x T
stats_final_list.append(stats)
def save_images(batch_idx, xs, ws_path):
for i, frames in enumerate(xs):
frames_PIL: list[Image.Image] = []
for j, frame in enumerate(frames):
save_path = ws_path / f"{i+batch_idx:05d}" / f"{j:05d}.png"
save_path.parent.mkdir(parents=True, exist_ok=True)
frame = (frame.permute(1, 2, 0).cpu().numpy() * 255).astype(
np.uint8
)
frame_PIL = Image.fromarray(frame)
frame_PIL.save(save_path)
frames_PIL.append(frame_PIL)
# Multimedia
frames_PIL[0].save(
ws_path / f"{i+batch_idx:05d}" / f"{0:05d}.gif",
save_all=True,
append_images=frames_PIL[1:],
duration=100, # fps = 10
loop=0,
)
tvio.write_video(
ws_path / f"{i+batch_idx:05d}" / f"{0:05d}.mp4",
(
rearrange(frames, "T C H W -> T H W C").cpu().numpy() * 255
).astype(np.uint8),
fps=10,
)
save_thread = threading.Thread(
target=save_images, args=(batch_idx, xs, ws_path)
)
save_thread.start()
threads.append(save_thread)
pbar.set_postfix({"n_f_evals": stats.mean(dtype=torch.float32).item()})
stats_final = torch.concatenate(stats_final_list, dim=0)
with open(ws_path / f"stats.pt", "wb") as f:
torch.save(stats_final.to("cpu"), f)
for thread in threads:
thread.join()
if __name__ == "__main__":
main()