Spaces:
Sleeping
Sleeping
tumor seg and chest xray added
Browse files- .gitattributes +2 -0
- app.py +53 -13
- predict.py +11 -0
- segment_model/V2/fingerprint.pb +3 -0
- segment_model/V2/info.txt +2 -0
- segment_model/V2/keras_metadata.pb +3 -0
- segment_model/V2/saved_model.pb +3 -0
- segment_model/V2/variables/variables.data-00000-of-00001 +3 -0
- segment_model/V2/variables/variables.index +0 -0
- tumorseg.py +73 -0
- xray_resnet_model/fingerprint.pb +3 -0
- xray_resnet_model/info.txt +2 -0
- xray_resnet_model/keras_metadata.pb +3 -0
- xray_resnet_model/saved_model.pb +3 -0
- xray_resnet_model/variables/variables.data-00000-of-00001 +3 -0
- xray_resnet_model/variables/variables.index +0 -0
.gitattributes
CHANGED
@@ -34,3 +34,5 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
37 |
+
segment_model/V2/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
38 |
+
xray_resnet_model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
app.py
CHANGED
@@ -1,20 +1,60 @@
|
|
1 |
import gradio as gr
|
2 |
-
from predict import makepredictions
|
|
|
3 |
|
4 |
with gr.Blocks() as demo:
|
5 |
-
gr.
|
6 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
13 |
gr.Markdown(
|
14 |
"""
|
15 |
-
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
18 |
""")
|
19 |
-
|
20 |
demo.launch()
|
|
|
1 |
import gradio as gr
|
2 |
+
from predict import makepredictions, xray_predict
|
3 |
+
from tumorseg import make_segmentation
|
4 |
|
5 |
with gr.Blocks() as demo:
|
6 |
+
with gr.Tab('Brain Tumor | Tumor Segmentation'):
|
7 |
+
gr.Markdown("""
|
8 |
+
<div align='center'>Tumor segmentations
|
9 |
+
</div>
|
10 |
+
<div align='center'>
|
11 |
+
> You could segment you mri image. I hope it will find you well
|
12 |
+
</div>
|
13 |
+
""")
|
14 |
+
with gr.Row():
|
15 |
+
image_input = gr.Image(label='Tumor Image', type='filepath', height=480, interactive=True)
|
16 |
+
output_image = gr.Image(label='label Segmented Image', height=480, interactive=False)
|
17 |
+
image_button = gr.Button("Segment image")
|
18 |
+
image_button.click(make_segmentation, inputs=image_input, outputs=output_image)
|
19 |
+
|
20 |
+
with gr.Tab('Brain Tumor | Tumor Classification'):
|
21 |
+
gr.Markdown("""
|
22 |
+
<div align='center'>
|
23 |
+
Brain Tumor Image classification
|
24 |
+
</div>
|
25 |
+
<div align='center'>
|
26 |
+
>We detect 4 Types of Tumor with 99.12% Accuracy:
|
27 |
+
>Glioma | Meningioma | No Tumor | Pituitary
|
28 |
+
""")
|
29 |
+
image_input = gr.Image(label='Tumor Image', type='filepath', height=480)
|
30 |
+
with gr.Row():
|
31 |
+
image_button = gr.Button("Predict")
|
32 |
+
y_pred = gr.Textbox(label="Tumor Type:")
|
33 |
+
image_button.click(makepredictions, inputs=image_input, outputs=y_pred)
|
34 |
+
|
35 |
+
with gr.Tab('Chest X-ray | COVID19, NORMAL, PNEUMONIA,TURBERCULOSIS'):
|
36 |
+
gr.Markdown("""
|
37 |
+
<div align='center'>Chest X-ray classifications
|
38 |
+
</div>
|
39 |
+
<div align='center'>
|
40 |
+
> We COVID19, NORMAL, PNEUMONIA,TURBERCULOSIS from your chest x-ray image
|
41 |
+
</div>
|
42 |
+
""")
|
43 |
+
|
44 |
+
image_input = gr.Image(label='Chest X-ray', type='filepath', height=480)
|
45 |
+
with gr.Row():
|
46 |
+
image_button = gr.Button("Predict")
|
47 |
+
y_pred = gr.Textbox(label="Disease :")
|
48 |
+
image_button.click(xray_predict, inputs=image_input, outputs=[y_pred])
|
49 |
+
|
50 |
gr.Markdown(
|
51 |
"""
|
52 |
+
<div align='center'>
|
53 |
+
These model is trained on the Kaggle dataset:
|
54 |
+
</div>
|
55 |
+
<div align='center'>
|
56 |
+
> Only for the educational purpose
|
57 |
+
> Play with that and Have fun.
|
58 |
+
</div>
|
59 |
""")
|
|
|
60 |
demo.launch()
|
predict.py
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
import tensorflow as tf
|
2 |
import tf_keras
|
3 |
model_mri = tf_keras.models.load_model('model')
|
|
|
4 |
|
5 |
def load_image_with_path(path):
|
6 |
img = tf.io.read_file(path)
|
@@ -24,3 +25,13 @@ def makepredictions(path):
|
|
24 |
a = "Result : Pituitary Tumor"
|
25 |
return a
|
26 |
# {'glioma': 0, 'meningioma': 1, 'notumor': 2, 'pituitary': 3}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import tensorflow as tf
|
2 |
import tf_keras
|
3 |
model_mri = tf_keras.models.load_model('model')
|
4 |
+
model_xray = tf_keras.models.load_model('xray_resnet_model')
|
5 |
|
6 |
def load_image_with_path(path):
|
7 |
img = tf.io.read_file(path)
|
|
|
25 |
a = "Result : Pituitary Tumor"
|
26 |
return a
|
27 |
# {'glioma': 0, 'meningioma': 1, 'notumor': 2, 'pituitary': 3}
|
28 |
+
|
29 |
+
def xray_predict(path):
|
30 |
+
img = load_image_with_path(path)
|
31 |
+
predications = model_xray.predict(tf.expand_dims(img, axis=0))
|
32 |
+
a = int(tf.argmax(tf.squeeze(predications)))
|
33 |
+
xray_classes = ['COVID19','NORMAL', 'PNEUMONIA',' TURBERCULOSIS']
|
34 |
+
a = xray_classes[a]
|
35 |
+
|
36 |
+
return a
|
37 |
+
# {'COVID19': 0, 'NORMAL': 1, 'PNEUMONIA': 2, 'TURBERCULOSIS': 3}
|
segment_model/V2/fingerprint.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0e6526424daeeaa19ccabb7aceafada7ff465e4b877fe0babed162fd3dda1693
|
3 |
+
size 56
|
segment_model/V2/info.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
binary accuracy:99%
|
2 |
+
best model till yet : 20-03-2024
|
segment_model/V2/keras_metadata.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:65a8d82887d611745ad6fbeecae6944a05f6996faeb3cc6997e527ace88e84e7
|
3 |
+
size 125191
|
segment_model/V2/saved_model.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e3b802b0e98405daeb5e53dc69cd72746ebbf8aa05d663fbcf6335916a818cf1
|
3 |
+
size 1158923
|
segment_model/V2/variables/variables.data-00000-of-00001
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:dbb9c2edd0e2cef3a589fb44c45dc9bc923acbbb7b5239c2832b1b79a4bcdb86
|
3 |
+
size 372540402
|
segment_model/V2/variables/variables.index
ADDED
Binary file (13.6 kB). View file
|
|
tumorseg.py
ADDED
@@ -0,0 +1,73 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import tensorflow as tf
|
3 |
+
from tensorflow.keras import backend as K
|
4 |
+
import tf_keras
|
5 |
+
import numpy as np
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import matplotlib
|
8 |
+
matplotlib.use('agg')
|
9 |
+
def dice_coefficients(y_true, y_pred, smooth=100):
|
10 |
+
y_true_flatten = K.flatten(y_true)
|
11 |
+
y_pred_flatten = K.flatten(y_pred)
|
12 |
+
|
13 |
+
intersection = K.sum(y_true_flatten * y_pred_flatten)
|
14 |
+
union = K.sum(y_true_flatten) + K.sum(y_pred_flatten)
|
15 |
+
return (2 * intersection + smooth) / (union + smooth)
|
16 |
+
|
17 |
+
|
18 |
+
def dice_coefficients_loss(y_true, y_pred, smooth=100):
|
19 |
+
return 1.0 - dice_coefficients(y_true, y_pred, smooth)
|
20 |
+
|
21 |
+
|
22 |
+
def iou(y_true, y_pred, smooth=100):
|
23 |
+
intersection = K.sum(y_true * y_pred)
|
24 |
+
sum = K.sum(y_true + y_pred)
|
25 |
+
iou = (intersection + smooth) / (sum - intersection + smooth)
|
26 |
+
return iou
|
27 |
+
|
28 |
+
|
29 |
+
def jaccard_distance(y_true, y_pred):
|
30 |
+
y_true_flatten = K.flatten(y_true)
|
31 |
+
y_pred_flatten = K.flatten(y_pred)
|
32 |
+
return -iou(y_true_flatten, y_pred_flatten)
|
33 |
+
|
34 |
+
segmodel = tf_keras.models.load_model("segment_model/V2", custom_objects={'dice_coefficients_loss': dice_coefficients_loss, 'iou': iou, 'dice_coefficients': dice_coefficients } )
|
35 |
+
|
36 |
+
def load_image_for_pred(image_path):
|
37 |
+
img = tf.keras.utils.load_img(
|
38 |
+
image_path,
|
39 |
+
color_mode='rgb',
|
40 |
+
target_size=(256, 256),
|
41 |
+
interpolation='nearest',
|
42 |
+
keep_aspect_ratio=False
|
43 |
+
)
|
44 |
+
img = tf.keras.utils.img_to_array(img) / 255
|
45 |
+
return np.array([img])
|
46 |
+
|
47 |
+
def make_segmentation(image_path):
|
48 |
+
img = load_image_for_pred(image_path)
|
49 |
+
predicted_img = segmodel.predict(img)
|
50 |
+
|
51 |
+
plt.figure(figsize=(5, 3))
|
52 |
+
plt.subplot(1, 3, 1)
|
53 |
+
plt.imshow(np.squeeze(img))
|
54 |
+
plt.title('Original Image')
|
55 |
+
plt.axis(False)
|
56 |
+
|
57 |
+
|
58 |
+
plt.subplot(1, 3, 2)
|
59 |
+
plt.imshow(np.squeeze(predicted_img) > 0.5)
|
60 |
+
plt.title('Prediction')
|
61 |
+
plt.axis(False)
|
62 |
+
|
63 |
+
plt.subplot(1, 4, 4)
|
64 |
+
plt.imshow(np.squeeze(img))
|
65 |
+
plt.imshow(np.squeeze(predicted_img) > 0.5, cmap='gray', alpha=0.5)
|
66 |
+
plt.title('Image w/h Mask')
|
67 |
+
plt.axis(False)
|
68 |
+
save_file_name = os.path.splitext(image_path)[0] + '_segmented.png'
|
69 |
+
plt.savefig(save_file_name)
|
70 |
+
|
71 |
+
return save_file_name
|
72 |
+
|
73 |
+
|
xray_resnet_model/fingerprint.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6ae9236c9c9287ca25c24110c97d624e0b57bb8961c8ef20f435c0c00cd6124f
|
3 |
+
size 57
|
xray_resnet_model/info.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
accuracy:93%
|
2 |
+
best model : 20-03-2024
|
xray_resnet_model/keras_metadata.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:3b251b17e58a01b8d485937055ac98409b680bcc5c126f977da35807827838ba
|
3 |
+
size 605636
|
xray_resnet_model/saved_model.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:51fec9fb52f34de9872bac4e8c20db741ed1b286b8cc4329b483690a1f82243d
|
3 |
+
size 3067953
|
xray_resnet_model/variables/variables.data-00000-of-00001
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a1fc2f8c15c651684e0d95d2e94d1367500afb712778a2dc9d6b6bea4d8eb289
|
3 |
+
size 216397949
|
xray_resnet_model/variables/variables.index
ADDED
Binary file (20.4 kB). View file
|
|