Spaces:
Runtime error
Runtime error
Duzduran
commited on
Commit
·
c185beb
1
Parent(s):
12b4877
initial release
Browse files- .DS_Store +0 -0
- .gitattributes +11 -0
- .idea/.gitignore +8 -0
- .idea/Brain-Tumor-Segmentation.iml +8 -0
- .idea/inspectionProfiles/Project_Default.xml +27 -0
- .idea/inspectionProfiles/profiles_settings.xml +6 -0
- .idea/modules.xml +8 -0
- .idea/vcs.xml +6 -0
- app.py +148 -0
- environment.yml +25 -0
- examples/.DS_Store +0 -0
- examples/ex_1/.DS_Store +0 -0
- examples/ex_1/BraTS20_Training_001_flair.nii +3 -0
- examples/ex_1/BraTS20_Training_001_seg.nii +3 -0
- examples/ex_1/BraTS20_Training_001_t1.nii +3 -0
- examples/ex_1/BraTS20_Training_001_t1ce.nii +3 -0
- examples/ex_1/BraTS20_Training_001_t2.nii +3 -0
- examples/ex_2/.DS_Store +0 -0
- examples/ex_2/BraTS20_Training_002_flair.nii +3 -0
- examples/ex_2/BraTS20_Training_002_seg.nii +3 -0
- examples/ex_2/BraTS20_Training_002_t1.nii +3 -0
- examples/ex_2/BraTS20_Training_002_t1ce.nii +3 -0
- examples/ex_2/BraTS20_Training_002_t2.nii +3 -0
- loss.py +57 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
.gitattributes
CHANGED
|
@@ -33,3 +33,14 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip 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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 33 |
*.zip 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 |
+
*.nii filter=lfs diff=lfs merge=lfs -text
|
| 37 |
+
examples/ex_1/BraTS20_Training_001_flair.nii filter=lfs diff=lfs merge=lfs -text
|
| 38 |
+
examples/ex_1/BraTS20_Training_001_seg.nii filter=lfs diff=lfs merge=lfs -text
|
| 39 |
+
examples/ex_1/BraTS20_Training_001_t1.nii filter=lfs diff=lfs merge=lfs -text
|
| 40 |
+
examples/ex_1/BraTS20_Training_001_t1ce.nii filter=lfs diff=lfs merge=lfs -text
|
| 41 |
+
examples/ex_1/BraTS20_Training_001_t2.nii filter=lfs diff=lfs merge=lfs -text
|
| 42 |
+
examples/ex_2/BraTS20_Training_002_flair.nii filter=lfs diff=lfs merge=lfs -text
|
| 43 |
+
examples/ex_2/BraTS20_Training_002_t2.nii filter=lfs diff=lfs merge=lfs -text
|
| 44 |
+
examples/ex_2/BraTS20_Training_002_t1ce.nii filter=lfs diff=lfs merge=lfs -text
|
| 45 |
+
examples/ex_2/BraTS20_Training_002_t1.nii filter=lfs diff=lfs merge=lfs -text
|
| 46 |
+
examples/ex_2/BraTS20_Training_002_seg.nii filter=lfs diff=lfs merge=lfs -text
|
.idea/.gitignore
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Default ignored files
|
| 2 |
+
/shelf/
|
| 3 |
+
/workspace.xml
|
| 4 |
+
# Editor-based HTTP Client requests
|
| 5 |
+
/httpRequests/
|
| 6 |
+
# Datasource local storage ignored files
|
| 7 |
+
/dataSources/
|
| 8 |
+
/dataSources.local.xml
|
.idea/Brain-Tumor-Segmentation.iml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<module type="PYTHON_MODULE" version="4">
|
| 3 |
+
<component name="NewModuleRootManager">
|
| 4 |
+
<content url="file://$MODULE_DIR$" />
|
| 5 |
+
<orderEntry type="inheritedJdk" />
|
| 6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
| 7 |
+
</component>
|
| 8 |
+
</module>
|
.idea/inspectionProfiles/Project_Default.xml
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<profile version="1.0">
|
| 3 |
+
<option name="myName" value="Project Default" />
|
| 4 |
+
<inspection_tool class="DuplicatedCode" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
| 5 |
+
<Languages>
|
| 6 |
+
<language minSize="89" name="Python" />
|
| 7 |
+
</Languages>
|
| 8 |
+
</inspection_tool>
|
| 9 |
+
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
|
| 10 |
+
<inspection_tool class="PyPep8NamingInspection" enabled="true" level="WEAK WARNING" enabled_by_default="true">
|
| 11 |
+
<option name="ignoredErrors">
|
| 12 |
+
<list>
|
| 13 |
+
<option value="N806" />
|
| 14 |
+
</list>
|
| 15 |
+
</option>
|
| 16 |
+
</inspection_tool>
|
| 17 |
+
<inspection_tool class="PyUnresolvedReferencesInspection" enabled="true" level="WARNING" enabled_by_default="true">
|
| 18 |
+
<option name="ignoredIdentifiers">
|
| 19 |
+
<list>
|
| 20 |
+
<option value="map.__getitem__" />
|
| 21 |
+
<option value="cv2.drawMarker" />
|
| 22 |
+
<option value="cv2.putText" />
|
| 23 |
+
</list>
|
| 24 |
+
</option>
|
| 25 |
+
</inspection_tool>
|
| 26 |
+
</profile>
|
| 27 |
+
</component>
|
.idea/inspectionProfiles/profiles_settings.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<component name="InspectionProjectProfileManager">
|
| 2 |
+
<settings>
|
| 3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
| 4 |
+
<version value="1.0" />
|
| 5 |
+
</settings>
|
| 6 |
+
</component>
|
.idea/modules.xml
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="ProjectModuleManager">
|
| 4 |
+
<modules>
|
| 5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/Brain-Tumor-Segmentation.iml" filepath="$PROJECT_DIR$/.idea/Brain-Tumor-Segmentation.iml" />
|
| 6 |
+
</modules>
|
| 7 |
+
</component>
|
| 8 |
+
</project>
|
.idea/vcs.xml
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
| 2 |
+
<project version="4">
|
| 3 |
+
<component name="VcsDirectoryMappings">
|
| 4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
| 5 |
+
</component>
|
| 6 |
+
</project>
|
app.py
ADDED
|
@@ -0,0 +1,148 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import numpy as np
|
| 2 |
+
import cv2
|
| 3 |
+
import nibabel as nib
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import io
|
| 6 |
+
import matplotlib.pyplot as plt
|
| 7 |
+
import gradio as gr
|
| 8 |
+
|
| 9 |
+
from huggingface_hub import from_pretrained_keras
|
| 10 |
+
|
| 11 |
+
model = from_pretrained_keras("duzduran/NeuroNest3D")
|
| 12 |
+
# Constants
|
| 13 |
+
IMG_SIZE = 128
|
| 14 |
+
VOLUME_SLICES = 100
|
| 15 |
+
VOLUME_START_AT = 22
|
| 16 |
+
SEGMENT_CLASSES = ['NOT tumor', 'ENHANCING', 'CORE', 'WHOLE']
|
| 17 |
+
|
| 18 |
+
|
| 19 |
+
def predictByPath(flair, ce):
|
| 20 |
+
X = np.empty((VOLUME_SLICES, IMG_SIZE, IMG_SIZE, 2))
|
| 21 |
+
|
| 22 |
+
for j in range(VOLUME_SLICES):
|
| 23 |
+
X[j, :, :, 0] = cv2.resize(flair[:, :, j + VOLUME_START_AT], (IMG_SIZE, IMG_SIZE))
|
| 24 |
+
X[j, :, :, 1] = cv2.resize(ce[:, :, j + VOLUME_START_AT], (IMG_SIZE, IMG_SIZE))
|
| 25 |
+
|
| 26 |
+
# Normalize and make predictions
|
| 27 |
+
X_normalized = X / np.max(X)
|
| 28 |
+
return model.predict(X_normalized, verbose=1)
|
| 29 |
+
|
| 30 |
+
|
| 31 |
+
def create_subplot_image(origImage, gt, predictions, slice_index, start_at, img_size):
|
| 32 |
+
plt.figure(figsize=(18, 10))
|
| 33 |
+
f, axarr = plt.subplots(1, 6, figsize=(18, 10))
|
| 34 |
+
|
| 35 |
+
for i in range(6):
|
| 36 |
+
axarr[i].imshow(cv2.resize(origImage[:, :, slice_index + start_at], (img_size, img_size)), cmap="gray",
|
| 37 |
+
interpolation='none')
|
| 38 |
+
|
| 39 |
+
# Original image flair
|
| 40 |
+
axarr[0].title.set_text('Original image flair')
|
| 41 |
+
|
| 42 |
+
# Ground truth
|
| 43 |
+
curr_gt = cv2.resize(gt[:, :, slice_index + start_at], (img_size, img_size), interpolation=cv2.INTER_NEAREST)
|
| 44 |
+
axarr[1].imshow(curr_gt, cmap="Reds", interpolation='none', alpha=0.3)
|
| 45 |
+
axarr[1].title.set_text('Ground truth')
|
| 46 |
+
|
| 47 |
+
# All classes
|
| 48 |
+
axarr[2].imshow(predictions[slice_index, :, :, 1:4], cmap="Reds", interpolation='none', alpha=0.3)
|
| 49 |
+
axarr[2].title.set_text('All classes')
|
| 50 |
+
|
| 51 |
+
SEGMENT_CLASSES
|
| 52 |
+
# Class-specific predictions
|
| 53 |
+
for i in range(1, 4): # Adjusted to loop over the available prediction classes
|
| 54 |
+
axarr[i + 2].imshow(predictions[slice_index, :, :, i], cmap="OrRd", interpolation='none', alpha=0.3)
|
| 55 |
+
axarr[i + 2].title.set_text(f'{SEGMENT_CLASSES[i]} predicted')
|
| 56 |
+
|
| 57 |
+
# Convert plot to image
|
| 58 |
+
buf = io.BytesIO()
|
| 59 |
+
plt.savefig(buf, format='png')
|
| 60 |
+
plt.close(f)
|
| 61 |
+
buf.seek(0)
|
| 62 |
+
img = Image.open(buf)
|
| 63 |
+
return img
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
examples = {
|
| 67 |
+
"Example 1": {"flair": "examples/ex_1/BraTS20_Training_001_flair.nii",
|
| 68 |
+
"t1ce": "examples/ex_1/BraTS20_Training_001_t1ce.nii",
|
| 69 |
+
"seg": "examples/ex_1/BraTS20_Training_001_seg.nii"},
|
| 70 |
+
|
| 71 |
+
"Example 2": {"flair": "examples/ex_2/BraTS20_Training_002_flair.nii",
|
| 72 |
+
"t1ce": "examples/ex_2/BraTS20_Training_002_t1ce.nii",
|
| 73 |
+
"seg": "examples/ex_2/BraTS20_Training_002_seg.nii"},
|
| 74 |
+
}
|
| 75 |
+
|
| 76 |
+
|
| 77 |
+
def automatic_process(example_key):
|
| 78 |
+
paths = examples[example_key]
|
| 79 |
+
print(paths["flair"])
|
| 80 |
+
|
| 81 |
+
flair = nib.load(paths["flair"]).get_fdata()
|
| 82 |
+
t1ce = nib.load(paths["t1ce"]).get_fdata()
|
| 83 |
+
seg = nib.load(paths["seg"]).get_fdata()
|
| 84 |
+
|
| 85 |
+
# Default slice index
|
| 86 |
+
slice_index = 50
|
| 87 |
+
|
| 88 |
+
return process_and_display_direct(flair, t1ce, seg, slice_index)
|
| 89 |
+
|
| 90 |
+
|
| 91 |
+
def process_and_display_direct(flair_data, t1ce_data, seg_data, slice_index):
|
| 92 |
+
flair = np.array(flair_data)
|
| 93 |
+
t1ce = np.array(t1ce_data)
|
| 94 |
+
seg = np.array(seg_data)
|
| 95 |
+
|
| 96 |
+
p = predictByPath(flair, t1ce)
|
| 97 |
+
|
| 98 |
+
# Create the subplot image
|
| 99 |
+
subplot_img = create_subplot_image(flair, seg, p, slice_index, VOLUME_START_AT, IMG_SIZE)
|
| 100 |
+
|
| 101 |
+
return subplot_img
|
| 102 |
+
|
| 103 |
+
|
| 104 |
+
def process_and_display(flair_file, t1ce_file, seg_file, slice_index):
|
| 105 |
+
if not flair_file or not t1ce_file or not seg_file:
|
| 106 |
+
return None # Ensure all files are uploaded
|
| 107 |
+
|
| 108 |
+
flair = nib.load(flair_file.name).get_fdata()
|
| 109 |
+
t1ce = nib.load(t1ce_file.name).get_fdata()
|
| 110 |
+
gt = nib.load(seg_file.name).get_fdata()
|
| 111 |
+
|
| 112 |
+
p = predictByPath(flair, t1ce)
|
| 113 |
+
|
| 114 |
+
# Create the subplot image
|
| 115 |
+
subplot_img = create_subplot_image(flair, gt, p, slice_index, VOLUME_START_AT, IMG_SIZE)
|
| 116 |
+
|
| 117 |
+
return subplot_img
|
| 118 |
+
|
| 119 |
+
|
| 120 |
+
# Gradio Interface
|
| 121 |
+
with gr.Blocks() as demo:
|
| 122 |
+
with gr.Row():
|
| 123 |
+
flair_input = gr.File(label="Upload Flair NIfTI File")
|
| 124 |
+
t1ce_input = gr.File(label="Upload T1ce NIfTI File")
|
| 125 |
+
seg_input = gr.File(label="Upload Seg NIfTI File")
|
| 126 |
+
slice_input = gr.Slider(minimum=0, maximum=VOLUME_SLICES - 1, label="Slice Index")
|
| 127 |
+
#eval_class_input = gr.Dropdown(choices=list(range(len(SEGMENT_CLASSES))), label="Select Class")
|
| 128 |
+
submit_button = gr.Button("Submit")
|
| 129 |
+
|
| 130 |
+
with gr.Row():
|
| 131 |
+
example_selector = gr.Dropdown(list(examples.keys()), label="Select Example")
|
| 132 |
+
auto_button = gr.Button("Load Example")
|
| 133 |
+
|
| 134 |
+
output_image = gr.Image(label="Visualization")
|
| 135 |
+
|
| 136 |
+
submit_button.click(
|
| 137 |
+
process_and_display,
|
| 138 |
+
inputs=[flair_input, t1ce_input, seg_input, slice_input],
|
| 139 |
+
outputs=output_image
|
| 140 |
+
)
|
| 141 |
+
|
| 142 |
+
auto_button.click(
|
| 143 |
+
automatic_process,
|
| 144 |
+
inputs=[example_selector],
|
| 145 |
+
outputs=output_image
|
| 146 |
+
)
|
| 147 |
+
|
| 148 |
+
demo.launch()
|
environment.yml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
name: my_ml_env
|
| 2 |
+
channels:
|
| 3 |
+
- conda-forge
|
| 4 |
+
- defaults
|
| 5 |
+
dependencies:
|
| 6 |
+
- python=3.8
|
| 7 |
+
- pip
|
| 8 |
+
- gradio
|
| 9 |
+
- numpy
|
| 10 |
+
- pandas
|
| 11 |
+
- seaborn
|
| 12 |
+
- matplotlib
|
| 13 |
+
- scikit-image
|
| 14 |
+
- pillow
|
| 15 |
+
- tensorflow
|
| 16 |
+
- keras
|
| 17 |
+
- scikit-learn
|
| 18 |
+
- opencv
|
| 19 |
+
- nibabel
|
| 20 |
+
- nilearn
|
| 21 |
+
- tensorflow-estimator
|
| 22 |
+
- h5py
|
| 23 |
+
- pip:
|
| 24 |
+
- git+https://github.com/miykael/gif_your_nifti
|
| 25 |
+
|
examples/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
examples/ex_1/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
examples/ex_1/BraTS20_Training_001_flair.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:1671dfab75fbaaede29989011cabcef12b7825c72a315f293441b698a8e8d39c
|
| 3 |
+
size 17858880
|
examples/ex_1/BraTS20_Training_001_seg.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:56f39a503ebc4df200a5b9872b1ace83d92fa775d55b6ce301f12d2f63507180
|
| 3 |
+
size 8930976
|
examples/ex_1/BraTS20_Training_001_t1.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:6440b711f433a7071379088fc61ad9e0ff01ae5f551a7e5f006135c5adcf0f83
|
| 3 |
+
size 17858880
|
examples/ex_1/BraTS20_Training_001_t1ce.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:ed3732f26987226748147562cc64e58958564a6a7e47611b726773fd4e057b99
|
| 3 |
+
size 17858880
|
examples/ex_1/BraTS20_Training_001_t2.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:0918a2aed1e8aac1a802bf899940f9032b0e61bae70b35c9fa93d35e7e27c569
|
| 3 |
+
size 17858880
|
examples/ex_2/.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
examples/ex_2/BraTS20_Training_002_flair.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f51b5715db978afb246dc00b9e2950bcdb7a48ff0933f0a1b8f4ebd0d08c8613
|
| 3 |
+
size 17858896
|
examples/ex_2/BraTS20_Training_002_seg.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:5e5476e303ce247b623ea9fb25fc08833831560c71bc9bc5b93e637f0482fdb5
|
| 3 |
+
size 8930976
|
examples/ex_2/BraTS20_Training_002_t1.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e0cc02bda2df29e8100459fe730b9cd5bf3350ce5d79a53ea17c39d8b9378c50
|
| 3 |
+
size 17858880
|
examples/ex_2/BraTS20_Training_002_t1ce.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:cd9402472cbc67d9c0f65102fc198c23649c509deceb418fc2c69cbd91ddbd78
|
| 3 |
+
size 17858880
|
examples/ex_2/BraTS20_Training_002_t2.nii
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:741f3823e914e21efeff9d1795e84bef7ee3acf701d50f758b32e658ab094cda
|
| 3 |
+
size 17858880
|
loss.py
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import keras.backend as K
|
| 2 |
+
|
| 3 |
+
# dice loss as defined above for 4 classes
|
| 4 |
+
def dice_coef(y_true, y_pred, smooth=1.0):
|
| 5 |
+
class_num = 4
|
| 6 |
+
for i in range(class_num):
|
| 7 |
+
y_true_f = K.flatten(y_true[:,:,:,i])
|
| 8 |
+
y_pred_f = K.flatten(y_pred[:,:,:,i])
|
| 9 |
+
intersection = K.sum(y_true_f * y_pred_f)
|
| 10 |
+
loss = ((2. * intersection + smooth) / (K.sum(y_true_f) + K.sum(y_pred_f) + smooth))
|
| 11 |
+
# K.print_tensor(loss, message='loss value for class {} : '.format(SEGMENT_CLASSES[i]))
|
| 12 |
+
if i == 0:
|
| 13 |
+
total_loss = loss
|
| 14 |
+
else:
|
| 15 |
+
total_loss = total_loss + loss
|
| 16 |
+
total_loss = total_loss / class_num
|
| 17 |
+
# K.print_tensor(total_loss, message=' total dice coef: ')
|
| 18 |
+
return total_loss
|
| 19 |
+
|
| 20 |
+
|
| 21 |
+
|
| 22 |
+
# define per class evaluation of dice coef
|
| 23 |
+
# inspired by https://github.com/keras-team/keras/issues/9395
|
| 24 |
+
def dice_coef_necrotic(y_true, y_pred, epsilon=1e-6):
|
| 25 |
+
intersection = K.sum(K.abs(y_true[:,:,:,1] * y_pred[:,:,:,1]))
|
| 26 |
+
return (2. * intersection) / (K.sum(K.square(y_true[:,:,:,1])) + K.sum(K.square(y_pred[:,:,:,1])) + epsilon)
|
| 27 |
+
|
| 28 |
+
def dice_coef_edema(y_true, y_pred, epsilon=1e-6):
|
| 29 |
+
intersection = K.sum(K.abs(y_true[:,:,:,2] * y_pred[:,:,:,2]))
|
| 30 |
+
return (2. * intersection) / (K.sum(K.square(y_true[:,:,:,2])) + K.sum(K.square(y_pred[:,:,:,2])) + epsilon)
|
| 31 |
+
|
| 32 |
+
def dice_coef_enhancing(y_true, y_pred, epsilon=1e-6):
|
| 33 |
+
intersection = K.sum(K.abs(y_true[:,:,:,3] * y_pred[:,:,:,3]))
|
| 34 |
+
return (2. * intersection) / (K.sum(K.square(y_true[:,:,:,3])) + K.sum(K.square(y_pred[:,:,:,3])) + epsilon)
|
| 35 |
+
|
| 36 |
+
|
| 37 |
+
|
| 38 |
+
# Computing Precision
|
| 39 |
+
def precision(y_true, y_pred):
|
| 40 |
+
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
| 41 |
+
predicted_positives = K.sum(K.round(K.clip(y_pred, 0, 1)))
|
| 42 |
+
precision = true_positives / (predicted_positives + K.epsilon())
|
| 43 |
+
return precision
|
| 44 |
+
|
| 45 |
+
|
| 46 |
+
# Computing Sensitivity
|
| 47 |
+
def sensitivity(y_true, y_pred):
|
| 48 |
+
true_positives = K.sum(K.round(K.clip(y_true * y_pred, 0, 1)))
|
| 49 |
+
possible_positives = K.sum(K.round(K.clip(y_true, 0, 1)))
|
| 50 |
+
return true_positives / (possible_positives + K.epsilon())
|
| 51 |
+
|
| 52 |
+
|
| 53 |
+
# Computing Specificity
|
| 54 |
+
def specificity(y_true, y_pred):
|
| 55 |
+
true_negatives = K.sum(K.round(K.clip((1-y_true) * (1-y_pred), 0, 1)))
|
| 56 |
+
possible_negatives = K.sum(K.round(K.clip(1-y_true, 0, 1)))
|
| 57 |
+
return true_negatives / (possible_negatives + K.epsilon())
|