-
Notifications
You must be signed in to change notification settings - Fork 41
/
predict_utils.py
137 lines (117 loc) · 4.38 KB
/
predict_utils.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
import glob
import json
import os
import numpy as np
import traceback
class NpEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, np.integer):
return int(obj)
if isinstance(obj, np.floating):
return float(obj)
if isinstance(obj, np.ndarray):
return obj.tolist()
return super(NpEncoder, self).default(obj)
class ScanSegmentation(): # SegmentationAlgorithm is not inherited in this class anymore
def __init__(self, model):
"""
Write your own input validators here
Initialize your model etc.
"""
self.chl_pipeline = model
#self.model = load_model()
#sef.device = "cuda"
pass
@staticmethod
def load_input(input_dir):
"""
Read from /input/
Check https://grand-challenge.org/algorithms/interfaces/
"""
# iterate over files in input_dir, assuming only 1 file available
inputs = glob.glob(f'{input_dir}/*.obj')
print("scan to process:", inputs)
return inputs
@staticmethod
def write_output(labels, instances, jaw, output_path):
"""
Write to /output/dental-labels.json your predicted labels and instances
Check https://grand-challenge.org/components/interfaces/outputs/
"""
pred_output = {'id_patient': "",
'jaw': jaw,
'labels': labels,
'instances': instances
}
# just for testing
#with open('./test/test_local/expected_output.json', 'w') as fp:
with open(output_path, 'w') as fp:
json.dump(pred_output, fp, cls=NpEncoder)
return
@staticmethod
def get_jaw(scan_path):
try:
# read jaw from filename
_, jaw = os.path.basename(scan_path).split('.')[0].split('_')
except:
# read from first line in obj file
try:
with open(scan_path, 'r') as f:
jaw = f.readline()[2:-1]
if jaw not in ["upper", "lower"]:
return None
except Exception as e:
print(str(e))
print(traceback.format_exc())
return None
return jaw
def predict(self, inputs):
"""
Your algorithm goes here
"""
try:
assert len(inputs) == 1, f"Expected only one path in inputs, got {len(inputs)}"
except AssertionError as e:
raise Exception(e.args)
scan_path = inputs[0]
#print(f"loading scan : {scan_path}")
# read input 3D scan .obj
try:
# you can use trimesh or other any loader we keep the same order
#mesh = trimesh.load(scan_path, process=False)
pred_result = self.chl_pipeline(scan_path)
jaw = self.get_jaw(scan_path)
if jaw == "lower":
pred_result["sem"][pred_result["sem"]>0] += 20
elif jaw=="upper":
pass
else:
raise "jaw name error"
print("jaw processed is:", jaw)
except Exception as e:
print(str(e))
print(traceback.format_exc())
raise
# preprocessing if needed
# prep_data = preprocess_function(mesh)
# inference data here
# labels, instances = self.model(mesh, jaw=None)
# extract number of vertices from mesh
nb_vertices = pred_result["sem"].shape[0]
# just for testing : generate dummy output instances and labels
instances = pred_result["ins"].astype(int).tolist()
labels = pred_result["sem"].astype(int).tolist()
try:
assert (len(labels) == len(instances) and len(labels) == nb_vertices),\
"length of output labels and output instances should be equal"
except AssertionError as e:
raise Exception(e.args)
return labels, instances, jaw
def process(self, input_path, output_path):
"""
Read input from /input, process with your algorithm and write to /output
assumption /input contains only 1 file
"""
#input = self.load_input(input_dir='./test/test_local')
labels, instances, jaw = self.predict([input_path])
self.write_output(labels=labels, instances=instances, jaw=jaw, output_path=output_path)