提交 b2be84e2 authored 作者: Frederic's avatar Frederic

pep8 fix.

上级 bfecc533
import numpy import numpy
import numpy.random
import theano import theano
from theano import shared, function from theano import shared, function
import theano.tensor as T import theano.tensor as T
from neighbours import images2neibs, neibs2images, Images2Neibs, GpuImages2Neibs from neighbours import (images2neibs, neibs2images,
Images2Neibs, GpuImages2Neibs)
# Skip test if cuda_ndarray is not available. # Skip test if cuda_ndarray is not available.
from nose.plugins.skip import SkipTest from nose.plugins.skip import SkipTest
import theano.sandbox.cuda as cuda import theano.sandbox.cuda as cuda
from theano.tests import unittest_tools from theano.tests import unittest_tools
if theano.config.mode=='FAST_COMPILE': if theano.config.mode == 'FAST_COMPILE':
mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu') mode_with_gpu = theano.compile.mode.get_mode('FAST_RUN').including('gpu')
mode_without_gpu = theano.compile.mode.get_mode('FAST_RUN').excluding('gpu') mode_without_gpu = theano.compile.mode.get_mode(
'FAST_RUN').excluding('gpu')
else: else:
mode_with_gpu = theano.compile.mode.get_default_mode().including('gpu') mode_with_gpu = theano.compile.mode.get_default_mode().including('gpu')
mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpu') mode_without_gpu = theano.compile.mode.get_default_mode().excluding('gpu')
def test_neibs(): def test_neibs():
shape = (100,40,18,18) shape = (100, 40, 18, 18)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((2,2))#(array((2,2), dtype='float32')) neib_shape = T.as_tensor_variable((2, 2))
f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu)
#print images.get_value(borrow=True) #print images.get_value(borrow=True)
neibs = f() neibs = f()
#print neibs #print neibs
g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu) g = function([], neibs2images(neibs, neib_shape, images.shape),
mode=mode_without_gpu)
#print g() #print g()
assert numpy.allclose(images.get_value(borrow=True),g()) assert numpy.allclose(images.get_value(borrow=True), g())
def test_neibs_bad_shape(): def test_neibs_bad_shape():
shape = (2,3,10,10) shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3,2)) neib_shape = T.as_tensor_variable((3, 2))
try: try:
f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
assert False,"An error was expected" assert False, "An error was expected"
except TypeError: except TypeError:
pass pass
shape = (2,3,10,10) shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((2,3)) neib_shape = T.as_tensor_variable((2, 3))
try: try:
f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
assert False,"An error was expected" assert False, "An error was expected"
except TypeError: except TypeError:
pass pass
def test_neibs_bad_shape_warp_centered(): def test_neibs_bad_shape_warp_centered():
shape = (2,3,10,10) shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3,2)) neib_shape = T.as_tensor_variable((3, 2))
try: try:
f = function([], images2neibs(images, neib_shape, mode="wrap_centered"), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
assert False,"An error was expected" assert False, "An error was expected"
except TypeError: except TypeError:
pass pass
shape = (2,3,10,10) shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((2,3)) neib_shape = T.as_tensor_variable((2, 3))
try: try:
f = function([], images2neibs(images, neib_shape, mode="wrap_centered"), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
assert False,"An error was expected" assert False, "An error was expected"
except TypeError: except TypeError:
pass pass
shape = (2,3,2,3) shape = (2, 3, 2, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3,3)) neib_shape = T.as_tensor_variable((3, 3))
try: try:
f = function([], images2neibs(images, neib_shape, mode="wrap_centered"), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
assert False,"An error was expected" assert False, "An error was expected"
except TypeError: except TypeError:
pass pass
shape = (2,3,3,2) shape = (2, 3, 3, 2)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3,3)) neib_shape = T.as_tensor_variable((3, 3))
try: try:
f = function([], images2neibs(images, neib_shape, mode="wrap_centered"), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
assert False,"An error was expected" assert False, "An error was expected"
except TypeError,e: except TypeError, e:
pass pass
shape = (2,3,3,3) shape = (2, 3, 3, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3,3)) neib_shape = T.as_tensor_variable((3, 3))
f = function([], images2neibs(images, neib_shape, mode="wrap_centered"), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape, mode="wrap_centered"),
mode=mode_without_gpu)
neibs = f() neibs = f()
#print neibs #print neibs
def test_neibs_manual(): def test_neibs_manual():
shape = (2,3,4,4) shape = (2, 3, 4, 4)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((2,2)) neib_shape = T.as_tensor_variable((2, 2))
f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu)
...@@ -148,29 +165,34 @@ def test_neibs_manual(): ...@@ -148,29 +165,34 @@ def test_neibs_manual():
[82, 83, 86, 87], [82, 83, 86, 87],
[88, 89, 92, 93], [88, 89, 92, 93],
[90, 91, 94, 95]]) [90, 91, 94, 95]])
g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu) g = function([], neibs2images(neibs, neib_shape, images.shape),
mode=mode_without_gpu)
#print g() #print g()
assert numpy.allclose(images.get_value(borrow=True), g()) assert numpy.allclose(images.get_value(borrow=True), g())
def test_neibs_step_manual(): def test_neibs_step_manual():
shape = (2,3,5,5) shape = (2, 3, 5, 5)
images = shared(numpy.asarray(numpy.arange(numpy.prod(shape)).reshape(shape),dtype='float32')) images = shared(numpy.asarray(numpy.arange(numpy.prod(
neib_shape = T.as_tensor_variable((3,3)) shape)).reshape(shape), dtype='float32'))
neib_step = T.as_tensor_variable((2,2)) neib_shape = T.as_tensor_variable((3, 3))
neib_step = T.as_tensor_variable((2, 2))
modes = [mode_without_gpu] modes = [mode_without_gpu]
if cuda.cuda_available: if cuda.cuda_available:
modes.append(mode_with_gpu) modes.append(mode_with_gpu)
for mode_idx,mode in enumerate(modes): for mode_idx, mode in enumerate(modes):
f = function([], images2neibs(images, neib_shape, neib_step), mode=mode) f = function([], images2neibs(images, neib_shape, neib_step),
mode=mode)
#print images.get_value(borrow=True) #print images.get_value(borrow=True)
neibs = f() neibs = f()
if mode_idx==0: if mode_idx == 0:
assert Images2Neibs in [type(node.op) for node in f.maker.env.toposort()] assert Images2Neibs in [type(node.op)
elif mode_idx==1: for node in f.maker.env.toposort()]
assert GpuImages2Neibs in [type(node.op) for node in f.maker.env.toposort()] elif mode_idx == 1:
assert GpuImages2Neibs in [type(node.op)
for node in f.maker.env.toposort()]
assert numpy.allclose(neibs, assert numpy.allclose(neibs,
[[ 0, 1, 2, 5, 6, 7, 10, 11, 12], [[ 0, 1, 2, 5, 6, 7, 10, 11, 12],
...@@ -202,6 +224,7 @@ def test_neibs_step_manual(): ...@@ -202,6 +224,7 @@ def test_neibs_step_manual():
#print g() #print g()
#assert numpy.allclose(images.get_value(borrow=True),g()) #assert numpy.allclose(images.get_value(borrow=True),g())
def test_neibs_wrap_centered_step_manual(): def test_neibs_wrap_centered_step_manual():
modes = [mode_without_gpu] modes = [mode_without_gpu]
...@@ -221,57 +244,63 @@ def test_neibs_wrap_centered_step_manual(): ...@@ -221,57 +244,63 @@ def test_neibs_wrap_centered_step_manual():
[ 22, 23, 24, 2, 3, 4, 7, 8, 9], [ 22, 23, 24, 2, 3, 4, 7, 8, 9],
[ 14, 10, 11, 19, 15, 16, 24, 20, 21], [ 14, 10, 11, 19, 15, 16, 24, 20, 21],
[ 12, 13, 14, 17, 18, 19, 22, 23, 24]] [ 12, 13, 14, 17, 18, 19, 22, 23, 24]]
expected3 = [[ 19, 15, 16, 24, 20, 21, 4, 0, 1, 9, 5, 6, 14, 10, 11], expected3 = [[19, 15, 16, 24, 20, 21, 4, 0, 1, 9, 5, 6, 14, 10, 11],
[ 17, 18, 19, 22, 23, 24, 2, 3, 4, 7, 8, 9, 12, 13, 14], [17, 18, 19, 22, 23, 24, 2, 3, 4, 7, 8, 9, 12, 13, 14],
[ 9, 5, 6, 14, 10, 11, 19, 15, 16, 24, 20, 21, 4, 0, 1], [ 9, 5, 6, 14, 10, 11, 19, 15, 16, 24, 20, 21, 4, 0, 1],
[ 7, 8, 9, 12, 13, 14, 17, 18, 19, 22, 23, 24, 2, 3, 4]] [ 7, 8, 9, 12, 13, 14, 17, 18, 19, 22, 23, 24, 2, 3, 4]]
expected4 = [[ 23, 24, 20, 21, 22, 3, 4, 0, 1, 2, 8, 9, 5, 6, 7], expected4 = [[23, 24, 20, 21, 22, 3, 4, 0, 1, 2, 8, 9, 5, 6, 7],
[ 21, 22, 23, 24, 20, 1, 2, 3, 4, 0, 6, 7, 8, 9, 5], [21, 22, 23, 24, 20, 1, 2, 3, 4, 0, 6, 7, 8, 9, 5],
[ 13, 14, 10, 11, 12, 18, 19, 15, 16, 17, 23, 24, 20, 21, 22], [13, 14, 10, 11, 12, 18, 19, 15, 16, 17, 23, 24, 20, 21, 22],
[ 11, 12, 13, 14, 10, 16, 17, 18, 19, 15, 21, 22, 23, 24, 20]] [11, 12, 13, 14, 10, 16, 17, 18, 19, 15, 21, 22, 23, 24, 20]]
expected5 = [[ 24, 20, 21, 4, 0, 1, 9, 5, 6], expected5 = [[24, 20, 21, 4, 0, 1, 9, 5, 6],
[ 22, 23, 24, 2, 3, 4, 7, 8, 9], [22, 23, 24, 2, 3, 4, 7, 8, 9],
[ 9, 5, 6, 14, 10, 11, 19, 15, 16], [ 9, 5, 6, 14, 10, 11, 19, 15, 16],
[ 7, 8, 9, 12, 13, 14, 17, 18, 19], [ 7, 8, 9, 12, 13, 14, 17, 18, 19],
[ 19, 15, 16, 24, 20, 21, 4, 0, 1], [19, 15, 16, 24, 20, 21, 4, 0, 1],
[ 17, 18, 19, 22, 23, 24, 2, 3, 4]] [17, 18, 19, 22, 23, 24, 2, 3, 4]]
expected6 = [[ 24, 20, 21, 4, 0, 1, 9, 5, 6], expected6 = [[24, 20, 21, 4, 0, 1, 9, 5, 6],
[ 21, 22, 23, 1, 2, 3, 6, 7, 8], [21, 22, 23, 1, 2, 3, 6, 7, 8],
[ 23, 24, 20, 3, 4, 0, 8, 9, 5], [23, 24, 20, 3, 4, 0, 8, 9, 5],
[ 14, 10, 11, 19, 15, 16, 24, 20, 21], [14, 10, 11, 19, 15, 16, 24, 20, 21],
[ 11, 12, 13, 16, 17, 18, 21, 22, 23], [11, 12, 13, 16, 17, 18, 21, 22, 23],
[ 13, 14, 10, 18, 19, 15, 23, 24, 20]] [13, 14, 10, 18, 19, 15, 23, 24, 20]]
#TODO test discontinous image #TODO test discontinous image
for shp_idx,(shape,neib_shape,neib_step,expected) in enumerate([ for shp_idx, (shape, neib_shape, neib_step, expected) in enumerate([
[(7,8,5,5),(3,3),(2,2),expected1], [(7, 8, 5, 5), (3, 3), (2, 2), expected1],
[(7,8,5,5),(3,3),(3,3),expected2], [(7, 8, 5, 5), (3, 3), (3, 3), expected2],
[(7,8,5,5),(5,3),(3,3),expected3], [(7, 8, 5, 5), (5, 3), (3, 3), expected3],
[(7,8,5,5),(3,5),(3,3),expected4], [(7, 8, 5, 5), (3, 5), (3, 3), expected4],
[(80,90,5,5),(3,3),(2,3),expected5], [(80, 90, 5, 5), (3, 3), (2, 3), expected5],
[(1025,9,5,5),(3,3),(3,2),expected6], [(1025, 9, 5, 5), (3, 3), (3, 2), expected6],
[(1,1,5,1035),(3,3),(3,3),None], [(1, 1, 5, 1035), (3, 3), (3, 3), None],
[(1,1,1045,5),(3,3),(3,3),None], [(1, 1, 1045, 5), (3, 3), (3, 3), None],
]): ]):
images = shared(numpy.asarray(numpy.arange(numpy.prod(shape)).reshape(shape),dtype='float32')) images = shared(numpy.asarray(numpy.arange(numpy.prod(
shape)).reshape(shape), dtype='float32'))
neib_shape = T.as_tensor_variable(neib_shape) neib_shape = T.as_tensor_variable(neib_shape)
neib_step = T.as_tensor_variable(neib_step) neib_step = T.as_tensor_variable(neib_step)
expected = numpy.asarray(expected) expected = numpy.asarray(expected)
for mode_idx,mode in enumerate(modes): for mode_idx, mode in enumerate(modes):
f = function([], images2neibs(images, neib_shape, neib_step, mode="wrap_centered"), mode=mode) f = function([], images2neibs(images, neib_shape, neib_step,
mode="wrap_centered"), mode=mode)
neibs = f() neibs = f()
if expected.size>1: if expected.size > 1:
for i in range(shape[0]*shape[1]): for i in range(shape[0] * shape[1]):
assert numpy.allclose(neibs[i*expected.shape[0]:(i+1)*expected.shape[0],:],expected+25*i), mode_idx assert numpy.allclose(neibs[i * expected.shape[0]:
(i + 1) * expected.shape[0], :],
expected + 25 * i), mode_idx
if mode_idx==0: if mode_idx == 0:
assert Images2Neibs in [type(node.op) for node in f.maker.env.toposort()] assert Images2Neibs in [type(node.op)
elif mode_idx==1: for node in f.maker.env.toposort()]
assert GpuImages2Neibs in [type(node.op) for node in f.maker.env.toposort()] elif mode_idx == 1:
assert GpuImages2Neibs in [type(node.op)
for node in f.maker.env.toposort()]
#g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu) #g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu)
...@@ -281,61 +310,64 @@ def test_neibs_wrap_centered_step_manual(): ...@@ -281,61 +310,64 @@ def test_neibs_wrap_centered_step_manual():
def test_neibs_gpu(): def test_neibs_gpu():
if cuda.cuda_available == False: if cuda.cuda_available == False:
raise SkipTest('Optional package cuda disabled') raise SkipTest('Optional package cuda disabled')
for shape, pshape in [((100,40,18,18),(2,2)), for shape, pshape in [((100, 40, 18, 18), (2, 2)),
((100,40,6,18),(3,2)), ((100, 40, 6, 18), (3, 2)),
((10,40,66,66),(33,33)), ((10, 40, 66, 66), (33, 33)),
((10,40,68,66),(34,33)) ((10, 40, 68, 66), (34, 33))
]: ]:
images = shared(numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)) images = shared(numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable(pshape) neib_shape = T.as_tensor_variable(pshape)
from theano.sandbox.cuda.basic_ops import gpu_from_host f = function([], images2neibs(images, neib_shape),
f = function([], images2neibs(images,neib_shape),
mode=mode_with_gpu) mode=mode_with_gpu)
f_gpu = function([], images2neibs(images,neib_shape), f_gpu = function([], images2neibs(images, neib_shape),
mode=mode_with_gpu) mode=mode_with_gpu)
assert any([isinstance(node.op,GpuImages2Neibs) for node in f_gpu.maker.env.toposort()]) assert any([isinstance(node.op, GpuImages2Neibs)
for node in f_gpu.maker.env.toposort()])
#print images.get_value(borrow=True) #print images.get_value(borrow=True)
neibs = numpy.asarray(f_gpu()) neibs = numpy.asarray(f_gpu())
assert numpy.allclose(neibs,f()) assert numpy.allclose(neibs, f())
#print neibs #print neibs
g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_with_gpu) g = function([], neibs2images(neibs, neib_shape, images.shape),
assert any([isinstance(node.op,GpuImages2Neibs) for node in f.maker.env.toposort()]) mode=mode_with_gpu)
assert any([isinstance(node.op, GpuImages2Neibs)
for node in f.maker.env.toposort()])
#print numpy.asarray(g()) #print numpy.asarray(g())
assert numpy.allclose(images.get_value(borrow=True), g()) assert numpy.allclose(images.get_value(borrow=True), g())
def speed_neibs(): def speed_neibs():
shape = (100,40,18,18) shape = (100, 40, 18, 18)
images = shared(numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)) images = shared(numpy.arange(numpy.prod(shape),
neib_shape = T.as_tensor_variable((3,3)) dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
from theano.sandbox.cuda.basic_ops import gpu_from_host
f = function([], images2neibs(images,neib_shape))#, mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape))
for i in range(1000): for i in range(1000):
f() f()
def speed_neibs_wrap_centered():
shape = (100,40,18,18)
images = shared(numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3,3))
from theano.sandbox.cuda.basic_ops import gpu_from_host def speed_neibs_wrap_centered():
shape = (100, 40, 18, 18)
images = shared(numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
f = function([], images2neibs(images,neib_shape,mode="wrap_centered"))#, mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape, mode="wrap_centered"))
for i in range(1000): for i in range(1000):
f() f()
def test_neibs_grad(): def test_neibs_grad():
shape = (2,3,4,4) shape = (2, 3, 4, 4)
images = shared(numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)) images = shared(numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape))
cost = T.sum(T.sqr(images2neibs(images, (2,2))), axis=[0,1]) cost = T.sum(T.sqr(images2neibs(images, (2, 2))), axis=[0, 1])
grad = T.grad(cost, images) grad = T.grad(cost, images)
...@@ -344,60 +376,64 @@ def test_neibs_grad(): ...@@ -344,60 +376,64 @@ def test_neibs_grad():
got = f() got = f()
should_get = [numpy.asarray(290320.0, dtype=numpy.float32), should_get = [numpy.asarray(290320.0, dtype=numpy.float32),
numpy.asarray([[[[ 0., 2., 4., 6.], numpy.asarray([[[[ 0., 2., 4., 6.],
[ 8., 10., 12., 14.], [ 8., 10., 12., 14.],
[ 16., 18., 20., 22.], [ 16., 18., 20., 22.],
[ 24., 26., 28., 30.]], [ 24., 26., 28., 30.]],
[[ 32., 34., 36., 38.], [[ 32., 34., 36., 38.],
[ 40., 42., 44., 46.], [ 40., 42., 44., 46.],
[ 48., 50., 52., 54.], [ 48., 50., 52., 54.],
[ 56., 58., 60., 62.]], [ 56., 58., 60., 62.]],
[[ 64., 66., 68., 70.], [[ 64., 66., 68., 70.],
[ 72., 74., 76., 78.], [ 72., 74., 76., 78.],
[ 80., 82., 84., 86.], [ 80., 82., 84., 86.],
[ 88., 90., 92., 94.]]], [ 88., 90., 92., 94.]]],
[[[ 96., 98., 100., 102.],
[[[ 96., 98., 100., 102.], [104., 106., 108., 110.],
[ 104., 106., 108., 110.], [112., 114., 116., 118.],
[ 112., 114., 116., 118.], [120., 122., 124., 126.]],
[ 120., 122., 124., 126.]],
[[128., 130., 132., 134.],
[[ 128., 130., 132., 134.], [136., 138., 140., 142.],
[ 136., 138., 140., 142.], [144., 146., 148., 150.],
[ 144., 146., 148., 150.], [152., 154., 156., 158.]],
[ 152., 154., 156., 158.]],
[[160., 162., 164., 166.],
[[ 160., 162., 164., 166.], [168., 170., 172., 174.],
[ 168., 170., 172., 174.], [176., 178., 180., 182.],
[ 176., 178., 180., 182.], [184., 186., 188., 190.]]]], dtype=numpy.float32)]
[ 184., 186., 188., 190.]]]], dtype=numpy.float32)]
assert numpy.allclose(got[0], should_get[0]) assert numpy.allclose(got[0], should_get[0])
assert numpy.allclose(got[1], should_get[1]) assert numpy.allclose(got[1], should_get[1])
def test_neibs_grad_verify_grad(): def test_neibs_grad_verify_grad():
shape = (2,3,4,4) shape = (2, 3, 4, 4)
images = T.dtensor4() images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape) images_val = numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (2,2))), axis=[0,1]) return T.sum(T.sqr(images2neibs(images, (2, 2))), axis=[0, 1])
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
if cuda.cuda_available: if cuda.cuda_available:
unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu)
def test_neibs_grad_verify_grad_warp_centered(): def test_neibs_grad_verify_grad_warp_centered():
# It is not implemented for now. So test that we raise an error. # It is not implemented for now. So test that we raise an error.
shape = (2,3,6,6) shape = (2, 3, 6, 6)
images = T.dtensor4() images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape) images_val = numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (3,3),mode='wrap_centered')), axis=[0,1]) return T.sum(T.sqr(images2neibs(images, (3, 3), mode='wrap_centered')),
axis=[0, 1])
try: try:
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
raise Exception("Expected an error") raise Exception("Expected an error")
...@@ -411,42 +447,48 @@ def test_neibs_grad_verify_grad_warp_centered(): ...@@ -411,42 +447,48 @@ def test_neibs_grad_verify_grad_warp_centered():
except NotImplementedError: except NotImplementedError:
pass pass
def test_neibs_ignore_border(): def test_neibs_ignore_border():
shape = (2,3,5,5) shape = (2, 3, 5, 5)
images = T.dtensor4() images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape) images_val = numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (2,2), mode='ignore_borders')), axis=[0,1]) return T.sum(T.sqr(images2neibs(images, (2, 2),
mode='ignore_borders')), axis=[0, 1])
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
# not implemented for gpu # not implemented for gpu
# if cuda.cuda_available: # if cuda.cuda_available:
# unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu) # unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu)
def test_neibs_valid_with_inconsistent_borders(): def test_neibs_valid_with_inconsistent_borders():
shape = (2,3,5,5) shape = (2, 3, 5, 5)
images = T.dtensor4() images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape) images_val = numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape)
def fn(images): def fn(images):
return T.sum(T.sqr(images2neibs(images, (2,2), mode='valid')), axis=[0,1]) return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
axis=[0, 1])
try: try:
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu) unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
assert False,"An error was expected" assert False, "An error was expected"
except TypeError: except TypeError:
# This is expected if the assert is there # This is expected if the assert is there
pass pass
def test_neibs2images_crash_on_grad(): def test_neibs2images_crash_on_grad():
# say we had images of size (2,3,20,20) # say we had images of size (2, 3, 20, 20)
# then we extracted 2x2 neighbors on this, we get (2*3*10*10, 4) # then we extracted 2x2 neighbors on this, we get (2 * 3 * 10 * 10, 4)
neibs = T.dmatrix() neibs = T.dmatrix()
neibs_val = numpy.random.rand(600,4) neibs_val = numpy.random.rand(600, 4)
to_images = T.sum(neibs2images(neibs, (2,2), (2,3,20,20))) to_images = T.sum(neibs2images(neibs, (2, 2), (2, 3, 20, 20)))
g = T.grad(to_images, neibs) g = T.grad(to_images, neibs)
fn = theano.function([neibs], to_images, mode=mode_without_gpu) fn = theano.function([neibs], to_images, mode=mode_without_gpu)
print "Compiled" print "Compiled"
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论