-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
150 lines (126 loc) · 4.53 KB
/
predict.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
import argparse
import os.path
import random
import sys
import torch
from lightning import Trainer
from tqdm import tqdm
from architectures.rl_glimpse import BaseRlMAE
from architectures.utils import RevNormalizer
from utils.prediction_hooks import RLStateReplaceHook, RLUserHook
from utils.prepare import experiment_from_args
from utils.visualisation_animate import animate_one
from utils.visualisation_grid import visualize_grid
random.seed(1)
torch.manual_seed(1)
torch.set_float32_matmul_precision("high")
def define_args(parent_parser):
parser = parent_parser.add_argument_group("predict.py")
parser.add_argument(
"--visualization-path",
help="path to save visualizations to",
type=str,
default=None,
)
parser.add_argument(
"--animate",
help="animate visualizations",
type=bool,
default=False,
action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--model-checkpoint",
help='path to a saved model state',
type=str,
required=True
)
parser.add_argument(
"--random-samples",
help='sample random images from the dataset',
type=int,
default=None
)
parser.add_argument(
"--save-all",
help='save all visualisation elements',
type=bool,
default=False,
action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--dump-avg-state",
help='dump average state values',
type=bool,
default=False,
action=argparse.BooleanOptionalAction
)
parser.add_argument(
"--replace-state",
help='replace state element with value from file',
type=str,
default=None,
choices=['patches', 'coords', 'importance', 'latent']
)
return parent_parser
def visualize(visualization_path, data_hook, model, save_all=False, animate=False):
data = data_hook.compute()
rev_normalizer = RevNormalizer()
images = rev_normalizer(data["images"]).to(torch.uint8)
patches = rev_normalizer(data["patches"]).to(torch.uint8)
for idx, (img, out, coord, patch, score, target, done) in enumerate(tqdm(
zip(images, data["out"], data["coords"], patches, data['scores'], data['targets'], data['done']),
total=images.shape[0])):
if animate:
animate_one(model, img, out, coord, patch, score, target, done,
os.path.join(visualization_path, f"{idx}.mp4"), rev_normalizer)
else:
visualize_grid(model, img, out, coord, patch, score, target, done,
os.path.join(visualization_path, f"{idx}.png"), rev_normalizer, save_all)
def dump_avg_state(data_hook):
data = data_hook.compute()
avg_patch = data["patches"].mean(dim=0).mean(dim=0)
avg_coords = data["coords"].mean(dim=0).mean(dim=0)
avg_importance = data["importance"][-1].mean(dim=0).mean(dim=0)
avg_latent = data["latent"].mean(dim=0).mean(dim=0)
torch.save({
"avg_patch": avg_patch,
"avg_coords": avg_coords,
"avg_importance": avg_importance,
"avg_latent": avg_latent
}, 'avg_state.pck')
def main():
model: BaseRlMAE
data_module, model, args = experiment_from_args(
sys.argv, add_argparse_args_fn=define_args
)
data_module.num_random_eval_samples = args.random_samples
model.parallel_games = 0
model.load_pretrained(args.model_checkpoint)
model.eval()
if not isinstance(model, BaseRlMAE):
raise RuntimeError(f"Unrecognized model type: {type(model)}")
data_hook = None
if args.visualization_path is not None or args.dump_avg_state:
data_hook = model.add_user_forward_hook(RLUserHook(avg_latent=args.dump_avg_state))
if args.replace_state is not None:
avg_state = torch.load('avg_state.pck')
replacement = {
'patches': 'avg_patch',
'coords': 'avg_coords',
'importance': 'avg_importance',
'latent': 'avg_latent'
}[args.replace_state]
model.add_user_forward_hook(RLStateReplaceHook(**{replacement: avg_state[replacement]}))
trainer = Trainer()
trainer.test(model)
if args.dump_avg_state:
dump_avg_state(data_hook)
return
if args.visualization_path is not None:
visualization_path = args.visualization_path
os.makedirs(visualization_path, exist_ok=True)
visualize(visualization_path, data_hook, model, save_all=args.save_all, animate=args.animate)
return
if __name__ == "__main__":
main()