提交 c49136eb authored 作者: Chiheb Trabelsi's avatar Chiheb Trabelsi

Functions parameters have been modified in pool.py for coherence purposes.…

Functions parameters have been modified in pool.py for coherence purposes. Deprecation warnings and exceptions have been added in order to deal with all possible scenarios.
上级 96be471e
......@@ -38,38 +38,96 @@ def max_pool_2d_same_size(input, patch_size):
return outs
def pool_2d(input, ds, ignore_border=None, st=None, padding=(0, 0),
mode='max'):
def pool_2d(input, ws=None, ignore_border=None, stride=None, pad=(0, 0),
mode='max', ds=None, st=None, padding=None):
"""Downscale the input by a specified factor
Takes as input a N-D tensor, where N >= 2. It downscales the input image by
the specified factor, by keeping only the maximum value of non-overlapping
patches of size (ds[0],ds[1])
patches of size (ws[0],ws[1])
Parameters
----------
input : N-D theano tensor of input images
Input images. Max pooling will be done over the 2 last dimensions.
ds : tuple of length 2 or theano vector of ints of size 2.
Factor by which to downscale (vertical ds, horizontal ds).
ws : tuple of length 2 or theano vector of ints of size 2.
Factor by which to downscale (vertical ws, horizontal ws).
(2,2) will halve the image in each dimension.
ignore_border : bool (default None, will print a warning and set to False)
When True, (5,5) input with ds=(2,2) will generate a (2,2) output.
When True, (5,5) input with ws=(2,2) will generate a (2,2) output.
(3,3) otherwise.
st : tuple of two ints or theano vector of ints of size 2.
stride : tuple of two ints or theano vector of ints of size 2.
Stride size, which is the number of shifts over rows/cols to get the
next pool region. If st is None, it is considered equal to ds
next pool region. If stride is None, it is considered equal to ws
(no overlap on pooling regions).
padding : tuple of two ints or theano vector of ints of size 2.
pad : tuple of two ints or theano vector of ints of size 2.
(pad_h, pad_w), pad zeros to extend beyond four borders of the
images, pad_h is the size of the top and bottom margins, and
pad_w is the size of the left and right margins.
mode : {'max', 'sum', 'average_inc_pad', 'average_exc_pad'}
Operation executed on each window. `max` and `sum` always exclude
the padding in the computation. `average` gives you the choice to
the pad in the computation. `average` gives you the choice to
include or exclude it.
"""
if ds is not None:
if ws is not None:
raise Exception(
"You can't provide different tuple values to 'ws' and 'ds'."
" You must provide only one of them as they both"
" refer to the window size. DEPRECATION NOTE: the"
" 'ds' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'ws'."
)
else:
warnings.warn(
"DEPRECATION: the 'ds' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'ws'.",
stacklevel=2
)
ws = ds
elif ds is None and ws is None:
raise Exception(
"You must provide a tuple value for the window size."
)
if st is not None:
if stride is not None:
raise Exception(
"You can't provide different tuple values to 'stride' and"
" st. You must provide only one of them as they"
" both refer to the stride size. DEPRECATION NOTE: the"
" 'st' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'stride'."
)
else:
warnings.warn(
"DEPRECATION: the 'st' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'stride'.",
stacklevel=2
)
stride = st
if padding is not None:
if pad is not (0, 0):
raise Exception(
"You can't provide different tuple value to 'pad' and"
" 'padding'. You must provide only one of them as they"
" both refer to the padding size. DEPRECATION NOTE: the"
" 'padding' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'pad'."
)
else:
warnings.warn(
"DEPRECATION: the 'padding' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'pad'.",
stacklevel=2
)
pad = padding
if input.ndim < 2:
raise NotImplementedError('pool_2d requires a dimension >= 2')
if ignore_border is None:
......@@ -81,48 +139,107 @@ def pool_2d(input, ds, ignore_border=None, st=None, padding=(0, 0),
" On the GPU, using ignore_border=True is needed to use cuDNN."
" When using ignore_border=False and not using cuDNN, the only"
" GPU combination supported is when"
" `ds == st and padding == (0, 0) and mode == 'max'`."
" `ws == stride and pad == (0, 0) and mode == 'max'`."
" Otherwise, the convolution will be executed on CPU.",
stacklevel=2)
ignore_border = False
op = Pool(ignore_border, ndim=2, mode=mode)
output = op(input, ds, st, padding)
output = op(input, ws, stride, pad)
return output
def pool_3d(input, ds, ignore_border=None, st=None, padding=(0, 0, 0),
mode='max'):
def pool_3d(input, ws=None, ignore_border=None, stride=None, pad=(0, 0, 0),
mode='max', ds=None, st=None, padding=None):
"""Downscale the input by a specified factor
Takes as input a N-D tensor, where N >= 3. It downscales the input image by
the specified factor, by keeping only the maximum value of non-overlapping
patches of size (ds[0],ds[1],ds[2])
patches of size (ws[0],ws[1],ws[2])
Parameters
----------
input : N-D theano tensor of input images
Input images. Max pooling will be done over the 3 last dimensions.
ds : tuple of length 3 or theano vector of ints of size 3
Factor by which to downscale (vertical ds, horizontal ds, depth ds).
ws : tuple of length 3 or theano vector of ints of size 3
Factor by which to downscale (vertical ws, horizontal ws, depth ws).
(2,2,2) will halve the image in each dimension.
ignore_border : bool (default None, will print a warning and set to False)
When True, (5,5,5) input with ds=(2,2,2) will generate a (2,2,2) output.
When True, (5,5,5) input with ws=(2,2,2) will generate a (2,2,2) output.
(3,3,3) otherwise.
st : tuple of three ints or theano vector of ints of size 3
stride : tuple of three ints or theano vector of ints of size 3
Stride size, which is the number of shifts over rows/cols/slices to get
the next pool region. If st is None, it is considered equal to ds
the next pool region. If stride is None, it is considered equal to ws
(no overlap on pooling regions).
padding : tuple of two ints or theano vector of ints of size 3
pad : tuple of two ints or theano vector of ints of size 3
(pad_h, pad_w, pad_d), pad zeros to extend beyond six borders of the
images, pad_h is the size of the top and bottom margins,
pad_w is the size of the left and right margins, and pad_d is the size
of the front and back margins
mode : {'max', 'sum', 'average_inc_pad', 'average_exc_pad'}
Operation executed on each window. `max` and `sum` always exclude
the padding in the computation. `average` gives you the choice to
the pad in the computation. `average` gives you the choice to
include or exclude it.
"""
if ds is not None:
if ws is not None:
raise Exception(
"You can't provide different tuple values to 'ws' and 'ds'."
" You must provide only one of them as they both"
" refer to the window size. DEPRECATION NOTE: the"
" 'ds' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'ws'."
)
else:
warnings.warn(
"DEPRECATION: the 'ds' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'ws'.",
stacklevel=2
)
ws = ds
elif ds is None and ws is None:
raise Exception(
"You must provide a tuple value for the window size."
)
if st is not None:
if stride is not None:
raise Exception(
"You can't provide different tuple values to 'stride' and"
" st. You must provide only one of them as they"
" both refer to the stride size. DEPRECATION NOTE: the"
" 'st' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'stride'."
)
else:
warnings.warn(
"DEPRECATION: the 'st' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'stride'.",
stacklevel=2
)
stride = st
if padding is not None:
if pad is not (0, 0, 0):
raise Exception(
"You can't provide different tuple value to 'pad' and"
" 'padding'. You must provide only one of them as they"
" both refer to the padding size. DEPRECATION NOTE: the"
" 'padding' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'pad'."
)
else:
warnings.warn(
"DEPRECATION: the 'padding' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'pad'.",
stacklevel=2
)
pad = padding
if input.ndim < 3:
raise NotImplementedError('pool_3d requires a dimension >= 3')
if ignore_border is None:
......@@ -134,12 +251,12 @@ def pool_3d(input, ds, ignore_border=None, st=None, padding=(0, 0, 0),
" On the GPU, using ignore_border=True is needed to use cuDNN."
" When using ignore_border=False and not using cuDNN, the only"
" GPU combination supported is when"
" `ds == st and padding == (0, 0, 0) and mode == 'max'`."
" `ws == stride and pad == (0, 0, 0) and mode == 'max'`."
" Otherwise, the convolution will be executed on CPU.",
stacklevel=2)
ignore_border = False
op = Pool(ignore_border, ndim=3, mode=mode)
output = op(input, ds, st, padding)
output = op(input, ws, stride, pad)
return output
......@@ -150,23 +267,23 @@ class Pool(OpenMPOp):
Parameters
----------
ds : list or tuple of N ints
ws : list or tuple of N ints
Downsample factor over rows, columns etc.
ds indicates the size of the pooling region.
ws indicates the size of the pooling region.
ignore_border : bool
If ds doesn't divide imgshape, do we include an extra row/col/slice
If ws doesn't divide imgshape, do we include an extra row/col/slice
of partial downsampling (False) or ignore it (True).
st : list or tuple of N ints or None
stride : list or tuple of N ints or None
Stride size, which is the number of shifts over rows/cols/slices to get the
next pool region. If st is None, it is considered equal to ds
next pool region. If stride is None, it is considered equal to ws
(no overlap on pooling regions).
padding : tuple of N ints or None
pad : tuple of N ints or None
For each downsampling dimension, this specifies the number of zeros to
add as padding on both sides. For 2D and (pad_h, pad_w), pad_h specifies the
add as pad on both sides. For 2D and (pad_h, pad_w), pad_h specifies the
size of the top and bottom margins, pad_w specifies the size of the left and
right margins. No padding is added if padding is None.
right margins. No pad is added if pad is None.
mode : {'max', 'sum', 'average_inc_pad', 'average_exc_pad'}
('average_inc_pad' excludes the padding from the count,
('average_inc_pad' excludes the pad from the count,
'average_exc_pad' include it)
ndim : int
The number of pooling dimensions N.
......@@ -177,7 +294,8 @@ class Pool(OpenMPOp):
__props__ = ('ignore_border', 'mode', 'ndim')
@staticmethod
def out_shape(imgshape, ds, ignore_border=False, st=None, padding=None, ndim=2):
def out_shape(imgshape, ws=None, ignore_border=False, stride=None,
pad=None, ndim=2, ds=None, st=None, padding=None):
"""
Return the shape of the output from this op, for input of given
shape and flags.
......@@ -187,21 +305,21 @@ class Pool(OpenMPOp):
imgshape : tuple, list, or similar of integer or scalar Theano variable
The shape of a tensor of images. The last N elements are
interpreted as the number of rows, and the number of cols.
ds : list or tuple of N ints
ws : list or tuple of N ints
Downsample factor over rows and column.
ds indicates the pool region size.
ws indicates the pool region size.
ignore_border : bool
If ds doesn't divide imgshape, do we include an extra row/col/slice
If ws doesn't divide imgshape, do we include an extra row/col/slice
of partial downsampling (False) or ignore it (True).
st : list or tuple of N ints or None
stride : list or tuple of N ints or None
Stride size, which is the number of shifts over rows/cols/slices to get the
next pool region. If st is None, it is considered equal to ds
next pool region. If stride is None, it is considered equal to ws
(no overlap on pooling regions).
padding : tuple of N ints or None
pad : tuple of N ints or None
For each downsampling dimension, this specifies the number of zeros to
add as padding on both sides. For 2D and (pad_h, pad_w), pad_h specifies the
add as pad on both sides. For 2D and (pad_h, pad_w), pad_h specifies the
size of the top and bottom margins, pad_w specifies the size of the left and
right margins. No padding is added if padding is None.
right margins. No pad is added if pad is None.
ndim : int
The number of pooling dimensions N.
The default is 2.
......@@ -214,17 +332,76 @@ class Pool(OpenMPOp):
elements reduced as per the downsampling & ignore_border flags.
"""
if ds is not None:
if ws is not None:
raise Exception(
"You can't provide different tuple values to 'ws' and"
" 'ds'. You must provide only one of them as they both"
" refer to the window size. DEPRECATION NOTE: the"
" 'ds' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'ws'."
)
else:
warnings.warn(
"DEPRECATION: the 'ds' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'ws'.",
stacklevel=2
)
ws = ds
elif ds is None and ws is None:
raise Exception(
"You must provide a tuple value for the window size."
)
if st is not None:
if stride is not None:
raise Exception(
"You can't provide different tuple values to 'stride' and"
" st. You must provide only one of them as they"
" both refer to the stride size. DEPRECATION NOTE: the"
" 'st' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'stride'."
)
else:
warnings.warn(
"DEPRECATION: the 'st' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'stride'.",
stacklevel=2
)
stride = st
if padding is not None:
if pad is not None:
raise Exception(
"You can't provide different tuple value to 'pad' and"
" 'padding'. You must provide only one of them as they"
" both refer to the padding size. DEPRECATION NOTE: the"
" 'padding' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'pad'."
)
else:
warnings.warn(
"DEPRECATION: the 'padding' parameter is not going to"
" exist anymore as it is going to be replaced by the"
" parameter 'pad'.",
stacklevel=2
)
pad = padding
if ndim is None:
ndim = 2
assert ndim > 0
if len(imgshape) < ndim:
raise TypeError('imgshape must have at least {} dimensions'.format(ndim))
if st is None:
st = ds
if padding is None:
padding = (0,) * ndim
patch_shape = tuple(tensor.extract_constant(imgshape[-ndim + i]) + padding[i] * 2
if stride is None:
stride = ws
if pad is None:
pad = (0,) * ndim
patch_shape = tuple(tensor.extract_constant(imgshape[-ndim + i]) + pad[i] * 2
for i in xrange(ndim))
def compute_out(v, downsample, stride):
......@@ -248,7 +425,7 @@ class Pool(OpenMPOp):
else:
return max(0, (v - 1 - downsample + stride) // stride) + 1
out_shape = [compute_out(patch_shape[i], ds[i], st[i]) for i in xrange(ndim)]
out_shape = [compute_out(patch_shape[i], ws[i], stride[i]) for i in xrange(ndim)]
rval = list(imgshape[:-ndim]) + out_shape
return rval
......@@ -266,11 +443,11 @@ class Pool(OpenMPOp):
def prepare_node(self, node, storage_map, compute_map, impl):
if len(node.inputs) == 1:
# Old interface
self.ndim = len(node.op.ds)
self.ndim = len(node.op.ws)
self.mode = node.op.mode
ws = theano.tensor.constant(node.op.ds)
st = theano.tensor.constant(node.op.st)
pad = theano.tensor.constant(node.op.padding)
ws = theano.tensor.constant(node.op.ws)
st = theano.tensor.constant(node.op.stride)
pad = theano.tensor.constant(node.op.pad)
node.inputs.append(ws)
node.inputs.append(st)
node.inputs.append(pad)
......@@ -715,7 +892,8 @@ class PoolGrad(OpenMPOp):
__props__ = ('ignore_border', 'mode', 'ndim')
@staticmethod
def out_shape(imgshape, ds, ignore_border=False, st=None, padding=None, ndim=2):
def out_shape(imgshape, ws=None, ignore_border=False, stride=None, pad=None, ndim=2,
ds=None, st=None, padding=None):
"""Return the shape of the output from this op, for input of given
shape and flags.
......@@ -724,21 +902,21 @@ class PoolGrad(OpenMPOp):
imgshape : tuple of integers or scalar Theano variables
the shape of a tensor of images. The last N elements are
interpreted as the downsampling dimensions.
ds : tuple of N ints
ws : tuple of N ints
downsample factor over rows and columns this parameter
indicates the size of the pooling region
ignore_border : bool
If ds doesn't divide imgshape, do we include an extra row/col/slice
If ws doesn't divide imgshape, do we include an extra row/col/slice
of partial downsampling (False) or ignore it (True).
st : list or tuple of N ints or None
stride : list or tuple of N ints or None
Stride size, which is the number of shifts over rows/cols/slices to get the
next pool region. If st is None, it is considered equal to ds
next pool region. If stride is None, it is considered equal to ws
(no overlap on pooling regions).
padding : tuple of N ints or None
pad : tuple of N ints or None
For each downsampling dimension, this specifies the number of zeros to
add as padding on both sides. For 2D and (pad_h, pad_w), pad_h specifies the
add as pad on both sides. For 2D and (pad_h, pad_w), pad_h specifies the
size of the top and bottom margins, pad_w specifies the size of the left and
right margins. No padding is added if padding is None.
right margins. No pad is added if pad is None.
ndim : int
The number of pooling dimensions N.
The default is 2.
......@@ -752,15 +930,72 @@ class PoolGrad(OpenMPOp):
ignore_border flags.
"""
if ds is not None:
if ws is not None:
raise Exception(
"You can't provide different tuple values to 'ws' and"
" 'ds'. You must provide only one of them as they both"
" refer to the window size. DEPRECATION NOTE: the"
" 'ds' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'ws'."
)
else:
warnings.warn(
"DEPRECATION: the 'ds' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'ws'.",
stacklevel=2
)
ws = ds
elif ds is None and ws is None:
raise Exception(
"You must provide a tuple value for the window size."
)
if st is not None:
if stride is not None:
raise Exception(
"You can't provide different tuple values to 'stride' and"
" st. You must provide only one of them as they"
" both refer to the stride size. DEPRECATION NOTE: the"
" 'st' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'stride'."
)
else:
warnings.warn(
"DEPRECATION: the 'st' parameter is not going to exist"
" anymore as it is going to be replaced by the parameter"
" 'stride'.",
stacklevel=2
)
stride = st
if padding is not None:
if pad is not None:
raise Exception(
"You can't provide different tuple value to 'pad' and"
" 'padding'. You must provide only one of them as they"
" both refer to the padding size. DEPRECATION NOTE: the"
" 'padding' parameter is not going to exist anymore as"
" it is going to be replaced by the parameter 'pad'."
)
else:
warnings.warn(
"DEPRECATION: the 'padding' parameter is not going to"
" exist anymore as it is going to be replaced by the"
" parameter 'pad'.",
stacklevel=2
)
pad = padding
if len(imgshape) < ndim:
raise TypeError('imgshape must have at least {} dimensions'.format(ndim))
if st is None:
st = ds
if padding is None:
padding = (0,) * ndim
patch_shape = tuple(tensor.extract_constant(imgshape[-ndim + i]) + padding[i] * 2
if stride is None:
stride = ws
if pad is None:
pad = (0,) * ndim
patch_shape = tuple(tensor.extract_constant(imgshape[-ndim + i]) + pad[i] * 2
for i in xrange(ndim))
def compute_out(v, downsample, stride):
......@@ -781,7 +1016,7 @@ class PoolGrad(OpenMPOp):
else:
return max(0, (v - 1 - downsample) // stride + 1) + 1
out_shape = [compute_out(patch_shape[i], ds[i], st[i]) for i in xrange(ndim)]
out_shape = [compute_out(patch_shape[i], ws[i], stride[i]) for i in xrange(ndim)]
rval = list(imgshape[:-ndim]) + out_shape
return rval
......@@ -799,11 +1034,11 @@ class PoolGrad(OpenMPOp):
def prepare_node(self, node, storage_map, compute_map, impl):
if len(node.inputs) < 5: # 5 for AveragePoolGrad, 6 for MaxPoolGrad
# Old interface
self.ndim = len(node.op.ds)
self.ndim = len(node.op.ws)
self.mode = node.op.mode
ws = theano.tensor.constant(node.op.ds)
st = theano.tensor.constant(node.op.st)
pad = theano.tensor.constant(node.op.padding)
ws = theano.tensor.constant(node.op.ws)
st = theano.tensor.constant(node.op.stride)
pad = theano.tensor.constant(node.op.pad)
node.inputs.append(ws)
node.inputs.append(st)
node.inputs.append(pad)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论