-
Notifications
You must be signed in to change notification settings - Fork 2
/
subpixel_conv2d.py
58 lines (51 loc) · 2.37 KB
/
subpixel_conv2d.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
from tensorflow.keras.layers import Layer
import tensorflow as tf
#from keras.utils.generic_utils import get_custom_objects
class SubpixelConv2D(Layer):
""" Subpixel Conv2D Layer
upsampling a layer from (h, w, c) to (h*r, w*r, c/(r*r)),
where r is the scaling factor, default to 4
# Arguments
upsampling_factor: the scaling factor
# Input shape
Arbitrary. Use the keyword argument `input_shape`
(tuple of integers, does not include the samples axis)
when using this layer as the first layer in a model.
# Output shape
the second and the third dimension increased by a factor of
`upsampling_factor`; the last layer decreased by a factor of
`upsampling_factor^2`.
# References
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
"""
def __init__(self, upsampling_factor=2, **kwargs):
super(SubpixelConv2D, self).__init__(**kwargs)
self.upsampling_factor = upsampling_factor
def build(self, input_shape):
last_dim = input_shape[-1]
factor = self.upsampling_factor * self.upsampling_factor
if last_dim % (factor) != 0:
raise ValueError('Channel ' + str(last_dim) + ' should be of '
'integer times of upsampling_factor^2: ' +
str(factor) + '.')
def call(self, inputs, **kwargs):
return tf.nn.depth_to_space( inputs, self.upsampling_factor )
def get_config(self):
config = { 'upsampling_factor': self.upsampling_factor, }
base_config = super(SubpixelConv2D, self).get_config()
return dict(list(base_config.items()) + list(config.items()))
def compute_output_shape(self, input_shape):
factor = self.upsampling_factor * self.upsampling_factor
input_shape_1 = None
if input_shape[1] is not None:
input_shape_1 = input_shape[1] * self.upsampling_factor
input_shape_2 = None
if input_shape[2] is not None:
input_shape_2 = input_shape[2] * self.upsampling_factor
dims = [ input_shape[0],
input_shape_1,
input_shape_2,
int(input_shape[3]/factor)
]
return tuple( dims )