-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualise.py
88 lines (79 loc) · 2.73 KB
/
visualise.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
import argparse
import torch
import torchshow as ts
import data
import tensorly as tl
import os
from data import PreprocessedEPICDataset
tl.set_backend('pytorch')
if torch.cuda.is_available():
DEVICE = torch.device('cuda')
else:
DEVICE = torch.device('cpu')
parser = argparse.ArgumentParser(
description="Tool for visualising dataset clips and compression",
formatter_class=argparse.ArgumentDefaultsHelpFormatter,
)
parser.add_argument(
"--label",
default="EPIC",
type=str,
help="Label prepended to preprocessed dataset files"
)
parser.add_argument(
"--model-label",
default=None,
type=str,
help="Label of saved model checkpoint"
)
parser.add_argument(
"--modes",
nargs='*',
default=None,
type=int,
help="Modes corresponding to measurement matrices"
)
parser.add_argument(
"--chunks",
default=1,
type=int,
help="Number of evenly sized chunks in preprocessed dataset"
)
parser.add_argument(
"--split",
choices=["train", "val", "test"],
default="train",
help="Dataset split to visualise from",
)
parser.add_argument(
"--index",
default=0,
type=int,
help="Index of clip to visualise"
)
def get_dataset(dataprocessor, args):
dataset = PreprocessedEPICDataset(dataprocessor, args.label, args.chunks, args.split)
return dataset
def main(args):
dataprocessor = data.DataProcessor('', 'annotations', 'data')
dataset = get_dataset(dataprocessor, args)
clip = dataset.__getitem__(args.index)[0].float().to(DEVICE)
ts.show(clip)
if args.modes != None:
if os.path.exists(f'checkpoints/{args.model_label}/phi_{args.model_label}.pt'):
phi_matrices = list(torch.load(f'checkpoints/{args.model_label}/phi_{args.model_label}.pt', map_location=DEVICE))
ts.show(phi_matrices, mode='image')
compressed_clip = tl.tenalg.multi_mode_dot(clip, phi_matrices, args.modes)
if compressed_clip.size(1) == 3: mode = 'image'
elif compressed_clip.size(1) == 1: mode = 'grayscale'
ts.show(compressed_clip, mode=mode)
if not os.path.exists(f'checkpoints/{args.model_label}/theta_{args.model_label}.pt'):
inferred_clip = tl.tenalg.multi_mode_dot(compressed_clip, phi_matrices, args.modes, transpose=True)
ts.show(inferred_clip)
else:
theta_matrices = list(torch.load(f'checkpoints/{args.model_label}/theta_{args.model_label}.pt', map_location=DEVICE))
ts.show(theta_matrices, mode='image')
inferred_clip = tl.tenalg.multi_mode_dot(compressed_clip, theta_matrices, args.modes, transpose=True)
ts.show(inferred_clip)
if __name__ == "__main__":
main(parser.parse_args())