-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathinference.py
176 lines (146 loc) · 4.13 KB
/
inference.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
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
"""
Inference
Usage example:
- `python inference.py --data_size 32 --batch_size 8 --partition test
--random --checkpoint_path ~/PROJs/NeuralCompression_results/checkpoints/
--epoch 440 --save_path inference_results --half`
"""
import argparse
from pathlib import Path
import tqdm
import numpy as np
import torch
from neuralcompress.utils.load_bcae_models import load_bcae_encoder
from neuralcompress.utils.tpc_dataloader import get_tpc_dataloaders
DATA_ROOT = '/data/datasets/sphenix/highest_framedata_3d/outer'
def inference():
"""
Inference
"""
parser = argparse.ArgumentParser(
description="Run BCAE inference"
)
parser.add_argument(
'--data_path',
required = False,
default = DATA_ROOT,
type = str,
help = "The path to data."
)
parser.add_argument(
'--device',
required = False,
default = 'cuda',
choices = ['cuda', 'cpu'],
type = str,
help = "The device to run the inference."
)
parser.add_argument(
'--data_size',
required = False,
default = 1,
type = int,
help = "Number of frames to load | default=1."
)
parser.add_argument(
'--batch_size',
required = False,
default = 1,
type = int,
help = "Batch size | default=1."
)
parser.add_argument(
'--partition',
required = False,
default = 'test',
choices=['train', 'valid', 'test'],
type = str,
help = "partition from which to load the data | default=test."
)
parser.add_argument(
'--random',
action = 'store_true',
help = "Whether to get a random sample."
)
parser.add_argument(
'--checkpoint_path',
required=True,
type=str,
help="The path to the checkpoints."
)
parser.add_argument(
'--epoch',
required=True,
type=int,
help="The epoch to load."
)
parser.add_argument(
'--save_path',
required = True,
type = str,
help = "The path to save output tensor."
)
parser.add_argument(
'--half',
action = 'store_true',
help = "Whether to save the output with half precision."
)
parser.add_argument(
'--prefix',
required = False,
default = 'output',
type = str,
help = "Output file prefix."
)
args = parser.parse_args()
# Load data
data_config = {
'batch_size' : args.batch_size,
'train_sz' : 0,
'valid_sz' : 0,
'test_sz' : 0,
'is_random' : args.random,
}
data_config[f'{args.partition}_sz'] = args.data_size
data_path = Path(args.data_path)
assert data_path.exists(), f'{data_path} does not exist!'
loaders = get_tpc_dataloaders(data_path, **data_config)
partition = args.partition
if partition == 'train':
loader = loaders[0]
elif partition == 'valid':
loader = loaders[1]
else:
loader = loaders[2]
# Load encoder
checkpoint_path = Path(args.checkpoint_path)
assert checkpoint_path.exists(), f'{checkpoint_path} does not exist!'
encoder = load_bcae_encoder(checkpoint_path, args.epoch)
encoder.to(args.device)
# run inference
progbar = tqdm.tqdm(
desc="BCAE Inference",
total=len(loader),
dynamic_ncols=True
)
outputs = []
with torch.no_grad():
for batch in loader:
output = encoder(batch.to(args.device))
outputs.append(output.detach().cpu().numpy())
progbar.update()
progbar.close()
save_path = Path(args.save_path)
if not save_path.exists():
save_path.mkdir(parents=True)
# save result
counter = 0
for output in outputs:
for frame in output:
if args.half:
frame = frame.astype('float16')
fname = save_path/f'{args.prefix}_{counter}'
np.savez(fname, data=frame)
counter += 1
if __name__ == '__main__':
inference()