def test_can_not_infer_nb_dim(self): # Was reported in gh-5613. Test that we do not crash # or that we crash in a few other case found while # investigating that case img = tt.tensor4("img") patches = tt.nnet.neighbours.images2neibs(img, [16, 16]) extractPatches = aesara.function([img], patches, mode=self.mode) patsRecovery = tt.matrix("patsRecovery") original_size = tt.ivector("original_size") for mode in ["valid", "ignore_borders"]: out = neibs2images(patsRecovery, (16, 16), original_size, mode=mode) f = aesara.function([patsRecovery, original_size], out, mode=self.mode) im_val = np.ones((1, 3, 320, 320), dtype=np.float32) neibs = extractPatches(im_val) f(neibs, im_val.shape) # Wrong number of dimensions with pytest.raises(ValueError): f(neibs, (1, 1, 3, 320, 320)) # End up with a step of 0 # This can lead to division by zero in DebugMode with pytest.raises((ValueError, ZeroDivisionError)): f(neibs, (3, 320, 320, 1))
def test_neibs(self): for shape, pshape in [ ((10, 7, 18, 18), (2, 2)), ((10, 7, 6, 18), (3, 2)), ((5, 7, 66, 66), (33, 33)), ((5, 7, 68, 66), (34, 33)), ]: for border in ["valid", "ignore_borders"]: for dtype in self.dtypes: images = shared( np.arange(np.prod(shape), dtype=dtype).reshape(shape)) neib_shape = tt.as_tensor_variable(pshape) f = function( [], images2neibs(images, neib_shape, mode=border), mode=self.mode, ) # print images.get_value(borrow=True) neibs = f() # print neibs g = function( [], neibs2images(neibs, neib_shape, images.shape), mode=self.mode, ) assert any([ isinstance(node.op, self.op) for node in f.maker.fgraph.toposort() ]) # print g() assert np.allclose(images.get_value(borrow=True), g())
def test_neibs_manual(self): shape = (2, 3, 4, 4) for dtype in self.dtypes: images = shared( np.arange(np.prod(shape), dtype=dtype).reshape(shape)) neib_shape = tt.as_tensor_variable((2, 2)) for border in ["valid", "ignore_borders"]: f = function([], images2neibs(images, neib_shape, mode=border), mode=self.mode) assert any([ isinstance(node.op, self.op) for node in f.maker.fgraph.toposort() ]) # print images.get_value(borrow=True) neibs = f() # print neibs assert np.allclose( neibs, [ [0, 1, 4, 5], [2, 3, 6, 7], [8, 9, 12, 13], [10, 11, 14, 15], [16, 17, 20, 21], [18, 19, 22, 23], [24, 25, 28, 29], [26, 27, 30, 31], [32, 33, 36, 37], [34, 35, 38, 39], [40, 41, 44, 45], [42, 43, 46, 47], [48, 49, 52, 53], [50, 51, 54, 55], [56, 57, 60, 61], [58, 59, 62, 63], [64, 65, 68, 69], [66, 67, 70, 71], [72, 73, 76, 77], [74, 75, 78, 79], [80, 81, 84, 85], [82, 83, 86, 87], [88, 89, 92, 93], [90, 91, 94, 95], ], ) g = function([], neibs2images(neibs, neib_shape, images.shape), mode=self.mode) assert np.allclose(images.get_value(borrow=True), g())
def fn(neibs): return neibs2images(neibs, (2, 2), (2, 3, 10, 10))