Repository navigation
Expand file tree
/
Copy pathmodel.py
More file actions
168 lines (156 loc) · 9.05 KB
/
Copy pathmodel.py
File metadata and controls
168 lines (156 loc) · 9.05 KB
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
import os
import sys
# Silence unwanted warnings
stderr = sys.stderr
sys.stderr = open(os.devnull, 'w')
sys.stderr = stderr
import keras.backend as K
import tensorflow as tf
from keras.layers import Input, Conv2D, BatchNormalization, UpSampling2D
from keras.models import Model
from keras.regularizers import l2
from keras.utils import plot_model
from config import img_rows, img_cols, num_colors, kernel_size
from config import layer_init as kernel_init
def build_model():
tf.logging.set_verbosity(tf.logging.ERROR)
# Initialize l2 regulator from keras
l2_reg = l2(1e-3)
# Input image of specified shape (black and white)
# Input data is a sizexsize tensor containing l values
input_tensor = Input(shape=(img_rows, img_cols, 1))
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 1 ----------------------------------
# ----------------------------------------------------------------------------
# 64 (output channels), 3x3 kernel
x = Conv2D(64, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv1_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(input_tensor)
# Spacial resolution of output = 224
x = Conv2D(64, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv1_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(2, 2))(x)
# Spacial resolution of output = 112
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 2 ----------------------------------
# ----------------------------------------------------------------------------
x = Conv2D(128, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv2_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 112
x = Conv2D(128, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv2_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(2, 2))(x)
# Spacial resolution of output = 56
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 3 ----------------------------------
# ----------------------------------------------------------------------------
x = Conv2D(256, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv3_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 56
x = Conv2D(256, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv3_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 56
x = Conv2D(256, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv3_3', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(2, 2))(x)
# Spacial resolution of output = 28
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 4 ----------------------------------
# ----------------------------------------------------------------------------
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv4_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv4_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv4_3', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 5 ----------------------------------
# ----------------------------------------------------------------------------
# Notice dilated convolution
# Dilated convolution is a basic convolution only applied to the input volume with defined gaps
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=2, name='conv5_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=2, name='conv5_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=2, name='conv5_3', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 6 ----------------------------------
# ----------------------------------------------------------------------------
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=2, name='conv6_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=2, name='conv6_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(512, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=2, name='conv6_3', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 7 ----------------------------------
# ----------------------------------------------------------------------------
# No more dilation
x = Conv2D(256, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv7_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(256, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv7_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = Conv2D(256, (kernel_size, kernel_size), activation='relu', padding='same',
name='conv7_3', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
# Spacial resolution of output = 28
x = BatchNormalization()(x)
# ----------------------------------------------------------------------------
# ---------------------------------- Conv 8 ----------------------------------
# ----------------------------------------------------------------------------
# UpSample before convolution
x = UpSampling2D(size=(2, 2))(x)
# Spacial resolution of output = 56
x = Conv2D(128, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=1, name='conv8_1', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
x = Conv2D(128, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=1, name='conv8_2', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
x = Conv2D(128, (kernel_size, kernel_size), activation='relu', padding='same',
dilation_rate=1, name='conv8_3', kernel_initializer=kernel_init,
kernel_regularizer=l2_reg, strides=(1, 1))(x)
outputs = Conv2D(num_colors, (1, 1), activation='softmax', padding='same',
dilation_rate=1, name='conv8_313')(x)
model = Model(inputs=input_tensor, outputs=outputs, name="ColorNet")
return model
def main():
encoder_decoder = build_model()
# Print specifics
print(encoder_decoder.summary())
plot_model(encoder_decoder, to_file='encoder_decoder.svg', show_layer_names=True, show_shapes=True)
K.clear_session()
if __name__ == '__main__':
main()