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