-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathadvanced.py
More file actions
199 lines (143 loc) · 5.8 KB
/
advanced.py
File metadata and controls
199 lines (143 loc) · 5.8 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
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
import itertools
import tensorflow as tf
from keras.layers import Lambda
from keras.callbacks import Callback
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))
''' 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)
#def SubpixelConv2D(input_shape, scale=4):
def SubpixelConv2D(input_shape, scale=4):
"""
Keras layer to do subpixel convolution.
NOTE: Tensorflow backend only. Uses tf.depth_to_space
Ref:
[1] Real-Time Single Image and Video Super-Resolution Using an Efficient Sub-Pixel Convolutional Neural Network
Shi et Al.
https://arxiv.org/abs/1609.05158
:param input_shape: tensor shape, (batch, height, width, channel)
:param scale: upsampling scale. Default=4
:return:
"""
# upsample using depth_to_space
def subpixel_shape(input_shape):
#input_shape=x.input_shape
dims = [input_shape[0],
input_shape[1] * scale,
input_shape[2] * scale,
int(input_shape[3] / (scale ** 2))]
output_shape = tuple(dims)
return output_shape
def subpixel(x):
return tf.depth_to_space(x, scale)
return Lambda(subpixel, output_shape=subpixel_shape, name='subpixel')