提交 5c172018 authored 作者: Harm de Vries's avatar Harm de Vries

gpu dnn pool takes tensor variables

上级 50e06772
差异被折叠。
...@@ -240,10 +240,11 @@ def test_pooling(): ...@@ -240,10 +240,11 @@ def test_pooling():
modes = ('max', 'average_inc_pad') modes = ('max', 'average_inc_pad')
else: else:
modes = ('max', 'average_inc_pad', 'average_exc_pad') modes = ('max', 'average_inc_pad', 'average_exc_pad')
x = T.ftensor4() x = T.ftensor4()
for mode, pad in product(modes, for mode, pad in product(modes,
((0, 0), (1, 0), (1, 0), (2, 3), (3, 2))): ((0, 0), (1, 0), (1, 0), (2, 3), (3, 2))):
if mode == 'max': if mode == 'max':
func = T.max func = T.max
else: else:
...@@ -285,22 +286,23 @@ def test_pooling(): ...@@ -285,22 +286,23 @@ def test_pooling():
a = f1(data).__array__() a = f1(data).__array__()
b = f2(data).__array__() b = f2(data).__array__()
utt.assert_allclose(a, b) assert numpy.allclose(a, b,
atol=numpy.finfo(numpy.float32).eps)
# Test the grad # Test the grad
for shp in [(1, 1, 2, 2), for shp in [(1, 1, 2, 2),
(1, 1, 3, 3)]: (1, 1, 3, 3)]:
data = numpy.random.normal(0, 1, shp).astype("float32") * 10 data = numpy.random.normal(0, 1, shp).astype("float32") * 10
ws = 2 ws = theano.shared(numpy.array([2, 2]))
stride = 2 stride = theano.shared(numpy.array([1, 1]))
if pad[0] > stride or pad[1] > stride: if pad[0] > 1 or pad[1] > 1:
# Not implemented # Not implemented
continue continue
pad_ = theano.shared(numpy.array(pad))
# This test the CPU grad + opt + GPU implemtentation ## This test the CPU grad + opt + GPU implemtentation
def fn(x): def fn(x):
return pool_2d(x, (ws, ws), ignore_border=True, return pool_2d(x, (2, 2), ignore_border=True,
padding=pad, mode=mode) padding=pad, mode=mode)
theano.tests.unittest_tools.verify_grad(fn, [data], theano.tests.unittest_tools.verify_grad(fn, [data],
cast_to_output_type=False, cast_to_output_type=False,
...@@ -310,15 +312,16 @@ def test_pooling(): ...@@ -310,15 +312,16 @@ def test_pooling():
mode=mode_with_gpu) mode=mode_with_gpu)
assert any([isinstance(node.op, cuda.dnn.GpuDnnPoolGrad) assert any([isinstance(node.op, cuda.dnn.GpuDnnPoolGrad)
for node in fg.maker.fgraph.toposort()]) for node in fg.maker.fgraph.toposort()])
# Test the GPU grad + GPU implementation # Test the GPU grad + GPU implementation
def fn(x): def fn(x):
dnn_op = cuda.dnn.dnn_pool( dnn_op = cuda.dnn.dnn_pool(
x, ws=(ws, ws), x, ws=ws,
stride=(stride, stride), stride=stride,
pad=pad, pad=pad_,
mode=mode) mode=mode)
return dnn_op return dnn_op
theano.tests.unittest_tools.verify_grad( theano.tests.unittest_tools.verify_grad(
fn, [data], fn, [data],
cast_to_output_type=False, cast_to_output_type=False,
...@@ -331,9 +334,10 @@ def test_pooling(): ...@@ -331,9 +334,10 @@ def test_pooling():
g_out = fg(data) g_out = fg(data)
# Compare again the CPU result # Compare again the CPU result
out = pool_2d(x, (ws, ws), out = pool_2d(x, (2, 2), st=(1, 1),
padding=pad, padding=pad,
ignore_border=True, mode=mode) ignore_border=True, mode=mode)
fc = theano.function([x], theano.grad(out.sum(), x), fc = theano.function([x], theano.grad(out.sum(), x),
mode=mode_without_gpu) mode=mode_without_gpu)
if mode == 'max': if mode == 'max':
...@@ -343,7 +347,7 @@ def test_pooling(): ...@@ -343,7 +347,7 @@ def test_pooling():
assert any([isinstance(node.op, AveragePoolGrad) assert any([isinstance(node.op, AveragePoolGrad)
for node in fc.maker.fgraph.toposort()]) for node in fc.maker.fgraph.toposort()])
c_out = fc(data) c_out = fc(data)
utt.assert_allclose(c_out, g_out) assert numpy.allclose(c_out, g_out)
def test_pooling3d(): def test_pooling3d():
...@@ -999,14 +1003,9 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -999,14 +1003,9 @@ class TestDnnInferShapes(utt.InferShapeTester):
[(1, 1), (2, 2), (3, 3)], [(1, 1), (2, 2), (3, 3)],
modes modes
): ):
desc = dnn.GpuDnnPoolDesc(
ws=params[0],
stride=params[1],
mode=params[2]
)()
self._compile_and_check( self._compile_and_check(
[img], [img],
[dnn.GpuDnnPool()(img, desc)], [dnn.GpuDnnPool(mode=params[2])(img, params[0], params[1], (0,0))],
[img_val], [img_val],
dnn.GpuDnnPool dnn.GpuDnnPool
) )
...@@ -1035,16 +1034,13 @@ class TestDnnInferShapes(utt.InferShapeTester): ...@@ -1035,16 +1034,13 @@ class TestDnnInferShapes(utt.InferShapeTester):
[(1, 1), (2, 2), (3, 3)], [(1, 1), (2, 2), (3, 3)],
['max', 'average_inc_pad'] ['max', 'average_inc_pad']
): ):
desc = dnn.GpuDnnPoolDesc(
ws=params[0],
stride=params[1],
mode=params[2]
)()
pool_grad = dnn.GpuDnnPoolGrad()( pool_grad = dnn.GpuDnnPoolGrad()(
img, img,
out, out,
img_grad, img_grad,
desc params[0],
params[1],
(0, 0)
) )
self._compile_and_check( self._compile_and_check(
[img, img_grad, out], [img, img_grad, out],
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论