Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F102779058
umorphnet.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Mon, Feb 24, 02:46
Size
3 KB
Mime Type
text/x-python
Expires
Wed, Feb 26, 02:46 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
24422751
Attached To
R964 lineseg
umorphnet.py
View Options
from
keras.models
import
Model
from
keras.layers
import
Input
,
merge
,
Convolution2D
,
MaxPooling2D
,
UpSampling2D
,
BatchNormalization
,
Activation
from
keras.optimizers
import
Adam
from
keras
import
backend
from
mirror_padding
import
MirrorPadding2D
import
numpy
as
np
from
keras.regularizers
import
WeightRegularizer
from
morphological
import
Dilation2D
,
Erosion2D
def
add_batch_normalization
(
x
):
return
BatchNormalization
(
axis
=
1
)(
x
)
def
conv_valid
(
x_in
,
n_filters
,
kernel_size
,
batch_norm
,
activation
):
x
=
MirrorPadding2D
((
int
(
np
.
floor
(
kernel_size
[
0
]
/
2
)),
int
(
np
.
floor
(
kernel_size
[
1
]
/
2
))))(
x_in
)
x
=
Dilation2D
(
n_filters
,
3
,
3
,
border_mode
=
'same'
,
activation
=
'relu'
)(
x
)
x
=
Convolution2D
(
n_filters
,
*
kernel_size
,
border_mode
=
'valid'
,
W_regularizer
=
WeightRegularizer
(
l2
=
0.00001
),
init
=
'he_normal'
)(
x
)
x
=
Erosion2D
(
n_filters
,
3
,
3
,
border_mode
=
'same'
,
activation
=
'relu'
)(
x
)
x
=
batch_norm
(
x
)
x
=
Activation
(
activation
=
activation
)(
x
)
return
x
def
build_u_net_rec
(
x
,
level
=
0
,
conv_per_level
=
3
,
max_level
=
3
,
k
=
16
,
batch_norm
=
lambda
y
:
y
,
kernel_size
=
(
3
,
3
),
activation
=
'relu'
):
n_filters
=
k
print
(
"Recursion {}"
.
format
(
level
))
if
level
==
max_level
:
for
i
in
range
(
conv_per_level
):
x
=
conv_valid
(
x
,
n_filters
,
kernel_size
,
batch_norm
=
batch_norm
,
activation
=
activation
)
return
x
,
x
for
_
in
range
(
conv_per_level
):
x
=
conv_valid
(
x
,
n_filters
,
kernel_size
,
batch_norm
=
batch_norm
,
activation
=
activation
)
to_lower
=
MaxPooling2D
(
pool_size
=
(
2
,
2
))(
x
)
lower_level
,
center
=
build_u_net_rec
(
to_lower
,
level
+
1
,
conv_per_level
,
max_level
,
k
,
batch_norm
)
x
=
merge
([
UpSampling2D
(
size
=
(
2
,
2
))(
lower_level
),
x
],
mode
=
'concat'
,
concat_axis
=
1
)
x
=
Convolution2D
(
n_filters
,
1
,
1
,
border_mode
=
'same'
)(
x
)
x
=
conv_valid
(
x
,
n_filters
,
kernel_size
,
batch_norm
=
batch_norm
,
activation
=
activation
)
x
=
Activation
(
activation
=
activation
)(
x
)
for
_
in
range
(
conv_per_level
-
1
):
x
=
conv_valid
(
x
,
n_filters
,
kernel_size
,
batch_norm
=
batch_norm
,
activation
=
activation
)
return
x
,
center
def
dice_coef_loss
(
y_true
,
y_pred
):
return
-
dice_coef
(
y_true
,
y_pred
)
def
get_unet
(
img_rows
=
512
,
img_cols
=
512
,
classification
=
True
,
batch_normalization
=
False
,
k
=
32
,
n_inputs
=
1
,
n_outputs
=
1
,
max_level
=
4
,
do_compile
=
True
,
conv_per_level
=
2
,
loss
=
dice_coef_loss
):
if
classification
:
inputs
=
Input
((
n_inputs
,
img_rows
,
img_cols
))
else
:
inputs
=
Input
((
2
,
img_rows
,
img_cols
))
cond_batch
=
add_batch_normalization
if
batch_normalization
else
lambda
var
:
var
x
=
Convolution2D
(
k
,
3
,
3
,
border_mode
=
'same'
)(
inputs
)
# no relu in first layer
x
,
center
=
build_u_net_rec
(
x
,
max_level
=
max_level
,
k
=
k
,
conv_per_level
=
conv_per_level
,
batch_norm
=
cond_batch
)
x
=
cond_batch
(
x
)
if
classification
:
x
=
cond_batch
(
x
)
x
=
Convolution2D
(
1
,
1
,
1
,
activation
=
'sigmoid'
)(
x
)
# print(" Segmentation.")
else
:
x
=
Convolution2D
(
2
,
1
,
1
)(
x
)
# print("Disparity.")
model
=
Model
(
input
=
inputs
,
output
=
x
)
print
(
model
.
summary
())
if
do_compile
:
if
classification
:
model
.
compile
(
optimizer
=
Adam
(
lr
=
1e-5
),
loss
=
loss
,
metrics
=
[
dice_coef
])
else
:
model
.
compile
(
optimizer
=
Adam
(
lr
=
1e-5
),
loss
=
'mse'
)
return
model
,
inputs
,
x
,
center
def
dice_coef
(
y_true
,
y_pred
,
smooth
=
1.
):
y_true_f
=
backend
.
flatten
(
y_true
)
y_pred_f
=
backend
.
flatten
(
y_pred
)
intersection
=
backend
.
sum
(
y_true_f
*
y_pred_f
)
return
(
2.
*
intersection
+
smooth
)
/
(
backend
.
sum
(
y_true_f
)
+
backend
.
sum
(
y_pred_f
)
+
smooth
)
Event Timeline
Log In to Comment