Spaces:
Runtime error
Runtime error
Update inference.py
Browse files- inference.py +104 -74
inference.py
CHANGED
|
@@ -1,75 +1,105 @@
|
|
| 1 |
-
import torch
|
| 2 |
-
import tempfile
|
| 3 |
-
import numpy as np
|
| 4 |
-
import nibabel as nib
|
| 5 |
-
from fastapi import FastAPI, UploadFile, File
|
| 6 |
-
|
| 7 |
-
def preprocess_input(flair_file: UploadFile, t1_file: UploadFile, t1ce_file: UploadFile, t2_file: UploadFile):
|
| 8 |
-
with tempfile.NamedTemporaryFile(suffix=".nii") as temp_flair, \
|
| 9 |
-
tempfile.NamedTemporaryFile(suffix=".nii") as temp_t1, \
|
| 10 |
-
tempfile.NamedTemporaryFile(suffix=".nii") as temp_t1ce, \
|
| 11 |
-
tempfile.NamedTemporaryFile(suffix=".nii") as temp_t2:
|
| 12 |
-
|
| 13 |
-
# Save uploaded files to temporary NIfTI files
|
| 14 |
-
flair_content = flair_file.file.read()
|
| 15 |
-
t1_content = t1_file.file.read()
|
| 16 |
-
t1ce_content = t1ce_file.file.read()
|
| 17 |
-
t2_content = t2_file.file.read()
|
| 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 |
return output_in_original_size
|
|
|
|
| 1 |
+
import torch
|
| 2 |
+
import tempfile
|
| 3 |
+
import numpy as np
|
| 4 |
+
import nibabel as nib
|
| 5 |
+
from fastapi import FastAPI, UploadFile, File
|
| 6 |
+
|
| 7 |
+
def preprocess_input(flair_file: UploadFile, t1_file: UploadFile, t1ce_file: UploadFile, t2_file: UploadFile):
|
| 8 |
+
with tempfile.NamedTemporaryFile(suffix=".nii.gz") as temp_flair, \
|
| 9 |
+
tempfile.NamedTemporaryFile(suffix=".nii.gz") as temp_t1, \
|
| 10 |
+
tempfile.NamedTemporaryFile(suffix=".nii.gz") as temp_t1ce, \
|
| 11 |
+
tempfile.NamedTemporaryFile(suffix=".nii.gz") as temp_t2:
|
| 12 |
+
|
| 13 |
+
# Save uploaded files to temporary NIfTI files
|
| 14 |
+
flair_content = flair_file.file.read()
|
| 15 |
+
t1_content = t1_file.file.read()
|
| 16 |
+
t1ce_content = t1ce_file.file.read()
|
| 17 |
+
t2_content = t2_file.file.read()
|
| 18 |
+
|
| 19 |
+
if flair_file.filename.endswith('.gz'):
|
| 20 |
+
with gzip.open(temp_flair.name, 'wb') as f:
|
| 21 |
+
f.write(flair_content)
|
| 22 |
+
else:
|
| 23 |
+
with open(temp_flair.name, 'wb') as f:
|
| 24 |
+
f.write(flair_content)
|
| 25 |
+
|
| 26 |
+
if t1_file.filename.endswith('.gz'):
|
| 27 |
+
with gzip.open(temp_t1.name, 'wb') as f:
|
| 28 |
+
f.write(t1_content)
|
| 29 |
+
else:
|
| 30 |
+
with open(temp_t1.name, 'wb') as f:
|
| 31 |
+
f.write(t1_content)
|
| 32 |
+
|
| 33 |
+
if t1ce_file.filename.endswith('.gz'):
|
| 34 |
+
with gzip.open(temp_t1ce.name, 'wb') as f:
|
| 35 |
+
f.write(t1ce_content)
|
| 36 |
+
else:
|
| 37 |
+
with open(temp_t1ce.name, 'wb') as f:
|
| 38 |
+
f.write(t1ce_content)
|
| 39 |
+
|
| 40 |
+
if t2_file.filename.endswith('.gz'):
|
| 41 |
+
with gzip.open(temp_t2.name, 'wb') as f:
|
| 42 |
+
f.write(t2_content)
|
| 43 |
+
else:
|
| 44 |
+
with open(temp_t2.name, 'wb') as f:
|
| 45 |
+
f.write(t2_content)
|
| 46 |
+
|
| 47 |
+
# Load and preprocess the NIfTI files
|
| 48 |
+
flair = nib.load(temp_flair.name).get_fdata()
|
| 49 |
+
t1 = nib.load(temp_t1.name).get_fdata()
|
| 50 |
+
t1ce = nib.load(temp_t1ce.name).get_fdata()
|
| 51 |
+
t2 = nib.load(temp_t2.name).get_fdata()
|
| 52 |
+
|
| 53 |
+
# Process the arrays
|
| 54 |
+
flair_cropped = flair[56:184, 56:184, 13:141]
|
| 55 |
+
t1_cropped = t1[56:184, 56:184, 13:141]
|
| 56 |
+
t1ce_cropped = t1ce[56:184, 56:184, 13:141]
|
| 57 |
+
t2_cropped = t2[56:184, 56:184, 13:141]
|
| 58 |
+
|
| 59 |
+
# Convert to PyTorch tensors
|
| 60 |
+
flair_tensor = torch.tensor(flair_cropped, dtype=torch.float32).unsqueeze(0)
|
| 61 |
+
t1_tensor = torch.tensor(t1_cropped, dtype=torch.float32).unsqueeze(0)
|
| 62 |
+
t1ce_tensor = torch.tensor(t1ce_cropped, dtype=torch.float32).unsqueeze(0)
|
| 63 |
+
t2_tensor = torch.tensor(t2_cropped, dtype=torch.float32).unsqueeze(0)
|
| 64 |
+
|
| 65 |
+
return flair_tensor, t1_tensor, t1ce_tensor, t2_tensor
|
| 66 |
+
|
| 67 |
+
|
| 68 |
+
def segment_wt(wt_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor):
|
| 69 |
+
wt_input = torch.cat((flair_tensor, t1_tensor, t1ce_tensor, t2_tensor), dim=0).unsqueeze(0)
|
| 70 |
+
with torch.no_grad():
|
| 71 |
+
wt_output = wt_model(wt_input)
|
| 72 |
+
return wt_output
|
| 73 |
+
|
| 74 |
+
|
| 75 |
+
def segment_tc(tc_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output):
|
| 76 |
+
tc_input = torch.cat((flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output.squeeze(0),
|
| 77 |
+
wt_output.squeeze(0), wt_output.squeeze(0), wt_output.squeeze(0)), dim=0).unsqueeze(0)
|
| 78 |
+
with torch.no_grad():
|
| 79 |
+
tc_output = tc_model(tc_input)
|
| 80 |
+
return tc_output
|
| 81 |
+
|
| 82 |
+
def segment_et(et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output, tc_output):
|
| 83 |
+
et_input = torch.cat((flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output.squeeze(0),
|
| 84 |
+
wt_output.squeeze(0), tc_output.squeeze(0), tc_output.squeeze(0)), dim=0).unsqueeze(0)
|
| 85 |
+
with torch.no_grad():
|
| 86 |
+
et_output = et_model(et_input)
|
| 87 |
+
return et_output
|
| 88 |
+
|
| 89 |
+
def segment_all(wt_model, tc_model, et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor):
|
| 90 |
+
wt_output = segment_wt(wt_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor)
|
| 91 |
+
tc_output = segment_tc(tc_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output)
|
| 92 |
+
et_output = segment_et(et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output, tc_output)
|
| 93 |
+
|
| 94 |
+
wt_label = torch.sigmoid(wt_output.squeeze(0, 1)) > 0.5
|
| 95 |
+
tc_label = torch.sigmoid(tc_output.squeeze(0, 1)) > 0.5
|
| 96 |
+
et_label = torch.sigmoid(et_output.squeeze(0, 1)) > 0.5
|
| 97 |
+
|
| 98 |
+
output = np.zeros((128, 128, 128))
|
| 99 |
+
output[(tc_label == 1) & (wt_label == 0)] = 2
|
| 100 |
+
output[(et_label == 1) & (tc_label == 0)] = 3
|
| 101 |
+
output[(et_label == 0) & (tc_label == 0)] = 1
|
| 102 |
+
|
| 103 |
+
output_in_original_size = np.zeros((240, 240, 155))
|
| 104 |
+
output_in_original_size[56:184, 56:184, 13:141] = output
|
| 105 |
return output_in_original_size
|