forked from Kommiu/GEM-CNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.py
More file actions
65 lines (48 loc) · 2.22 KB
/
test.py
File metadata and controls
65 lines (48 loc) · 2.22 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
import argparse
from pathlib import Path
import torch
import pandas as pd
from pytorch_lightning import Trainer
from pytorch_lightning.logging.neptune import NeptuneLogger
from gem_cnn.models import MeshNetwork
from gem_cnn.transforms import GetLocalPatch
from time import sleep
def predict_mesh(path, path_out, model: MeshNetwork, patch_radius, num_runs):
data = torch.load(path)
device = torch.device('cuda')
data.predictions = torch.zeros_like(data.y)
data.count = torch.zeros_like(data.y)
for i in range(num_runs):
sample = GetLocalPatch(data, patch_radius, len(model.gem_network.gem_convs)).to(device)
data.predictions[sample.original_points] += model(sample).cpu()
data.count[sample.original_points] += 1
y = data.y.numpy()
y_hat = (data.predictions/data.count).numpy()
pd.DataFrame({'target': y, 'prediction': y_hat}).to_csv(path_out, index=False)
class LoadFromFile (argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
with values as f:
parser.parse_args(f.read().split(), namespace)
def main():
conf_parser = argparse.ArgumentParser()
conf_parser.add_argument('--config')
model_parser = MeshNetwork.add_model_specific_args(argparse.ArgumentParser())
test_parser = argparse.ArgumentParser()
test_parser.add_argument('--test_input_dir')
test_parser.add_argument('--test_output_dir')
test_parser.add_argument('--test_num_runs', dtype=int, default=100)
test_parser.add_argument('--test_checkpoint_path')
args = conf_parser.parse_args()
config = args.config
with open(config) as f:
config_lines = f.read().split()
model_args, config_lines = model_parser.parse_known_args(config_lines)
test_args, _ = test_parser.parse_known_args(config_lines)
model = MeshNetwork(hparams=model_args)
state_dict = torch.load(test_args.test_checkpoint_path)
model.load_state_dict(state_dict).to(torch.device('cuda'))
for path in Path(test_args.test_input_path).glob('*.pt'):
output_path = Path(test_args.test_output_path, path.stem, '.csv')
predict_mesh(path, output_path, model, model_args.patch_radius, test_args.test_num_runs)
if __name__ == '__main__':
main()