Spaces:
Runtime error
Runtime error
Update app.py as per #1
Browse files
app.py
CHANGED
@@ -2,11 +2,70 @@ import numpy as np
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import from_pretrained_keras
|
4 |
|
5 |
-
def
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
model = from_pretrained_keras('jalFaizy/3D_CNN')
|
12 |
|
@@ -18,7 +77,7 @@ iface = gr.Interface(
|
|
18 |
filepath,
|
19 |
text,
|
20 |
title='3D CNN for CT scans',
|
21 |
-
examples=['example_1_normal.
|
22 |
)
|
23 |
|
24 |
iface.launch()
|
|
|
2 |
import gradio as gr
|
3 |
from huggingface_hub import from_pretrained_keras
|
4 |
|
5 |
+
def read_nifti_file(filepath):
|
6 |
+
"""Read and load volume"""
|
7 |
+
# Read file
|
8 |
+
scan = nib.load(filepath)
|
9 |
+
# Get raw data
|
10 |
+
scan = scan.get_fdata()
|
11 |
+
return scan
|
12 |
+
|
13 |
+
def normalize(volume):
|
14 |
+
"""Normalize the volume"""
|
15 |
+
min = -1000
|
16 |
+
max = 400
|
17 |
+
volume[volume < min] = min
|
18 |
+
volume[volume > max] = max
|
19 |
+
volume = (volume - min) / (max - min)
|
20 |
+
volume = volume.astype("float32")
|
21 |
+
return volume
|
22 |
+
|
23 |
+
def resize_volume(img):
|
24 |
+
"""Resize across z-axis"""
|
25 |
+
# Set the desired depth
|
26 |
+
desired_depth = 64
|
27 |
+
desired_width = 128
|
28 |
+
desired_height = 128
|
29 |
+
# Get current depth
|
30 |
+
current_depth = img.shape[-1]
|
31 |
+
current_width = img.shape[0]
|
32 |
+
current_height = img.shape[1]
|
33 |
+
# Compute depth factor
|
34 |
+
depth = current_depth / desired_depth
|
35 |
+
width = current_width / desired_width
|
36 |
+
height = current_height / desired_height
|
37 |
+
depth_factor = 1 / depth
|
38 |
+
width_factor = 1 / width
|
39 |
+
height_factor = 1 / height
|
40 |
+
# Rotate
|
41 |
+
img = ndimage.rotate(img, 90, reshape=False)
|
42 |
+
# Resize across z-axis
|
43 |
+
img = ndimage.zoom(img, (width_factor, height_factor, depth_factor), order=1)
|
44 |
+
return img
|
45 |
+
|
46 |
+
def process_scan(path):
|
47 |
+
"""Read and resize volume"""
|
48 |
+
# Read scan
|
49 |
+
volume = read_nifti_file(path)
|
50 |
+
# Normalize
|
51 |
+
volume = normalize(volume)
|
52 |
+
# Resize width, height and depth
|
53 |
+
volume = resize_volume(volume)
|
54 |
+
return volume
|
55 |
+
|
56 |
+
def infer(filename):
|
57 |
+
vol = process_scan(filename.name)
|
58 |
+
vol = np.expand_dims(vol, axis=0)
|
59 |
+
prediction = model.predict(vol)[0]
|
60 |
+
|
61 |
+
scores = [1 - prediction[0], prediction[0]]
|
62 |
+
|
63 |
+
class_names = ["normal", "abnormal"]
|
64 |
+
result = []
|
65 |
+
for score, name in zip(scores, class_names):
|
66 |
+
result = result + [f"This model is {(100 * score):.2f} percent confident that CT scan is {name}"]
|
67 |
+
|
68 |
+
return result
|
69 |
|
70 |
model = from_pretrained_keras('jalFaizy/3D_CNN')
|
71 |
|
|
|
77 |
filepath,
|
78 |
text,
|
79 |
title='3D CNN for CT scans',
|
80 |
+
examples=['example_1_normal.nii.gz']
|
81 |
)
|
82 |
|
83 |
iface.launch()
|