提交 c918af6e authored 作者: amrithasuresh's avatar amrithasuresh

Updated numpy as np

上级 7039dda4
from __future__ import absolute_import, print_function, division from __future__ import absolute_import, print_function, division
import numpy import numpy as np
import unittest import unittest
import theano import theano
...@@ -25,7 +25,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -25,7 +25,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for border in ['valid', 'ignore_borders']: for border in ['valid', 'ignore_borders']:
for dtype in self.dtypes: for dtype in self.dtypes:
images = shared( images = shared(
numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape)) np.arange(np.prod(shape), dtype=dtype).reshape(shape))
neib_shape = T.as_tensor_variable(pshape) neib_shape = T.as_tensor_variable(pshape)
f = function([], f = function([],
...@@ -42,13 +42,13 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -42,13 +42,13 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for node in f.maker.fgraph.toposort()]) for node in f.maker.fgraph.toposort()])
# print g() # print g()
assert numpy.allclose(images.get_value(borrow=True), g()) assert np.allclose(images.get_value(borrow=True), g())
def test_neibs_manual(self): def test_neibs_manual(self):
shape = (2, 3, 4, 4) shape = (2, 3, 4, 4)
for dtype in self.dtypes: for dtype in self.dtypes:
images = shared( images = shared(
numpy.arange(numpy.prod(shape), dtype=dtype).reshape(shape)) np.arange(np.prod(shape), dtype=dtype).reshape(shape))
neib_shape = T.as_tensor_variable((2, 2)) neib_shape = T.as_tensor_variable((2, 2))
for border in ['valid', 'ignore_borders']: for border in ['valid', 'ignore_borders']:
...@@ -60,7 +60,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -60,7 +60,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# print images.get_value(borrow=True) # print images.get_value(borrow=True)
neibs = f() neibs = f()
# print neibs # print neibs
assert numpy.allclose(neibs, [ assert np.allclose(neibs, [
[0, 1, 4, 5], [0, 1, 4, 5],
[2, 3, 6, 7], [2, 3, 6, 7],
[8, 9, 12, 13], [8, 9, 12, 13],
...@@ -88,12 +88,12 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -88,12 +88,12 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
g = function([], neibs2images(neibs, neib_shape, images.shape), g = function([], neibs2images(neibs, neib_shape, images.shape),
mode=self.mode) mode=self.mode)
assert numpy.allclose(images.get_value(borrow=True), g()) assert np.allclose(images.get_value(borrow=True), g())
def test_neibs_manual_step(self): def test_neibs_manual_step(self):
shape = (2, 3, 5, 5) shape = (2, 3, 5, 5)
for dtype in self.dtypes: for dtype in self.dtypes:
images = shared(numpy.asarray(numpy.arange(numpy.prod( images = shared(np.asarray(np.arange(np.prod(
shape)).reshape(shape), dtype=dtype)) shape)).reshape(shape), dtype=dtype))
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))
...@@ -107,7 +107,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -107,7 +107,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
assert self.op in [type(node.op) assert self.op in [type(node.op)
for node in f.maker.fgraph.toposort()] for node in f.maker.fgraph.toposort()]
assert numpy.allclose(neibs, [ assert np.allclose(neibs, [
[0, 1, 2, 5, 6, 7, 10, 11, 12], [0, 1, 2, 5, 6, 7, 10, 11, 12],
[2, 3, 4, 7, 8, 9, 12, 13, 14], [2, 3, 4, 7, 8, 9, 12, 13, 14],
[10, 11, 12, 15, 16, 17, 20, 21, 22], [10, 11, 12, 15, 16, 17, 20, 21, 22],
...@@ -143,8 +143,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -143,8 +143,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_neibs_bad_shape(self): def test_neibs_bad_shape(self):
shape = (2, 3, 10, 10) shape = (2, 3, 10, 10)
for dtype in self.dtypes: for dtype in self.dtypes:
images = shared(numpy.arange( images = shared(np.arange(
numpy.prod(shape), dtype=dtype).reshape(shape)) np.prod(shape), dtype=dtype).reshape(shape))
for neib_shape in [(3, 2), (2, 3)]: for neib_shape in [(3, 2), (2, 3)]:
neib_shape = T.as_tensor_variable(neib_shape) neib_shape = T.as_tensor_variable(neib_shape)
...@@ -212,11 +212,11 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -212,11 +212,11 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
for dtype in self.dtypes: for dtype in self.dtypes:
images = shared(numpy.asarray(numpy.arange(numpy.prod( images = shared(np.asarray(np.arange(np.prod(
shape)).reshape(shape), dtype=dtype)) shape)).reshape(shape), dtype=dtype))
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 = np.asarray(expected)
f = function([], images2neibs(images, neib_shape, neib_step, f = function([], images2neibs(images, neib_shape, neib_step,
mode="wrap_centered"), mode="wrap_centered"),
...@@ -225,7 +225,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -225,7 +225,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
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( assert np.allclose(
neibs[i * expected.shape[0]:(i + 1) * expected.shape[0], :], neibs[i * expected.shape[0]:(i + 1) * expected.shape[0], :],
expected + 25 * i), "wrap_centered" expected + 25 * i), "wrap_centered"
...@@ -240,8 +240,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -240,8 +240,8 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
shape = (2, 3, 10, 10) shape = (2, 3, 10, 10)
for dtype in self.dtypes: for dtype in self.dtypes:
images = shared(numpy.arange( images = shared(np.arange(
numpy.prod(shape), dtype=dtype np.prod(shape), dtype=dtype
).reshape(shape)) ).reshape(shape))
for neib_shape in [(3, 2), (2, 3)]: for neib_shape in [(3, 2), (2, 3)]:
...@@ -253,7 +253,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -253,7 +253,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
self.assertRaises(TypeError, f) self.assertRaises(TypeError, f)
for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]: for shape in [(2, 3, 2, 3), (2, 3, 3, 2)]:
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(np.arange(np.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, f = function([], images2neibs(images, neib_shape,
mode="wrap_centered"), mode="wrap_centered"),
...@@ -262,7 +262,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -262,7 +262,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
# Test a valid shapes # Test a valid shapes
shape = (2, 3, 3, 3) shape = (2, 3, 3, 3)
images = shared(numpy.arange(numpy.prod(shape)).reshape(shape)) images = shared(np.arange(np.prod(shape)).reshape(shape))
neib_shape = T.as_tensor_variable((3, 3)) neib_shape = T.as_tensor_variable((3, 3))
f = function([], f = function([],
...@@ -273,7 +273,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -273,7 +273,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_grad_wrap_centered(self): def test_grad_wrap_centered(self):
# 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_val = numpy.random.rand(*shape).astype('float32') images_val = np.random.rand(*shape).astype('float32')
def fn(images): def fn(images):
return images2neibs(images, (3, 3), mode='wrap_centered') return images2neibs(images, (3, 3), mode='wrap_centered')
...@@ -283,7 +283,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -283,7 +283,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_grad_valid(self): def test_grad_valid(self):
shape = (2, 3, 6, 6) shape = (2, 3, 6, 6)
images_val = numpy.random.rand(*shape).astype('float32') images_val = np.random.rand(*shape).astype('float32')
def fn(images): def fn(images):
return images2neibs(images, (2, 2)) return images2neibs(images, (2, 2))
...@@ -305,7 +305,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -305,7 +305,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_grad_ignore_border(self): def test_grad_ignore_border(self):
shape = (2, 3, 5, 5) shape = (2, 3, 5, 5)
images_val = numpy.random.rand(*shape).astype('float32') images_val = np.random.rand(*shape).astype('float32')
def fn(images): def fn(images):
return images2neibs(images, (2, 2), return images2neibs(images, (2, 2),
...@@ -317,7 +317,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -317,7 +317,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_neibs2images_grad(self): def test_neibs2images_grad(self):
# say we had images of size (2, 3, 10, 10) # say we had images of size (2, 3, 10, 10)
# then we extracted 2x2 neighbors on this, we get (2 * 3 * 5 * 5, 4) # then we extracted 2x2 neighbors on this, we get (2 * 3 * 5 * 5, 4)
neibs_val = numpy.random.rand(150, 4) neibs_val = np.random.rand(150, 4)
def fn(neibs): def fn(neibs):
return neibs2images(neibs, (2, 2), (2, 3, 10, 10)) return neibs2images(neibs, (2, 2), (2, 3, 10, 10))
...@@ -327,7 +327,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -327,7 +327,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_neibs_valid_with_inconsistent_borders(self): 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 = np.arange(np.prod(shape),
dtype='float32').reshape(shape) dtype='float32').reshape(shape)
def fn(images): def fn(images):
...@@ -356,7 +356,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -356,7 +356,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
original_size, mode=mode) original_size, mode=mode)
f = theano.function([patsRecovery, original_size], out) f = theano.function([patsRecovery, original_size], out)
im_val = numpy.ones((1, 3, 320, 320), dtype=numpy.float32) im_val = np.ones((1, 3, 320, 320), dtype=np.float32)
neibs = extractPatches(im_val) neibs = extractPatches(im_val)
f(neibs, im_val.shape) f(neibs, im_val.shape)
# Wrong number of dimensions # Wrong number of dimensions
...@@ -368,7 +368,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -368,7 +368,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def speed_neibs(self): def speed_neibs(self):
shape = (100, 40, 18, 18) shape = (100, 40, 18, 18)
images = shared(numpy.arange(numpy.prod(shape), images = shared(np.arange(np.prod(shape),
dtype='float32').reshape(shape)) dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3, 3)) neib_shape = T.as_tensor_variable((3, 3))
...@@ -380,7 +380,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -380,7 +380,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def speed_neibs_wrap_centered(self): def speed_neibs_wrap_centered(self):
shape = (100, 40, 18, 18) shape = (100, 40, 18, 18)
images = shared(numpy.arange(numpy.prod(shape), images = shared(np.arange(np.prod(shape),
dtype='float32').reshape(shape)) dtype='float32').reshape(shape))
neib_shape = T.as_tensor_variable((3, 3)) neib_shape = T.as_tensor_variable((3, 3))
...@@ -393,7 +393,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -393,7 +393,7 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
def test_infer_shape(self): def test_infer_shape(self):
shape = (100, 40, 6, 3) shape = (100, 40, 6, 3)
images = numpy.ones(shape).astype('float32') images = np.ones(shape).astype('float32')
x = T.ftensor4() x = T.ftensor4()
self._compile_and_check( self._compile_and_check(
[x], [images2neibs(x, neib_shape=(2, 1), mode='valid')], [x], [images2neibs(x, neib_shape=(2, 1), mode='valid')],
...@@ -402,14 +402,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -402,14 +402,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[x], [images2neibs(x, neib_shape=(2, 3), mode='valid')], [x], [images2neibs(x, neib_shape=(2, 3), mode='valid')],
[images], Images2Neibs) [images], Images2Neibs)
shape = (100, 40, 5, 4) shape = (100, 40, 5, 4)
images = numpy.ones(shape).astype('float32') images = np.ones(shape).astype('float32')
x = T.ftensor4() x = T.ftensor4()
self._compile_and_check( self._compile_and_check(
[x], [images2neibs( [x], [images2neibs(
x, neib_shape=(2, 1), mode='ignore_borders')], x, neib_shape=(2, 1), mode='ignore_borders')],
[images], Images2Neibs) [images], Images2Neibs)
shape = (100, 40, 5, 3) shape = (100, 40, 5, 3)
images = numpy.ones(shape).astype('float32') images = np.ones(shape).astype('float32')
x = T.ftensor4() x = T.ftensor4()
self._compile_and_check( self._compile_and_check(
[x], [images2neibs( [x], [images2neibs(
...@@ -417,14 +417,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester): ...@@ -417,14 +417,14 @@ class T_Images2Neibs(unittest_tools.InferShapeTester):
[images], Images2Neibs) [images], Images2Neibs)
shape = (100, 40, 6, 7) shape = (100, 40, 6, 7)
images = numpy.ones(shape).astype('float32') images = np.ones(shape).astype('float32')
x = T.ftensor4() x = T.ftensor4()
self._compile_and_check( self._compile_and_check(
[x], [images2neibs( [x], [images2neibs(
x, neib_shape=(2, 2), mode='ignore_borders')], x, neib_shape=(2, 2), mode='ignore_borders')],
[images], Images2Neibs) [images], Images2Neibs)
shape = (100, 40, 5, 10) shape = (100, 40, 5, 10)
images = numpy.ones(shape).astype('float32') images = np.ones(shape).astype('float32')
x = T.ftensor4() x = T.ftensor4()
self._compile_and_check( self._compile_and_check(
[x], [images2neibs( [x], [images2neibs(
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论