-
Notifications
You must be signed in to change notification settings - Fork 1
/
unet.py
97 lines (82 loc) · 4.23 KB
/
unet.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
from keras.models import Model
from keras.layers import Input
from keras.layers import Conv2D
from keras.layers import concatenate
from keras.layers import MaxPooling2D
from keras.layers import UpSampling2D
from keras.layers.advanced_activations import ELU
from keras.layers.normalization import BatchNormalization
def get_unet(input_shape, n_classes=1, n_filters=32):
"""
UNet without crop. May be exposed to some uncertaineties near the boundaries, but works good on our tasks
input shape should be with "channels_last"
:param input_shape: input shape of the images
:param n_classes: the number of classes for prediction mask
:param n_filters: parameter of the network, channels multiplier
"""
inputs = Input(shape=(None, None, input_shape[-1]))
conv1 = Conv2D(n_filters, (3, 3), padding='same', kernel_initializer='he_uniform')(inputs)
conv1 = BatchNormalization()(conv1)
conv1 = ELU()(conv1)
conv1 = Conv2D(n_filters, (3, 3), padding='same', kernel_initializer='he_uniform')(conv1)
conv1 = BatchNormalization()(conv1)
conv1 = ELU()(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(n_filters * 2, (3, 3), padding='same', kernel_initializer='he_uniform')(pool1)
conv2 = BatchNormalization()(conv2)
conv2 = ELU()(conv2)
conv2 = Conv2D(n_filters * 2, (3, 3), padding='same', kernel_initializer='he_uniform')(conv2)
conv2 = BatchNormalization()(conv2)
conv2 = ELU()(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(n_filters * 4, (3, 3), padding='same', kernel_initializer='he_uniform')(pool2)
conv3 = BatchNormalization()(conv3)
conv3 = ELU()(conv3)
conv3 = Conv2D(n_filters * 4, (3, 3), padding='same', kernel_initializer='he_uniform')(conv3)
conv3 = BatchNormalization()(conv3)
conv3 = ELU()(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(n_filters * 8, (3, 3), padding='same', kernel_initializer='he_uniform')(pool3)
conv4 = BatchNormalization()(conv4)
conv4 = ELU()(conv4)
conv4 = Conv2D(n_filters * 8, (3, 3), padding='same', kernel_initializer='he_uniform')(conv4)
conv4 = BatchNormalization()(conv4)
conv4 = ELU()(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(conv4)
conv5 = Conv2D(n_filters * 16, (3, 3), padding='same', kernel_initializer='he_uniform')(pool4)
conv5 = BatchNormalization()(conv5)
conv5 = ELU()(conv5)
conv5 = Conv2D(n_filters * 16, (3, 3), padding='same', kernel_initializer='he_uniform')(conv5)
conv5 = BatchNormalization()(conv5)
conv5 = ELU()(conv5)
up6 = concatenate([UpSampling2D(size=(2, 2))(conv5), conv4], axis=3)
conv6 = Conv2D(n_filters * 8, (3, 3), padding='same', kernel_initializer='he_uniform')(up6)
conv6 = BatchNormalization()(conv6)
conv6 = ELU()(conv6)
conv6 = Conv2D(n_filters * 8, (3, 3), padding='same', kernel_initializer='he_uniform')(conv6)
conv6 = BatchNormalization()(conv6)
conv6 = ELU()(conv6)
up7 = concatenate([UpSampling2D(size=(2, 2))(conv6), conv3], axis=3)
conv7 = Conv2D(n_filters * 4, (3, 3), padding='same', kernel_initializer='he_uniform')(up7)
conv7 = BatchNormalization()(conv7)
conv7 = ELU()(conv7)
conv7 = Conv2D(n_filters * 4, (3, 3), padding='same', kernel_initializer='he_uniform')(conv7)
conv7 = BatchNormalization()(conv7)
conv7 = ELU()(conv7)
up8 = concatenate([UpSampling2D(size=(2, 2))(conv7), conv2], axis=3)
conv8 = Conv2D(n_filters * 2, (3, 3), padding='same', kernel_initializer='he_uniform')(up8)
conv8 = BatchNormalization()(conv8)
conv8 = ELU()(conv8)
conv8 = Conv2D(n_filters * 2, (3, 3), padding='same', kernel_initializer='he_uniform')(conv8)
conv8 = BatchNormalization()(conv8)
conv8 = ELU()(conv8)
up9 = concatenate([UpSampling2D(size=(2, 2))(conv8), conv1], axis=3)
conv9 = Conv2D(n_filters, (3, 3), padding='same', kernel_initializer='he_uniform')(up9)
conv9 = BatchNormalization()(conv9)
conv9 = ELU()(conv9)
conv9 = Conv2D(n_filters, (3, 3), padding='same', kernel_initializer='he_uniform')(conv9)
conv9 = BatchNormalization()(conv9)
conv9 = ELU()(conv9)
conv10 = Conv2D(n_classes, (1, 1), activation='sigmoid')(conv9)
model = Model(inputs=inputs, outputs=conv10)
return model