提交 28bfa8d5 authored 作者: Frederic's avatar Frederic

big refactoring and Images2Neibs tests and some update to it at the same time.

No need to review in detail. This is sandbox stuff.
上级 165ac99b
...@@ -20,85 +20,44 @@ else: ...@@ -20,85 +20,44 @@ else:
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(): class T_Images2Neibs(unittest_tools.InferShapeTester):
shape = (100, 40, 18, 18) def __init__(self, name):
self.mode = mode_without_gpu
self.op = Images2Neibs
return super(T_Images2Neibs, self).__init__(name)
def test_neibs(self):
for shape, pshape in [((100, 40, 18, 18), (2, 2)),
((100, 40, 6, 18), (3, 2)),
((10, 40, 66, 66), (33, 33)),
((10, 40, 68, 66), (34, 33))
]:
for border in ['valid', 'ignore_borders']:
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(pshape)
f = function([], images2neibs(images, neib_shape), mode=mode_without_gpu) f = function([], images2neibs(images, neib_shape, mode=border),
mode=self.mode)
#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), g = function([], neibs2images(neibs, neib_shape, images.shape),
mode=mode_without_gpu) mode=self.mode)
assert any([isinstance(node.op, self.op)
for node in f.maker.fgraph.toposort()])
#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_manual(self):
def test_neibs_bad_shape():
shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
for neib_shape in [(3, 2), (2, 3)]:
neib_shape = T.as_tensor_variable(neib_shape)
try:
f = function([], images2neibs(images, neib_shape),
mode=mode_without_gpu)
f()
assert False, "An error was expected"
except TypeError:
pass
def test_neibs_bad_shape_warp_centered():
shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
for neib_shape in [(3, 2), (2, 3)]:
neib_shape = T.as_tensor_variable(neib_shape)
try:
f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=mode_without_gpu)
f()
assert False, "An error was expected"
except TypeError:
pass
shape = (2, 3, 2, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
try:
f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=mode_without_gpu)
f()
assert False, "An error was expected"
except TypeError:
pass
# Test a valid shapes
shape = (2, 3, 3, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
f = function([], images2neibs(images, neib_shape, mode="wrap_centered"),
mode=mode_without_gpu)
f()
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) for border in ['valid', 'ignore_borders']:
f = function([], images2neibs(images, neib_shape, mode=border),
mode=self.mode)
#print images.get_value(borrow=True) #print images.get_value(borrow=True)
neibs = f() neibs = f()
...@@ -128,32 +87,22 @@ def test_neibs_manual(): ...@@ -128,32 +87,22 @@ def test_neibs_manual():
[88, 89, 92, 93], [88, 89, 92, 93],
[90, 91, 94, 95]]) [90, 91, 94, 95]])
g = function([], neibs2images(neibs, neib_shape, images.shape), g = function([], neibs2images(neibs, neib_shape, images.shape),
mode=mode_without_gpu) mode=self.mode)
#print g()
assert numpy.allclose(images.get_value(borrow=True), g()) assert numpy.allclose(images.get_value(borrow=True), g())
def test_neibs_manual_step(self):
def test_neibs_step_manual():
shape = (2, 3, 5, 5) shape = (2, 3, 5, 5)
images = shared(numpy.asarray(numpy.arange(numpy.prod( images = shared(numpy.asarray(numpy.arange(numpy.prod(
shape)).reshape(shape), dtype='float32')) shape)).reshape(shape), dtype='float32'))
neib_shape = T.as_tensor_variable((3, 3)) neib_shape = T.as_tensor_variable((3, 3))
neib_step = T.as_tensor_variable((2, 2)) neib_step = T.as_tensor_variable((2, 2))
modes = [mode_without_gpu] for border in ['valid', 'ignore_borders']:
if cuda.cuda_available: f = function([], images2neibs(images, neib_shape, neib_step, mode=border),
modes.append(mode_with_gpu) mode=self.mode)
for mode_idx, mode in enumerate(modes):
f = function([], images2neibs(images, neib_shape, neib_step),
mode=mode)
#print images.get_value(borrow=True)
neibs = f() neibs = f()
if mode_idx == 0: assert self.op in [type(node.op)
assert Images2Neibs in [type(node.op)
for node in f.maker.fgraph.toposort()]
elif mode_idx == 1:
assert GpuImages2Neibs in [type(node.op)
for node in f.maker.fgraph.toposort()] for node in f.maker.fgraph.toposort()]
assert numpy.allclose(neibs, assert numpy.allclose(neibs,
...@@ -181,17 +130,29 @@ def test_neibs_step_manual(): ...@@ -181,17 +130,29 @@ def test_neibs_step_manual():
[127, 128, 129, 132, 133, 134, 137, 138, 139], [127, 128, 129, 132, 133, 134, 137, 138, 139],
[135, 136, 137, 140, 141, 142, 145, 146, 147], [135, 136, 137, 140, 141, 142, 145, 146, 147],
[137, 138, 139, 142, 143, 144, 147, 148, 149]]) [137, 138, 139, 142, 143, 144, 147, 148, 149]])
#g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu)
#neibs2images do not seam to support step != neib_shape
#g = function([], neibs2images(neibs, neib_shape, images.shape),
# mode=self.mode)
#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(self):
shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
def test_neibs_wrap_centered_step_manual(): for neib_shape in [(3, 2), (2, 3)]:
neib_shape = T.as_tensor_variable(neib_shape)
f = function([], images2neibs(images, neib_shape), mode=self.mode)
self.assertRaises(TypeError, f)
#Test that ignore border work in that case.
f = function([], images2neibs(images, neib_shape, mode='ignore_borders'),
mode=self.mode)
f()
modes = [mode_without_gpu] def test_neibs_wrap_centered_step_manual(self):
if cuda.cuda_available:
modes.append(mode_with_gpu)
expected1 = [[24, 20, 21, 4, 0, 1, 9, 5, 6], expected1 = [[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],
...@@ -246,9 +207,9 @@ def test_neibs_wrap_centered_step_manual(): ...@@ -246,9 +207,9 @@ def test_neibs_wrap_centered_step_manual():
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):
f = function([], images2neibs(images, neib_shape, neib_step, f = function([], images2neibs(images, neib_shape, neib_step,
mode="wrap_centered"), mode=mode) mode="wrap_centered"),
mode=self.mode)
neibs = f() neibs = f()
if expected.size > 1: if expected.size > 1:
...@@ -257,74 +218,54 @@ def test_neibs_wrap_centered_step_manual(): ...@@ -257,74 +218,54 @@ def test_neibs_wrap_centered_step_manual():
(i + 1) * expected.shape[0], :], (i + 1) * expected.shape[0], :],
expected + 25 * i), mode_idx expected + 25 * i), mode_idx
if mode_idx == 0: assert self.op in [type(node.op)
assert Images2Neibs in [type(node.op)
for node in f.maker.fgraph.toposort()]
elif mode_idx == 1:
assert GpuImages2Neibs in [type(node.op)
for node in f.maker.fgraph.toposort()] for node in f.maker.fgraph.toposort()]
#g = function([], neibs2images(neibs, neib_shape, images.shape), mode=mode_without_gpu) #g = function([], neibs2images(neibs, neib_shape, images.shape), mode=self.mode)
#TODO: why this is commented?
#assert numpy.allclose(images.get_value(borrow=True), g()) #assert numpy.allclose(images.get_value(borrow=True), g())
def test_neibs_bad_shape_wrap_centered(self):
shape = (2, 3, 10, 10)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
def test_neibs_gpu(): for neib_shape in [(3, 2), (2, 3)]:
if cuda.cuda_available == False: neib_shape = T.as_tensor_variable(neib_shape)
raise SkipTest('Optional package cuda disabled')
for shape, pshape in [((100, 40, 18, 18), (2, 2)),
((100, 40, 6, 18), (3, 2)),
((10, 40, 66, 66), (33, 33)),
((10, 40, 68, 66), (34, 33))
]:
images = shared(numpy.arange(numpy.prod(shape),
dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable(pshape)
f = function([], images2neibs(images, neib_shape),
mode=mode_with_gpu)
f_gpu = function([], images2neibs(images, neib_shape),
mode=mode_with_gpu)
assert any([isinstance(node.op, GpuImages2Neibs)
for node in f_gpu.maker.fgraph.toposort()])
#print images.get_value(borrow=True)
neibs = numpy.asarray(f_gpu())
assert numpy.allclose(neibs, f())
#print neibs
g = function([], neibs2images(neibs, neib_shape, images.shape),
mode=mode_with_gpu)
assert any([isinstance(node.op, GpuImages2Neibs)
for node in f.maker.fgraph.toposort()])
#print numpy.asarray(g())
assert numpy.allclose(images.get_value(borrow=True), g())
f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"),
mode=self.mode)
self.assertRaises(TypeError, f)
def speed_neibs(): for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
shape = (100, 40, 18, 18) images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
images = shared(numpy.arange(numpy.prod(shape),
dtype='float32').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=self.mode)
self.assertRaises(TypeError, f)
f = function([], images2neibs(images, neib_shape)) # Test a valid shapes
shape = (2, 3, 3, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
for i in range(1000): f = function([], images2neibs(images, neib_shape, mode="wrap_centered"),
mode=self.mode)
f() f()
def test_grad_wrap_centered(self):
# It is not implemented for now. So test that we raise an error.
shape = (2, 3, 6, 6)
images_val = numpy.random.rand(*shape).astype('float32')
def speed_neibs_wrap_centered(): def fn(images):
shape = (100, 40, 18, 18) return images2neibs(images, (3, 3), mode='wrap_centered')
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"))
for i in range(1000): self.assertRaises(NotImplementedError, unittest_tools.verify_grad,
f() fn, [images_val], mode=self.mode)
class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_grad_valid(self): def test_grad_valid(self):
shape = (2, 3, 4, 4) shape = (2, 3, 4, 4)
images_val = numpy.random.rand(*shape).astype('float32') images_val = numpy.random.rand(*shape).astype('float32')
...@@ -332,10 +273,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -332,10 +273,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def fn(images): def fn(images):
return images2neibs(images, (2, 2)) return images2neibs(images, (2, 2))
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu, unittest_tools.verify_grad(fn, [images_val], mode=self.mode,
eps=0.1)
if cuda.cuda_available:
unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu,
eps=0.1) eps=0.1)
# The grad is not defined when the neib_shape and neib_step # The grad is not defined when the neib_shape and neib_step
...@@ -344,22 +282,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -344,22 +282,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
return images2neibs(images, (2, 2), (1, 1)) return images2neibs(images, (2, 2), (1, 1))
self.assertRaises(NotImplementedError, self.assertRaises(NotImplementedError,
unittest_tools.verify_grad, fn, [images_val], unittest_tools.verify_grad, fn, [images_val],
mode=mode_without_gpu) mode=self.mode)
def test_grad_warp_centered(self):
# It is not implemented for now. So test that we raise an error.
shape = (2, 3, 6, 6)
images_val = numpy.random.rand(*shape).astype('float32')
def fn(images):
return images2neibs(images, (3, 3), mode='wrap_centered')
self.assertRaises(NotImplementedError, unittest_tools.verify_grad,
fn, [images_val], mode=mode_without_gpu)
if cuda.cuda_available:
self.assertRaises(NotImplementedError, unittest_tools.verify_grad,
fn, [images_val], mode=mode_with_gpu)
def test_grad_ignore_border(self): def test_grad_ignore_border(self):
shape = (2, 3, 5, 5) shape = (2, 3, 5, 5)
...@@ -370,28 +293,21 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -370,28 +293,21 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
return images2neibs(images, (2, 2), return images2neibs(images, (2, 2),
mode='ignore_borders') mode='ignore_borders')
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu, unittest_tools.verify_grad(fn, [images_val], mode=self.mode,
eps=0.1) eps=0.1)
# GPU code not implemented in that case, but is should still def test_neibs2images_grad(self):
# not crash.
if cuda.cuda_available:
unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu,
eps=0.1)
def test_neibs2images_crash_on_grad(self):
# 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)))
g = T.grad(to_images, neibs)
fn = theano.function([neibs], to_images, mode=mode_without_gpu)
#print "Compiled"
fn(neibs_val)
def fn(neibs):
return neibs2images(neibs, (2, 2), (2, 3, 20, 20))
unittest_tools.verify_grad(fn, [neibs_val], mode=self.mode,
eps=0.1)
def test_neibs_valid_with_inconsistent_borders(): def test_neibs_valid_with_inconsistent_borders(self):
shape = (2, 3, 5, 5) shape = (2, 3, 5, 5)
images = T.dtensor4() images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), images_val = numpy.arange(numpy.prod(shape),
...@@ -403,14 +319,39 @@ def test_neibs_valid_with_inconsistent_borders(): ...@@ -403,14 +319,39 @@ def test_neibs_valid_with_inconsistent_borders():
f = theano.function([images], f = theano.function([images],
T.sqr(images2neibs(images, (2, 2), mode='valid')), T.sqr(images2neibs(images, (2, 2), mode='valid')),
mode=mode_without_gpu) mode=self.mode)
try: self.assertRaises(TypeError, f, images_val)
f(images_val)
assert False, "An error was expected" def speed_neibs(self):
except TypeError, e: shape = (100, 40, 18, 18)
# This is expected if the assert is there images = shared(numpy.arange(numpy.prod(shape),
pass dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
f = function([], images2neibs(images, neib_shape),
mode=self.mode)
for i in range(1000):
f()
def speed_neibs_wrap_centered(self):
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=self.mode)
for i in range(1000):
f()
class T_GpuImages2Neibs(T_Images2Neibs):
def __init__(self, name):
self.mode = mode_with_gpu
self.op = GpuImages2Neibs
return super(T_GpuImages2Neibs, self).__init__(name)
if __name__ == '__main__': if __name__ == '__main__':
#test_neibs_gpu() #test_neibs_gpu()
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论