forked from insait-institute/GenieRedux
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathevaluate.py
More file actions
265 lines (210 loc) · 9.05 KB
/
evaluate.py
File metadata and controls
265 lines (210 loc) · 9.05 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
import os
from pathlib import Path
from tqdm import tqdm
from models import construct_model
from training.evaluation import Evaluator
os.chdir(os.path.dirname(os.path.abspath(__file__)))
from einops import rearrange
import torch
from data.data import (
DatasetOutputFormat,
TransformsGenerator,
EnvironmentDataset,
video_tensor_to_gif,
video_tensor_to_pil_images,
)
from torch.utils.data import DataLoader
from PIL import Image
from accelerate import Accelerator, DistributedType
from accelerate.utils import DistributedDataParallelKwargs
import logging
logging.basicConfig(level=logging.INFO)
from tools.logger import getLogger
log = getLogger(__name__)
def get_inference_method(model, args, is_distributed=False):
if args.model == "tokenizer":
return model
if "genie" in args.model:
if args.eval.inference_method == "autoregressive":
if is_distributed:
sample_method = model.generate_interactive_video
else:
sample_method = model.module.generate_interactive_video
elif args.eval.inference_method == "one_go":
if is_distributed:
sample_method = model.sample
else:
sample_method = model.module.sample
return sample_method
def convert_index_to_one_hot(index, num_classes):
one_hot = torch.zeros((*index.shape, num_classes), device=index.device)
one_hot.scatter_(-1, index.unsqueeze(-1), 1)
return one_hot
def generate_random_different_action_indices(actions_indices, device, num_actions=7):
shape = actions_indices.shape
random_actions = torch.randint(0, num_actions, shape, device=device)
while torch.any(random_actions == actions_indices):
random_actions = torch.where(
random_actions == actions_indices,
torch.randint(0, num_actions, shape, device=device),
random_actions,
)
return random_actions
def evaluate(
model,
evaluator,
test_loader,
device,
args,
is_main_process=True,
is_distributed=False,
):
inference_method = get_inference_method(model, args, is_distributed)
with torch.no_grad():
psnr_scores = []
ssim_scores = []
delta_psnr_scores = []
for i, videos in enumerate(
tqdm(
test_loader,
total=len(test_loader),
desc="Evaluating",
disable=not is_main_process,
)
):
actions = videos["actions"]
videos = videos["input_frames"]
sample_num_frames = args.eval.sample_num_frames
delta_psnr_horizon = args.eval.delta_psnr_horizon
num_first_frames = args.eval.num_first_frames
dream_length = args.eval.dream_length
num_actions = args.eval.num_actions
actions = actions.to(device)[:, : num_first_frames + sample_num_frames - 1]
actions = actions.argmax(dim=-1)
if args.eval.action_to_take != -1:
actions = actions * 0 + args.eval.action_to_take
videos = videos.to(device)[
:,
: num_first_frames + sample_num_frames,
]
videos = rearrange(videos, "b f c h w -> b c f h w")
first_frames = videos[:, :, :num_first_frames]
recons = inference_method(
videos=videos,
prime_frames=first_frames,
actions=actions,
num_frames=sample_num_frames,
inference_steps=args.eval.inference_steps,
dream_length=dream_length,
return_recons_only=True,
)
recons = torch.clamp(recons, min=0, max=1)
videos = videos[:, :, num_first_frames:]
recons_random = None
delta_psnr = None
if args.eval.eval_control:
random_actions = generate_random_different_action_indices(
actions[:, : num_first_frames + delta_psnr_horizon - 1],
device,
num_actions=num_actions,
)
new_actions = actions[
:, : num_first_frames + delta_psnr_horizon - 1
].clone()
new_actions[:, -1] = random_actions[:, -1]
recons_random = inference_method(
videos=videos,
prime_frames=first_frames,
actions=new_actions,
num_frames=delta_psnr_horizon,
dream_length=dream_length,
return_recons_only=True,
)
recons_random = torch.clamp(recons_random, min=0, max=1)
delta_psnr = evaluator.delta_psnr(
videos[:, :, delta_psnr_horizon - 1 : delta_psnr_horizon],
recons[:, :, delta_psnr_horizon - 1 : delta_psnr_horizon],
recons_random[:, :, -1:],
)
delta_psnr_scores.append(delta_psnr)
evaluator.fid_update_batch(videos, recons)
psnr = evaluator.psnr(videos, recons)
ssim = evaluator.ssim(
rearrange(videos, "b c f h w -> (b f) c h w"),
rearrange(recons, "b c f h w -> (b f) c h w"),
)
psnr_scores.append(psnr)
ssim_scores.append(ssim)
log.i(f"Current scores: {psnr} PSNR; {ssim} SSIM, {delta_psnr} Delta PSNR")
# continue
if i < 10 and is_main_process:
sampled_videos_path = Path(args.eval.save_root_dpath) / f"{args.eval.dataset_name}/{args.eval.model_name}/samples_{i}"
(sampled_videos_path).mkdir(parents=True, exist_ok=True)
for j, recons_frames in enumerate(recons.unbind(dim=0)):
if j >= 10:
break
recons_frames = torch.cat([first_frames[j], recons_frames], dim=1)
orig_frames = torch.cat([first_frames[j], videos[j]], dim=1)
combined_frames = torch.cat([orig_frames, recons_frames], dim=2)
recon_frames = video_tensor_to_pil_images(
recons_frames.cpu(), only_first_image=False
)
orig_frames = video_tensor_to_pil_images(
orig_frames.cpu(), only_first_image=False
)
combined_height = orig_frames.height + recon_frames.height
combined_image = Image.new(
"RGB", (orig_frames.width, combined_height)
)
# Paste the images into the combined image
combined_image.paste(orig_frames, (0, 0))
combined_image.paste(recon_frames, (0, orig_frames.height))
video_tensor_to_gif(
combined_frames.cpu(),
str(sampled_videos_path / f"{j}.gif"),
)
combined_image.save(sampled_videos_path / f"{j}.png")
if is_main_process:
print(f"Batch {i} Evaluation done!")
psnr_score = torch.mean(torch.tensor(psnr_scores, device=device))
ssim_score = torch.mean(torch.tensor(ssim_scores, device=device))
delta_psnr_score = torch.mean(torch.tensor(delta_psnr_scores, device=device))
psnr_score = psnr_score.mean().item()
ssim_score = ssim_score.mean().item()
delta_psnr_score = delta_psnr_score.mean().item()
fid_score = evaluator.fid()
log.i(
f"Device: {device},Average FID: {fid_score}, Average PSNR: {psnr_score}, Average SSIM: {ssim_score}, Average Delta PSNR: {delta_psnr_score}"
)
@torch.no_grad()
def run(args):
dataset_folder = f"{args.eval.dataset_root_dpath}/{args.eval.dataset_name}/{args.eval.dataset_name}"
kwargs = DistributedDataParallelKwargs(find_unused_parameters=True)
accelerator = Accelerator(kwargs_handlers=[kwargs])
device = accelerator.device
evaluator = Evaluator(device)
model = construct_model(args)
model_state_dict = torch.load(args.eval.model_fpath, map_location="cpu")
model.load_state_dict(model_state_dict["model"])
del model_state_dict
transforms = TransformsGenerator.get_final_transforms(model.image_size, None)
test_data_set = EnvironmentDataset(
dataset_folder,
seq_length_input=args.eval.num_frames - 1,
seq_step=args.eval.seq_step,
split_type="instance",
split="all",
transform=transforms["test"],
format=DatasetOutputFormat.IVG,
enable_cache=True,
cache_dpath=f"cache/evaluation/{args.eval.dataset_name}",
)
test_loader = DataLoader(
test_data_set, batch_size=args.eval.batch_size, shuffle=False, num_workers=6
)
model, test_loader, evaluator = accelerator.prepare(model, test_loader, evaluator)
is_main_process = accelerator.is_main_process
is_distributed = accelerator.distributed_type == DistributedType.NO
evaluate(
model, evaluator, test_loader, device, args, is_main_process, is_distributed
)