-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
222 lines (180 loc) · 9.71 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
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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
from tensorflow.keras.models import *
from tensorflow.keras import models
from tensorflow.keras import layers
from tensorflow.keras.layers import *
from tensorflow.keras.optimizers import *
def xnet(input_shape=(512, 512, 3), classes=2, kernel_size=3, filter_depth=(64, 128, 256, 512, 0)):
img_input = Input(shape=input_shape)
# Encoder
conv1 = Conv2D(filter_depth[0], (kernel_size, kernel_size), padding="same")(img_input)
batch1 = BatchNormalization()(conv1)
act1 = Activation("relu")(batch1)
pool1 = MaxPooling2D(pool_size=(2, 2))(act1)
# 100x100
conv2 = Conv2D(filter_depth[1], (kernel_size, kernel_size), padding="same")(pool1)
batch2 = BatchNormalization()(conv2)
act2 = Activation("relu")(batch2)
pool2 = MaxPooling2D(pool_size=(2, 2))(act2)
# 50x50
conv3 = Conv2D(filter_depth[2], (kernel_size, kernel_size), padding="same")(pool2)
batch3 = BatchNormalization()(conv3)
act3 = Activation("relu")(batch3)
pool3 = MaxPooling2D(pool_size=(2, 2))(act3)
# 25x25
# Flat
conv4 = Conv2D(filter_depth[3], (kernel_size, kernel_size), padding="same")(pool3)
batch4 = BatchNormalization()(conv4)
act4 = Activation("relu")(batch4)
# 25x25
conv5 = Conv2D(filter_depth[3], (kernel_size, kernel_size), padding="same")(act4)
batch5 = BatchNormalization()(conv5)
act5 = Activation("relu")(batch5)
# 25x25
# Up
up6 = UpSampling2D(size=(2, 2))(act5)
conv6 = Conv2D(filter_depth[2], (kernel_size, kernel_size), padding="same")(up6)
batch6 = BatchNormalization()(conv6)
act6 = Activation("relu")(batch6)
concat6 = Concatenate()([act3, act6])
# 50x50
up7 = UpSampling2D(size=(2, 2))(concat6)
conv7 = Conv2D(filter_depth[1], (kernel_size, kernel_size), padding="same")(up7)
batch7 = BatchNormalization()(conv7)
act7 = Activation("relu")(batch7)
concat7 = Concatenate()([act2, act7])
# 100x100
# Down
conv8 = Conv2D(filter_depth[1], (kernel_size, kernel_size), padding="same")(concat7)
batch8 = BatchNormalization()(conv8)
act8 = Activation("relu")(batch8)
pool8 = MaxPooling2D(pool_size=(2, 2))(act8)
# 50x50
conv9 = Conv2D(filter_depth[2], (kernel_size, kernel_size), padding="same")(pool8)
batch9 = BatchNormalization()(conv9)
act9 = Activation("relu")(batch9)
pool9 = MaxPooling2D(pool_size=(2, 2))(act9)
# 25x25
# Flat
conv10 = Conv2D(filter_depth[3], (kernel_size, kernel_size), padding="same")(pool9)
batch10 = BatchNormalization()(conv10)
act10 = Activation("relu")(batch10)
# 25x25
conv11 = Conv2D(filter_depth[3], (kernel_size, kernel_size), padding="same")(act10)
batch11 = BatchNormalization()(conv11)
act11 = Activation("relu")(batch11)
# 25x25
# Encoder
up12 = UpSampling2D(size=(2, 2))(act11)
conv12 = Conv2D(filter_depth[2], (kernel_size, kernel_size), padding="same")(up12)
batch12 = BatchNormalization()(conv12)
act12 = Activation("relu")(batch12)
concat12 = Concatenate()([act9, act12])
# 50x50
up13 = UpSampling2D(size=(2, 2))(concat12)
conv13 = Conv2D(filter_depth[1], (kernel_size, kernel_size), padding="same")(up13)
batch13 = BatchNormalization()(conv13)
act13 = Activation("relu")(batch13)
concat13 = Concatenate()([act8, act13])
# 100x100
up14 = UpSampling2D(size=(2, 2))(concat13)
conv14 = Conv2D(filter_depth[0], (kernel_size, kernel_size), padding="same")(up14)
batch14 = BatchNormalization()(conv14)
act14 = Activation("relu")(batch14)
concat14 = Concatenate()([act1, act14])
# 200x200
conv15 = Conv2D(classes, (1, 1), padding="valid")(concat14)
reshape15 = Reshape((input_shape[0] * input_shape[1], classes))(conv15)
act15 = Activation("softmax")(reshape15)
model = Model(img_input, act15)
model.compile(optimizer=RMSprop(learning_rate=1e-4), loss='binary_crossentropy', metrics=['accuracy'])
model.summary()
return model
def unet(pretrained_weights=None, input_size=(512, 512, 1)):
inputs = Input(input_size)
conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(inputs)
conv1 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv1)
pool1 = MaxPooling2D(pool_size=(2, 2))(conv1)
conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool1)
conv2 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv2)
pool2 = MaxPooling2D(pool_size=(2, 2))(conv2)
conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool2)
conv3 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv3)
pool3 = MaxPooling2D(pool_size=(2, 2))(conv3)
conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool3)
conv4 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv4)
drop4 = Dropout(0.5)(conv4)
pool4 = MaxPooling2D(pool_size=(2, 2))(drop4)
conv5 = Conv2D(1024, 3, activation='relu', padding='same', kernel_initializer='he_normal')(pool4)
conv5 = Conv2D(1024, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv5)
drop5 = Dropout(0.5)(conv5)
up6 = Conv2D(512, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(drop5))
merge6 = concatenate([drop4, up6], axis=3)
conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge6)
conv6 = Conv2D(512, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv6)
up7 = Conv2D(256, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(conv6))
merge7 = concatenate([conv3, up7], axis=3)
conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge7)
conv7 = Conv2D(256, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv7)
up8 = Conv2D(128, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(conv7))
merge8 = concatenate([conv2, up8], axis=3)
conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge8)
conv8 = Conv2D(128, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv8)
up9 = Conv2D(64, 2, activation='relu', padding='same', kernel_initializer='he_normal')(
UpSampling2D(size=(2, 2))(conv8))
merge9 = concatenate([conv1, up9], axis=3)
conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(merge9)
conv9 = Conv2D(64, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9)
conv9 = Conv2D(2, 3, activation='relu', padding='same', kernel_initializer='he_normal')(conv9)
conv10 = Conv2D(1, 1, activation='sigmoid')(conv9)
m = Model(inputs=inputs, outputs=conv10)
m.compile(optimizer=SGD(lr=0.015), loss='binary_crossentropy', metrics=['accuracy'])
# model.summary()
if pretrained_weights:
m.load_weights(pretrained_weights)
return m
def VGG16(include_top=True, weights='imagenet', input_shape=(512, 512, 3)):
img_input = layers.Input(shape=input_shape)
# Block 1
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1')(img_input)
x = layers.Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)
# Block 2
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1')(x)
x = layers.Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)
# Block 3
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2')(x)
x = layers.Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)
# Block 4
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)
# Block 5
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2')(x)
x = layers.Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3')(x)
x = layers.MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool')(x)
if include_top:
x = layers.Flatten(name='flatten')(x)
x = layers.Dense(4096, activation='relu', name='fc1')(x)
x = layers.Dense(4096, activation='relu', name='fc2')(x)
x = layers.Dense(1000, activation='softmax', name='predictions')(x)
else:
x = layers.GlobalAveragePooling2D(name='toplayerGAP')(x)
x = layers.Dense(1, activation='sigmoid', name='toplayerDENSE')(x)
inputs = img_input
model = models.Model(inputs, x, name='vgg16')
# Load weights.
if weights == 'imagenet':
if include_top:
weights_path = "/Users/alex/Downloads/vgg16_weights_tf_dim_ordering_tf_kernels.h5"
else:
weights_path = "/Users/alex/Downloads/vgg16_weights_tf_dim_ordering_tf_kernels_notop.h5"
model.load_weights(weights_path)
return model