-
Notifications
You must be signed in to change notification settings - Fork 269
/
advanced.py
345 lines (261 loc) · 10.8 KB
/
advanced.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
import itertools
from keras.layers import Activation, Reshape, Lambda, concatenate, dot, add
from keras.layers import Conv1D, Conv2D, Conv3D
from keras.layers import MaxPool1D
from keras.callbacks import Callback, TensorBoard
from keras.engine.topology import Layer
from keras import backend as K
''' Callbacks '''
class HistoryCheckpoint(Callback):
'''Callback that records events
into a `History` object.
It then saves the history after each epoch into a file.
To read the file into a python dict:
history = {}
with open(filename, "r") as f:
history = eval(f.read())
This may be unsafe since eval() will evaluate any string
A safer alternative:
import ast
history = {}
with open(filename, "r") as f:
history = ast.literal_eval(f.read())
'''
def __init__(self, filename):
super(Callback, self).__init__()
self.filename = filename
def on_train_begin(self, logs={}):
self.epoch = []
self.history = {}
def on_epoch_end(self, epoch, logs={}):
self.epoch.append(epoch)
for k, v in logs.items():
if k not in self.history:
self.history[k] = []
self.history[k].append(v)
with open(self.filename, "w") as f:
f.write(str(self.history))
'''
Below is a modification to the TensorBoard callback to perform
batchwise writing to the tensorboard, instead of only at the end
of the batch.
'''
class TensorBoardBatch(TensorBoard):
def __init__(self, log_dir='./logs',
histogram_freq=0,
batch_size=32,
write_graph=True,
write_grads=False,
write_images=False,
embeddings_freq=0,
embeddings_layer_names=None,
embeddings_metadata=None):
super(TensorBoardBatch, self).__init__(log_dir,
histogram_freq=histogram_freq,
batch_size=batch_size,
write_graph=write_graph,
write_grads=write_grads,
write_images=write_images,
embeddings_freq=embeddings_freq,
embeddings_layer_names=embeddings_layer_names,
embeddings_metadata=embeddings_metadata)
# conditionally import tensorflow iff TensorBoardBatch is created
self.tf = __import__('tensorflow')
self.global_step = 1
def on_batch_end(self, batch, logs=None):
logs = logs or {}
for name, value in logs.items():
if name in ['batch', 'size']:
continue
summary = self.tf.Summary()
summary_value = summary.value.add()
summary_value.simple_value = value.item()
summary_value.tag = name
self.writer.add_summary(summary, self.global_step)
self.global_step += 1
self.writer.flush()
def on_epoch_end(self, epoch, logs=None):
logs = logs or {}
for name, value in logs.items():
if name in ['batch', 'size']:
continue
summary = self.tf.Summary()
summary_value = summary.value.add()
summary_value.simple_value = value.item()
summary_value.tag = name
self.writer.add_summary(summary, self.global_step)
self.global_step += 1
self.writer.flush()
''' Theano Backend function '''
def depth_to_scale(x, scale, output_shape, dim_ordering=K.image_dim_ordering(), name=None):
''' Uses phase shift algorithm [1] to convert channels/depth for spacial resolution '''
import theano.tensor as T
scale = int(scale)
if dim_ordering == "tf":
x = x.transpose((0, 3, 1, 2))
out_row, out_col, out_channels = output_shape
else:
out_channels, out_row, out_col = output_shape
b, k, r, c = x.shape
out_b, out_k, out_r, out_c = b, k // (scale * scale), r * scale, c * scale
out = K.reshape(x, (out_b, out_k, out_r, out_c))
for channel in range(out_channels):
channel += 1
for i in range(out_row):
for j in range(out_col):
a = i // scale #T.floor(i / scale).astype('int32')
b = j // scale #T.floor(j / scale).astype('int32')
d = channel * scale * (j % scale) + channel * (i % scale)
T.set_subtensor(out[:, channel - 1, i, j], x[:, d, a, b], inplace=True)
if dim_ordering == 'tf':
out = out.transpose((0, 2, 3, 1))
return out
''' Theano Backend function '''
def depth_to_scale_th(input, scale, channels):
''' Uses phase shift algorithm [1] to convert channels/depth for spacial resolution '''
import theano.tensor as T
b, k, row, col = input.shape
output_shape = (b, channels, row * scale, col * scale)
out = T.zeros(output_shape)
r = scale
for y, x in itertools.product(range(scale), repeat=2):
out = T.inc_subtensor(out[:, :, y::r, x::r], input[:, r * y + x :: r * r, :, :])
return out
''' Tensorflow Backend Function '''
def depth_to_scale_tf(input, scale, channels):
try:
import tensorflow as tf
except ImportError:
print("Could not import Tensorflow for depth_to_scale operation. Please install Tensorflow or switch to Theano backend")
exit()
def _phase_shift(I, r):
''' Function copied as is from https://github.com/Tetrachrome/subpixel/blob/master/subpixel.py'''
bsize, a, b, c = I.get_shape().as_list()
bsize = tf.shape(I)[0] # Handling Dimension(None) type for undefined batch dim
X = tf.reshape(I, (bsize, a, b, r, r))
X = tf.transpose(X, (0, 1, 2, 4, 3)) # bsize, a, b, 1, 1
X = tf.split(1, a, X) # a, [bsize, b, r, r]
X = tf.concat(2, [tf.squeeze(x) for x in X]) # bsize, b, a*r, r
X = tf.split(1, b, X) # b, [bsize, a*r, r]
X = tf.concat(2, [tf.squeeze(x) for x in X]) # bsize, a*r, b*r
return tf.reshape(X, (bsize, a * r, b * r, 1))
if channels > 1:
Xc = tf.split(3, 3, input)
X = tf.concat(3, [_phase_shift(x, scale) for x in Xc])
else:
X = _phase_shift(input, scale)
return X
'''
Implementation is incomplete. Use lambda layer for now.
'''
class SubPixelUpscaling(Layer):
def __init__(self, r, channels, **kwargs):
super(SubPixelUpscaling, self).__init__(**kwargs)
self.r = r
self.channels = channels
def build(self, input_shape):
pass
def call(self, x, mask=None):
if K.backend() == "theano":
y = depth_to_scale_th(x, self.r, self.channels)
else:
y = depth_to_scale_tf(x, self.r, self.channels)
return y
def get_output_shape_for(self, input_shape):
if K.image_dim_ordering() == "th":
b, k, r, c = input_shape
return (b, self.channels, r * self.r, c * self.r)
else:
b, r, c, k = input_shape
return (b, r * self.r, c * self.r, self.channels)
''' Non Local Blocks '''
def non_local_block(ip, computation_compression=2, mode='embedded'):
channel_dim = 1 if K.image_data_format() == 'channels_first' else -1
ip_shape = K.int_shape(ip)
if mode not in ['gaussian', 'embedded', 'dot', 'concatenate']:
raise ValueError('`mode` must be one of `gaussian`, `embedded`, `dot` or `concatenate`')
dim1, dim2, dim3 = None, None, None
if len(ip_shape) == 3: # time series data
rank = 3
batchsize, dim1, channels = ip_shape
elif len(ip_shape) == 4: # image data
rank = 4
if channel_dim == 1:
batchsize, channels, dim1, dim2 = ip_shape
else:
batchsize, dim1, dim2, channels = ip_shape
elif len(ip_shape) == 5: # Video / Voxel data
rank = 5
if channel_dim == 1:
batchsize, channels, dim1, dim2, dim3 = ip_shape
else:
batchsize, dim1, dim2, dim3, channels = ip_shape
else:
raise ValueError('Input dimension has to be either 3 (temporal), 4 (spatial) or 5 (spatio-temporal)')
if mode == 'gaussian': # Gaussian instantiation
x1 = Reshape((-1, channels))(ip) # xi
x2 = Reshape((-1, channels))(ip) # xj
f = dot([x1, x2], axes=2)
f = Activation('softmax')(f)
elif mode == 'dot': # Dot instantiation
# theta path
theta = _convND(ip, rank, channels // 2)
theta = Reshape((-1, channels // 2))(theta)
# phi path
phi = _convND(ip, rank, channels // 2)
phi = Reshape((-1, channels // 2))(phi)
f = dot([theta, phi], axes=2)
# scale the values to make it size invariant
if batchsize is not None:
f = Lambda(lambda z: 1./ batchsize * z)(f)
else:
f = Lambda(lambda z: 1. / 128 * z)(f)
elif mode == 'concatenate': # Concatenation instantiation
raise NotImplemented('Concatenation mode has not been implemented yet')
else: # Embedded Gaussian instantiation
# theta path
theta = _convND(ip, rank, channels // 2)
theta = Reshape((-1, channels // 2))(theta)
# phi path
phi = _convND(ip, rank, channels // 2)
phi = Reshape((-1, channels // 2))(phi)
if computation_compression > 1:
# shielded computation
phi = MaxPool1D(computation_compression)(phi)
f = dot([theta, phi], axes=2)
f = Activation('softmax')(f)
# g path
g = _convND(ip, rank, channels // 2)
g = Reshape((-1, channels // 2))(g)
if computation_compression > 1 and mode == 'embedded':
# shielded computation
g = MaxPool1D(computation_compression)(g)
# compute output path
y = dot([f, g], axes=[2, 1])
# reshape to input tensor format
if rank == 3:
y = Reshape((dim1, channels // 2))(y)
elif rank == 4:
if channel_dim == -1:
y = Reshape((dim1, dim2, channels // 2))(y)
else:
y = Reshape((channels // 2, dim1, dim2))(y)
else:
if channel_dim == -1:
y = Reshape((dim1, dim2, dim3, channels // 2))(y)
else:
y = Reshape((channels // 2, dim1, dim2, dim3))(y)
# project filters
y = _convND(y, rank, channels)
# residual connection
residual = add([ip, y])
return residual
def _convND(ip, rank, channels):
assert rank in [3, 4, 5], "Rank of input must be 3, 4 or 5"
if rank == 3:
x = Conv1D(channels, 1, padding='same', use_bias=False)(ip)
elif rank == 4:
x = Conv2D(channels, (1, 1), padding='same', use_bias=False)(ip)
else:
x = Conv3D(channels, (1, 1, 1), padding='same', use_bias=False)(ip)
return x