提交 bff32d9a authored 作者: Yann N. Dauphin's avatar Yann N. Dauphin

testing different strides and pooling modes

上级 33d16586
...@@ -6,6 +6,8 @@ import theano ...@@ -6,6 +6,8 @@ import theano
from theano.gof.python25 import any from theano.gof.python25 import any
import theano.tensor as T import theano.tensor as T
import theano.tests.unittest_tools as utt import theano.tests.unittest_tools as utt
from theano.sandbox.neighbours import images2neibs, neibs2images
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
...@@ -21,14 +23,39 @@ else: ...@@ -21,14 +23,39 @@ else:
mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpu') mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpu')
def pool_2d_i2n(input, ds=(2, 2), strides=None, pool_function=T.max, mode='ignore_borders'):
if strides is None:
strides = ds
if strides[0] > ds[0] or strides[1] > ds[1]:
raise RuntimeError("strides should be smaller than or equal to ds, strides=(%d, %d) and ds=(%d, %d)" %
(strides + ds))
shape = input.shape
neibs = images2neibs(input, ds, strides, mode=mode)
pooled_neibs = pool_function(neibs, axis=1)
output_width = (shape[2] - ds[0]) // strides[0] + 1
output_height = (shape[3] - ds[1]) // strides[1] + 1
pooled_output = pooled_neibs.reshape((shape[0], shape[1], output_width, output_height))
return pooled_output
def test_pooling(): def test_pooling():
if not cuda.dnn.dnn_available(): if not cuda.dnn.dnn_available():
raise SkipTest(cuda.dnn.dnn_available.msg) raise SkipTest(cuda.dnn.dnn_available.msg)
x = T.tensor4() x = T.tensor4()
out1 = cuda.dnn.dnn_pool(x, ws=(5,5), stride=(5, 5)) for func in (T.max, T.mean):
out2 = T.signal.downsample.max_pool_2d(x, (5, 5)) for ws in (4, 5):
for stride in (2, 3):
out1 = cuda.dnn.dnn_pool(x, ws=(ws, ws), stride=(stride, stride),
mode='max' if func is T.max else "average")
out2 = pool_2d_i2n(x, ds=(ws, ws), strides=(stride, stride),
pool_function=func)
f1 = theano.function([x], out1) f1 = theano.function([x], out1)
f2 = theano.function([x], out2) f2 = theano.function([x], out2)
...@@ -38,13 +65,4 @@ def test_pooling(): ...@@ -38,13 +65,4 @@ def test_pooling():
b = f2(data).__array__() b = f2(data).__array__()
assert numpy.allclose(a, b) assert numpy.allclose(a, b, atol=numpy.finfo(numpy.float32).eps)
gf1 = theano.function([x], theano.grad(out1.sum(), x))
gf2 = theano.function([x], theano.grad(out2.sum(), x))
ga = gf1(data).__array__()
gb = gf2(data).__array__()
assert numpy.allclose(ga, gb)
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论