Skip to content
GitLab
Menu
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
Anton Naumov
Retina_classifier
Commits
ffaf576a
Commit
ffaf576a
authored
May 06, 2020
by
Anton Naumov
Browse files
edited shap functions and notebook
parent
4ddcaf2e
Changes
2
Expand all
Hide whitespace changes
Inline
Side-by-side
notebooks/custom_functions/shap_functions.py
View file @
ffaf576a
import
numpy
as
np
import
cv2
import
matplotlib.pyplot
as
plt
import
shap
__all__
=
[
'standardize'
,
'get_background'
,
'get_organoidlike_background'
'get_organoidlike_background'
,
'print_shap_images'
]
def
standardize
(
input_arr
):
...
...
@@ -14,8 +18,13 @@ def standardize(input_arr):
x
/=
(
np
.
std
(
x
,
keepdims
=
True
)
+
1e-6
)
return
x
def
get_background
(
img
):
def
compute_background
(
img
):
if
len
(
img
.
shape
)
==
4
:
img
=
img
[
0
]
elif
len
(
img
.
shape
)
==
3
:
pass
else
:
raise
Exception
(
"Shape of the imput image must be equal to 3 or 4!"
)
gray
=
cv2
.
cvtColor
(
img
.
astype
(
np
.
uint8
),
cv2
.
COLOR_RGB2GRAY
)
ret
,
thresh
=
cv2
.
threshold
(
gray
,
100
,
255
,
cv2
.
THRESH_BINARY_INV
+
cv2
.
THRESH_OTSU
)
ret
,
labels
=
cv2
.
connectedComponents
(
thresh
)
...
...
@@ -24,6 +33,12 @@ def get_background(img):
background_label
=
np
.
argsort
(
areas
)[
-
2
]
organoid_label
=
np
.
argsort
(
areas
)[
-
1
]
return
img
,
labels
,
areas
,
background_label
,
organoid_label
def
get_background
(
img
):
img
,
labels
,
areas
,
_
,
background_label
=
compute_background
(
img
)
background_area
=
areas
[
background_label
]
background_mask
=
(
labels
==
background_label
)
...
...
@@ -31,16 +46,39 @@ def get_background(img):
return
np
.
expand_dims
((
np
.
ones_like
(
img
)
*
mean_background
),
axis
=
0
)
def
get_organoidlike_background
(
img
):
gray
=
cv2
.
cvtColor
(
img
.
astype
(
np
.
uint8
),
cv2
.
COLOR_RGB2GRAY
)
ret
,
thresh
=
cv2
.
threshold
(
gray
,
100
,
255
,
cv2
.
THRESH_BINARY_INV
+
cv2
.
THRESH_OTSU
)
ret
,
labels
=
cv2
.
connectedComponents
(
thresh
)
areas
=
np
.
sum
((
labels
==
np
.
arange
(
ret
).
reshape
(
-
1
,
1
,
1
)),
axis
=
(
1
,
2
))
background_label
=
np
.
argsort
(
areas
)[
-
2
]
organoid_label
=
np
.
argsort
(
areas
)[
-
1
]
img
,
labels
,
areas
,
organoid_label
,
_
=
compute_background
(
img
)
organoid_area
=
areas
[
organoid_label
]
organoid_mask
=
(
labels
==
organoid_label
)
mean_background
=
(
img
[:,:,
0
]
*
organoid_mask
).
sum
()
/
organoid_area
return
np
.
expand_dims
((
np
.
ones_like
(
img
)
*
mean_background
),
axis
=
0
)
\ No newline at end of file
return
np
.
expand_dims
((
np
.
ones_like
(
img
)
*
mean_background
),
axis
=
0
)
def
print_shap_images
(
model
=
None
,
image_to_process
=
None
,
reference_image
=
None
,
image_name
=
None
,
background
=
None
,
config_dict
=
None
,
scores_dict
=
None
,
output_file_path
=
None
):
explainer
=
shap
.
DeepExplainer
(
model
,
background
)
shap_values
=
explainer
.
shap_values
(
image_to_process
)
shap
.
image_plot
(
shap_values
,
reference_image
.
astype
(
np
.
float32
),
show
=
False
)
title
=
image_name
title
=
" "
.
join
([
title
,
" Network type:"
,
config_dict
[
'keras_model_name'
]])
title
=
"
\n
"
.
join
([
title
,
"Network prediction:"
])
title
=
" "
.
join
(
[
title
,
str
(
np
.
round
(
model
.
predict
(
image_to_process
)[
0
,
0
],
decimals
=
3
)),
" Threshold: "
,
str
(
np
.
round
(
scores_dict
[
'threshold'
],
decimals
=
3
))
]
)
plt
.
suptitle
(
title
,
y
=
0.9
)
plt
.
savefig
(
output_file_path
,
dpi
=
600
)
\ No newline at end of file
notebooks/shap_values/shap_values.ipynb
View file @
ffaf576a
This diff is collapsed.
Click to expand it.
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