提交 6b344997 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Merge pull request #2579 from nouiz/bugfix_pool_pad

[BUG] pool with padding
......@@ -220,10 +220,7 @@ class DownsampleFactorMax(Op):
z_shape = self.out_shape(x.shape, self.ds, self.ignore_border, self.st,
self.padding)
if (z[0] is None) or (z[0].shape != z_shape):
z[0] = numpy.empty(
self.out_shape(x.shape, self.ds, self.ignore_border,
self.st, self.padding),
dtype=x.dtype)
z[0] = numpy.empty(z_shape, dtype=x.dtype)
zz = z[0]
# number of pooling output rows
pr = zz.shape[-2]
......@@ -237,11 +234,14 @@ class DownsampleFactorMax(Op):
img_cols = x.shape[-1] + 2 * pad_w
# pad the image
fill = x.min()-1.
y = numpy.zeros(
(x.shape[0], x.shape[1], img_rows, img_cols),
dtype=x.dtype) + fill
y[:, :, pad_h:(img_rows-pad_h), pad_w:(img_cols-pad_w)] = x
if self.padding != (0, 0):
fill = x.min()-1.
y = numpy.zeros(
(x.shape[0], x.shape[1], img_rows, img_cols),
dtype=x.dtype) + fill
y[:, :, pad_h:(img_rows-pad_h), pad_w:(img_cols-pad_w)] = x
else:
y = x
# max pooling
for n in xrange(x.shape[0]):
for k in xrange(x.shape[1]):
......@@ -272,7 +272,7 @@ class DownsampleFactorMax(Op):
# No implementation is currently for the case where
# the stride size and the pooling size are different.
# An exception is raised for such a case.
if self.ds != self.st:
if self.ds != self.st or self.padding != (0, 0):
raise theano.gof.utils.MethodNotDefined()
x, = inp
z, = out
......@@ -346,7 +346,7 @@ class DownsampleFactorMax(Op):
""" % locals()
def c_code_cache_version(self):
return (0, 1)
return (0, 2)
class DownsampleFactorMaxGrad(Op):
......@@ -389,10 +389,14 @@ class DownsampleFactorMaxGrad(Op):
img_cols = x.shape[-1] + 2 * pad_w
# pad the image
fill = x.min()-1
y = numpy.zeros(
(x.shape[0], x.shape[1], img_rows, img_cols), dtype=x.dtype) + fill
y[:, :, pad_h:(img_rows-pad_h), pad_w:(img_cols-pad_w)] = x
if self.padding != (0, 0):
fill = x.min()-1
y = numpy.zeros(
(x.shape[0], x.shape[1], img_rows, img_cols),
dtype=x.dtype) + fill
y[:, :, pad_h:(img_rows-pad_h), pad_w:(img_cols-pad_w)] = x
else:
y = x
gx = numpy.zeros_like(y)
for n in xrange(x.shape[0]):
for k in xrange(x.shape[1]):
......@@ -429,7 +433,7 @@ class DownsampleFactorMaxGrad(Op):
self, 2, gz, 'Hessian not implemented with padding')]
def c_code(self, node, name, inp, out, sub):
if self.ds != self.st:
if self.ds != self.st or self.padding != (0, 0):
raise theano.gof.utils.MethodNotDefined()
x, z, gz = inp
gx, = out
......@@ -523,7 +527,7 @@ class DownsampleFactorMaxGrad(Op):
""" % locals()
def c_code_cache_version(self):
return (0, 1)
return (0, 2)
class DownsampleFactorMaxGradGrad(Op):
......
......@@ -242,10 +242,10 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def test_DownsampleFactorMaxPaddingStride(self):
ignore_border = True # padding does not support ignore_border=False
rng = numpy.random.RandomState(utt.fetch_seed())
maxpoolsizes = [(3, 3), (4, 4), (3, 4), (4, 3)]
stridesizes = [(2, 2), (2, 2), (1, 1), (1, 2)]
paddingsizes = [(2, 2), (1, 2), (2, 1), (0, 0)]
imgsizes = [(5, 5), (5, 5), (5, 6), (6, 5)]
maxpoolsizes = [(3, 3), (4, 4), (3, 4), (4, 3), (2, 2)]
stridesizes = [(2, 2), (2, 2), (1, 1), (1, 2), (2, 2)]
paddingsizes = [(2, 2), (1, 2), (2, 1), (0, 0), (1, 1)]
imgsizes = [(5, 5), (5, 5), (5, 6), (6, 5), (5, 5)]
m = 4 # minibatch
c = 10 # channel size
images = tensor.dtensor4()
......@@ -267,10 +267,10 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
def test_DownsampleFactorMaxPaddingStride_grad(self):
rng = numpy.random.RandomState(utt.fetch_seed())
imgsizes = ((10, 10), (10, 5))
maxpoolsizes = ((5, 3),(3, 5))
stridesizes = ((3, 2), (2, 3))
paddingsizes = ((2, 2),(2, 1))
imgsizes = ((10, 10), (10, 5), (5, 5))
maxpoolsizes = ((5, 3),(3, 5), (3, 3))
stridesizes = ((3, 2), (2, 3), (3, 3))
paddingsizes = ((2, 2),(2, 1), (2, 2))
for i in range(len(imgsizes)):
imgsize = imgsizes[i]
imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论