MCPcopy Create free account
hub / github.com/OpenDriveLab/ReSim / log_video

Function log_video

sat/train_video.py:63–113  ·  view source on GitHub ↗
(batch, model, args, only_log_video_latents=False)

Source from the content-addressed store, hash-verified

61
62
63def log_video(batch, model, args, only_log_video_latents=False):
64 texts = batch["txt"]
65 text_save_dir = os.path.join(args.save, "video_texts")
66 os.makedirs(text_save_dir, exist_ok=True)
67 save_texts(texts, text_save_dir, args.iteration)
68
69 gpu_autocast_kwargs = {
70 "enabled": torch.is_autocast_enabled(),
71 "dtype": torch.get_autocast_gpu_dtype(),
72 "cache_enabled": torch.is_autocast_cache_enabled(),
73 }
74 with torch.no_grad(), torch.cuda.amp.autocast(**gpu_autocast_kwargs):
75 videos = model.log_video(batch, only_log_video_latents=only_log_video_latents)
76
77 if torch.distributed.get_rank() == 0:
78 root = os.path.join(args.save, "video")
79
80 if only_log_video_latents:
81 root = os.path.join(root, "latents")
82 filename = "{}_gs-{:06}".format("latents", args.iteration)
83 path = os.path.join(root, filename)
84 os.makedirs(os.path.split(path)[0], exist_ok=True)
85 os.makedirs(path, exist_ok=True)
86 torch.save(videos["latents"], os.path.join(path, "latent.pt"))
87 else:
88 for k in videos:
89 N = videos[k].shape[0]
90 if not isheatmap(videos[k]):
91 videos[k] = videos[k][:N]
92 if isinstance(videos[k], torch.Tensor):
93 videos[k] = videos[k].detach().float().cpu()
94 if not isheatmap(videos[k]):
95 videos[k] = torch.clamp(videos[k], -1.0, 1.0)
96
97 num_frames = batch["num_frames"][0]
98 fps = batch["fps"][0].cpu().item()
99 if only_log_video_latents:
100 root = os.path.join(root, "latents")
101 filename = "{}_gs-{:06}".format("latents", args.iteration)
102 path = os.path.join(root, filename)
103 os.makedirs(os.path.split(path)[0], exist_ok=True)
104 os.makedirs(path, exist_ok=True)
105 torch.save(videos["latents"], os.path.join(path, "latents.pt"))
106 else:
107 for k in videos:
108 samples = (videos[k] + 1.0) / 2.0
109 filename = "{}_gs-{:06}".format(k, args.iteration)
110
111 path = os.path.join(root, filename)
112 os.makedirs(os.path.split(path)[0], exist_ok=True)
113 save_video_as_grid_and_mp4(samples, path, num_frames // fps, fps, args, k)
114
115
116def broad_cast_batch(batch):

Callers

nothing calls this directly

Calls 5

isheatmapFunction · 0.90
save_textsFunction · 0.85
log_videoMethod · 0.80
saveMethod · 0.80

Tested by

no test coverage detected