-
Notifications
You must be signed in to change notification settings - Fork 8
/
test.py
189 lines (147 loc) · 6.29 KB
/
test.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
177
178
179
180
181
182
183
184
185
186
187
188
189
"""
Evaluation of PolyGNN.
"""
import pickle
from pathlib import Path
import multiprocessing
import numpy as np
import torch
import hydra
from omegaconf import DictConfig
from tqdm import tqdm
from torch.nn.parallel import DistributedDataParallel
from torch_geometric.loader import DataLoader
import torch.multiprocessing as mp
from torchmetrics.classification import BinaryAccuracy
import torch.distributed as dist
from torch_geometric import compile
from network import PolyGNN
from dataset import CityDataset, TestOnlyDataset
from utils import init_device, Sampler, set_seed, attach_to_log, setup_runner
class PredictionSaver:
"""
Asynchronous prediction saving.
"""
def __init__(self, processes):
self.pool = multiprocessing.Pool(processes=processes)
@staticmethod
def save(args):
pred, name, cfg = args
indices_cells = np.where(pred.cpu().numpy())[0]
if len(indices_cells) > 0:
complex_path = f'{cfg.complex_dir}/{name}.cc'
with open(complex_path, 'rb') as handle:
cell_complex = pickle.load(handle)
output_path = f'{cfg.output_dir}/{name}.npy'
output = np.zeros([cell_complex.num_cells], dtype=int)
output[indices_cells] = 1
if cfg.evaluate.seal:
cells_boundary = cell_complex.cells_boundary()
output[cells_boundary] = 0
np.save(output_path, output)
def run_eval(rank, world_size, dataset_test, cfg):
"""
Runner function for distributed inference of PolyGNN.
"""
# set up runner
setup_runner(rank, world_size, cfg.master_addr, cfg.master_port)
# limit number of threads
torch.set_num_threads(cfg.num_workers // world_size)
# initialize logging
logger = attach_to_log(filepath='./outputs/test.log')
# indicate device
logger.debug(f"Device activated: " + f"CUDA: {cfg.gpu_ids[rank]}")
# initialize metric
metric = BinaryAccuracy()
# split test indices into `world_size` many chunks
eval_indices = torch.arange(len(dataset_test))
eval_indices = eval_indices.split(len(eval_indices) // world_size)[rank]
dataloader_test = DataLoader(dataset_test[eval_indices], batch_size=cfg.batch_size // world_size,
shuffle=cfg.shuffle, num_workers=cfg.num_workers // world_size,
pin_memory=True, prefetch_factor=8)
# initialize model
model = PolyGNN(cfg)
model.metric = metric
model = model.to(rank)
# distributed parallelization
model = DistributedDataParallel(model, device_ids=[rank])
# compile model for better performance
compile(model, dynamic=True, fullgraph=True)
# load from checkpoint
map_location = f'cuda:{rank}'
if rank == 0:
logger.info(f'Resuming from {cfg.checkpoint_path}')
state = torch.load(cfg.checkpoint_path, map_location=map_location)
state_dict = state['state_dict']
model.load_state_dict(state_dict, strict=False)
# specify data attributes
if cfg.sample.strategy == 'grid':
points_suffix = f'_{cfg.sample.resolution}'
elif cfg.sample.strategy == 'random':
points_suffix = f'_{cfg.sample.length}'
else:
points_suffix = ''
# start inference
model.eval()
pbar = tqdm(dataloader_test, desc=f'eval', disable=rank != 0)
# initialize PredictionSaver instance
prediction_saver = PredictionSaver(processes=cfg.num_workers // world_size)
with torch.no_grad():
for batch in pbar:
batch = batch.to(rank, f'points{points_suffix}', f'batch_points{points_suffix}', 'queries',
'edge_index', 'batch', 'y')
outs = model(batch)
outs = outs.argmax(dim=1)
targets = batch.y
# metric on current batch
_accuracy = metric(outs, targets)
pbar.set_postfix_str('acc={:.2f}'.format(_accuracy))
# save prediction as numpy file
if cfg.evaluate.save:
Path(cfg.output_dir).mkdir(exist_ok=True)
_, boundary_indices = torch.unique(batch.batch, return_counts=True)
preds = torch.split(outs, split_size_or_sections=boundary_indices.tolist(), dim=0)
names = batch.name
# asynchronous file saving
prediction_saver.pool.map(prediction_saver.save, zip(preds, names, [cfg] * len(preds)))
# metric on all batches and all accelerators using custom accumulation
accuracy = metric.compute()
if rank == 0:
logger.info(f"Evaluation accuracy: {accuracy}")
# reset internal state such that metric ready for new data
metric.reset()
dist.barrier()
dist.destroy_process_group()
@hydra.main(config_path='./conf', config_name='config', version_base='1.2')
def test(cfg: DictConfig):
"""
Test PolyGNN for reconstruction.
Parameters
----------
cfg: DictConfig
Hydra configuration
"""
# initialize logger
logger = attach_to_log(filepath='./outputs/test.log')
# initialize device
init_device(cfg.gpu_ids, register_freeze=cfg.gpu_freeze)
logger.info(f"Device initialized: " + f"CUDA: {cfg.gpu_ids}")
# fix randomness
set_seed(cfg.seed)
logger.info(f"Random seed set to {cfg.seed}")
# initialize data sampler
sampler = Sampler(strategy=cfg.sample.strategy, length=cfg.sample.length, ratio=cfg.sample.ratio,
resolutions=cfg.sample.resolutions, duplicate=cfg.sample.duplicate, seed=cfg.seed)
transform = sampler.sample if cfg.sample.transform else None
pre_transform = sampler.sample if cfg.sample.pre_transform else None
# initialize dataset
if cfg.dataset in {'munich', 'munich_perturb', 'munich_subsample', 'munich_truncate', 'munich_haswall', 'campus_ldbv'}:
dataset = TestOnlyDataset(pre_transform=pre_transform, transform=transform, root=cfg.data_dir,
split='test', num_workers=cfg.num_workers)
else:
dataset = CityDataset(pre_transform=pre_transform, transform=transform, root=cfg.data_dir,
split='test', num_workers=cfg.num_workers)
world_size = len(cfg.gpu_ids)
mp.spawn(run_eval, args=(world_size, dataset, cfg), nprocs=world_size, join=True)
if __name__ == '__main__':
test()