-
Notifications
You must be signed in to change notification settings - Fork 1
/
fcn.py
executable file
·187 lines (142 loc) · 5.92 KB
/
fcn.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
from keras.models import *
from keras.layers import *
from keras.optimizers import *
# local
from .vgg16 import get_vgg_encoder
from .basic_models import vanilla_encoder
from .BilinearUpSampling import *
"""
Code taken from:
https://github.com/divamgupta/image-segmentation-keras
"""
IMAGE_ORDERING = "channels_last"
# crop o1 wrt o2
def crop(o1, o2, i):
o_shape2 = Model(i, o2).output_shape
if IMAGE_ORDERING == 'channels_first':
output_height2 = o_shape2[2]
output_width2 = o_shape2[3]
else:
output_height2 = o_shape2[1]
output_width2 = o_shape2[2]
o_shape1 = Model(i, o1).output_shape
if IMAGE_ORDERING == 'channels_first':
output_height1 = o_shape1[2]
output_width1 = o_shape1[3]
else:
output_height1 = o_shape1[1]
output_width1 = o_shape1[2]
cx = abs(output_width1 - output_width2)
cy = abs(output_height2 - output_height1)
if output_width1 > output_width2:
o1 = Cropping2D(cropping=((0, 0), (0, cx)),
data_format=IMAGE_ORDERING)(o1)
else:
o2 = Cropping2D(cropping=((0, 0), (0, cx)),
data_format=IMAGE_ORDERING)(o2)
if output_height1 > output_height2:
o1 = Cropping2D(cropping=((0, cy), (0, 0)),
data_format=IMAGE_ORDERING)(o1)
else:
o2 = Cropping2D(cropping=((0, cy), (0, 0)),
data_format=IMAGE_ORDERING)(o2)
return o1, o2
def fcn_8(n_classes, encoder=vanilla_encoder, input_height=224,
input_width=224):
img_input, levels = encoder(
input_height=input_height, input_width=input_width)
[f1, f2, f3, f4, f5] = levels
o = f5
o = (Conv2D(4096, (7, 7), activation='relu',
padding='same', data_format=IMAGE_ORDERING))(o)
o = Dropout(0.5)(o)
o = (Conv2D(4096, (1, 1), activation='relu',
padding='same', data_format=IMAGE_ORDERING))(o)
o = Dropout(0.5)(o)
o = (Conv2D(n_classes, (1, 1), kernel_initializer='he_normal',
data_format=IMAGE_ORDERING))(o)
o = Conv2DTranspose(n_classes, kernel_size=(4, 4), strides=(
2, 2), use_bias=False, data_format=IMAGE_ORDERING)(o)
o2 = f4
o2 = (Conv2D(n_classes, (1, 1), kernel_initializer='he_normal',
data_format=IMAGE_ORDERING))(o2)
o, o2 = crop(o, o2, img_input)
o = Add()([o, o2])
o = Conv2DTranspose(n_classes, kernel_size=(4, 4), strides=(
2, 2), use_bias=False, data_format=IMAGE_ORDERING)(o)
o2 = f3
o2 = (Conv2D(n_classes, (1, 1), kernel_initializer='he_normal',
data_format=IMAGE_ORDERING))(o2)
o2, o = crop(o2, o, img_input)
o = Add()([o2, o])
# o = Conv2DTranspose(n_classes, kernel_size=(16, 16), strides=(
# 8, 8), use_bias=False, data_format=IMAGE_ORDERING)(o)
o = BilinearUpSampling2D(size=(8, 8))(o)
# Add segmentaition output
out = Conv2D(n_classes, 3, padding='same', activation='sigmoid')(o)
model = Model(input=img_input, output=out)
model.compile(optimizer=Adam(lr=1e-4),
loss='binary_crossentropy',
metrics=['accuracy'])
# model = get_segmentation_model(img_input, o)
# model.model_name = "fcn_8"
return model
def fcn_32(n_classes, encoder=vanilla_encoder, input_height=224,
input_width=224):
img_input, levels = encoder(
input_height=input_height, input_width=input_width)
[f1, f2, f3, f4, f5] = levels
o = f5
o = (Conv2D(4096, (7, 7), activation='relu',
padding='same', data_format=IMAGE_ORDERING))(o)
o = Dropout(0.5)(o)
o = (Conv2D(4096, (1, 1), activation='relu',
padding='same', data_format=IMAGE_ORDERING))(o)
o = Dropout(0.5)(o)
o = (Conv2D(n_classes, (1, 1), kernel_initializer='he_normal',
data_format=IMAGE_ORDERING))(o)
# o = Conv2DTranspose(n_classes, kernel_size=(32, 32), strides=(
# 32, 32), use_bias=False, data_format=IMAGE_ORDERING)(o)
o = BilinearUpSampling2D(size=(32, 32))(o)
# Add segmentaition output
out = Conv2D(3, 3, padding='same', activation='sigmoid', name='last_layer')(o)
model = Model(input=img_input, output=out)
model.compile(optimizer=Adam(lr=1e-4),
loss='binary_crossentropy',
metrics=['accuracy'])
# model = get_segmentation_model(img_input, o)
# model.model_name = "fcn_32"
return model
def fcn_8_vgg(n_classes, input_height=224, input_width=224):
model = fcn_8(n_classes, get_vgg_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "fcn_8_vgg"
return model
def fcn_32_vgg(n_classes, input_height=416, input_width=608):
model = fcn_32(n_classes, get_vgg_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "fcn_32_vgg"
return model
def fcn_8_resnet50(n_classes, input_height=416, input_width=608):
model = fcn_8(n_classes, get_resnet50_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "fcn_8_resnet50"
return model
def fcn_32_resnet50(n_classes, input_height=416, input_width=608):
model = fcn_32(n_classes, get_resnet50_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "fcn_32_resnet50"
return model
def fcn_8_mobilenet(n_classes, input_height=416, input_width=608):
model = fcn_8(n_classes, get_mobilenet_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "fcn_8_mobilenet"
return model
def fcn_32_mobilenet(n_classes, input_height=416, input_width=608):
model = fcn_32(n_classes, get_mobilenet_encoder,
input_height=input_height, input_width=input_width)
model.model_name = "fcn_32_mobilenet"
return model
# if __name__ == '__main__':
# m = fcn_8(101)
# m = fcn_32(101)