|
a |
|
b/examples/local.py |
|
|
1 |
#!/usr/bin/env python |
|
|
2 |
|
|
|
3 |
__doc__ = """ |
|
|
4 |
Simple example script showing how to use the Sybil library locally to predict risk scores for a set of DICOM files. |
|
|
5 |
""" |
|
|
6 |
|
|
|
7 |
import sybil |
|
|
8 |
from sybil import visualize_attentions |
|
|
9 |
|
|
|
10 |
from utils import get_demo_data |
|
|
11 |
|
|
|
12 |
|
|
|
13 |
def main(): |
|
|
14 |
# Load a trained model |
|
|
15 |
model = sybil.Sybil("sybil_ensemble") |
|
|
16 |
|
|
|
17 |
dicom_files = get_demo_data() |
|
|
18 |
|
|
|
19 |
# Get risk scores |
|
|
20 |
serie = sybil.Serie(dicom_files) |
|
|
21 |
print(f"Processing {len(dicom_files)} DICOM files") |
|
|
22 |
prediction = model.predict([serie], return_attentions=True) |
|
|
23 |
scores = prediction.scores |
|
|
24 |
|
|
|
25 |
print(f"Risk scores: {scores}") |
|
|
26 |
|
|
|
27 |
# Visualize attention maps |
|
|
28 |
output_dir = "sybil_attention_output" |
|
|
29 |
|
|
|
30 |
print(f"Writing attention images to {output_dir}") |
|
|
31 |
series_with_attention = visualize_attentions( |
|
|
32 |
serie, |
|
|
33 |
attentions=prediction.attentions, |
|
|
34 |
save_directory=output_dir, |
|
|
35 |
gain=3, |
|
|
36 |
) |
|
|
37 |
|
|
|
38 |
print(f"Finished writing attention images to {output_dir}") |
|
|
39 |
|
|
|
40 |
if __name__ == "__main__": |
|
|
41 |
main() |