提交 b8bee3c6 authored 作者: Aleksandar Botev's avatar Aleksandar Botev

Added mode 'half' to Images2Neibs. Tests pass. #5938

上级 22eaec56
...@@ -236,6 +236,31 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -236,6 +236,31 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# TODO: why this is commented? # 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_half_step_by_valid(self):
for shp_idx, (shape, neib_step) in enumerate([
[(7, 8, 5, 5), (1, 1)],
[(7, 8, 5, 5), (2, 2)],
[(7, 8, 5, 5), (4, 4)],
[(7, 8, 5, 5), (1, 4)],
[(7, 8, 5, 5), (4, 1)],
[(80, 90, 5, 5), (1, 2)],
[(1025, 9, 5, 5), (2, 1)],
[(1, 1, 5, 1037), (2, 4)],
[(1, 1, 1045, 5), (4, 2)]]
):
for neib_shape in [(3, 3), (3, 5), (5, 3)]:
for dtype in self.dtypes:
x = theano.shared(np.random.randn(*shape).astype(dtype))
extra = (neib_shape[0] // 2, neib_shape[1] // 2)
padded_shape = (x.shape[0], x.shape[1], x.shape[2] + 2 * extra[0], x.shape[3] + 2 * extra[1])
padded_x = T.zeros(padded_shape)
padded_x = T.set_subtensor(padded_x[:, :, extra[0]:-extra[0], extra[1]:-extra[1]], x)
x_using_valid = images2neibs(padded_x, neib_shape, neib_step, mode="valid")
x_using_half = images2neibs(x, neib_shape, neib_step, mode="half")
close = T.allclose(x_using_valid, x_using_half)
f = theano.function([], close, mode=self.mode)
assert f()
def test_neibs_bad_shape_wrap_centered(self): def test_neibs_bad_shape_wrap_centered(self):
shape = (2, 3, 10, 10) shape = (2, 3, 10, 10)
...@@ -281,6 +306,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -281,6 +306,17 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
self.assertRaises(TypeError, unittest_tools.verify_grad, self.assertRaises(TypeError, unittest_tools.verify_grad,
fn, [images_val], mode=self.mode) fn, [images_val], mode=self.mode)
def test_grad_half(self):
# It is not implemented for now. So test that we raise an error.
shape = (2, 3, 6, 6)
images_val = np.random.rand(*shape).astype('float32')
def fn(images):
return images2neibs(images, (3, 3), mode='half')
self.assertRaises(TypeError, unittest_tools.verify_grad,
fn, [images_val], mode=self.mode)
def test_grad_valid(self): def test_grad_valid(self):
shape = (2, 3, 6, 6) shape = (2, 3, 6, 6)
images_val = np.random.rand(*shape).astype('float32') images_val = np.random.rand(*shape).astype('float32')
...@@ -330,15 +366,22 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -330,15 +366,22 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
images_val = np.arange(np.prod(shape), images_val = np.arange(np.prod(shape),
dtype='float32').reshape(shape) dtype='float32').reshape(shape)
def fn(images):
return T.sum(T.sqr(images2neibs(images, (2, 2), mode='valid')),
axis=[0, 1])
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=self.mode) mode=self.mode)
self.assertRaises(TypeError, f, images_val) self.assertRaises(TypeError, f, images_val)
def test_neibs_half_with_inconsistent_borders(self):
shape = (2, 3, 5, 5)
images = T.dtensor4()
images_val = np.arange(np.prod(shape),
dtype='float32').reshape(shape)
f = theano.function([images],
T.sqr(images2neibs(images, (2, 2), mode='half')),
mode=self.mode)
self.assertRaises(TypeError, f, images_val)
def test_can_not_infer_nb_dim(self): def test_can_not_infer_nb_dim(self):
# Was reported in gh-5613. Test that we do not crash # Was reported in gh-5613. Test that we do not crash
# or that we crash in a few other case found while # or that we crash in a few other case found while
...@@ -346,7 +389,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -346,7 +389,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
img = T.tensor4('img') img = T.tensor4('img')
patches = T.nnet.neighbours.images2neibs(img, [16, 16]) patches = T.nnet.neighbours.images2neibs(img, [16, 16])
extractPatches = theano.function([img], patches) extractPatches = theano.function([img], patches, mode=self.mode)
patsRecovery = T.matrix('patsRecovery') patsRecovery = T.matrix('patsRecovery')
original_size = T.ivector('original_size') original_size = T.ivector('original_size')
...@@ -354,7 +397,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -354,7 +397,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for mode in ['valid', 'ignore_borders']: for mode in ['valid', 'ignore_borders']:
out = neibs2images(patsRecovery, (16, 16), out = neibs2images(patsRecovery, (16, 16),
original_size, mode=mode) original_size, mode=mode)
f = theano.function([patsRecovery, original_size], out) f = theano.function([patsRecovery, original_size], out,
mode=self.mode)
im_val = np.ones((1, 3, 320, 320), dtype=np.float32) im_val = np.ones((1, 3, 320, 320), dtype=np.float32)
neibs = extractPatches(im_val) neibs = extractPatches(im_val)
...@@ -364,8 +408,13 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -364,8 +408,13 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
(1, 1, 3, 320, 320)) (1, 1, 3, 320, 320))
# End up with a step of 0 # End up with a step of 0
# This can lead to division by zero in DebugMode # This can lead to division by zero in DebugMode
self.assertRaises((ValueError, ZeroDivisionError), f, neibs, # This can not be ran on the GPU since from the C code we get
(3, 320, 320, 1)) # no ZeroDivisionError, but rather the whole processes crashes
# with floating point exception.
if "gpu" not in self.mode.provided_optimizer.include and \
"gpuarray" not in self.mode.provided_optimizer.include:
self.assertRaises((ValueError, ZeroDivisionError), f, neibs,
(3, 320, 320, 1))
def speed_neibs(self): def speed_neibs(self):
shape = (100, 40, 18, 18) shape = (100, 40, 18, 18)
...@@ -392,6 +441,19 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -392,6 +441,19 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for i in range(1000): for i in range(1000):
f() f()
def speed_neibs_half(self):
shape = (100, 40, 18, 18)
images = shared(np.arange(np.prod(shape),
dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3, 3))
f = function([],
images2neibs(images, neib_shape, mode="half"),
mode=self.mode)
for i in range(1000):
f()
def test_infer_shape(self): def test_infer_shape(self):
shape = (100, 40, 6, 3) shape = (100, 40, 6, 3)
images = np.ones(shape).astype('float32') images = np.ones(shape).astype('float32')
...@@ -431,6 +493,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -431,6 +493,15 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[x], [images2neibs( [x], [images2neibs(
x, neib_shape=(3, 3), mode='wrap_centered')], x, neib_shape=(3, 3), mode='wrap_centered')],
[images], Images2Neibs) [images], Images2Neibs)
shape = (100, 40, 6, 4)
images = np.ones(shape).astype('float32')
x = T.ftensor4()
self._compile_and_check(
[x], [images2neibs(x, neib_shape=(2, 1), mode='half')],
[images], Images2Neibs)
self._compile_and_check(
[x], [images2neibs(x, neib_shape=(2, 3), mode='half')],
[images], Images2Neibs)
if __name__ == '__main__': if __name__ == '__main__':
unittest.main() unittest.main()
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论