提交 7f9a7034 authored 作者: Pascal Lamblin's avatar Pascal Lamblin

Merge pull request #2691 from rolfvandam/max_pool_same_size

implements max_pool_2d_same_size + test
...@@ -18,6 +18,23 @@ def max_pool2D(*args, **kwargs): ...@@ -18,6 +18,23 @@ def max_pool2D(*args, **kwargs):
print >> sys.stderr, "DEPRECATION: max_pool2D renamed to max_pool_2d" print >> sys.stderr, "DEPRECATION: max_pool2D renamed to max_pool_2d"
return max_pool_2d(*args, **kwargs) return max_pool_2d(*args, **kwargs)
def max_pool_2d_same_size(input, patch_size):
"""
Takes as input a 4-D tensor. It sets all non maximum values
of non-overlapping patches of size (patch_size[0],patch_size[1]) to zero,
keeping only the maximum values. The output has the same dimensions as
the input.
:type input: 4-D theano tensor of input images.
:param input: input images. Max pooling will be done over the 2 last
dimensions.
:type patch_size: tuple of length 2
:param patch_size: size of the patch (patch height, patch width).
(2,2) will retain only one non-zero value per patch of 4 values.
"""
output = DownsampleFactorMax(patch_size, True)(input)
outs = DownsampleFactorMaxGrad(patch_size, True)(input, output, output)
return outs
def max_pool_2d(input, ds, ignore_border=False, st=None, padding=(0, 0)): def max_pool_2d(input, ds, ignore_border=False, st=None, padding=(0, 0)):
""" """
...@@ -43,7 +60,6 @@ def max_pool_2d(input, ds, ignore_border=False, st=None, padding=(0, 0)): ...@@ -43,7 +60,6 @@ def max_pool_2d(input, ds, ignore_border=False, st=None, padding=(0, 0)):
of the images, pad_h is the size of the top and bottom margins, 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. and pad_w is the size of the left and right margins.
:type padding: tuple of two ints :type padding: tuple of two ints
""" """
if input.ndim < 2: if input.ndim < 2:
raise NotImplementedError('max_pool_2d requires a dimension >= 2') raise NotImplementedError('max_pool_2d requires a dimension >= 2')
......
...@@ -4,7 +4,7 @@ import numpy ...@@ -4,7 +4,7 @@ import numpy
import theano.tensor as tensor import theano.tensor as tensor
from theano.tests import unittest_tools as utt from theano.tests import unittest_tools as utt
from theano.tensor.signal.downsample import (DownsampleFactorMax, max_pool_2d, from theano.tensor.signal.downsample import (DownsampleFactorMax, max_pool_2d,
DownsampleFactorMaxGrad) DownsampleFactorMaxGrad, max_pool_2d_same_size)
from theano import function from theano import function
...@@ -464,6 +464,29 @@ class TestDownsampleFactorMax(utt.InferShapeTester): ...@@ -464,6 +464,29 @@ class TestDownsampleFactorMax(utt.InferShapeTester):
return max_pool_2d(input, maxpoolshp, ignore_border) return max_pool_2d(input, maxpoolshp, ignore_border)
utt.verify_grad(mp, [imval], rng=rng) utt.verify_grad(mp, [imval], rng=rng)
def test_max_pool_2d_2D_same_size(self):
rng = numpy.random.RandomState(utt.fetch_seed())
test_input_array = numpy.array([[[
[1.,2.,3.,4.],
[5.,6.,7.,8.]
]]])
test_answer_array = numpy.array([[[
[0.,0.,0.,0.],
[0.,6.,0.,8.]
]]])
input = tensor.tensor4(name='input')
patch_size = (2,2)
op = max_pool_2d_same_size(input, patch_size)
op_output = function([input], op)(test_input_array)
assert numpy.all(op_output == test_answer_array), (
"op_output is %s, test_answer_array is %s" % (
op_output, numpy_output_val
)
)
def mp(input):
return max_pool_2d_same_size(input, patch_size)
utt.verify_grad(mp, [test_input_array], rng=rng)
def test_max_pool_2d_3D(self): def test_max_pool_2d_3D(self):
rng = numpy.random.RandomState(utt.fetch_seed()) rng = numpy.random.RandomState(utt.fetch_seed())
maxpoolshps = [(1, 2)] maxpoolshps = [(1, 2)]
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论