LouayMagdy commited on
Commit
28d75ea
·
verified ·
1 Parent(s): 7686256

Update inference.py

Browse files
Files changed (1) hide show
  1. 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
- temp_flair.write(flair_content)
20
- temp_t1.write(t1_content)
21
- temp_t1ce.write(t1ce_content)
22
- temp_t2.write(t2_content)
23
-
24
- # Load and preprocess the NIfTI files
25
- flair = nib.load(temp_flair.name).get_fdata()
26
- t1 = nib.load(temp_t1.name).get_fdata()
27
- t1ce = nib.load(temp_t1ce.name).get_fdata()
28
- t2 = nib.load(temp_t2.name).get_fdata()
29
-
30
- flair_tensor = torch.tensor(flair[56:184, 56:184, 13:141], dtype=torch.float32).unsqueeze(0)
31
- t1_tensor = torch.tensor(t1[56:184, 56:184, 13:141], dtype=torch.float32).unsqueeze(0)
32
- t1ce_tensor = torch.tensor(t1ce[56:184, 56:184, 13:141], dtype=torch.float32).unsqueeze(0)
33
- t2_tensor = torch.tensor(t2[56:184, 56:184, 13:141], dtype=torch.float32).unsqueeze(0)
34
-
35
- return flair_tensor, t1_tensor, t1ce_tensor, t2_tensor
36
-
37
-
38
- def segment_wt(wt_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor):
39
- wt_input = torch.cat((flair_tensor, t1_tensor, t1ce_tensor, t2_tensor), dim=0).unsqueeze(0)
40
- with torch.no_grad():
41
- wt_output = wt_model(wt_input)
42
- return wt_output
43
-
44
-
45
- def segment_tc(tc_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output):
46
- tc_input = torch.cat((flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output.squeeze(0),
47
- wt_output.squeeze(0), wt_output.squeeze(0), wt_output.squeeze(0)), dim=0).unsqueeze(0)
48
- with torch.no_grad():
49
- tc_output = tc_model(tc_input)
50
- return tc_output
51
-
52
- def segment_et(et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output, tc_output):
53
- et_input = torch.cat((flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output.squeeze(0),
54
- wt_output.squeeze(0), tc_output.squeeze(0), tc_output.squeeze(0)), dim=0).unsqueeze(0)
55
- with torch.no_grad():
56
- et_output = et_model(et_input)
57
- return et_output
58
-
59
- def segment_all(wt_model, tc_model, et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor):
60
- wt_output = segment_wt(wt_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor)
61
- tc_output = segment_tc(tc_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output)
62
- et_output = segment_et(et_model, flair_tensor, t1_tensor, t1ce_tensor, t2_tensor, wt_output, tc_output)
63
-
64
- wt_label = torch.sigmoid(wt_output.squeeze(0, 1)) > 0.5
65
- tc_label = torch.sigmoid(tc_output.squeeze(0, 1)) > 0.5
66
- et_label = torch.sigmoid(et_output.squeeze(0, 1)) > 0.5
67
-
68
- output = np.zeros((128, 128, 128))
69
- output[(tc_label == 1) & (wt_label == 0)] = 2
70
- output[(et_label == 1) & (tc_label == 0)] = 3
71
- output[(et_label == 0) & (tc_label == 0)] = 1
72
-
73
- output_in_original_size = np.zeros((240, 240, 155))
74
- output_in_original_size[56:184, 56:184, 13:141] = output
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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