-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
105 lines (75 loc) · 3.83 KB
/
model.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
from keras.layers import Dense, Dropout, Activation, \
Flatten, Convolution2D, MaxPooling2D, \
BatchNormalization, Conv2D, Input,merge,AveragePooling2D,concatenate
from keras.models import Model
from utils.BilinearUpSampling import *
class SegModel(object):
def __init__(self, input_size):
self.input_size=input_size
self._build_model()
def relu(self,x):
return Activation('relu')(x)
def ResidualNet(self,nfilter,s):
def Res_unit(x):
BottleN = int(nfilter / 4)
b_filter = BottleN
x = BatchNormalization(axis=-1)(x)
x = self.relu(x)
ident_map = x
x = Conv2D(b_filter,(1,1),strides=(s,s))(x)
x = BatchNormalization(axis=-1)(x)
x = self.relu(x)
x = Conv2D(b_filter,3,3,border_mode='same')(x)
x = BatchNormalization(axis=-1)(x)
x = self.relu(x)
x = Conv2D(nfilter,(1,1))(x)
ident_map = Conv2D(nfilter,(1,1),strides=(s,s))(ident_map)
out = merge([ident_map,x],mode='sum')
return out
return Res_unit
def Res_Group(self,nfilter,layers,_stride):
def Res_unit(x):
for i in range(layers):
if i==0:
x = self.ResidualNet(nfilter,_stride)(x)
else:
x = self.ResidualNet(nfilter,1)(x)
return x
return Res_unit
#-------------------Pyramid Dilated Convolution--------------------------------
def PDC(self,input_layer,stride_,number_kernel,kernel_size,dconv_filters):
l = BatchNormalization(axis=-1)(input_layer)
l = self.relu(l)
conv1 = Conv2D(number_kernel, kernel_size, activation = 'relu',padding= 'same', strides=stride_)(l)
a1 = Conv2D(dconv_filters, 1, activation = 'relu', padding = 'same', dilation_rate = 1)(conv1)
a2 = Conv2D(dconv_filters, 3, activation = 'relu', padding = 'same', dilation_rate = 3)(conv1)
a3 = Conv2D(dconv_filters, 3, activation = 'relu', padding = 'same', dilation_rate = 6)(conv1)
a4 = Conv2D(dconv_filters, 3, activation = 'relu', padding = 'same', dilation_rate = 12)(conv1)
concat = merge([a1,a2,a3,a4], mode = 'concat', concat_axis = 3)
return concat
def _build_model(self):
#--------------------encoder---------
inp = Input(shape=(self.input_size))
i = inp
i = Conv2D(16,7,padding='same')(i)
#----------------------------------------
i = self.Res_Group(32,3,1)(i)
#----------------------------------------
i = self.Res_Group(64,3,2)(i)
out_pdc1=self.PDC(i,4,64,3,16)
#---------------------------------------
i = self.Res_Group(128,3,2)(i)
out_pdc2 = self.PDC(i,2,128,3,32)
#---------------------------------------
i = self.Res_Group(256,3,2)(i)
out_pdc3 = self.PDC(i,1,256,1,64)
#--------------------------------------
i = self.Res_Group(512,3,1)(i)
out_pdc4 = self.PDC(i,1,256,1,64)
#-----------------------decoder----------------
concat_f = merge([out_pdc1,out_pdc2,out_pdc3,out_pdc4], mode = 'concat', concat_axis = 3)
i_dec = Dropout(0.5)(concat_f)
conv_f = Conv2D(1,(1, 1), activation='sigmoid', padding='same')(i_dec)
up_dec=BilinearUpSampling2D(size = (8,8))(conv_f)
model = Model(inputs=inp, outputs=up_dec )
self.model=model