-
Notifications
You must be signed in to change notification settings - Fork 36
/
model.py
67 lines (53 loc) · 2.41 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
# see:
# https://github.com/preddy5/segnet/blob/master/segnet.py
# https://github.com/0bserver07/Keras-SegNet-Basic/blob/master/SegNet-Basic.py
# https://github.com/imlab-uiip/keras-segnet/blob/master/build_model.py
from keras.models import Sequential
from keras.layers import Layer, Conv2D, BatchNormalization, Activation, MaxPooling2D, Reshape, Permute, UpSampling2D, ZeroPadding2D
from keras import backend as K
K.set_image_data_format("channels_first")
def model(img_channels=3, img_width=256, img_height=256, classes=7):
"""define a basic segnet model."""
model = Sequential()
# encoder
model.add(ZeroPadding2D(padding=1, input_shape=(img_channels, img_height, img_width)))
model.add(Conv2D(filters=64, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=2))
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=128, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=2))
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=256, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(Activation("relu"))
model.add(MaxPooling2D(pool_size=2))
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=512, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(Activation("relu"))
# decoder
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=512, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(UpSampling2D(size=2))
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=256, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(UpSampling2D(size=2))
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=128, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
model.add(UpSampling2D(size=2))
model.add(ZeroPadding2D(padding=1))
model.add(Conv2D(filters=64, kernel_size=3, padding="valid"))
model.add(BatchNormalization())
# output layer
model.add(Conv2D(filters=classes, kernel_size=1, padding="valid"))
model.add(Reshape((classes, img_width*img_height), input_shape=(classes, img_height, img_width)))
model.add(Permute((2, 1)))
model.add(Activation("softmax"))
return model