-
Notifications
You must be signed in to change notification settings - Fork 5
/
model.py
277 lines (217 loc) · 8.31 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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# -*- coding: utf-8 -*-
"""
Created on Sun Jun 2 00:39:56 2019
@author: Wei-Hsiang, Shen
"""
import tensorflow as tf
from tensorflow.keras import layers
class IdentityBlock(object):
"""
Identity block in ResNet
No conv layer at shortcut (skip connection)
"""
def __init__(self, num_feature):
self.num_feature = num_feature
def __call__(self, x):
shortcut = x
x = layers.Conv2D(self.num_feature, kernel_size=(1,1), use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
x = layers.Conv2D(self.num_feature, kernel_size=(3,3), use_bias=False, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
x = layers.Conv2D(self.num_feature, kernel_size=(1,1), use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.add([x, shortcut])
x = layers.ReLU()(x)
return x
class ConvBlock(object):
"""
Convolution block in ResNet
Contain conv layer at shortcut (projection connection)
"""
def __init__(self, num_feature_in, num_feature_out, strides=(1,1)):
self.num_feature_in = num_feature_in
self.num_feature_out = num_feature_out
self.strides = strides
def __call__(self, x):
shortcut = x
x = layers.Conv2D(self.num_feature_in, kernel_size=(1,1), use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
x = layers.Conv2D(self.num_feature_in, kernel_size=(3,3), use_bias=False, strides=self.strides, padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
x = layers.Conv2D(self.num_feature_out, kernel_size=(1,1), use_bias=False)(x)
x = layers.BatchNormalization()(x)
# projection connection with 1x1 convolution layer and the same strides
shortcut = layers.Conv2D(self.num_feature_out, kernel_size=(1, 1), use_bias=False, strides=self.strides)(shortcut)
shortcut = layers.BatchNormalization()(shortcut)
x = layers.add([x, shortcut])
x = layers.ReLU()(x)
return x
class Conv_Batch_LReLu(object):
def __init__(self, num_features, strides=(1,1)):
self.num_features = num_features
self.strides = strides
def __call__(self, x):
x = layers.Conv2D(self.num_features, kernel_size=(3,3), strides=self.strides, padding='same', use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
return x
class Conv_Batch_ReLu(object):
def __init__(self, num_features, strides=(1,1)):
self.num_features = num_features
self.strides = strides
def __call__(self, x):
x = layers.Conv2D(self.num_features, kernel_size=(3,3), strides=self.strides, padding='same', use_bias=False)(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
return x
def Low_res_colorizer_new():
"""
Input: Gray-scale image in full resolution
Output: Colorized image in low resolution
"""
inputs = tf.keras.Input(shape=(256, 256, 1))
x = inputs
# Scaling down
x = Conv_Batch_ReLu(64)(x)
x = ConvBlock(64, 128, (2,2))(x)
x = IdentityBlock(128)(x)
x = IdentityBlock(128)(x)
x = ConvBlock(128, 256, (2,2))(x)
x = IdentityBlock(256)(x)
x = IdentityBlock(256)(x)
x = ConvBlock(256, 512, (2,2))(x)
x = IdentityBlock(512)(x)
x = IdentityBlock(512)(x)
# Colorization network
x = ConvBlock(512, 256)(x)
x = IdentityBlock(256)(x)
x = ConvBlock(256, 128)(x)
x = IdentityBlock(128)(x)
x = ConvBlock(128, 64)(x)
x = IdentityBlock(64)(x)
x = ConvBlock(64, 32)(x)
x = IdentityBlock(32)(x)
x = layers.Conv2D(3, (3,3), padding='same', activation='relu')(x)
outputs = x
model = tf.keras.Model(inputs=[inputs], outputs=[outputs], name='low_res_colorizer')
return model
def Colorizer():
"""
Input: Gray-scale image in full resolution
Output: Colorized image in full resolution
"""
inputs = tf.keras.Input(shape=(256, 256, 1))
x = inputs
# Low-level features network
x = Conv_Batch_LReLu(64)(x)
x = ConvBlock(64, 128, (2,2))(x)
x = Conv_Batch_LReLu(64, (2,2))(x)
x = Conv_Batch_LReLu(128)(x)
x = Conv_Batch_LReLu(128, (2,2))(x)
x = Conv_Batch_LReLu(256)(x)
x = Conv_Batch_LReLu(256, (2,2))(x)
x = Conv_Batch_LReLu(512)(x)
# Mid-Level features network
x = Conv_Batch_LReLu(512)(x)
x = Conv_Batch_LReLu(256)(x)
# Colorization network
x = Conv_Batch_LReLu(128)(x)
x = layers.UpSampling2D(size=(2, 2))(x)
x = Conv_Batch_LReLu(64)(x)
x = Conv_Batch_LReLu(64)(x)
x = layers.UpSampling2D(size=(2, 2))(x)
x = Conv_Batch_LReLu(32)(x)
x = Conv_Batch_LReLu(32)(x)
x = layers.UpSampling2D(size=(2, 2))(x)
x = Conv_Batch_LReLu(16)(x)
x = Conv_Batch_LReLu(3)(x)
outputs = x
model = tf.keras.Model(inputs=[inputs], outputs=[outputs], name='low_res_colorizer')
return model
def Low_res_colorizer():
"""
Input: Gray-scale image in full resolution
Output: Colorized image in low resolution
"""
inputs = tf.keras.Input(shape=(256, 256, 1))
# Low-level features network
x = inputs
x = Conv_Batch_ReLu(64, (2,2))(x)
x = Conv_Batch_ReLu(128)(x)
x = Conv_Batch_ReLu(128, (2,2))(x)
x = Conv_Batch_ReLu(256)(x)
x = Conv_Batch_ReLu(256, (2,2))(x)
x = Conv_Batch_ReLu(512)(x)
# Colorization network
x = Conv_Batch_ReLu(512)(x)
x = Conv_Batch_ReLu(256)(x)
x = Conv_Batch_ReLu(128)(x)
x = Conv_Batch_ReLu(64)(x)
x = Conv_Batch_ReLu(32)(x)
x = layers.Conv2D(3, (3,3), padding='same', activation='relu')(x)
outputs = x
model = tf.keras.Model(inputs=[inputs], outputs=[outputs], name='low_res_colorizer')
return model
def Polishing_network():
"""
Input: Gray-scale image in full resolution (256,256,1)
Low-resolution color image in full resolution (32,32,3)
Output: Colorized image in full resolution (256,256,3)
"""
inputs_gray = tf.keras.Input(shape=(256, 256, 1))
inputs_color = tf.keras.Input(shape=(32, 32, 3))
inputs_color_up = tf.image.resize(inputs_color, size=(256,256))
x = tf.concat([inputs_gray, inputs_color_up], axis=-1)
# encoder
for num_filters in list([64, 128, 256, 512]):
x = layers.Conv2D(num_filters, kernel_size=(3,3), strides=(2,2), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
for _ in range(5):
x = layers.Conv2D(512, kernel_size=(3,3), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.LeakyReLU()(x)
# decoder
for num_filters in list([512, 256, 128, 64]):
x = layers.UpSampling2D(size=(2,2))(x)
x = layers.Conv2D(num_filters, kernel_size=(3,3), padding='same')(x)
x = layers.BatchNormalization()(x)
x = layers.ReLU()(x)
x = layers.Conv2D(3, kernel_size=(3,3), padding='same')(x)
x = layers.ReLU()(x)
outputs = x
model = tf.keras.Model(inputs=[inputs_gray, inputs_color], outputs=[outputs], name='polishing_network')
return model
def Polishing_network_small():
"""
Input: Gray-scale image in full resolution (256,256,1)
Low-resolution color image in full resolution (32,32,3)
Output: Colorized image in full resolution (256,256,3)
"""
inputs_gray = tf.keras.Input(shape=(256, 256, 1))
inputs_color = tf.keras.Input(shape=(32, 32, 3))
inputs_color_up = tf.image.resize(inputs_color, size=(256,256))
x = tf.concat([inputs_gray, inputs_color_up], axis=-1)
# encoder
for _features in list([64, 128, 256]):
x = Conv_Batch_LReLu(64, (2,2))(x)
x = Conv_Batch_LReLu(_features*2)(x)
for _ in range(5):
x = Conv_Batch_LReLu(512)(x)
# decoder
for _features in list([256, 128]):
x = layers.UpSampling2D(size=(2,2))(x)
x = Conv_Batch_ReLu(_features*2)(x)
x = Conv_Batch_ReLu(_features)(x)
x = layers.UpSampling2D(size=(2,2))(x)
x = tf.concat([x, inputs_gray], axis=-1)
x = Conv_Batch_ReLu(128)(x)
x = Conv_Batch_ReLu(64)(x)
x = layers.Conv2D(3, kernel_size=(3,3), padding='same', activation='relu')(x)
outputs = x
model = tf.keras.Model(inputs=[inputs_gray, inputs_color], outputs=[outputs], name='polishing_network')
return model