Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
sjromuel
Masterarbeit
Commits
18552d7e
Commit
18552d7e
authored
Nov 27, 2020
by
sjromuel
Browse files
dice_ct_seg for mr models
parent
69c0844c
Changes
1
Hide whitespace changes
Inline
Side-by-side
RobinNet/dice_gt_seg.py
View file @
18552d7e
...
...
@@ -18,66 +18,181 @@ from nets.Unet import dice_loss
def
main
():
mr
=
True
if
mr
:
file_path
=
"data/npy_thresh/"
gt_types
=
[
'ctseg'
,
'ctthresh'
,
'unetpred'
]
for
gt_type
in
gt_types
:
#if gt_type == 'ctseg': file_path = "data/npy/"
dice_Score16
=
[]
hdd_16
=
[]
all3dhdds
=
[]
for
patientnumber
in
range
(
16
):
y_pred3d
=
[]
patient_dsc
=
[]
patient_hdd
=
[]
if
gt_type
==
'ctseg'
:
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_segmr.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
)
noisygt_npy
=
cut_mr_ctunetpred
(
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
,
noisygt_npy
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
elif
gt_type
==
'unetpred'
:
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_segmr.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_mr_ctunetpred.gipl.npy"
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
elif
gt_type
==
'ctthresh'
:
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_segmr.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_100_thresh.gipl.npy"
)
noisygt_npy
=
cut_mr_ctunetpred
(
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
,
noisygt_npy
)
seg_npy
=
np
.
append
(
seg_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_segmr.gipl.npy"
),
axis
=
0
)
noisygt_npy
=
np
.
append
(
noisygt_npy
,
cut_mr_ctunetpred
(
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_250_thresh.gipl.npy"
)),
axis
=
0
)
seg_npy
=
np
.
append
(
seg_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_segmr.gipl.npy"
),
axis
=
0
)
noisygt_npy
=
np
.
append
(
noisygt_npy
,
cut_mr_ctunetpred
(
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_400_thresh.gipl.npy"
)),
axis
=
0
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
for
features
in
test_dataset
:
noisygt
,
seg
=
features
noisygt
=
onehotencode
(
noisygt
,
autoencoder
=
True
)
seg
=
onehotencode
(
seg
,
autoencoder
=
True
)
slice_dsc
=
dice_loss
(
noisygt
,
seg
)
patient_dsc
.
append
(
slice_dsc
)
try
:
y_true_np
=
np
.
squeeze
(
seg
[
0
,
:,
:,
0
].
numpy
()
>
0.5
)
y_true_np
=
y_true_np
.
astype
(
np
.
float_
)
pred_np
=
np
.
squeeze
(
noisygt
[
0
,
:,
:,
0
].
numpy
()
>
0.5
)
pred_np
=
pred_np
.
astype
(
np
.
float_
)
hausdorff_distance_filter
=
sitk
.
HausdorffDistanceImageFilter
()
hausdorff_distance_filter
.
Execute
(
sitk
.
GetImageFromArray
(
y_true_np
),
sitk
.
GetImageFromArray
(
pred_np
))
patient_hdd
.
append
(
hausdorff_distance_filter
.
GetHausdorffDistance
())
if
y_pred3d
==
[]:
y_pred3d
=
np
.
reshape
(
pred_np
,
newshape
=
(
1
,
512
,
512
))
y_true3d
=
np
.
reshape
(
y_true_np
,
newshape
=
(
1
,
512
,
512
))
else
:
y_pred3d
=
np
.
append
(
np
.
reshape
(
pred_np
,
newshape
=
(
1
,
512
,
512
)),
y_pred3d
,
axis
=
0
)
y_true3d
=
np
.
append
(
np
.
reshape
(
y_true_np
,
newshape
=
(
1
,
512
,
512
)),
y_true3d
,
axis
=
0
)
except
:
pass
print
(
"Dice Score for patient"
,
patientnumber
,
":"
,
np
.
mean
(
patient_dsc
))
dice_Score16
.
append
(
patient_dsc
)
file_path
=
"data/npy/"
gt_types
=
[
'ctfgt2'
,
'thresh'
]
for
gt_type
in
gt_types
:
dice_Score16
=
[]
hdd_16
=
[]
for
patientnumber
in
range
(
16
):
patient_dsc
=
[]
patient_hdd
=
[]
try
:
hausdorff_distance_filter
=
sitk
.
HausdorffDistanceImageFilter
()
hausdorff_distance_filter
.
Execute
(
sitk
.
GetImageFromArray
(
y_true3d
),
sitk
.
GetImageFromArray
(
y_pred3d
))
hdd3d
=
hausdorff_distance_filter
.
GetHausdorffDistance
()
print
(
"3D HDD for patient"
,
patientnumber
,
":"
,
hdd3d
)
all3dhdds
.
append
(
hdd3d
)
except
:
pass
if
gt_type
==
'ctfgt2'
:
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_ctfgt2.gipl.npy"
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
elif
gt_type
==
'thresh'
:
file_path
=
"data/npy_thresh/"
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_100_thresh.gipl.npy"
)
seg_npy
=
np
.
append
(
seg_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
),
axis
=
0
)
noisygt_npy
=
np
.
append
(
noisygt_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_250_thresh.gipl.npy"
),
axis
=
0
)
seg_npy
=
np
.
append
(
seg_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
),
axis
=
0
)
noisygt_npy
=
np
.
append
(
noisygt_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_400_thresh.gipl.npy"
),
axis
=
0
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
print
(
"All 3d-HDDs for"
,
gt_type
,
": "
,
all3dhdds
)
print
(
"Dice-Score mean: "
,
np
.
mean
(
dice_Score16
))
print
(
"Dice-Score std: "
,
np
.
std
(
dice_Score16
))
print
(
"HDD mean: "
,
np
.
mean
(
all3dhdds
))
print
(
"HDD std: "
,
np
.
std
(
all3dhdds
))
else
:
file_path
=
"data/npy/"
gt_types
=
[
'ctfgt2'
,
'thresh'
]
for
gt_type
in
gt_types
:
dice_Score16
=
[]
hdd_16
=
[]
all3dhdds
=
[]
for
patientnumber
in
range
(
16
):
y_pred3d
=
[]
patient_dsc
=
[]
patient_hdd
=
[]
if
gt_type
==
'ctfgt2'
:
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_ctfgt2.gipl.npy"
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
elif
gt_type
==
'thresh'
:
file_path
=
"data/npy_thresh/"
seg_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
)
noisygt_npy
=
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_100_thresh.gipl.npy"
)
seg_npy
=
np
.
append
(
seg_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
),
axis
=
0
)
noisygt_npy
=
np
.
append
(
noisygt_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_250_thresh.gipl.npy"
),
axis
=
0
)
seg_npy
=
np
.
append
(
seg_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_seg.gipl.npy"
),
axis
=
0
)
noisygt_npy
=
np
.
append
(
noisygt_npy
,
np
.
load
(
file_path
+
"P"
+
str
(
patientnumber
+
1
).
zfill
(
2
)
+
"_400_thresh.gipl.npy"
),
axis
=
0
)
seg_npy
=
np
.
reshape
(
seg_npy
,
(
seg_npy
.
shape
[
0
],
512
,
512
,
1
))
noisygt_npy
=
np
.
reshape
(
noisygt_npy
,
(
noisygt_npy
.
shape
[
0
],
512
,
512
,
1
))
test_dataset
=
tf
.
data
.
Dataset
.
from_tensor_slices
((
noisygt_npy
,
seg_npy
))
test_dataset
=
test_dataset
.
batch
(
batch_size
=
1
)
for
features
in
test_dataset
:
noisygt
,
seg
=
features
noisygt
=
onehotencode
(
noisygt
,
autoencoder
=
True
)
seg
=
onehotencode
(
seg
,
autoencoder
=
True
)
slice_dsc
=
dice_loss
(
noisygt
,
seg
)
patient_dsc
.
append
(
slice_dsc
)
y_true_np
=
np
.
squeeze
(
seg
[
0
,
:,
:,
0
].
numpy
()
>
0.5
)
y_true_np
=
y_true_np
.
astype
(
np
.
float_
)
pred_np
=
np
.
squeeze
(
noisygt
[
0
,
:,
:,
0
].
numpy
()
>
0.5
)
pred_np
=
pred_np
.
astype
(
np
.
float_
)
hausdorff_distance_filter
=
sitk
.
HausdorffDistanceImageFilter
()
hausdorff_distance_filter
.
Execute
(
sitk
.
GetImageFromArray
(
y_true_np
),
sitk
.
GetImageFromArray
(
pred_np
))
patient_hdd
.
append
(
hausdorff_distance_filter
.
GetHausdorffDistance
())
if
y_pred3d
==
[]:
y_pred3d
=
np
.
reshape
(
pred_np
,
newshape
=
(
1
,
512
,
512
))
y_true3d
=
np
.
reshape
(
y_true_np
,
newshape
=
(
1
,
512
,
512
))
else
:
y_pred3d
=
np
.
append
(
np
.
reshape
(
pred_np
,
newshape
=
(
1
,
512
,
512
)),
y_pred3d
,
axis
=
0
)
y_true3d
=
np
.
append
(
np
.
reshape
(
y_true_np
,
newshape
=
(
1
,
512
,
512
)),
y_true3d
,
axis
=
0
)
for
features
in
test_dataset
:
noisygt
,
seg
=
features
noisygt
=
onehotencode
(
noisygt
,
autoencoder
=
True
)
seg
=
onehotencode
(
seg
,
autoencoder
=
True
)
slice_dsc
=
dice_loss
(
noisygt
,
seg
)
patient_dsc
.
append
(
slice_dsc
)
y_true_np
=
np
.
squeeze
(
seg
[
0
,
:,
:,
0
].
numpy
()
>
0.5
)
y_true_np
=
y_true_np
.
astype
(
np
.
float_
)
pred_np
=
np
.
squeeze
(
noisygt
[
0
,
:,
:,
0
].
numpy
()
>
0.5
)
pred_np
=
pred_np
.
astype
(
np
.
float_
)
hausdorff_distance_filter
=
sitk
.
HausdorffDistanceImageFilter
()
hausdorff_distance_filter
.
Execute
(
sitk
.
GetImageFromArray
(
y_true_np
),
sitk
.
GetImageFromArray
(
pred_np
))
patient_hdd
.
append
(
hausdorff_distance_filter
.
GetHausdorffDistance
())
hdd_16
.
append
(
np
.
mean
(
patient_hdd
))
dice_Score16
.
append
(
np
.
mean
(
patient_dsc
))
print
(
gt_type
,
dice_Score16
)
print
(
gt_type
,
"HDD: "
,
hdd_16
)
print
(
"Dice-Score mean: "
,
np
.
mean
(
dice_Score16
))
print
(
"Dice-Score std: "
,
np
.
std
(
dice_Score16
))
print
(
"HDD mean: "
,
np
.
mean
(
hdd_16
))
print
(
"HDD std: "
,
np
.
std
(
hdd_16
))
try
:
hausdorff_distance_filter
=
sitk
.
HausdorffDistanceImageFilter
()
hausdorff_distance_filter
.
Execute
(
sitk
.
GetImageFromArray
(
y_true3d
),
sitk
.
GetImageFromArray
(
y_pred3d
))
hdd3d
=
hausdorff_distance_filter
.
GetHausdorffDistance
()
print
(
"3D HDD for patient"
,
patientnumber
,
":"
,
hdd3d
)
all3dhdds
.
append
(
hdd3d
)
except
:
pass
print
(
"All 3d-HDDs for"
,
gt_type
,
": "
,
all3dhdds
)
#print("Dice-Score mean: ", np.mean(dice_Score16))
#print("Dice-Score std: ", np.std(dice_Score16))
print
(
"HDD mean: "
,
np
.
mean
(
all3dhdds
))
print
(
"HDD std: "
,
np
.
std
(
all3dhdds
))
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment