forked from divamgupta/image-segmentation-keras
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathVGGSegnet.py
98 lines (71 loc) · 4.32 KB
/
VGGSegnet.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
from keras.layers.convolutional import Conv2D, ZeroPadding2D, UpSampling2D
from keras.layers.core import Flatten, Dense, Reshape, Permute, Activation
from keras.layers.normalization import BatchNormalization
from keras.layers.pooling import MaxPooling2D
from keras.models import *
import os
file_path = os.path.dirname(os.path.abspath(__file__))
VGG_Weights_path = file_path + "/data/vgg16_weights_th_dim_ordering_th_kernels.h5"
def VGGSegnet(n_classes, input_height=416, input_width=608, vgg_level=3):
img_input = Input(shape=(3, input_height, input_width))
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv1', data_format='channels_first')(img_input)
x = Conv2D(64, (3, 3), activation='relu', padding='same', name='block1_conv2', data_format='channels_first')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool1', data_format='channels_first')(x)
f1 = x
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv1', data_format='channels_first')(x)
x = Conv2D(128, (3, 3), activation='relu', padding='same', name='block2_conv2', data_format='channels_first')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool', data_format='channels_first')(x)
f2 = x
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv1', data_format='channels_first')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv2', data_format='channels_first')(x)
x = Conv2D(256, (3, 3), activation='relu', padding='same', name='block3_conv3', data_format='channels_first')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool1', data_format='channels_first')(x)
f3 = x
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv1', data_format='channels_first')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv2', data_format='channels_first')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block4_conv3', data_format='channels_first')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool1', data_format='channels_first')(x)
f4 = x
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv1', data_format='channels_first')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv2', data_format='channels_first')(x)
x = Conv2D(512, (3, 3), activation='relu', padding='same', name='block5_conv3', data_format='channels_first')(x)
x = MaxPooling2D((2, 2), strides=(2, 2), name='block5_pool1', data_format='channels_first')(x)
f5 = x
x = Flatten(name='flatten')(x)
x = Dense(4096, activation='relu', name='fc1')(x)
x = Dense(4096, activation='relu', name='fc2')(x)
x = Dense(1000, activation='relu', name='predictions')(x)
vgg = Model(img_input, x)
vgg.load_weights(VGG_Weights_path)
levels = [f1, f2, f3, f4, f5]
o = levels[vgg_level]
o = ZeroPadding2D((1,1),data_format='channels_first')(o)
o = Conv2D(512,(3,3),padding='valid',data_format='channels_first')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2,2),data_format='channels_first')(o)
o = ZeroPadding2D((1,1),data_format='channels_first')(o)
o = Conv2D(256,(3,3),padding='valid',data_format='channels_first')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2,2),data_format='channels_first')(o)
o = ZeroPadding2D((1,1),data_format='channels_first')(o)
o = Conv2D(128,(3,3),padding='valid',data_format='channels_first')(o)
o = BatchNormalization()(o)
o = UpSampling2D((2, 2), data_format='channels_first')(o)
o = ZeroPadding2D((1, 1), data_format='channels_first')(o)
o = Conv2D(64, (3, 3), padding='valid', data_format='channels_first')(o)
o = BatchNormalization()(o)
o = Conv2D(n_classes,(3,3),padding='same',data_format='channels_first')(o)
o_shape = Model(img_input,o).output_shape
outputHeight = o_shape[2]
outputWidth = o_shape[3]
o = (Reshape((-1,outputHeight*outputWidth)))(o)
o = (Permute((2,1)))(o)
o = (Activation('softmax'))(o)
model = Model(img_input,o)
model.outputWidth = outputWidth
model.outputHeight = outputHeight
return model
if __name__ == '__main__':
m = VGGSegnet(101)
from keras.utils import plot_model
plot_model(m,show_shapes=True,to_file='model.png')