提交 8dccbe6e authored 作者: Pascal Lamblin's avatar Pascal Lamblin 提交者: GitHub

Merge pull request #6460 from erakra/upsamp2

small simplification
...@@ -1761,8 +1761,7 @@ def bilinear_kernel_1D(ratio, normalize=True): ...@@ -1761,8 +1761,7 @@ def bilinear_kernel_1D(ratio, normalize=True):
def frac_bilinear_upsampling(input, def frac_bilinear_upsampling(input,
ratio=None, frac_ratio):
frac_ratio=None):
"""Compute bilinear upsampling """Compute bilinear upsampling
This function will build the symbolic graph for upsampling This function will build the symbolic graph for upsampling
a tensor by the given ratio using bilinear interpolation. a tensor by the given ratio using bilinear interpolation.
...@@ -1772,10 +1771,7 @@ def frac_bilinear_upsampling(input, ...@@ -1772,10 +1771,7 @@ def frac_bilinear_upsampling(input,
input: symbolic 4D tensor input: symbolic 4D tensor
mini-batch of feature map stacks, of shape (batch size, mini-batch of feature map stacks, of shape (batch size,
input channels, input rows, input columns) that will be upsampled. input channels, input rows, input columns) that will be upsampled.
ratio: `int or Constant or Scalar Tensor of int* dtype` frac_ratio: tuple of int or tuple of tuples of int
the ratio by which the input is upsampled in the 2D space (row and
col size).
frac_ratio: None, tuple of int or tuple of tuples of int
The tuple defining the fractional ratio by which the input is The tuple defining the fractional ratio by which the input is
upsampled in the 2D space. One fractional ratio should be upsampled in the 2D space. One fractional ratio should be
represented as (numerator, denominator). If row and col ratios are represented as (numerator, denominator). If row and col ratios are
...@@ -1790,42 +1786,35 @@ def frac_bilinear_upsampling(input, ...@@ -1790,42 +1786,35 @@ def frac_bilinear_upsampling(input,
Notes Notes
----- -----
:note: The kernel used for bilinear interpolation is fixed (not learned). :note: The kernel used for bilinear interpolation is fixed (not learned).
:note: When the upsampling ratio is even, the last row and column is :note: When the upsampling frac_ratio numerator is even, the
repeated one extra time compared to the first row and column which makes last row and column is repeated one extra time compared to the first
the upsampled tensor asymmetrical on both sides. This does not happen when row and column which makes the upsampled tensor asymmetrical on both
the upsampling ratio is odd. sides. This does not happen when it is odd.
:note: This function must get either ratio or frac_ratio as parameter and
never both at once.
""" """
T = theano.tensor T = theano.tensor
row, col = input.shape[2:] row, col = input.shape[2:]
up_input = input.reshape((-1, 1, row, col)) up_input = input.reshape((-1, 1, row, col))
# redefince the ratio depending on the case # define the upsampling ratio depending on the case
if frac_ratio is None: if not isinstance(frac_ratio, tuple):
if not isinstance(ratio, tuple): raise ValueError("frac_ratio must be a tuple")
ratio = (ratio, ratio)
subsample = (1, 1)
else: else:
if not isinstance(frac_ratio, tuple): if isinstance(frac_ratio[0], tuple):
raise ValueError("frac_ratio must be a tuple") f_r = []
else: for i, fr in enumerate(frac_ratio):
if isinstance(frac_ratio[0], tuple): p, q = fr
f_r = []
for i, fr in enumerate(frac_ratio):
p, q = fr
div = gcd(p, q)
f_r.append(tuple(np.array(fr) // div))
frac_ratio = tuple(f_r)
ratio = (frac_ratio[0][0], frac_ratio[1][0])
subsample = (frac_ratio[0][1], frac_ratio[1][1])
else:
p, q = frac_ratio
div = gcd(p, q) div = gcd(p, q)
frac_ratio = tuple(np.array(frac_ratio) // div) f_r.append(tuple(np.array(fr) // div))
ratio = (frac_ratio[0], frac_ratio[0]) frac_ratio = tuple(f_r)
subsample = (frac_ratio[1], frac_ratio[1]) ratio = (frac_ratio[0][0], frac_ratio[1][0])
subsample = (frac_ratio[0][1], frac_ratio[1][1])
else:
p, q = frac_ratio
div = gcd(p, q)
frac_ratio = tuple(np.array(frac_ratio) // div)
ratio = (frac_ratio[0], frac_ratio[0])
subsample = (frac_ratio[1], frac_ratio[1])
# duplicate borders of the input # duplicate borders of the input
concat_mat = T.concatenate((up_input[:, :, :1, :], up_input, concat_mat = T.concatenate((up_input[:, :, :1, :], up_input,
...@@ -1853,7 +1842,7 @@ def frac_bilinear_upsampling(input, ...@@ -1853,7 +1842,7 @@ def frac_bilinear_upsampling(input,
dtype=theano.config.floatX)), dtype=theano.config.floatX)),
axis=3) axis=3)
# upsample the input by passing it as kernl of conv and using filter_dilation # upsample the input by passing it as kernel of conv and using filter_dilation
upsamp = T.nnet.conv2d(pad_kern, concat_mat, border_mode='valid', upsamp = T.nnet.conv2d(pad_kern, concat_mat, border_mode='valid',
filter_dilation=ratio, subsample=subsample) filter_dilation=ratio, subsample=subsample)
...@@ -1914,12 +1903,11 @@ def bilinear_upsampling(input, ...@@ -1914,12 +1903,11 @@ def bilinear_upsampling(input,
if frac_ratio: if frac_ratio:
if use_1D_kernel: if use_1D_kernel:
raise ValueError('For fractional ratios 1D kernel' raise ValueError('For fractional ratios 1D kernel '
'method not implemented. You may want to pass ' 'method not implemented. You may want to pass '
'use_1D_kernel as False') 'use_1D_kernel as False')
return frac_bilinear_upsampling(input, # case of fractional 2D upsampling
ratio=ratio, return frac_bilinear_upsampling(input, frac_ratio=frac_ratio)
frac_ratio=frac_ratio)
# the remaining case if integer ratio with use_1D_kernel # the remaining case if integer ratio with use_1D_kernel
T = theano.tensor T = theano.tensor
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论