21
21
from ..param_attr import ParamAttr
22
22
from layer_function_generator import autodoc
23
23
from tensor import concat
24
+ import utils
24
25
25
26
__all__ = [
26
27
'fc' ,
@@ -1138,8 +1139,8 @@ def sequence_conv(input,
1138
1139
def conv2d (input ,
1139
1140
num_filters ,
1140
1141
filter_size ,
1141
- stride = None ,
1142
- padding = None ,
1142
+ stride = 1 ,
1143
+ padding = 0 ,
1143
1144
groups = None ,
1144
1145
param_attr = None ,
1145
1146
bias_attr = None ,
@@ -1252,12 +1253,10 @@ def conv2d(input,
1252
1253
raise ValueError ("num_channels must be divisible by groups." )
1253
1254
num_filter_channels = num_channels / groups
1254
1255
1255
- if isinstance (filter_size , int ):
1256
- filter_size = [filter_size , filter_size ]
1257
- if isinstance (stride , int ):
1258
- stride = [stride , stride ]
1259
- if isinstance (padding , int ):
1260
- padding = [padding , padding ]
1256
+ filter_size = utils .convert_to_list (filter_size , 2 , 'filter_size' )
1257
+ stride = utils .convert_to_list (stride , 2 , 'stride' )
1258
+ padding = utils .convert_to_list (padding , 2 , 'padding' )
1259
+
1261
1260
if not isinstance (use_cudnn , bool ):
1262
1261
raise ValueError ("use_cudnn should be True or False" )
1263
1262
@@ -1432,31 +1431,31 @@ def sequence_last_step(input):
1432
1431
1433
1432
1434
1433
def pool2d (input ,
1435
- pool_size ,
1436
- pool_type ,
1437
- pool_stride = None ,
1438
- pool_padding = None ,
1434
+ pool_size = - 1 ,
1435
+ pool_type = "max" ,
1436
+ pool_stride = 1 ,
1437
+ pool_padding = 0 ,
1439
1438
global_pooling = False ,
1440
1439
use_cudnn = True ,
1441
1440
name = None ):
1442
1441
"""
1443
1442
This function adds the operator for pooling in 2 dimensions, using the
1444
1443
pooling configurations mentioned in input parameters.
1445
1444
"""
1446
- if pool_padding is None :
1447
- pool_padding = [0 , 0 ]
1448
- if pool_stride is None :
1449
- pool_stride = [1 , 1 ]
1450
1445
if pool_type not in ["max" , "avg" ]:
1451
1446
raise ValueError (
1452
1447
"Unknown pool_type: '%s'. It can only be 'max' or 'avg'." ,
1453
1448
str (pool_type ))
1454
- if isinstance (pool_size , int ):
1455
- pool_size = [pool_size , pool_size ]
1456
- if isinstance (pool_stride , int ):
1457
- pool_stride = [pool_stride , pool_stride ]
1458
- if isinstance (pool_padding , int ):
1459
- pool_padding = [pool_padding , pool_padding ]
1449
+
1450
+ if global_pooling is False and pool_size == - 1 :
1451
+ raise ValueError (
1452
+ "When the global_pooling is False, pool_size must be passed "
1453
+ "and be a valid value. Received pool_size: " + str (pool_size ))
1454
+
1455
+ pool_size = utils .convert_to_list (pool_size , 2 , 'pool_size' )
1456
+ pool_padding = utils .convert_to_list (pool_padding , 2 , 'pool_padding' )
1457
+ pool_stride = utils .convert_to_list (pool_stride , 2 , 'pool_stride' )
1458
+
1460
1459
if not isinstance (use_cudnn , bool ):
1461
1460
raise ValueError ("use_cudnn should be True or False" )
1462
1461
@@ -1685,9 +1684,9 @@ def conv2d_transpose(input,
1685
1684
num_filters ,
1686
1685
output_size = None ,
1687
1686
filter_size = None ,
1688
- padding = None ,
1689
- stride = None ,
1690
- dilation = None ,
1687
+ padding = 0 ,
1688
+ stride = 1 ,
1689
+ dilation = 1 ,
1691
1690
param_attr = None ,
1692
1691
use_cudnn = True ,
1693
1692
name = None ):
@@ -1783,37 +1782,19 @@ def conv2d_transpose(input,
1783
1782
raise TypeError ("Input of conv2d_transpose must be Variable" )
1784
1783
input_channel = input .shape [1 ]
1785
1784
1786
- op_attr = dict ()
1787
-
1788
- if isinstance (padding , int ):
1789
- op_attr ['paddings' ] = [padding , padding ]
1790
- elif padding is not None :
1791
- op_attr ['paddings' ] = padding
1792
-
1793
- if isinstance (stride , int ):
1794
- op_attr ['strides' ] = [stride , stride ]
1795
- elif stride is not None :
1796
- op_attr ['strides' ] = stride
1797
-
1798
- if isinstance (dilation , int ):
1799
- op_attr ['dilations' ] = [dilation , dilation ]
1800
- elif dilation is not None :
1801
- op_attr ['dilations' ] = dilation
1785
+ padding = utils .convert_to_list (padding , 2 , 'padding' )
1786
+ stride = utils .convert_to_list (stride , 2 , 'stride' )
1787
+ dilation = utils .convert_to_list (dilation , 2 , 'dilation' )
1802
1788
1803
1789
if not isinstance (use_cudnn , bool ):
1804
1790
raise ValueError ("use_cudnn should be True or False" )
1805
- op_attr ['use_cudnn' ] = use_cudnn
1806
1791
1807
1792
if filter_size is None :
1808
1793
if output_size is None :
1809
1794
raise ValueError ("output_size must be set when filter_size is None" )
1810
1795
if isinstance (output_size , int ):
1811
1796
output_size = [output_size , output_size ]
1812
1797
1813
- padding = op_attr .get ('paddings' , [0 , 0 ])
1814
- stride = op_attr .get ('strides' , [1 , 1 ])
1815
- dilation = op_attr .get ('dilations' , [1 , 1 ])
1816
-
1817
1798
h_in = input .shape [2 ]
1818
1799
w_in = input .shape [3 ]
1819
1800
@@ -1822,9 +1803,9 @@ def conv2d_transpose(input,
1822
1803
filter_size_w = (output_size [1 ] - (w_in - 1 ) * stride [1 ] + 2 *
1823
1804
padding [1 ] - 1 ) / dilation [1 ] + 1
1824
1805
filter_size = [filter_size_h , filter_size_w ]
1825
-
1826
- elif isinstance (filter_size , int ):
1827
- filter_size = [ filter_size , filter_size ]
1806
+ else :
1807
+ filter_size = utils . convert_to_list (filter_size , 2 ,
1808
+ 'conv2d_transpose. filter_size' )
1828
1809
1829
1810
filter_shape = [input_channel , num_filters ] + filter_size
1830
1811
img_filter = helper .create_parameter (
@@ -1836,7 +1817,12 @@ def conv2d_transpose(input,
1836
1817
inputs = {'Input' : [input ],
1837
1818
'Filter' : [img_filter ]},
1838
1819
outputs = {'Output' : out },
1839
- attrs = op_attr )
1820
+ attrs = {
1821
+ 'strides' : stride ,
1822
+ 'paddings' : padding ,
1823
+ 'dilations' : dilation ,
1824
+ 'use_cudnn' : use_cudnn
1825
+ })
1840
1826
1841
1827
return out
1842
1828
0 commit comments