-
Notifications
You must be signed in to change notification settings - Fork 4
/
net.py
215 lines (182 loc) · 7.83 KB
/
net.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
from collections import OrderedDict
import numpy as np
import tensorflow as tf
import utils.tf_utils as tfu
class Net:
def __init__(self, num_basis=90, ksz=15, burst_length=2):
self.weights = {}
self.activations = OrderedDict()
self.num_basis = num_basis
self.ksz = ksz
self.burst_length = burst_length
def conv(
self, name, inp, outch, ksz=3,
stride=1, relu=True, pad='SAME', activation_name=None):
'''Wrapper of conv'''
inch = inp.get_shape().as_list()[-1]
ksz = [ksz, ksz, inch, outch]
wnm = name + "_w"
if wnm in self.weights.keys():
w = self.weights[wnm]
else:
sq = np.sqrt(3.0 / np.float32(ksz[0] * ksz[1] * ksz[2]))
w = tf.Variable(tf.random_uniform(
ksz, minval=-sq, maxval=sq, dtype=tf.float32))
self.weights[wnm] = w
out = tf.nn.conv2d(inp, w, [1, stride, stride, 1], pad)
bnm = name + "_b"
if bnm in self.weights.keys():
b = self.weights[bnm]
else:
b = tf.Variable(tf.constant(0, shape=[ksz[-1]], dtype=tf.float32))
self.weights[bnm] = b
out = out + b
if relu:
out = tf.nn.relu(out)
if activation_name is not None:
self.activations[activation_name] = out
return out
def down_block(self, out, nch, pfx=''):
'''
Downsampling block, including two conv layers w/ one maxpooling layer
Args:
out: output from previous layer
nch: number of channels for the block
pfx: prefix of names for layers in this block
Return:
down: output of the block after downsampling
out: output of the block right before downsampling
'''
out = self.conv(pfx + '_1', out, nch, ksz=3, stride=1)
out = self.conv(pfx + '_2', out, nch, ksz=3, stride=1)
down = tf.nn.max_pool(out, [1, 2, 2, 1], [1, 2, 2, 1], 'SAME')
self.activations[pfx] = down
self.activations['skip_' + pfx] = out
return down, out
def up_block(self, out, nch, skip, pfx=''):
'''
Upsampling block, including:
one upsampling with bilinear resizing,
one conv layer,
skip connection
two more conv layers
Args:
out: output from previous layer
skip: output from the layer that is skip connected to this block
nch: number of channels for the block
pfx: prefix of names for layers in this block
Return:
out: output of this block
'''
out = tf.image.resize_bilinear(out, 2 * tf.shape(out)[1:3])
out = self.conv(pfx + '_1', out, nch, ksz=3, stride=1)
out = tf.concat([out, skip], axis=-1)
out = self.conv(pfx + '_2', out, nch, ksz=3, stride=1)
out = self.conv(pfx + '_3', out, nch, ksz=3,
stride=1, activation_name=pfx)
return out
def kernel_up_block(self, out, nch, skip, pfx=''):
'''
Upsampling block, including:
one upsampling with bilinear resizing,
one conv layer,
skip connection (with gloal average pooling)
two more conv layers
Args:
out: output from previous layer
skip: output from the layer that is skip connected to this block
nch: number of channels for the block
pfx: prefix of names for layers in this block
Return:
out: output of this block
'''
shape = tf.shape(out)
out = tf.image.resize_bilinear(out, 2 * shape[1:3])
out = self.conv(pfx + '_1', out, nch, ksz=3, stride=1)
# resize the skip connection
skip = tf.reduce_mean(skip, axis=[1, 2], keepdims=True)
skip = tf.tile(skip, [1, 2 * shape[1], 2 * shape[2], 1])
out = tf.concat([out, skip], axis=-1)
out = self.conv(pfx + '_2', out, nch, ksz=3, stride=1)
out = self.conv(pfx + '_3', out, nch, ksz=3,
stride=1, activation_name=pfx)
return out
def encode(self, out, pfx=''):
out = self.conv(pfx + 'inp', out, 64)
out, d1 = self.down_block(out, 64, pfx + 'down1')
out, d2 = self.down_block(out, 128, pfx + 'down2')
out, d3 = self.down_block(out, 256, pfx + 'down3')
out, d4 = self.down_block(out, 512, pfx + 'down4')
out, d5 = self.down_block(out, 1024, pfx + 'down5')
out = self.conv(pfx + 'bottleneck_1', out, 1024)
out = self.conv(pfx + 'bottleneck_2', out, 1024,
activation_name=pfx + 'bottleneck')
return out, [d1, d2, d3, d4, d5]
def decode(self, out, skips, pfx=''):
d1, d2, d3, d4, d5 = skips
out = self.up_block(out, 512, d5, pfx + 'up1')
out = self.up_block(out, 256, d4, pfx + 'up2')
out = self.up_block(out, 128, d3, pfx + 'up3')
out = self.up_block(out, 64, d2, pfx + 'up4')
out = self.up_block(out, 64, d1, pfx + 'up5')
out = self.conv(pfx + 'end_1', out, 64)
out = self.conv(pfx + 'end_2', out, 64, activation_name=pfx + 'end')
return out
def create_basis(self):
'''Predict image-specific basis'''
assert self.ksz == 15
bottleneck = self.activations['bottleneck']
out = tf.reduce_mean(bottleneck, axis=[1, 2], keepdims=True) # 1x1
out = self.kernel_up_block(
out, 512, self.activations['skip_down5'], 'k_up1') # 2x2
out = self.kernel_up_block(
out, 256, self.activations['skip_down4'], 'k_up2') # 4x4
out = self.kernel_up_block(
out, 256, self.activations['skip_down3'], 'k_up3') # 8x8
out = self.kernel_up_block(
out, 128, self.activations['skip_down2'], 'k_up4') # 16x16
out = self.conv('k_conv', out, 128, ksz=2, stride=1, pad='VALID')
out = self.conv('k_output_1', out, 128)
out = self.conv('k_output_2', out, 3 * 2 * self.num_basis, relu=False)
out = tf.reshape(
out, [-1, self.ksz * self.ksz * 3 * 2, self.num_basis])
self.basis = tf.transpose(out, [0, 2, 1])
def predict_coeff(self, inp):
'''Predict per-pixel coefficient vector given the input'''
self.imsp = tf.shape(inp)
out, skips = self.encode(inp)
out = self.decode(out, skips)
out = self.conv('output', out, self.num_basis + 3, relu=False)
self.coeffs_pre_soft = out
self.coeffs = out[..., :self.num_basis]
self.scale = out[..., -3:]
self.activations['output'] = self.coeffs
def combine(self):
'''Combine coeffs and basis to get a per-pixel kernel'''
imsp = self.imsp
coeffs = tf.reshape(
self.coeffs, [-1, imsp[1] * imsp[2], self.num_basis])
self.kernels = tf.matmul(
coeffs,
self.basis
) # (h * w) x (ksz * ksz * 3 * 2)
self.kernels = tf.reshape(
self.kernels, [-1, imsp[1], imsp[2], self.ksz * self.ksz * 3, 2])
self.activations['decoding'] = self.kernels
def forward(self, inp):
self.predict_coeff(inp)
self.create_basis()
self.combine()
filtered_ambient = tfu.apply_filtering(
inp[:, :, :, :3], self.kernels[..., 0])
# "Bilinearly upsample kernels + filtering"
# is equivalent to
# "filter the image with a bilinear kernel + dilated filter the image
# with the original kernel".
# This will save more memory.
smoothed_ambient = tfu.bilinear_filter(inp[:, :, :, :3], ksz=7)
smoothed_ambient = tfu.apply_dilated_filtering(
smoothed_ambient, self.kernels[..., 1], dilation=4)
filtered_ambient = filtered_ambient + smoothed_ambient
denoised = filtered_ambient * self.scale
return denoised