Repository navigation
Expand file tree
/
Copy pathinfer.py
More file actions
383 lines (337 loc) · 12.8 KB
/
Copy pathinfer.py
File metadata and controls
383 lines (337 loc) · 12.8 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
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
# Copyright (c) Meta Platforms, Inc. and affiliates.
# All rights reserved.
#
import torch
torch.backends.cuda.matmul.allow_tf32 = True
torch.backends.cudnn.allow_tf32 = True
import argparse
import os
import numpy as np
from anwm.config import load_runtime_config, split_path
from anwm.data.airvln import EvalDataset
from anwm.diffusion import create_diffusion
from anwm.distributed import init_distributed
import anwm.distributed as dist
from anwm.model import CDiT_models
from anwm.rollout import model_forward_wrapper
import anwm.utils as misc
from PIL import Image
def save_image(output_file, img, unnormalize_img):
img = img.detach().cpu()
if unnormalize_img:
img = misc.unnormalize(img)
img = img * 255
img = img.byte()
image = Image.fromarray(img.permute(1, 2, 0).numpy(), mode="RGB")
image.save(output_file)
def get_dataset_eval(config, dataset_name, eval_type, predefined_index=True):
data_config = config["eval_datasets"][dataset_name]
if predefined_index:
predefined_index = str(split_path(dataset_name, f"{eval_type}_16.pkl"))
# predefined_index = f"data/splits/airvln_16/test/dataset_dist_-64_to_64_n4_len_traj_pred_64.pkl"
else:
predefined_index = None
dataset = EvalDataset(
data_folder=data_config["data_folder"],
data_split_folder=data_config["test"],
dataset_name=dataset_name,
image_size=config["image_size"],
min_dist_cat=config["eval_distance"]["eval_min_dist_cat"],
max_dist_cat=config["eval_distance"]["eval_max_dist_cat"],
len_traj_pred=config["eval_len_traj_pred"],
traj_stride=config["traj_stride"],
context_size=config["eval_context_size"],
normalize=config["normalize"],
transform=misc.transform,
goals_per_obs=4,
predefined_index=predefined_index,
traj_names="traj_names.txt",
)
return dataset
def generate_rollout(
args,
output_dir,
rollout_fps,
idxs,
all_models,
obs_image,
gt_image,
delta,
num_cond,
device,
x_supervised,
):
rollout_stride = args.input_fps // rollout_fps
gt_image = gt_image[:, rollout_stride - 1 :: rollout_stride]
delta = delta.unflatten(1, (-1, rollout_stride)).sum(2)
curr_obs = obs_image.clone().to(device)
sup_image = x_supervised[
:, rollout_stride - 1 :: rollout_stride
] # (B, T_roll, C, H, W)
assert (
sup_image.shape == gt_image.shape
), f"x_sup_strided shape={sup_image.shape} != gt shape={gt_image.shape}"
for i in range(gt_image.shape[1]):
curr_delta = delta[:, i : i + 1].to(device)
if args.gt:
x_pred_pixels = gt_image[:, i].clone().to(device)
else:
x_pred_pixels = model_forward_wrapper(
all_models,
curr_obs,
curr_delta,
rollout_stride,
args.latent_size,
num_cond=num_cond,
num_goals=1,
device=device,
x_supervised=sup_image[:, i : i + 1],
)
curr_obs = torch.cat(
(curr_obs, x_pred_pixels.unsqueeze(1)), dim=1
) # append current prediction
curr_obs = curr_obs[:, 1:] # remove first observation
visualize_preds(output_dir, idxs, i, x_pred_pixels)
def generate_time_original(
args,
output_dir,
idxs,
all_models,
obs_image,
gt_output,
delta,
secs,
num_cond,
device,
x_supervised,
):
eval_timesteps = [sec * args.input_fps for sec in secs]
for sec, timestep in zip(secs, eval_timesteps):
curr_delta = delta[:, :timestep].sum(dim=1, keepdim=True)
if args.gt:
x_pred_pixels = gt_output[:, timestep - 1].clone().to(device)
else:
x_pred_pixels = model_forward_wrapper(
all_models,
obs_image,
curr_delta,
timestep,
args.latent_size,
num_cond=num_cond,
num_goals=1,
device=device,
x_supervised=x_supervised,
)
visualize_preds(output_dir, idxs, sec, x_pred_pixels)
def generate_time(
args,
output_dir,
idxs,
all_models,
obs_image,
gt_output,
delta,
secs,
num_cond,
device,
x_supervised,
):
eval_timesteps = [sec * args.input_fps for sec in secs]
num_goals = len(secs)
B = obs_image.size(0)
# 1. 构造多个 goal 的动作输入(B, num_goals, 4)
delta_goals = []
for timestep in eval_timesteps:
d = delta[:, :timestep].sum(dim=1, keepdim=True) # (B, 1, 4)
delta_goals.append(d)
delta_goals = torch.cat(delta_goals, dim=1) # (B, num_goals, 4)
# 2. 扩展所有输入以匹配 B * num_goals
# obs_image = obs_image.unsqueeze(1).repeat(1, num_goals, 1, 1, 1, 1).flatten(0, 1) # (B*num_goals, T, C, H, W)
delta_goals = delta_goals.flatten(0, 1) # (B*num_goals, 4)
if x_supervised is not None:
x_supervised = (
x_supervised.unsqueeze(1).repeat(1, num_goals, 1, 1, 1, 1).flatten(0, 1)
) # (B*num_goals, T, C, H, W)
if args.gt:
# 直接从 ground truth 中提取目标帧
x_pred_pixels = []
for timestep in eval_timesteps:
x_pred_pixels.append(
gt_output[:, timestep - 1].clone().to(device)
) # (B, C, H, W)
x_pred_pixels = torch.stack(x_pred_pixels, dim=1) # (B, num_goals, C, H, W)
else:
# 3. 执行一次 forward,预测多个目标时间点
x_pred_pixels = model_forward_wrapper(
all_models,
obs_image,
delta_goals,
max(eval_timesteps),
args.latent_size,
num_cond=num_cond,
num_goals=num_goals,
device=device,
x_supervised=x_supervised,
) # 返回 (B * num_goals, C, H, W)
x_pred_pixels = x_pred_pixels.view(
B, num_goals, *x_pred_pixels.shape[1:]
) # (B, num_goals, C, H, W)
for goal_idx, sec in enumerate(secs):
visualize_preds(output_dir, idxs, sec, x_pred_pixels[:, goal_idx])
def visualize_preds(output_dir, idxs, sec, x_pred_pixels):
for batch_idx, sample_idx in enumerate(idxs.squeeze()):
sample_idx = int(sample_idx.item())
sample_folder = os.path.join(output_dir, f"id_{sample_idx}")
os.makedirs(sample_folder, exist_ok=True)
image_file = os.path.join(sample_folder, f"{sec}.png")
save_image(image_file, x_pred_pixels[batch_idx], True)
@torch.no_grad()
def main(args):
_, _, device, _ = init_distributed()
print(args)
device = torch.device(device)
num_tasks = dist.get_world_size()
global_rank = dist.get_rank()
exp_eval = args.exp
# model & config setup
if args.gt:
args.save_output_dir = os.path.join(args.output_dir, "gt")
else:
exp_name = os.path.basename(exp_eval).split(".")[0]
args.save_output_dir = os.path.join(args.output_dir, exp_name)
if not args.gt and args.ckp != "0200000":
args.save_output_dir = args.save_output_dir + "_%s" % (args.ckp)
os.makedirs(args.save_output_dir, exist_ok=True)
config = load_runtime_config(exp_eval)
latent_size = config["image_size"] // 8
args.latent_size = config["image_size"] // 8
num_cond = config["context_size"]
print("loading")
model_lst = (None, None, None)
if not args.gt:
from diffusers.models import AutoencoderKL
model = CDiT_models[config["model"]](
context_size=num_cond, input_size=latent_size, in_channels=4
)
ckp = torch.load(
f'{config["results_dir"]}/{config["run_name"]}/checkpoints/{args.ckp}.pth.tar',
map_location="cpu",
weights_only=False,
)
print(model.load_state_dict(ckp["ema"], strict=True))
model.eval()
model.to(device)
model = torch.compile(model)
diffusion = create_diffusion(str(250))
vae = AutoencoderKL.from_pretrained("stabilityai/sd-vae-ft-ema").to(device)
model = torch.nn.parallel.DistributedDataParallel(
model, device_ids=[device], find_unused_parameters=False
)
model_lst = (model, diffusion, vae)
# Loading Datasets
dataset_names = args.datasets.split(",")
datasets = {}
for dataset_name in dataset_names:
dataset_val = get_dataset_eval(
config, dataset_name, args.eval_type, predefined_index=True
)
# dataset_val = Subset(dataset_val, list(range(1000)))
# print(len(dataset_val))
if len(dataset_val) % num_tasks != 0:
print(
"Warning: Enabling distributed evaluation with an eval dataset not divisible by process number. "
"This will slightly alter validation results as extra duplicate entries are added to achieve "
"equal num of samples per-process."
)
sampler_val = torch.utils.data.DistributedSampler(
dataset_val, num_replicas=num_tasks, rank=global_rank, shuffle=False
)
curr_data_loader = torch.utils.data.DataLoader(
dataset_val,
sampler=sampler_val,
batch_size=args.batch_size,
num_workers=args.num_workers,
pin_memory=True,
drop_last=False,
)
datasets[dataset_name] = curr_data_loader
print_freq = 1
header = "Evaluation: "
metric_logger = dist.MetricLogger(delimiter=" ")
for dataset_name in dataset_names:
dataset_save_output_dir = os.path.join(args.save_output_dir, dataset_name)
os.makedirs(dataset_save_output_dir, exist_ok=True)
curr_data_loader = datasets[dataset_name]
for data_iter_step, (idxs, obs_image, gt_image, delta, aug_image) in enumerate(
metric_logger.log_every(curr_data_loader, print_freq, header)
):
with torch.amp.autocast("cuda", enabled=True, dtype=torch.bfloat16):
obs_image = obs_image[:, -num_cond:].to(device)
gt_image = gt_image.to(device)
num_cond = config["context_size"]
if args.eval_type == "rollout":
for rollout_fps in args.rollout_fps_values:
curr_rollout_output_dir = os.path.join(
dataset_save_output_dir, f"rollout_{rollout_fps}fps"
)
os.makedirs(curr_rollout_output_dir, exist_ok=True)
generate_rollout(
args,
curr_rollout_output_dir,
rollout_fps,
idxs,
model_lst,
obs_image,
gt_image,
delta,
num_cond,
device,
aug_image,
)
elif args.eval_type == "time":
secs = np.array([2**i for i in range(0, args.num_sec_eval)])
curr_time_output_dir = os.path.join(dataset_save_output_dir, "time")
os.makedirs(curr_time_output_dir, exist_ok=True)
generate_time(
args,
curr_time_output_dir,
idxs,
model_lst,
obs_image,
gt_image,
delta,
secs,
num_cond,
device,
aug_image,
)
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--output_dir", type=str, default="outputs/inference", help="output directory"
)
parser.add_argument(
"--exp", type=str, default="config/anwm.yaml", help="experiment config"
)
parser.add_argument("--ckp", type=str, default="0200000")
parser.add_argument("--num_sec_eval", type=int, default=5)
parser.add_argument("--input_fps", type=int, default=4)
parser.add_argument(
"--datasets", type=str, default="airvln_16", help="dataset name"
)
parser.add_argument("--num_workers", type=int, default=8, help="num workers")
parser.add_argument("--batch_size", type=int, default=16, help="batch size")
parser.add_argument(
"--eval_type", type=str, default="rollout", choices=["time", "rollout"]
)
# Rollout Evaluation Args
parser.add_argument("--rollout_fps_values", type=str, default="1,4", help="")
parser.add_argument(
"--gt",
type=int,
default=0,
help="set to 1 to produce ground truth evaluation set",
)
args = parser.parse_args()
args.rollout_fps_values = [int(fps) for fps in args.rollout_fps_values.split(",")]
main(args)