提交 2fe68421 authored 作者: Philippe Hamel's avatar Philippe Hamel

Added comments and test for ignore_border mode in neighbours.py

上级 29c92ffc
...@@ -12,6 +12,14 @@ if cuda_available: ...@@ -12,6 +12,14 @@ if cuda_available:
class Images2Neibs(Op): class Images2Neibs(Op):
def __init__(self, mode='valid'): def __init__(self, mode='valid'):
"""
Modes:
valid : Reshapes the input as a a 2D tensor where each row is a pooling example.
Requires an input that is a multiple of the pooling factor (in each direction)
ignore_borders : Same as valid, but will ignore the borders if the shape(s) of the input
is not a multiple of the pooling factor(s)
wrap_centered : ?? TODO comment
"""
if mode not in ['valid','wrap_centered','ignore_borders']: if mode not in ['valid','wrap_centered','ignore_borders']:
raise NotImplementedError("Only the mode valid, ignore_borders and wrap_centered have been implemented for the op Images2Neibs") raise NotImplementedError("Only the mode valid, ignore_borders and wrap_centered have been implemented for the op Images2Neibs")
self.mode = mode self.mode = mode
...@@ -55,7 +63,7 @@ class Images2Neibs(Op): ...@@ -55,7 +63,7 @@ class Images2Neibs(Op):
raise NotImplementedError() raise NotImplementedError()
def c_code_cache_version(self): def c_code_cache_version(self):
return (4,) return (5,)
def c_code(self, node, name, inp, out, sub): def c_code(self, node, name, inp, out, sub):
ten4, neib_shape, neib_step = inp ten4, neib_shape, neib_step = inp
......
...@@ -411,6 +411,36 @@ def test_neibs_grad_verify_grad_warp_centered(): ...@@ -411,6 +411,36 @@ def test_neibs_grad_verify_grad_warp_centered():
except NotImplementedError: except NotImplementedError:
pass pass
def test_neibs_ignore_border():
shape = (2,3,5,5)
images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)
def fn(images):
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)
# not implemented for gpu
# if cuda.cuda_available:
# unittest_tools.verify_grad(fn, [images_val], mode=mode_with_gpu)
def test_neibs_valid_with_inconsistent_borders():
shape = (2,3,5,5)
images = T.dtensor4()
images_val = numpy.arange(numpy.prod(shape), dtype='float32').reshape(shape)
def fn(images):
return T.sum(T.sqr(images2neibs(images, (2,2), mode='valid')), axis=[0,1])
try:
unittest_tools.verify_grad(fn, [images_val], mode=mode_without_gpu)
assert False,"An error was expected"
except TypeError:
# This is expected if the assert is there
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)
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论