-
Notifications
You must be signed in to change notification settings - Fork 6
/
models.py
87 lines (84 loc) · 4.3 KB
/
models.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
from keras.models import Model, Sequential
from keras.layers import Input, Convolution2D, MaxPooling2D, UpSampling2D, Lambda, Dropout, merge
def encoder_decoder(input_shape):
return Sequential([
Lambda(lambda x: x / 127.5 - 1.0, input_shape=input_shape),
Convolution2D(8, 3, 3, activation='relu', border_mode='same'),
Convolution2D(8, 3, 3, activation='relu', border_mode='same'),
MaxPooling2D((2,2), strides=(2,2)),
Dropout(0.2),
Convolution2D(16, 3, 3, activation='relu', border_mode='same'),
Convolution2D(16, 3, 3, activation='relu', border_mode='same'),
MaxPooling2D((2,2), strides=(2,2)),
Dropout(0.2),
Convolution2D(32, 3, 3, activation='relu', border_mode='same'),
Convolution2D(32, 3, 3, activation='relu', border_mode='same'),
MaxPooling2D((2,2), strides=(2,2)),
Dropout(0.2),
Convolution2D(64, 3, 3, activation='relu', border_mode='same'),
Convolution2D(64, 3, 3, activation='relu', border_mode='same'),
MaxPooling2D((2,2), strides=(2,2)),
Dropout(0.2),
Convolution2D(128, 3, 3, activation='relu', border_mode='same'),
Convolution2D(128, 3, 3, activation='relu', border_mode='same'),
UpSampling2D(size=(2,2)),
Dropout(0.2),
Convolution2D(64, 3, 3, activation='relu', border_mode='same'),
Convolution2D(64, 3, 3, activation='relu', border_mode='same'),
UpSampling2D(size=(2,2)),
Dropout(0.2),
Convolution2D(32, 3, 3, activation='relu', border_mode='same'),
Convolution2D(32, 3, 3, activation='relu', border_mode='same'),
UpSampling2D(size=(2,2)),
Dropout(0.2),
Convolution2D(16, 3, 3, activation='relu', border_mode='same'),
Convolution2D(16, 3, 3, activation='relu', border_mode='same'),
UpSampling2D(size=(2,2)),
Dropout(0.2),
Convolution2D(8, 3, 3, activation='relu', border_mode='same'),
Convolution2D(8, 3, 3, activation='relu', border_mode='same'),
Convolution2D(1, 1, 1, activation='sigmoid')
])
def unet(input_shape):
i = Input(input_shape)
Lambda(lambda x: x / 127.5 - 1.0)
c1 = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(i)
c1 = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(c1)
m1 = MaxPooling2D(pool_size=(2,2))(c1)
d1 = Dropout(0.2)(m1)
c2 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(d1)
c2 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(c2)
m2 = MaxPooling2D(pool_size=(2,2))(c2)
d2 = Dropout(0.2)(m2)
c3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(d2)
c3 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(c3)
m3 = MaxPooling2D(pool_size=(2,2))(c3)
d3 = Dropout(0.2)(m3)
c4 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(d3)
c4 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(c4)
m4 = MaxPooling2D(pool_size=(2,2))(c4)
d4 = Dropout(0.2)(m4)
c5 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(d4)
c5 = Convolution2D(128, 3, 3, activation='relu', border_mode='same')(c5)
u1 = UpSampling2D(size=(2,2))(c5)
d5 = Dropout(0.2)(u1)
c6 = merge([d5,c4], mode='concat', concat_axis=3)
c6 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(c6)
c6 = Convolution2D(64, 3, 3, activation='relu', border_mode='same')(c6)
u2 = UpSampling2D(size=(2,2))(c6)
d6 = Dropout(0.2)(u2)
c7 = merge([d6,c3], mode='concat', concat_axis=3)
c7 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(c7)
c7 = Convolution2D(32, 3, 3, activation='relu', border_mode='same')(c7)
u3 = UpSampling2D(size=(2,2))(c7)
d7 = Dropout(0.2)(u3)
c8 = merge([d7,c2], mode='concat', concat_axis=3)
c8 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(c8)
c8 = Convolution2D(16, 3, 3, activation='relu', border_mode='same')(c8)
u4 = UpSampling2D(size=(2,2))(c8)
d8 = Dropout(0.2)(u4)
c9 = merge([d8,c1], mode='concat', concat_axis=3)
c9 = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(c9)
c9 = Convolution2D(8, 3, 3, activation='relu', border_mode='same')(c9)
o = Convolution2D(1, 1, 1, activation='sigmoid')(c9)
return Model(input=i, output=o)