加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
EDSR_utils.py 3.16 KB
一键复制 编辑 原始数据 按行查看 历史
chris1992212 提交于 2018-10-11 15:00 . SR_crop
import tensorflow as tf
import tensorflow.contrib.slim as slim
"""
Creates a convolutional residual block
as defined in the paper. More on
this inside model.py
x: input to pass through the residual block
channels: number of channels to compute
stride: convolution stride
"""
def resBlock(x,channels=64,kernel_size=[3,3],scale=1):
tmp = slim.conv2d(x,channels,kernel_size,activation_fn=None)
tmp = tf.nn.relu(tmp)
tmp = slim.conv2d(tmp,channels,kernel_size,activation_fn=None)
tmp *= scale
return x + tmp
"""
Method to upscale an image using
conv2d transpose. Based on upscaling
method defined in the paper
x: input to be upscaled
scale: scale increase of upsample
features: number of features to compute
activation: activation function
"""
def upsample(x,channels=2,scale=2,features=64,activation=tf.nn.relu):
assert scale in [1,2,3,4]
x = slim.conv2d(x,features,[3,3],activation_fn=activation)
if scale == 1:
x = slim.conv2d(x, 1, [3, 3], activation_fn=activation)
if scale == 2:
ps_features = channels*(scale**2)
x = slim.conv2d(x,ps_features,[3,3],activation_fn=activation)
##add by mingliang
# x = PS_complex(x, 2, color=False)
# x = PS_complex_multi_channel(x, 2, color=False)
x = PS(x, 2, color=False)
# x = PS(x,2,color=True)
elif scale == 3:
ps_features =3*(scale**2)
x = slim.conv2d(x,ps_features,[3,3],activation_fn=activation)
#x = slim.conv2d_transpose(x,ps_features,9,stride=1,activation_fn=activation)
x = PS(x,3,color=True)
elif scale == 4:
ps_features = 3*(2**2)
for i in range(2):
x = slim.conv2d(x,ps_features,[3,3],activation_fn=activation)
#x = slim.conv2d_transpose(x,ps_features,6,stride=1,activation_fn=activation)
x = PS(x,2,color=True)
return x
"""
Borrowed from https://github.com/tetrachrome/subpixel
Used for subpixel phase shifting after deconv operations
"""
def _phase_shift(I, r):
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(X, a, 1) # a, [bsize, b, r, r]
X = tf.concat([tf.squeeze(x, axis=1) for x in X],2) # bsize, b, a*r, r
X = tf.split(X, b, 1) # b, [bsize, a*r, r]
X = tf.concat([tf.squeeze(x, axis=1) for x in X],2) # bsize, a*r, b*r
return tf.reshape(X, (bsize, a*r, b*r, 1))
"""
Borrowed from https://github.com/tetrachrome/subpixel
Used for subpixel phase shifting after deconv operations
"""
def PS(X, r, color=False):
if color:
Xc = tf.split(X, 3, 3)
X = tf.concat([_phase_shift(x, r) for x in Xc],3)
else:
X = _phase_shift(X, r)
return X
def PS_complex(X, r, color=False):
Xc = tf.split(X, 2, 3)
X = tf.concat([_phase_shift(x, r) for x in Xc],3)
return X
def PS_complex_multi_channel(X, r, color=False):
Xc = tf.split(X, 12, 3)
X = tf.concat([_phase_shift(x, r) for x in Xc],3)
return X
"""
Tensorflow log base 10.
Found here: https://github.com/tensorflow/tensorflow/issues/1666
"""
def log10(x):
numerator = tf.log(x)
denominator = tf.log(tf.constant(10, dtype=numerator.dtype))
return numerator / denominator
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化