提交 94b43288 authored 作者: Cesar Laurent's avatar Cesar Laurent

Adressed Freds comments

上级 1ad6fae7
.. _libdoc_tensor_signal_downsample:
======================================================
:mod:`downsample` -- Down-Sampling
======================================================
.. module:: downsample
:platform: Unix, Windows
:synopsis: ops for performing various forms of downsampling
.. moduleauthor:: LISA
.. note::
This module is deprecated. Use the functions in :func:`theano.tensor.nnet.signal.pool`
...@@ -21,3 +21,4 @@ forms of signal processing. ...@@ -21,3 +21,4 @@ forms of signal processing.
conv conv
pool pool
downsample
...@@ -243,7 +243,6 @@ class Pool(OpenMPOp): ...@@ -243,7 +243,6 @@ class Pool(OpenMPOp):
def prepare_node(self, node, storage_map, compute_map): def prepare_node(self, node, storage_map, compute_map):
if len(node.inputs) == 1: if len(node.inputs) == 1:
warnings.warn("Theano Pool internal changed.", stacklevel=3)
# Old interface # Old interface
self.mode = node.op.mode self.mode = node.op.mode
ws = theano.tensor.constant(node.op.ds) ws = theano.tensor.constant(node.op.ds)
...@@ -277,12 +276,10 @@ class Pool(OpenMPOp): ...@@ -277,12 +276,10 @@ class Pool(OpenMPOp):
if stride is None: if stride is None:
stride = ws stride = ws
if isinstance(pad, (tuple, list)): if isinstance(pad, (tuple, list)):
pad = tuple(pad) if tuple(pad) != (0, 0) and not self.ignore_border:
if pad != (0, 0) and not self.ignore_border:
raise NotImplementedError( raise NotImplementedError(
'padding works only with ignore_border=True') 'padding works only with ignore_border=True')
if isinstance(ws, (tuple, list)): if isinstance(ws, (tuple, list)):
ws = tuple(ws)
if pad[0] >= ws[0] or pad[1] >= ws[1]: if pad[0] >= ws[0] or pad[1] >= ws[1]:
raise NotImplementedError( raise NotImplementedError(
'padding_h and padding_w must be smaller than strides') 'padding_h and padding_w must be smaller than strides')
...@@ -308,6 +305,7 @@ class Pool(OpenMPOp): ...@@ -308,6 +305,7 @@ class Pool(OpenMPOp):
def perform(self, node, inp, out): def perform(self, node, inp, out):
x, ws, stride, pad = inp x, ws, stride, pad = inp
z, = out z, = out
assert ws.shape == stride.shape == pad.shape == (2,)
if len(x.shape) != 4: if len(x.shape) != 4:
raise NotImplementedError( raise NotImplementedError(
'Pool requires 4D input for now') 'Pool requires 4D input for now')
...@@ -401,6 +399,21 @@ class Pool(OpenMPOp): ...@@ -401,6 +399,21 @@ class Pool(OpenMPOp):
else: else:
omp_parallel = '' omp_parallel = ''
ccode = """ ccode = """
if(PyArray_DIM(%(ws)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "ws must be a vector of size 2");
%(fail)s;
}
if(PyArray_DIM(%(stride)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "stride must be a vector of size 2");
%(fail)s;
}
if(PyArray_DIM(%(pad)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "pad must be a vector of size 2");
%(fail)s;
}
// Getting ws, stride and pad // Getting ws, stride and pad
int ws0, ws1, st0, st1, pd0, pd1; int ws0, ws1, st0, st1, pd0, pd1;
ws0 = *((npy_intp*)PyArray_GETPTR1(%(ws)s, 0)); ws0 = *((npy_intp*)PyArray_GETPTR1(%(ws)s, 0));
...@@ -675,7 +688,6 @@ class PoolGrad(OpenMPOp): ...@@ -675,7 +688,6 @@ class PoolGrad(OpenMPOp):
def prepare_node(self, node, storage_map, compute_map): def prepare_node(self, node, storage_map, compute_map):
if len(node.inputs) < 5: # 5 for AveragePoolGrad, 6 for MaxPoolGrad if len(node.inputs) < 5: # 5 for AveragePoolGrad, 6 for MaxPoolGrad
warnings.warn("Theano PoolGrad internal changed.", stacklevel=3)
# Old interface # Old interface
self.mode = node.op.mode self.mode = node.op.mode
ws = theano.tensor.constant(node.op.ds) ws = theano.tensor.constant(node.op.ds)
...@@ -728,12 +740,19 @@ class MaxPoolGrad(PoolGrad): ...@@ -728,12 +740,19 @@ class MaxPoolGrad(PoolGrad):
assert isinstance(ws, Variable) and ws.ndim == 1 assert isinstance(ws, Variable) and ws.ndim == 1
assert isinstance(stride, Variable) and stride.ndim == 1 assert isinstance(stride, Variable) and stride.ndim == 1
assert isinstance(pad, Variable) and pad.ndim == 1 assert isinstance(pad, Variable) and pad.ndim == 1
if not ws.dtype.startswith('int'):
raise TypeError('Pool downsample parameters must be ints.')
if not stride.dtype.startswith('int'):
raise TypeError('Stride parameters must be ints.')
if not pad.dtype.startswith('int'):
raise TypeError('Padding parameters must be ints.')
return Apply(self, [x, maxout, gz, ws, stride, pad], [x.type()]) return Apply(self, [x, maxout, gz, ws, stride, pad], [x.type()])
def perform(self, node, inp, out): def perform(self, node, inp, out):
assert self.mode == 'max' assert self.mode == 'max'
x, maxout, gz, ws, stride, pad = inp x, maxout, gz, ws, stride, pad = inp
gx_stg, = out gx_stg, = out
assert ws.shape == stride.shape == pad.shape == (2,)
# number of pooling output rows # number of pooling output rows
pr = maxout.shape[-2] pr = maxout.shape[-2]
# number of pooling output cols # number of pooling output cols
...@@ -817,6 +836,21 @@ class MaxPoolGrad(PoolGrad): ...@@ -817,6 +836,21 @@ class MaxPoolGrad(PoolGrad):
PyErr_SetString(PyExc_ValueError, "gz must be a 4d ndarray"); PyErr_SetString(PyExc_ValueError, "gz must be a 4d ndarray");
%(fail)s; %(fail)s;
} }
if(PyArray_DIM(%(ws)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "ws must be a vector of size 2");
%(fail)s;
}
if(PyArray_DIM(%(stride)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "stride must be a vector of size 2");
%(fail)s;
}
if(PyArray_DIM(%(pad)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "pad must be a vector of size 2");
%(fail)s;
}
// Getting ws, stride and pad // Getting ws, stride and pad
int ws0, ws1, st0, st1, pd0, pd1; int ws0, ws1, st0, st1, pd0, pd1;
ws0 = *((npy_intp*)PyArray_GETPTR1(%(ws)s, 0)); ws0 = *((npy_intp*)PyArray_GETPTR1(%(ws)s, 0));
...@@ -927,11 +961,18 @@ class AveragePoolGrad(PoolGrad): ...@@ -927,11 +961,18 @@ class AveragePoolGrad(PoolGrad):
assert isinstance(ws, Variable) and ws.ndim == 1 assert isinstance(ws, Variable) and ws.ndim == 1
assert isinstance(stride, Variable) and stride.ndim == 1 assert isinstance(stride, Variable) and stride.ndim == 1
assert isinstance(pad, Variable) and pad.ndim == 1 assert isinstance(pad, Variable) and pad.ndim == 1
if not ws.dtype.startswith('int'):
raise TypeError('Pool downsample parameters must be ints.')
if not stride.dtype.startswith('int'):
raise TypeError('Stride parameters must be ints.')
if not pad.dtype.startswith('int'):
raise TypeError('Padding parameters must be ints.')
return Apply(self, [x, gz, ws, stride, pad], [x.type()]) return Apply(self, [x, gz, ws, stride, pad], [x.type()])
def perform(self, node, inp, out): def perform(self, node, inp, out):
x, gz, ws, stride, pad = inp x, gz, ws, stride, pad = inp
gx_stg, = out gx_stg, = out
assert ws.shape == stride.shape == pad.shape == (2,)
if self.mode == 'average_exc_pad' and pad[0] != 0 and pad[1] != 0: if self.mode == 'average_exc_pad' and pad[0] != 0 and pad[1] != 0:
raise NotImplementedError() raise NotImplementedError()
z_shape = self.out_shape(x.shape, ws, self.ignore_border, stride, pad) z_shape = self.out_shape(x.shape, ws, self.ignore_border, stride, pad)
...@@ -1016,12 +1057,10 @@ class DownsampleFactorMaxGradGrad(OpenMPOp): ...@@ -1016,12 +1057,10 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
if stride is None: if stride is None:
stride = ws stride = ws
if isinstance(pad, (tuple, list)): if isinstance(pad, (tuple, list)):
pad = tuple(pad) if tuple(pad) != (0, 0) and not self.ignore_border:
if pad != (0, 0) and not self.ignore_border:
raise NotImplementedError( raise NotImplementedError(
'padding works only with ignore_border=True') 'padding works only with ignore_border=True')
if isinstance(ws, (tuple, list)): if isinstance(ws, (tuple, list)):
ws = tuple(ws)
if pad[0] >= ws[0] or pad[1] >= ws[1]: if pad[0] >= ws[0] or pad[1] >= ws[1]:
raise NotImplementedError( raise NotImplementedError(
'padding_h and padding_w must be smaller than strides') 'padding_h and padding_w must be smaller than strides')
...@@ -1042,6 +1081,7 @@ class DownsampleFactorMaxGradGrad(OpenMPOp): ...@@ -1042,6 +1081,7 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
def perform(self, node, inp, out): def perform(self, node, inp, out):
x, maxout, ggx, ws, stride, pad = inp x, maxout, ggx, ws, stride, pad = inp
z, = out z, = out
assert ws.shape == stride.shape == pad.shape == (2,)
if len(x.shape) != 4: if len(x.shape) != 4:
raise NotImplementedError( raise NotImplementedError(
'DownsampleFactorMaxGradGrad requires 4D input for now') 'DownsampleFactorMaxGradGrad requires 4D input for now')
...@@ -1114,6 +1154,21 @@ class DownsampleFactorMaxGradGrad(OpenMPOp): ...@@ -1114,6 +1154,21 @@ class DownsampleFactorMaxGradGrad(OpenMPOp):
else: else:
omp_parallel = '' omp_parallel = ''
return """ return """
if(PyArray_DIM(%(ws)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "ws must be a vector of size 2");
%(fail)s;
}
if(PyArray_DIM(%(stride)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "stride must be a vector of size 2");
%(fail)s;
}
if(PyArray_DIM(%(pad)s, 0)!=2)
{
PyErr_SetString(PyExc_ValueError, "pad must be a vector of size 2");
%(fail)s;
}
// Getting ws, stride and pad // Getting ws, stride and pad
int ws0, ws1, st0, st1, pd0, pd1; int ws0, ws1, st0, st1, pd0, pd1;
ws0 = *((npy_intp*)PyArray_GETPTR1(%(ws)s, 0)); ws0 = *((npy_intp*)PyArray_GETPTR1(%(ws)s, 0));
......
...@@ -842,6 +842,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -842,6 +842,7 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def test_old_pool_interface(self): def test_old_pool_interface(self):
if sys.version_info[0] != 3: if sys.version_info[0] != 3:
# Only tested with python 3 because of pickling issues.
raise SkipTest('Skip old pool interface with python 2.x') raise SkipTest('Skip old pool interface with python 2.x')
# 1. Load the old version # 1. Load the old version
testfile_dir = os.path.dirname(os.path.realpath(__file__)) testfile_dir = os.path.dirname(os.path.realpath(__file__))
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论