def test_infer_shape(self): image = tensor.dtensor4() maxout = tensor.dtensor4() gz = tensor.dtensor4() rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3), (3, 2)) image_val = rng.rand(4, 6, 7, 9) out_shapes = [[[[4, 6, 7, 9], [4, 6, 7, 9]], [[4, 6, 3, 4], [4, 6, 4, 5]], [[4, 6, 2, 3], [4, 6, 3, 3]], [[4, 6, 3, 3], [4, 6, 4, 3]], [[4, 6, 2, 4], [4, 6, 3, 5]]], [[None, None], [[4, 6, 4, 5], None], [[4, 6, 3, 3], None], [[4, 6, 4, 3], None], [[4, 6, 3, 5], None]], [[None, None], [None, None], [[4, 6, 3, 4], None], [[4, 6, 4, 4], None], [None, None]]] for i, maxpoolshp in enumerate(maxpoolshps): for j, ignore_border in enumerate([True, False]): for k, padding in enumerate([(0, 0), (1, 1), (1, 2)]): if out_shapes[k][i][j] is None: continue # checking shapes generated by Pool self._compile_and_check([image], [Pool(maxpoolshp, ignore_border=ignore_border, padding=padding)(image)], [image_val], Pool) # checking shapes generated by MaxPoolGrad maxout_val = rng.rand(*out_shapes[k][i][j]) gz_val = rng.rand(*out_shapes[k][i][j]) self._compile_and_check([image, maxout, gz], [MaxPoolGrad(maxpoolshp, ignore_border=ignore_border, padding=padding) (image, maxout, gz)], [image_val, maxout_val, gz_val], MaxPoolGrad, warn=False) # checking with broadcastable input image = tensor.tensor(dtype='float64', broadcastable=(False, False, True, True)) image_val = rng.rand(4, 6, 1, 1) self._compile_and_check( [image], [Pool((2, 2), ignore_border=True, padding=(0, 0))(image)], [image_val], Pool)
def test_AveragePoolGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: for stride in stridesizes: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(maxpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] maxpoolshp = maxpoolshps[indx] for ignore_border in [True, False]: grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool(ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxPaddingStride(self): ignore_border = True # padding does not support ignore_border=False rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolsizes = [(3, 3), (4, 4), (3, 4), (4, 3), (2, 2)] stridesizes = [(2, 2), (2, 2), (1, 1), (1, 2), (2, 2)] paddingsizes = [(2, 2), (1, 2), (2, 1), (0, 0), (1, 1)] imgsizes = [(5, 5), (5, 5), (5, 6), (6, 5), (5, 5)] m = 4 # minibatch c = 2 # channel size images = tensor.dtensor4() for indx, mode in product(numpy.arange(len(maxpoolsizes)), ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): imgsize = imgsizes[indx] imval = rng.rand(m, c, imgsize[0], imgsize[1]) - 0.5 stridesize = stridesizes[indx] maxpoolsize = maxpoolsizes[indx] paddingsize = paddingsizes[indx] numpy_output_val = self.numpy_max_pool_2d_stride_padding( imval, maxpoolsize, ignore_border, stridesize, paddingsize, mode) maxpool_op = Pool( maxpoolsize, ignore_border=ignore_border, st=stridesize, padding=paddingsize, mode=mode)(images) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxStride(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 3), (5, 3), (16, 16)) stridesizes = ( (1, 1), (3, 3), (5, 7), ) # generate random images imval = rng.rand(4, 10, 16, 16) # The same for each mode outputshps = ( (4, 10, 16, 16), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 16, 16), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 14, 14), (4, 10, 5, 5), (4, 10, 3, 2), (4, 10, 14, 14), (4, 10, 6, 6), (4, 10, 4, 3), (4, 10, 12, 14), (4, 10, 4, 5), (4, 10, 3, 2), (4, 10, 12, 14), (4, 10, 5, 6), (4, 10, 4, 3), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), (4, 10, 1, 1), ) images = tensor.dtensor4() indx = 0 for mode, maxpoolshp, ignore_border in product( ['max', 'sum', 'average_inc_pad', 'average_exc_pad'], maxpoolshps, [True, False]): for stride in stridesizes: outputshp = outputshps[indx % len(outputshps)] indx += 1 # Pool op numpy_output_val = \ self.numpy_max_pool_2d_stride(imval, maxpoolshp, ignore_border, stride, mode) assert numpy_output_val.shape == outputshp, ( "outshape is %s, calculated shape is %s" % (outputshp, numpy_output_val.shape)) maxpool_op = \ Pool(ignore_border=ignore_border, mode=mode)( images, maxpoolshp, stride) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) imgsizes = ((10, 10), (10, 5), (5, 5)) maxpoolsizes = ((5, 3), (3, 5), (3, 3)) stridesizes = ((3, 2), (2, 3), (3, 3)) paddingsizes = ((2, 2), (2, 1), (2, 2)) for i in range(len(imgsizes)): imgsize = imgsizes[i] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 maxpoolsize = maxpoolsizes[i] stridesize = stridesizes[i] paddingsize = paddingsizes[i] grad_shape = Pool.out_shape(imval.shape, maxpoolsize, st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, )(input) grad_op = MaxPoolGrad(maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: for stride in stridesizes: grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool( maxpoolshp, ignore_border=ignore_border, st=stride)(input) grad_op = MaxPoolGrad( maxpoolshp, ignore_border=ignore_border, st=stride) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def get_dim(self, name): if name == 'input_': return self.input_dim if name == 'output': return tuple(Pool.out_shape( self.input_dim, self.pooling_size, st=self.step, ignore_border=self.ignore_border, padding=self.padding))
def test_AveragePoolGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(avgpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] avgpoolshp = avgpoolshps[indx] for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # avgpool, stride, padding, input sizes examples = ( ((3,), (2,), (2,), (10,)), ((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 1, 10,)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ) for (avgpoolshp, stridesize, paddingsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # 'average_exc_pad' with non-zero padding is not implemented for mode in ['sum', 'average_inc_pad']: grad_shape = Pool.out_shape(imval.shape, avgpoolshp, ndim=len(avgpoolshp), st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=True, mode=mode) return grad_op(input, grad, avgpoolshp, stridesize, paddingsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) imgsizes = ((10, 10), (10, 5), (5, 5)) avgpoolsizes = ((5, 3), (3, 5), (3, 3)) stridesizes = ((3, 2), (2, 3), (3, 3)) paddingsizes = ((2, 2), (2, 1), (2, 2)) for i in range(len(imgsizes)): imgsize = imgsizes[i] imval = rng.rand(1, 1, imgsize[0], imgsize[1]) * 10.0 avgpoolsize = avgpoolsizes[i] stridesize = stridesizes[i] paddingsize = paddingsizes[i] # 'average_exc_pad' with non-zero padding is not implemented for mode in ['sum', 'average_inc_pad']: grad_shape = Pool.out_shape(imval.shape, avgpoolsize, st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(avgpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxPaddingStride(self): ignore_border = True # padding does not support ignore_border=False rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, stride, padding, input sizes examples = ( ((3, ), (2, ), (2, ), (5, )), ((3, ), (2, ), (2, ), (4, 5)), ((3, ), (2, ), (2, ), (4, 2, 5)), ((3, ), (2, ), (2, ), (4, 2, 5, 5)), ((3, 3), (2, 2), (2, 2), (4, 2, 5, 5)), ((4, 4), (2, 2), (1, 2), (4, 2, 5, 5)), ((3, 4), (1, 1), (2, 1), (4, 2, 5, 6)), ((4, 3), (1, 2), (0, 0), (4, 2, 6, 5)), ((2, 2), (2, 2), (1, 1), (4, 2, 5, 5)), ((4, 3, 2), (1, 2, 2), (0, 2, 1), (4, 6, 6, 5)), ((4, 3, 2), (1, 2, 2), (0, 2, 1), (4, 2, 6, 5, 5)), ) for example, mode in product( examples, ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): (maxpoolshp, stridesize, paddingsize, inputsize) = example imval = rng.rand(*inputsize) - 0.5 images = theano.shared(imval) numpy_output_val = self.numpy_max_pool_nd_stride_padding( imval, maxpoolshp, ignore_border, stridesize, paddingsize, mode) maxpool_op = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border, mode=mode)(images, maxpoolshp, stridesize, paddingsize) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxGrad_grad_st(self, example, ignore_border): # checks the gradient of the gradient for # the case that stride is used rng = numpy.random.RandomState(utt.fetch_seed()) (maxpoolshp, stride, inputsize) = example imval = rng.rand(*inputsize) grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border, st=stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) != 0: grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 2, 16, 16) images = tensor.dtensor4() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"] ): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([images], [output]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(maxpoolshp, ignore_border=ignore_border, mode=mode)(images) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_AveragePoolGrad_grad_stride(self, example, ignore_border, mode): # checks the gradient of the gradient for # the case that stride is used rng = np.random.RandomState(utt.fetch_seed()) (avgpoolshp, stride, inputsize) = example imval = rng.rand(*inputsize) grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border, stride=stride, ) # skip the grad verification when the output is empty if np.prod(grad_shape) != 0: grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def mp(input): return Pool( maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, mode=mode, )(input)
def test_DownsampleFactorMaxGrad_grad_st_extra(self): """checks the gradient of the gradient for the case that stride is used for extra examples""" rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) for indx in numpy.arange(len(maxpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(1, 2, imvsize[0], imvsize[1]) stride = stridesizes[indx] maxpoolshp = maxpoolshps[indx] for ignore_border in [True, False]: grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): out = Pool(ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) == 0: continue utt.verify_grad(mp, [imval, grad_val], rng=rng)
def mp(input1, input2): op1 = Pool(ndim=len(maxpoolshp), ignore_border=True) pooled_out = op1(input1, maxpoolshp, stridesize, paddingsize) op2 = DownsampleFactorMaxGradGrad(ndim=len(maxpoolshp), ignore_border=True) out = op2(input1, pooled_out, input2, maxpoolshp, stridesize, paddingsize) return out
def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=True, )(input, maxpoolshp, stridesize, paddingsize) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=True) return grad_op(input, out, grad, maxpoolshp, stridesize, paddingsize)
def mp(input, grad): out = Pool( maxpoolshp, ignore_border=ignore_border, st=stride)(input) grad_op = MaxPoolGrad( maxpoolshp, ignore_border=ignore_border, st=stride) return grad_op(input, out, grad)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input size examples = ( ((2,), (16,)), ((2,), (4, 16,)), ((2,), (4, 2, 16,)), ((1, 1), (4, 2, 16, 16)), ((2, 2), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)), ((2, 3, 2), (3, 2, 16, 16, 16)), ((2, 2, 3), (3, 2, 16, 16, 16)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ) for example, ignore_border, mode in product(examples, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): (maxpoolshp, inputsize) = example imval = rng.rand(*inputsize) images = theano.shared(imval) # Pure Numpy computation numpy_output_val = self.numpy_max_pool_nd(imval, maxpoolshp, ignore_border, mode=mode) # The pool_2d or pool_3d helper methods if len(maxpoolshp) == 2: output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) elif len(maxpoolshp) == 3: output = pool_3d(images, maxpoolshp, ignore_border, mode=mode) f = function([], [output, ]) output_val = f() utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border, mode=mode)(images, maxpoolshp) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([], maxpool_op) output_val = f() utt.assert_allclose(output_val, numpy_output_val)
def mp(input, grad): out = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp, stride) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def mp(input, grad): out = Pool( maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize, )(input) grad_op = MaxPoolGrad(maxpoolsize, ignore_border=True, st=stridesize, padding=paddingsize) return grad_op(input, out, grad)
def test_max_pool3d_grad_grad(): shps = [(1, 1, 12), (1, 1, 1, 1, 1), (1, 1, 1, 1, 1025), (1, 1, 2, 2, 2), (1, 1, 7, 7, 7), (1, 1, 9, 10, 11), (1, 6, 18, 18, 18), (1, 1, 6, 24, 24), (1, 10, 1, 24, 24), (1, 10, 6, 24, 24), (1, 30, 6, 12, 12), (1, 30, 2, 24, 24), (1, 30, 6, 24, 24), (1, 10, 10, 10, 11), (1, 1, 10, 10, 1025), (1, 1, 10, 10, 1023), (1, 1, 10, 1025, 10), (1, 1, 10, 1023, 10), ] numpy.random.RandomState(utt.fetch_seed()).shuffle(shps) test_ds = (2, 2, 2), (3, 2, 3), (1, 1, 1) test_st = (2, 2, 2), (2, 3, 2), (1, 1, 1) for shp in shps: for ds, st in itertools.product(test_ds, test_st): if ds[0] > shp[-3] or ds[1] > shp[-2] or ds[2] > shp[-1]: continue for ignore_border, pad in zip((True, False), [(1, 1, 1), (0, 0, 0)]): if pad[0] >= ds[0] or pad[1] >= ds[1] or pad[2] >= ds[2]: continue # print('test_downsample', shp, ds, st, pad, ignore_border) ds_op = Pool(ndim=len(ds), ignore_border=ignore_border) a = theano.shared(rand(*shp), 'a') ggf = gradient.Lop(tensor.grad((ds_op( tensor.as_tensor_variable(a), ds, st, pad)**2).sum(), a), a, a) ref_mode = copy.copy(mode_without_gpu) ref_mode.check_py_code = False gpu_mode = copy.copy(mode_with_gpu) gpu_mode.check_py_code = False gg = theano.function([], ggf, mode=gpu_mode) gg2 = theano.function([], ggf, mode=ref_mode) assert any([ isinstance(node.op, GpuDownsampleFactorMaxGradGrad) for node in gg.maker.fgraph.toposort() ]) assert any([ isinstance(node.op, DownsampleFactorMaxGradGrad) for node in gg2.maker.fgraph.toposort() ]) assert numpy.allclose(gg(), gg2()), (shp, ds, st, ignore_border)
def test_downsample(self): rng = np.random.RandomState(utt.fetch_seed()) # ws, shp examples = ( ((2, ), (16, )), ( (2, ), ( 4, 16, ), ), ( (2, ), ( 4, 2, 16, ), ), ((1, 1), (4, 2, 16, 16)), ((2, 2), (4, 2, 16, 16)), ((3, 3), (4, 2, 16, 16)), ((3, 2), (4, 2, 16, 16)), ((3, 2, 2), (3, 2, 16, 16, 16)), ((2, 3, 2), (3, 2, 16, 16, 16)), ((2, 2, 3), (3, 2, 16, 16, 16)), ((2, 2, 3, 2), (3, 2, 6, 6, 6, 5)), ) for example, ignore_border in itertools.product( examples, [True, False]): (ws, shp) = example vx = rng.rand(*shp) vex = rng.rand(*shp) x = theano.shared(vx) ex = theano.shared(vex) maxpool_op = Pool(ignore_border, ndim=len(ws)) a_pooled = maxpool_op(x, ws).flatten() yv = tensor.Rop(a_pooled, x, ex) mode = None if theano.config.mode == "FAST_COMPILE": mode = "FAST_RUN" rop_f = function([], yv, on_unused_input="ignore", mode=mode) sy, _ = theano.scan( lambda i, y, x, v: (tensor.grad(y[i], x) * v).sum(), sequences=tensor.arange(a_pooled.shape[0]), non_sequences=[a_pooled, x, ex], mode=mode, ) scan_f = function([], sy, on_unused_input="ignore", mode=mode) v1 = rop_f() v2 = scan_f() assert np.allclose(v1, v2), f"Rop mismatch: {v1} {v2}"
def test_DownsampleFactorMaxPaddingStride_grad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # maxpool, stride, pad, input sizes examples = ( ((3, ), (2, ), (2, ), (10, )), ( (3, ), (2, ), (2, ), ( 2, 10, ), ), ( (3, ), (2, ), (2, ), ( 2, 1, 10, ), ), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ) for (maxpoolshp, stridesize, padsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ndim=len(maxpoolshp), stride=stridesize, ignore_border=True, pad=padsize, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=True, )(input, maxpoolshp, stridesize, padsize) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=True) return grad_op(input, out, grad, maxpoolshp, stridesize, padsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolPaddingStride_grad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # avgpool, stride, pad, input sizes examples = ( ((3, ), (2, ), (2, ), (10, )), ( (3, ), (2, ), (2, ), ( 2, 10, ), ), ( (3, ), (2, ), (2, ), ( 2, 1, 10, ), ), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((5, 3, 2), (3, 2, 1), (2, 2, 2), (1, 1, 10, 5, 5)), ) for (avgpoolshp, stridesize, padsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # 'average_exc_pad' with non-zero padding is not implemented for mode in ["sum", "average_inc_pad"]: grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), stride=stridesize, ignore_border=True, pad=padsize, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=True, mode=mode) return grad_op(input, grad, avgpoolshp, stridesize, padsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMax(self): rng = numpy.random.RandomState(utt.fetch_seed()) # generate random images maxpoolshps = ((1, 1), (2, 2), (3, 3), (2, 3)) imval = rng.rand(4, 2, 16, 16) images = tensor.dtensor4() for maxpoolshp, ignore_border, mode in product( maxpoolshps, [True, False], ['max', 'sum', 'average_inc_pad', 'average_exc_pad']): # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # Pure Numpy computation numpy_output_val = self.numpy_max_pool_2d(imval, maxpoolshp, ignore_border, mode=mode) output = pool_2d(images, maxpoolshp, ignore_border, mode=mode) f = function([ images, ], [ output, ]) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val) # Pool op maxpool_op = Pool(maxpoolshp, ignore_border=ignore_border, mode=mode)(images) output_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) utt.assert_allclose(numpy.asarray(output_shape), numpy_output_val.shape) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def test_DownsampleFactorMaxStrideExtra(self): rng = np.random.RandomState(utt.fetch_seed()) maxpoolshps = ((5, 3), (5, 3), (5, 3), (5, 5), (3, 2), (7, 7), (9, 9)) stridesizes = ((3, 2), (7, 5), (10, 6), (1, 1), (2, 3), (10, 10), (1, 1)) imvsizs = ((16, 16), (16, 16), (16, 16), (8, 5), (8, 5), (8, 5), (8, 5)) outputshps = ( (4, 10, 4, 7), (4, 10, 5, 8), (4, 10, 2, 3), (4, 10, 3, 4), (4, 10, 2, 3), (4, 10, 2, 3), (4, 10, 4, 1), (4, 10, 4, 1), (4, 10, 3, 2), (4, 10, 4, 2), (4, 10, 1, 0), (4, 10, 1, 1), (4, 10, 0, 0), (4, 10, 1, 1), ) images = tensor.dtensor4() for indx in np.arange(len(maxpoolshps)): imvsize = imvsizs[indx] imval = rng.rand(4, 10, imvsize[0], imvsize[1]) stride = stridesizes[indx] maxpoolshp = maxpoolshps[indx] for ignore_border, mode in product( [True, False], ["max", "sum", "average_inc_pad", "average_exc_pad"]): indx_out = indx * 2 if not ignore_border: indx_out += 1 outputshp = outputshps[indx_out] # Pool op numpy_output_val = self.numpy_max_pool_2d_stride( imval, maxpoolshp, ignore_border, stride, mode) assert (numpy_output_val.shape == outputshp ), "outshape is {}, calculated shape is {}".format( outputshp, numpy_output_val.shape, ) maxpool_op = Pool(ignore_border=ignore_border, ndim=len(maxpoolshp), mode=mode)(images, maxpoolshp, stride) f = function([images], maxpool_op) output_val = f(imval) utt.assert_allclose(output_val, numpy_output_val)
def mp(input1, input2): op1 = Pool(ignore_border=True) pooled_out = op1(input1, maxpoolsize, stride=stridesize, pad=paddingsize) op2 = DownsampleFactorMaxGradGrad(ignore_border=True) out = op2(input1, pooled_out, input2, ws=maxpoolsize, stride=stridesize, pad=paddingsize) return out
def test_AveragePoolGrad_grad_st(self): """checks the gradient of the gradient for the case that stride is used""" rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 3), (5, 3)) stridesizes = ((1, 1), (3, 3), (5, 7)) imval = rng.rand(1, 2, 16, 16) for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ["sum", "average_inc_pad", "average_exc_pad"]: for stride in stridesizes: grad_shape = Pool.out_shape(imval.shape, avgpoolshp, ignore_border=ignore_border, st=stride) grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad(avgpoolshp, ignore_border=ignore_border, st=stride, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ["sum", "average_inc_pad", "average_exc_pad"]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape(imval.shape, avgpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(avgpoolshp, ignore_border=ignore_border, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool(maxpoolshp, ignore_border=ignore_border)(input) grad_op = MaxPoolGrad(maxpoolshp, ignore_border=ignore_border) return grad_op(input, out, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # maxpool, input sizes examples = ( ((2, ), (2, )), ((2, ), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((1, 1, 1), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 3, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 1, 3, 3, 4)), ) for (maxpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool(ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) maxpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for maxpoolshp in maxpoolshps: for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool(ignore_border=ignore_border)(input, maxpoolshp) grad_op = MaxPoolGrad(ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) avgpoolshps = ((1, 1), (3, 2), (2, 3)) imval = rng.rand(2, 3, 3, 4) * 10.0 # more variance means numeric gradient will be more accurate for avgpoolshp in avgpoolshps: for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad( avgpoolshp, ignore_border=ignore_border, mode=mode) return grad_op(input, grad) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad_st(self, example, ignore_border, mode): # checks the gradient of the gradient for # the case that stride is used rng = numpy.random.RandomState(utt.fetch_seed()) (avgpoolshp, stride, inputsize) = example imval = rng.rand(*inputsize) grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border, st=stride) # skip the grad verification when the output is empty if numpy.prod(grad_shape) != 0: grad_val = rng.rand(*grad_shape) def mp(input, grad): grad_op = AveragePoolGrad( ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp, stride) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, input sizes examples = ( ((2,), (2,)), ((2,), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((1, 1, 1), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 3, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 1, 3, 3, 4)), ) for (maxpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, maxpoolshp, ndim=len(maxpoolshp), ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=ignore_border)(input, maxpoolshp) grad_op = MaxPoolGrad( ndim=len(maxpoolshp), ignore_border=ignore_border) return grad_op(input, out, grad, maxpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = np.random.RandomState(utt.fetch_seed()) # avgpool, input sizes examples = ( ((2, ), (2, )), ((2, ), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ) for (avgpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: for mode in ["sum", "average_inc_pad", "average_exc_pad"]: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border, ) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad(ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_AveragePoolGrad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # avgpool, input sizes examples = ( ((2,), (2,)), ((2,), (2, 3)), ((1, 1), (2, 3, 3, 4)), ((3, 2), (2, 3, 3, 4)), ((2, 3), (2, 3, 3, 4)), ((1, 1, 1), (2, 3, 3, 4)), ((3, 2, 2), (2, 3, 3, 4)), ((2, 3, 2), (2, 3, 3, 4)), ((2, 2, 3), (2, 3, 3, 4)), ((2, 2, 3), (2, 1, 3, 3, 4)), ) for (avgpoolshp, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 # more variance means numeric gradient will be more accurate for ignore_border in [True, False]: for mode in ['sum', 'average_inc_pad', 'average_exc_pad']: # print 'maxpoolshp =', maxpoolshp # print 'ignore_border =', ignore_border # The shape of the gradient will be the shape of the output grad_shape = Pool.out_shape( imval.shape, avgpoolshp, ndim=len(avgpoolshp), ignore_border=ignore_border) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): grad_op = AveragePoolGrad( ndim=len(avgpoolshp), ignore_border=ignore_border, mode=mode) return grad_op(input, grad, avgpoolshp) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def test_DownsampleFactorMaxPaddingStride_grad_grad(self): rng = numpy.random.RandomState(utt.fetch_seed()) # maxpool, stride, padding, input sizes examples = ( ((3,), (2,), (2,), (10,)), ((3,), (2,), (2,), (2, 10,)), ((3,), (2,), (2,), (2, 1, 10,)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((5, 3), (3, 2), (2, 2), (1, 1, 10, 10)), ((3, 5), (2, 3), (2, 1), (1, 1, 10, 5)), ((3, 3), (3, 3), (2, 2), (1, 1, 5, 5)), ((5, 3, 3), (3, 2, 2), (2, 2, 2), (1, 1, 10, 5, 5)), ((3, 5, 3), (2, 3, 2), (2, 1, 2), (1, 1, 5, 10, 5)), ((3, 3, 5), (2, 2, 3), (2, 2, 1), (1, 1, 5, 5, 10)), ) for (maxpoolshp, stridesize, paddingsize, inputsize) in examples: imval = rng.rand(*inputsize) * 10.0 grad_shape = Pool.out_shape(imval.shape, maxpoolshp, ndim=len(maxpoolshp), st=stridesize, ignore_border=True, padding=paddingsize) grad_val = rng.rand(*grad_shape) * 10.0 def mp(input, grad): out = Pool( ndim=len(maxpoolshp), ignore_border=True, )(input, maxpoolshp, stridesize, paddingsize) grad_op = MaxPoolGrad(ndim=len(maxpoolshp), ignore_border=True) return grad_op(input, out, grad, maxpoolshp, stridesize, paddingsize) utt.verify_grad(mp, [imval, grad_val], rng=rng)
def infer_shape(self, node, in_shapes): ws, stride, pad = [node.inputs[1], node.inputs[2], node.inputs[3]] shp = Pool.out_shape(in_shapes[0], ws, self.ignore_border, stride, pad, self.ndim) return [shp]
def test_out_shape(self): assert Pool.out_shape((9, 8, 6), (2, 2)) == [9, 4, 3] assert Pool.out_shape((8, 6), (2, 2)) == [4, 3]
def maxpool_2d(z, in_dim, poolsize, poolstride): z = pool_2d(z, ds=poolsize, st=poolstride) output_size = tuple(Pool.out_shape(in_dim, poolsize, st=poolstride)) return z, output_size