def __init__(self, input, poolsize=(2, 2), store_pooling_indices=False, keep_original_size=False): self.poolsize=poolsize self.input = input self.store_pooling_indices = store_pooling_indices if self.store_pooling_indices: self.pooling_indices = downsample.max_pool_2d_same_size( input=self.input.dimshuffle(0,1,3,2) + 10.0, #TODO fix this nicely. # 10 is arbitrary and can still fail for -10 entries. But at least with standard non-linearities this will hopefully fail less often. patch_size = self.poolsize ).dimshuffle(0,1,3,2) # the two dimshuffles are needed because the last dimensions output cannot be over 512! self.pooling_indices = T.neq(self.pooling_indices, 0.0) if keep_original_size: self.output = downsample.max_pool_2d_same_size( input=self.input.dimshuffle(0,1,3,2), patch_size = self.poolsize ).dimshuffle(0,1,3,2) else: self.output = downsample.max_pool_2d( input=self.input.dimshuffle(0,1,3,2), ds=self.poolsize, ignore_border=False ).dimshuffle(0,1,3,2) # the two dimshuffles are needed because the last dimensions output cannot be over 512!
def test_max_pool_2d_2D_same_size(self): rng = numpy.random.RandomState(utt.fetch_seed()) test_input_array = numpy.array([[[[1.0, 2.0, 3.0, 4.0], [5.0, 6.0, 7.0, 8.0]]]]).astype(theano.config.floatX) test_answer_array = numpy.array([[[[0.0, 0.0, 0.0, 0.0], [0.0, 6.0, 0.0, 8.0]]]]).astype(theano.config.floatX) input = tensor.tensor4(name="input") patch_size = (2, 2) op = max_pool_2d_same_size(input, patch_size) op_output = function([input], op)(test_input_array) assert numpy.all(op_output == test_answer_array), "op_output is %s, test_answer_array is %s" % ( op_output, numpy_output_val, ) def mp(input): return max_pool_2d_same_size(input, patch_size) utt.verify_grad(mp, [test_input_array], rng=rng)
def test_max_pool_2d_2D_same_size(self): rng = numpy.random.RandomState(utt.fetch_seed()) test_input_array = numpy.array([[[[1., 2., 3., 4.], [5., 6., 7., 8.]]]]).astype(theano.config.floatX) test_answer_array = numpy.array([[[[0., 0., 0., 0.], [0., 6., 0., 8.]]] ]).astype(theano.config.floatX) input = tensor.tensor4(name='input') patch_size = (2, 2) op = max_pool_2d_same_size(input, patch_size) op_output = function([input], op)(test_input_array) assert numpy.all(op_output == test_answer_array), ( "op_output is %s, test_answer_array is %s" % (op_output, test_answer_array)) def mp(input): return max_pool_2d_same_size(input, patch_size) utt.verify_grad(mp, [test_input_array], rng=rng)
def apply(self, X): """Apply same size max-pooling operation Parameters ---------- X : 4D tensor Max pooling will be done over the 2 last dimensions. Returns ------- pooled : 4D tensor Pooled feature maps """ if theano.__version__ == "0.7.0": raise ValueError("Same size pooling is not supported in %s" % (theano.__version__)) return downsample.max_pool_2d_same_size(X, self.pool_size)
def apply(self, X): """Apply same size max-pooling operation Parameters ---------- X : 4D tensor Max pooling will be done over the 2 last dimensions. Returns ------- pooled : 4D tensor Pooled feature maps """ if theano.__version__=="0.7.0": raise ValueError("Same size pooling is not supported in %s" % (theano.__version__)); return downsample.max_pool_2d_same_size(X, self.pool_size);
def test_max_pool_2d_2D_same_size(self): rng = numpy.random.RandomState(utt.fetch_seed()) test_input_array = numpy.array([[[ [1., 2., 3., 4.], [5., 6., 7., 8.] ]]]) test_answer_array = numpy.array([[[ [0., 0., 0., 0.], [0., 6., 0., 8.] ]]]) input = tensor.tensor4(name='input') patch_size = (2, 2) op = max_pool_2d_same_size(input, patch_size) op_output = function([input], op)(test_input_array) assert numpy.all(op_output == test_answer_array), ( "op_output is %s, test_answer_array is %s" % ( op_output, numpy_output_val ) ) def mp(input): return max_pool_2d_same_size(input, patch_size) utt.verify_grad(mp, [test_input_array], rng=rng)
def mp(input): return max_pool_2d_same_size(input, patch_size)
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): """ Allocate a LeNetConvPoolLayer with shared variable internal parameters. :type rng: numpy.random.RandomState :param rng: a random number generator used to initialize weights :type input: theano.tensor.dtensor4 :param input: symbolic image tensor, of shape image_shape :type filter_shape: tuple or list of length 4 :param filter_shape: (number of filters, num input feature maps, filter height, filter width) :type image_shape: tuple or list of length 4 :param image_shape: (batch size, num input feature maps, image height, image width) :type poolsize: tuple or list of length 2 :param poolsize: the downsampling (pooling) factor (#rows, #cols) """ assert image_shape[1] == filter_shape[1] self.input = input # there are "num input feature maps * filter height * filter width" # inputs to each hidden unit fan_in = numpy.prod(filter_shape[1:]) # each unit in the lower layer receives a gradient from: # "num output feature maps * filter height * filter width" / # pooling size fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) // numpy.prod(poolsize)) # initialize weights with random weights W_bound = numpy.sqrt(6. / (fan_in + fan_out)) self.W = theano.shared(numpy.asarray(rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), dtype=theano.config.floatX), borrow=True) # the bias is a 1D tensor -- one bias per output feature map b_values = numpy.zeros((filter_shape[0], ), dtype=theano.config.floatX) self.b = theano.shared(value=b_values, borrow=True) # convolve input feature maps with filters conv_out = conv2d(input=input, filters=self.W, filter_shape=filter_shape, input_shape=image_shape) # downsample each feature map individually, using maxpooling pooled_out = downsample.max_pool_2d(input=conv_out, ds=poolsize, ignore_border=True) # pooled_out2 = downsample.max_pool_2d_same_size(input=conv_out, patch_size=poolsize) # add the bias term. Since the bias is a vector (1D array), we first # reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will # thus be broadcasted across mini-batches and feature map # width & height tanh self.output = T.maximum( 0, pooled_out2 + self.b.dimshuffle('x', 0, 'x', 'x')) # store parameters of this layer self.params = [self.W, self.b] # keep track of model input self.input = input
def __init__(self, rng, input, filter_shape, image_shape, poolsize=(2, 2)): """ Allocate a LeNetConvPoolLayer with shared variable internal parameters. :type rng: numpy.random.RandomState :param rng: a random number generator used to initialize weights :type input: theano.tensor.dtensor4 :param input: symbolic image tensor, of shape image_shape :type filter_shape: tuple or list of length 4 :param filter_shape: (number of filters, num input feature maps, filter height, filter width) :type image_shape: tuple or list of length 4 :param image_shape: (batch size, num input feature maps, image height, image width) :type poolsize: tuple or list of length 2 :param poolsize: the downsampling (pooling) factor (#rows, #cols) """ assert image_shape[1] == filter_shape[1] self.input = input # there are "num input feature maps * filter height * filter width" # inputs to each hidden unit fan_in = numpy.prod(filter_shape[1:]) # each unit in the lower layer receives a gradient from: # "num output feature maps * filter height * filter width" / # pooling size fan_out = (filter_shape[0] * numpy.prod(filter_shape[2:]) // numpy.prod(poolsize)) # initialize weights with random weights W_bound = numpy.sqrt(6. / (fan_in + fan_out)) self.W = theano.shared( numpy.asarray( rng.uniform(low=-W_bound, high=W_bound, size=filter_shape), dtype=theano.config.floatX ), borrow=True ) # the bias is a 1D tensor -- one bias per output feature map b_values = numpy.zeros((filter_shape[0],), dtype=theano.config.floatX) self.b = theano.shared(value=b_values, borrow=True) # convolve input feature maps with filters conv_out = conv2d( input=input, filters=self.W, filter_shape=filter_shape, input_shape=image_shape ) # downsample each feature map individually, using maxpooling pooled_out = downsample.max_pool_2d( input=conv_out, ds=poolsize, ignore_border=True ) # pooled_out2 =downsample.max_pool_2d_same_size(input = conv_out, patch_size = poolsize) # add the bias term. Since the bias is a vector (1D array), we first # reshape it to a tensor of shape (1, n_filters, 1, 1). Each bias will # thus be broadcasted across mini-batches and feature map # width & height tanh self.output = T.maximum(0, pooled_out2 + self.b.dimshuffle('x', 0, 'x', 'x')) # store parameters of this layer self.params = [self.W, self.b] # keep track of model input self.input = input