@@ -5575,7 +5575,8 @@ def image_resize(input,
5575
5575
out_shape = None ,
5576
5576
scale = None ,
5577
5577
name = None ,
5578
- resample = 'BILINEAR' ):
5578
+ resample = 'BILINEAR' ,
5579
+ actual_shape = None ):
5579
5580
"""
5580
5581
**Resize a Batch of Images**
5581
5582
@@ -5600,25 +5601,50 @@ def image_resize(input,
5600
5601
Default: None
5601
5602
name(str|None): A name for this layer(optional). If set None, the layer
5602
5603
will be named automatically.
5603
- resample(str): The resample method. It can only be 'BILINEAR' currently.
5604
+ resample(str): The resample method. It supports 'BILINEAR' and 'NEAREST'
5605
+ currently.
5604
5606
Default: 'BILINEAR'
5607
+ actual_shape(Variable): An optional input to specify output shape
5608
+ dynamically. If provided, image resize
5609
+ according to this given shape rather than
5610
+ :attr:`out_shape` and :attr:`scale` specifying
5611
+ shape. That is to say actual_shape has the
5612
+ highest priority. It is recommended to use
5613
+ actual_shape instead of :attr:`out_shape` if you
5614
+ want to specify output shape dynamically. When
5615
+ using actual_shape to specify output shape, one of
5616
+ :attr:`out_shape` and :attr:`scale` should also be
5617
+ set, otherwise errors would be occured in graph
5618
+ constructing stage.
5619
+ Default: None
5605
5620
5606
5621
Returns:
5607
5622
Variable: The output is a 4-D tensor of the shape
5608
5623
(num_batches, channls, out_h, out_w).
5609
5624
5625
+ Raises:
5626
+ TypeError: out_shape should be a list or tuple or Variable.
5627
+ TypeError: actual_shape should either be Variable or None.
5628
+ ValueError: The 'resample' of image_resize can only be 'BILINEAR'
5629
+ or 'NEAREST' currently.
5630
+ ValueError: One of out_shape and scale must not be None.
5631
+ ValueError: out_shape length should be 2.
5632
+
5610
5633
Examples:
5611
5634
.. code-block:: python
5612
5635
5613
5636
out = fluid.layers.image_resize(input, out_shape=[12, 12])
5614
5637
"""
5615
- resample_methods = {'BILINEAR' : 'bilinear' , 'NEAREST' : 'nearest' }
5638
+ resample_methods = {
5639
+ 'BILINEAR' : 'bilinear' ,
5640
+ 'NEAREST' : 'nearest' ,
5641
+ }
5616
5642
if resample not in resample_methods :
5617
5643
raise ValueError (
5618
- "The 'resample' of image_resize can only be 'BILINEAR' and 'NEAREST' currently."
5644
+ "The 'resample' of image_resize can only be 'BILINEAR' or 'NEAREST' currently."
5619
5645
)
5620
5646
if out_shape is None and scale is None :
5621
- raise ValueError ("One of out_shape and scale must not be None" )
5647
+ raise ValueError ("One of out_shape and scale must not be None. " )
5622
5648
helper = LayerHelper ('interpolate' , ** locals ())
5623
5649
dtype = helper .input_dtype ()
5624
5650
@@ -5629,19 +5655,28 @@ def _is_list_or_turple_(data):
5629
5655
out_w = 0
5630
5656
inputs = {"X" : input }
5631
5657
if out_shape is not None :
5632
- if not (_is_list_or_turple_ (out_shape ) and
5633
- len (out_shape ) == 2 ) and not isinstance (out_shape , Variable ):
5634
- raise ValueError ('out_shape should be a list or tuple or variable' )
5635
- if _is_list_or_turple_ (out_shape ):
5636
- out_shape = list (map (int , out_shape ))
5637
- out_h = out_shape [0 ]
5638
- out_w = out_shape [1 ]
5639
- else :
5658
+ if isinstance (out_shape , Variable ):
5659
+ warnings .warn ("out_shape as Variable type is deprecated, \
5660
+ it is recommended to use actual_shape instead of \
5661
+ out_shape to specify output shape dynamically." )
5640
5662
inputs ['OutSize' ] = out_shape
5663
+ elif not (_is_list_or_turple_ (out_shape )):
5664
+ raise TypeError ("out_shape should be a list or tuple or Variable." )
5665
+ elif len (out_shape ) != 2 :
5666
+ raise ValueError ("out_shape length should be 2." )
5667
+
5668
+ out_shape = list (map (int , out_shape ))
5669
+ out_h = out_shape [0 ]
5670
+ out_w = out_shape [1 ]
5641
5671
else :
5642
5672
out_h = int (input .shape [2 ] * scale )
5643
5673
out_w = int (input .shape [3 ] * scale )
5644
5674
5675
+ if isinstance (actual_shape , Variable ):
5676
+ inputs ["OutSize" ] = actual_shape
5677
+ elif actual_shape is not None :
5678
+ raise TypeError ("actual_shape should either be Variable or None." )
5679
+
5645
5680
out = helper .create_variable_for_type_inference (dtype )
5646
5681
helper .append_op (
5647
5682
type = 'interpolate' ,
@@ -5656,9 +5691,24 @@ def _is_list_or_turple_(data):
5656
5691
5657
5692
5658
5693
@templatedoc (op_type = "interpolate" )
5659
- def resize_bilinear (input , out_shape = None , scale = None , name = None ):
5694
+ def resize_bilinear (input ,
5695
+ out_shape = None ,
5696
+ scale = None ,
5697
+ name = None ,
5698
+ actual_shape = None ):
5660
5699
"""
5661
- ${comment}
5700
+ Resize input by performing bilinear interpolation based on given
5701
+ output shape which specified by actual_shape, out_shape and scale
5702
+ in priority order.
5703
+
5704
+ Bilinear interpolation is an extension of linear interpolation for
5705
+ interpolating functions of two variables (e.g. H-direction and
5706
+ W-direction in this op) on a rectilinear 2D grid. The key idea is
5707
+ to perform linear interpolation first in one direction, and then
5708
+ again in the other direction.
5709
+
5710
+ For details of bilinear interpolation, please refer to Wikipedia:
5711
+ https://en.wikipedia.org/wiki/Bilinear_interpolation
5662
5712
5663
5713
Args:
5664
5714
input(${x_type}): ${x_comment}.
@@ -5670,18 +5720,41 @@ def resize_bilinear(input, out_shape=None, scale=None, name=None):
5670
5720
a higher priority than scale. Default: None.
5671
5721
5672
5722
name(str|None): The output variable name.
5723
+ actual_shape(Variable): An optional input to specify output shape
5724
+ dynamically. If provided, image resize
5725
+ according to this given shape rather than
5726
+ :attr:`out_shape` and :attr:`scale` specifying
5727
+ shape. That is to say actual_shape has the
5728
+ highest priority. It is recommended to use
5729
+ actual_shape instead of :attr:`out_shape` if you
5730
+ want to specify output shape dynamically. When
5731
+ using actual_shape to specify output shape, one of
5732
+ :attr:`out_shape` and :attr:`scale` should also be
5733
+ set, otherwise errors would be occured in graph
5734
+ constructing stage.
5735
+ Default: None
5673
5736
5674
5737
Returns:
5675
5738
${out_comment}.
5676
5739
"""
5677
5740
5678
- return image_resize (input , out_shape , scale , name , 'BILINEAR' )
5741
+ return image_resize (input , out_shape , scale , name , 'BILINEAR' , actual_shape )
5679
5742
5680
5743
5681
5744
@templatedoc (op_type = "interpolate" )
5682
- def resize_nearest (input , out_shape = None , scale = None , name = None ):
5745
+ def resize_nearest (input ,
5746
+ out_shape = None ,
5747
+ scale = None ,
5748
+ name = None ,
5749
+ actual_shape = None ):
5683
5750
"""
5684
- ${comment}
5751
+ Resize input by performing nearest neighbor interpolation in both the
5752
+ 3rd dimention(in height direction) and the 4th dimention(in width
5753
+ direction) based on given output shape which specified by actual_shape,
5754
+ out_shape and scale in priority order.
5755
+
5756
+ For details of nearest neighbor interpolation, please refer to Wikipedia:
5757
+ https://en.wikipedia.org/wiki/Nearest-neighbor_interpolation
5685
5758
5686
5759
Args:
5687
5760
input(${x_type}): ${x_comment}.
@@ -5693,12 +5766,25 @@ def resize_nearest(input, out_shape=None, scale=None, name=None):
5693
5766
a higher priority than scale. Default: None.
5694
5767
5695
5768
name(str|None): The output variable name.
5769
+ actual_shape(Variable): An optional input to specify output shape
5770
+ dynamically. If provided, image resize
5771
+ according to this given shape rather than
5772
+ :attr:`out_shape` and :attr:`scale` specifying
5773
+ shape. That is to say actual_shape has the
5774
+ highest priority. It is recommended to use
5775
+ actual_shape instead of :attr:`out_shape` if you
5776
+ want to specify output shape dynamically. When
5777
+ using actual_shape to specify output shape, one of
5778
+ :attr:`out_shape` and :attr:`scale` should also be
5779
+ set, otherwise errors would be occured in graph
5780
+ constructing stage.
5781
+ Default: None
5696
5782
5697
5783
Returns:
5698
5784
${out_comment}.
5699
5785
"""
5700
5786
5701
- return image_resize (input , out_shape , scale , name , 'NEAREST' )
5787
+ return image_resize (input , out_shape , scale , name , 'NEAREST' , actual_shape )
5702
5788
5703
5789
5704
5790
def image_resize_short (input , out_short_len , resample = 'BILINEAR' ):
0 commit comments