|
|
|
@ -5575,7 +5575,8 @@ def image_resize(input,
|
|
|
|
|
out_shape=None,
|
|
|
|
|
scale=None,
|
|
|
|
|
name=None,
|
|
|
|
|
resample='BILINEAR'):
|
|
|
|
|
resample='BILINEAR',
|
|
|
|
|
actual_shape=None):
|
|
|
|
|
"""
|
|
|
|
|
**Resize a Batch of Images**
|
|
|
|
|
|
|
|
|
@ -5600,25 +5601,50 @@ def image_resize(input,
|
|
|
|
|
Default: None
|
|
|
|
|
name(str|None): A name for this layer(optional). If set None, the layer
|
|
|
|
|
will be named automatically.
|
|
|
|
|
resample(str): The resample method. It can only be 'BILINEAR' currently.
|
|
|
|
|
resample(str): The resample method. It supports 'BILINEAR' and 'NEAREST'
|
|
|
|
|
currently.
|
|
|
|
|
Default: 'BILINEAR'
|
|
|
|
|
actual_shape(Variable): An optional input to specify output shape
|
|
|
|
|
dynamically. If provided, image resize
|
|
|
|
|
according to this given shape rather than
|
|
|
|
|
:attr:`out_shape` and :attr:`scale` specifying
|
|
|
|
|
shape. That is to say actual_shape has the
|
|
|
|
|
highest priority. It is recommended to use
|
|
|
|
|
actual_shape instead of :attr:`out_shape` if you
|
|
|
|
|
want to specify output shape dynamically. When
|
|
|
|
|
using actual_shape to specify output shape, one of
|
|
|
|
|
:attr:`out_shape` and :attr:`scale` should also be
|
|
|
|
|
set, otherwise errors would be occured in graph
|
|
|
|
|
constructing stage.
|
|
|
|
|
Default: None
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
Variable: The output is a 4-D tensor of the shape
|
|
|
|
|
(num_batches, channls, out_h, out_w).
|
|
|
|
|
|
|
|
|
|
Raises:
|
|
|
|
|
TypeError: out_shape should be a list or tuple or Variable.
|
|
|
|
|
TypeError: actual_shape should either be Variable or None.
|
|
|
|
|
ValueError: The 'resample' of image_resize can only be 'BILINEAR'
|
|
|
|
|
or 'NEAREST' currently.
|
|
|
|
|
ValueError: One of out_shape and scale must not be None.
|
|
|
|
|
ValueError: out_shape length should be 2.
|
|
|
|
|
|
|
|
|
|
Examples:
|
|
|
|
|
.. code-block:: python
|
|
|
|
|
|
|
|
|
|
out = fluid.layers.image_resize(input, out_shape=[12, 12])
|
|
|
|
|
"""
|
|
|
|
|
resample_methods = {'BILINEAR': 'bilinear', 'NEAREST': 'nearest'}
|
|
|
|
|
resample_methods = {
|
|
|
|
|
'BILINEAR': 'bilinear',
|
|
|
|
|
'NEAREST': 'nearest',
|
|
|
|
|
}
|
|
|
|
|
if resample not in resample_methods:
|
|
|
|
|
raise ValueError(
|
|
|
|
|
"The 'resample' of image_resize can only be 'BILINEAR' and 'NEAREST' currently."
|
|
|
|
|
"The 'resample' of image_resize can only be 'BILINEAR' or 'NEAREST' currently."
|
|
|
|
|
)
|
|
|
|
|
if out_shape is None and scale is None:
|
|
|
|
|
raise ValueError("One of out_shape and scale must not be None")
|
|
|
|
|
raise ValueError("One of out_shape and scale must not be None.")
|
|
|
|
|
helper = LayerHelper('interpolate', **locals())
|
|
|
|
|
dtype = helper.input_dtype()
|
|
|
|
|
|
|
|
|
@ -5629,19 +5655,28 @@ def image_resize(input,
|
|
|
|
|
out_w = 0
|
|
|
|
|
inputs = {"X": input}
|
|
|
|
|
if out_shape is not None:
|
|
|
|
|
if not (_is_list_or_turple_(out_shape) and
|
|
|
|
|
len(out_shape) == 2) and not isinstance(out_shape, Variable):
|
|
|
|
|
raise ValueError('out_shape should be a list or tuple or variable')
|
|
|
|
|
if _is_list_or_turple_(out_shape):
|
|
|
|
|
out_shape = list(map(int, out_shape))
|
|
|
|
|
out_h = out_shape[0]
|
|
|
|
|
out_w = out_shape[1]
|
|
|
|
|
else:
|
|
|
|
|
if isinstance(out_shape, Variable):
|
|
|
|
|
warnings.warn("out_shape as Variable type is deprecated, \
|
|
|
|
|
it is recommended to use actual_shape instead of \
|
|
|
|
|
out_shape to specify output shape dynamically.")
|
|
|
|
|
inputs['OutSize'] = out_shape
|
|
|
|
|
elif not (_is_list_or_turple_(out_shape)):
|
|
|
|
|
raise TypeError("out_shape should be a list or tuple or Variable.")
|
|
|
|
|
elif len(out_shape) != 2:
|
|
|
|
|
raise ValueError("out_shape length should be 2.")
|
|
|
|
|
|
|
|
|
|
out_shape = list(map(int, out_shape))
|
|
|
|
|
out_h = out_shape[0]
|
|
|
|
|
out_w = out_shape[1]
|
|
|
|
|
else:
|
|
|
|
|
out_h = int(input.shape[2] * scale)
|
|
|
|
|
out_w = int(input.shape[3] * scale)
|
|
|
|
|
|
|
|
|
|
if isinstance(actual_shape, Variable):
|
|
|
|
|
inputs["OutSize"] = actual_shape
|
|
|
|
|
elif actual_shape is not None:
|
|
|
|
|
raise TypeError("actual_shape should either be Variable or None.")
|
|
|
|
|
|
|
|
|
|
out = helper.create_variable_for_type_inference(dtype)
|
|
|
|
|
helper.append_op(
|
|
|
|
|
type='interpolate',
|
|
|
|
@ -5656,9 +5691,24 @@ def image_resize(input,
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@templatedoc(op_type="interpolate")
|
|
|
|
|
def resize_bilinear(input, out_shape=None, scale=None, name=None):
|
|
|
|
|
def resize_bilinear(input,
|
|
|
|
|
out_shape=None,
|
|
|
|
|
scale=None,
|
|
|
|
|
name=None,
|
|
|
|
|
actual_shape=None):
|
|
|
|
|
"""
|
|
|
|
|
${comment}
|
|
|
|
|
Resize input by performing bilinear interpolation based on given
|
|
|
|
|
output shape which specified by actual_shape, out_shape and scale
|
|
|
|
|
in priority order.
|
|
|
|
|
|
|
|
|
|
Bilinear interpolation is an extension of linear interpolation for
|
|
|
|
|
interpolating functions of two variables (e.g. H-direction and
|
|
|
|
|
W-direction in this op) on a rectilinear 2D grid. The key idea is
|
|
|
|
|
to perform linear interpolation first in one direction, and then
|
|
|
|
|
again in the other direction.
|
|
|
|
|
|
|
|
|
|
For details of bilinear interpolation, please refer to Wikipedia:
|
|
|
|
|
https://en.wikipedia.org/wiki/Bilinear_interpolation
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input(${x_type}): ${x_comment}.
|
|
|
|
@ -5670,18 +5720,41 @@ def resize_bilinear(input, out_shape=None, scale=None, name=None):
|
|
|
|
|
a higher priority than scale. Default: None.
|
|
|
|
|
|
|
|
|
|
name(str|None): The output variable name.
|
|
|
|
|
actual_shape(Variable): An optional input to specify output shape
|
|
|
|
|
dynamically. If provided, image resize
|
|
|
|
|
according to this given shape rather than
|
|
|
|
|
:attr:`out_shape` and :attr:`scale` specifying
|
|
|
|
|
shape. That is to say actual_shape has the
|
|
|
|
|
highest priority. It is recommended to use
|
|
|
|
|
actual_shape instead of :attr:`out_shape` if you
|
|
|
|
|
want to specify output shape dynamically. When
|
|
|
|
|
using actual_shape to specify output shape, one of
|
|
|
|
|
:attr:`out_shape` and :attr:`scale` should also be
|
|
|
|
|
set, otherwise errors would be occured in graph
|
|
|
|
|
constructing stage.
|
|
|
|
|
Default: None
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
${out_comment}.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return image_resize(input, out_shape, scale, name, 'BILINEAR')
|
|
|
|
|
return image_resize(input, out_shape, scale, name, 'BILINEAR', actual_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@templatedoc(op_type="interpolate")
|
|
|
|
|
def resize_nearest(input, out_shape=None, scale=None, name=None):
|
|
|
|
|
def resize_nearest(input,
|
|
|
|
|
out_shape=None,
|
|
|
|
|
scale=None,
|
|
|
|
|
name=None,
|
|
|
|
|
actual_shape=None):
|
|
|
|
|
"""
|
|
|
|
|
${comment}
|
|
|
|
|
Resize input by performing nearest neighbor interpolation in both the
|
|
|
|
|
3rd dimention(in height direction) and the 4th dimention(in width
|
|
|
|
|
direction) based on given output shape which specified by actual_shape,
|
|
|
|
|
out_shape and scale in priority order.
|
|
|
|
|
|
|
|
|
|
For details of nearest neighbor interpolation, please refer to Wikipedia:
|
|
|
|
|
https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
|
|
|
|
|
|
|
|
|
|
Args:
|
|
|
|
|
input(${x_type}): ${x_comment}.
|
|
|
|
@ -5693,12 +5766,25 @@ def resize_nearest(input, out_shape=None, scale=None, name=None):
|
|
|
|
|
a higher priority than scale. Default: None.
|
|
|
|
|
|
|
|
|
|
name(str|None): The output variable name.
|
|
|
|
|
actual_shape(Variable): An optional input to specify output shape
|
|
|
|
|
dynamically. If provided, image resize
|
|
|
|
|
according to this given shape rather than
|
|
|
|
|
:attr:`out_shape` and :attr:`scale` specifying
|
|
|
|
|
shape. That is to say actual_shape has the
|
|
|
|
|
highest priority. It is recommended to use
|
|
|
|
|
actual_shape instead of :attr:`out_shape` if you
|
|
|
|
|
want to specify output shape dynamically. When
|
|
|
|
|
using actual_shape to specify output shape, one of
|
|
|
|
|
:attr:`out_shape` and :attr:`scale` should also be
|
|
|
|
|
set, otherwise errors would be occured in graph
|
|
|
|
|
constructing stage.
|
|
|
|
|
Default: None
|
|
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
|
${out_comment}.
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
|
|
return image_resize(input, out_shape, scale, name, 'NEAREST')
|
|
|
|
|
return image_resize(input, out_shape, scale, name, 'NEAREST', actual_shape)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def image_resize_short(input, out_short_len, resample='BILINEAR'):
|
|
|
|
|