示例#1
0
    def maxPoolingLayer(self, featureMaps, featureMapShape, poolingSize):
        (n, f, h, w) = featureMapShape
        numOfSubregions_y = int(h / poolingSize)
        numOfSubregions_x = int(w / poolingSize)

        s = featureMaps.flatten()
        minVal = s.min() - 1
        maxVals = max_pool_2d_same_size(input=featureMaps - minVal,
                                        patch_size=(poolingSize, poolingSize))
        # maxLocations = (maxVals > 0).nonzero()
        # maxLocations = T.switch(maxVals > 0, np.arange(1,np.prod(list(featureMapShape))+1,dtype=np.int).reshape(featureMapShape), np.zeros(featureMapShape,dtype=np.int)).nonzero_values()-1
        maxLocations = T.switch(
            maxVals > 0,
            np.arange(1, np.prod(list(featureMapShape)) + 1,
                      dtype=np.int).reshape(featureMapShape),
            np.zeros(featureMapShape, dtype=np.int))
        maxLocations2 = pool_2d(input=maxLocations.reshape(featureMapShape),
                                ds=(poolingSize, poolingSize),
                                ignore_border=True)
        maxLocations2 = maxLocations2.nonzero_values() - 1

        pooledLayer = pool_2d(input=featureMaps,
                              ds=(poolingSize, poolingSize),
                              ignore_border=True)

        return pooledLayer, maxLocations2
        def __init__(self, rng, input, 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)
            """

            self.input = input
            
            # pool each feature map individually, using maxpooling
            pooled_out = pool.max_pool_2d_same_size(
                input=input,
                patch_size=poolsize
            )

            self.output = pooled_out
            # store parameters of this layer

            # keep track of model input
            self.input = input
示例#3
0
    def __init__(self,
                 input,
                 poolsize=(2, 2),
                 stride=None,
                 ig_border=True,
                 mode='max',
                 same=False):
        """
        Allocate a LeNetConvPoolLayer with shared variable internal parameters.
        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows, #cols)
        """
        self.input = input

        if same:
            pooled_out = pool.max_pool_2d_same_size(input=self.input,
                                                    patch_size=poolsize)
        else:
            # downsample each feature map individually
            pooled_out = pool.pool_2d(input=self.input,
                                      ws=poolsize,
                                      ignore_border=ig_border,
                                      stride=stride,
                                      mode=mode)
        self.output = pooled_out
示例#4
0
    def __init__(self,
                 input,
                 ds,
                 img_shp,
                 mode='max',
                 ignore_border=True,
                 verbose=1):

        if verbose >= 3:
            print("... Creating pooling operator")

        if mode == 'max_same_size':  # output same size but maxpool with zeros for non outputs
            self.out = max_pool_2d_same_size(
                input=input,
                patch_size=ds,
            )
            _out_height = img_shp[2]
            _out_width = img_shp[3]

        elif mode == 'max' or mode == 'sum':  # normal maxpool
            self.out = pool_2d(input=input,
                               ws=ds,
                               ignore_border=ignore_border,
                               mode=mode)
            _out_height = int(floor(img_shp[2] / float(ds[0])))
            _out_width = int(floor(img_shp[3] / float(ds[1])))

        elif mode == 'mean':
            self.out = pool_2d(input=input,
                               ws=ds,
                               ignore_border=ignore_border,
                               mode='average_exc_pad')
        # ignore_border has some issue. False seems to pull things off GPU.
        self.out_shp = (img_shp[0], img_shp[1], _out_height, _out_width)
示例#5
0
    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 = conv.conv2d(input=input,
                               filters=self.W,
                               filter_shape=filter_shape,
                               image_shape=image_shape)

        # downsample each feature map individually, using maxpooling
        pooled_out = pool.max_pool_2d_same_size(conv_out, 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
        self.output = T.tanh(pooled_out + self.b.dimshuffle('x', 0, 'x', 'x'))

        # store parameters of this layer
        self.params = [self.W, self.b]
示例#6
0
 def set_inpt(self, inpt, inpt_dropout, mini_batch_size):
     self.inpt = inpt.reshape(self.image_shape)
     conv_out = conv.conv2d(input=self.inpt,
                            filters=self.w,
                            filter_shape=self.filter_shape,
                            image_shape=self.image_shape)
     #        pooled_out = downsample.max_pool_2d(
     #            input=conv_out, ds=self.poolsize, ignore_border=True)
     pooled_out = pool.max_pool_2d_same_size(input=conv_out,
                                             patch_size=self.poolsize)
     self.output = self.activation_fn(pooled_out +
                                      self.b.dimshuffle('x', 0, 'x', 'x'))
     self.output_dropout = self.output  # no dropout in the convolutional layers
示例#7
0
    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)
        utt.assert_allclose(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)
示例#8
0
    def __init__(self, input, patch_size=(2, 2)):
        """
        :type input: theano.tensor.dtensor4
        :param input: symbolic image tensor, of shape image_shape

        :type poolsize: tuple or list of length 2
        :param poolsize: the downsampling (pooling) factor (#rows, #cols)

        :type ignore_border: boolean
        :param ignore_border: indicates whether to ingnore_border when pooling or not
        """

        # downsample each feature map individually, using maxpooling
        pooled_out = max_pool_2d_same_size(input=input, patch_size=patch_size)
        self.output = pooled_out
示例#9
0
    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)
        utt.assert_allclose(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)
示例#10
0
	def get_encoder_output(self, input):
		conv = T.nnet.conv2d(
			input=input,
			filters=self.W,
			filter_flip=False,
			border_mode='valid'
			)
		if self.batch_norm:
			mu = T.mean(conv, axis=0, keepdims=True)
			var = T.mean((conv - mu)**2, axis=0, keepdims=True)
			output = (conv - mu) / T.sqrt(var + 1e-5) * self.gamma_in.dimshuffle('x',0,'x','x') + self.b.dimshuffle('x',0,'x','x')
			output = T.nnet.sigmoid(output)
		else:
			output = T.nnet.sigmoid(conv + self.b.dimshuffle('x',0,'x','x'))

		if self.pool_shape is not None:
			output = max_pool_2d_same_size(output, self.pool_shape)
		return output
示例#11
0
    def __init__ (      self,
                        input,
                        ds,
                        img_shp,                     
                        mode = 'max',  
                        ignore_border = True,                                                    
                        verbose = 1
                ):

        if verbose >=3:
            print "... Creating pooling operator"
                
        if mode == 'max_same_size':      # output same size but maxpool with zeros for non outputs
            self.out = max_pool_2d_same_size(
                            input=input,
                            patch_size=ds,
                            )
            _out_height = img_shp[2] 
            _out_width  = img_shp[3]                
                
        elif mode == 'max' or mode == 'sum':    # normal maxpool
            self.out = pool_2d(
                input=input,
                ds=ds,
                ignore_border = ignore_border,
                mode = mode
                )
            _out_height = int(floor(img_shp[2] / float(ds[0])))
            _out_width  = int(floor(img_shp[3] / float(ds[1])))                 
                
        elif mode == 'mean':
            self.out = pool_2d(
                input=input,
                ds=ds,
                ignore_border = ignore_border,
                mode = 'average_exc_pad'
                )        
        # ignore_border has some issue. False seems to pull things off GPU.
        self.out_shp = (img_shp[0], img_shp[1], _out_height, _out_width)          
示例#12
0
    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)
示例#13
0
 def mp(input):
     return max_pool_2d_same_size(input, patch_size)
示例#14
0
 def mp(input):
     return max_pool_2d_same_size(input, patch_size)
示例#15
0
    new_img.show()
    sys.stdout.flush()

    #		RELU
    #relu now holds the feture maps corresponding with each filter of shape(# filters, width, height, (r,g,b))
    relu = T.tensor.nnet.relu(fmap_collection)
    #change the index to get diffrent value for the filter from 0-#filters
    relu = np.asarray(relu[0], dtype="uint8").reshape(
        fmap_collection[0].shape
    )  #tensor with the each feature map from layer 1 going through RELU
    relu_img = Image.fromarray(relu)
    relu_img.show()

    # 		MAX POOLING
    max_pool1 = max_pool_2d_same_size(relu.reshape(relu.shape[2],
                                                   relu.shape[0],
                                                   relu.shape[1]),
                                      patch_size=(3, 3))
    poolresult = max_pool1.eval()

    #an array holding the feature maps after they go through max pooling of shape(x,y,(r,g,b))
    new_result = np.asarray(poolresult,
                            dtype="uint8").reshape(poolresult.shape[1],
                                                   poolresult.shape[2],
                                                   poolresult.shape[0])
    max_pool_img = Image.fromarray(new_result)
    max_pool_img.show()

    #		CONV LAYER 2
    data_layer2 = list(max_pool_img.getdata())
    red_values_layer2 = [x[0] for x in data_layer2]
    blue_values_layer2 = [x[2] for x in data_layer2]

input = T.tensor4()
pool_out = pool_2d(input, ds=(2,2),ignore_border=True,mode='max')
method = 1

if method==1:	
	#indices = T.grad(None, wrt=input, known_grads={pool_out: T.ones_like(pool_out)})
	indices = MaxPoolGrad((2,2), True)(input, pool_out, T.ones_like(pool_out))	
	unpool = indices*pool_out.repeat(2, axis=2).repeat(2, axis=3)
		
elif method==2:
	indices,pool_out = pool_2d(input, ds=(2,2),ignore_border=True,mode='max') #modifiy the code of max_pool_2d, to be completed
	
elif method==3:
	pool_same_size = max_pool_2d_same_size(input,(2,2))
	#indices = pool_same_size>0 #get 0/1 indicating non/argmax
	#unpool = T.set_subtensor(T.flatten(T.zeros_like(input))[T.flatten(indices)],T.flatten(pool_out)).reshape(input.shape)
	
	
	#indices = pool_same_size.nonzero() #get an array of the cordinates of argmax	
	#unpool = T.set_subtensor(T.zeros_like(input)[indices],T.flatten(pool_out))
f_pool = theano.function([input],pool_out)
f_pool_out_same_size = theano.function([input],pool_out_same_size)
f_index = theano.function([input],indices)
f_unpool = theano.function([input],unpool)
a = np.arange(16).reshape(1, 1, 4, 4)

print f(a)	

from theano.tensor.signal.pool import max_pool_2d_same_size, pool_2d, MaxPoolGrad

input = T.tensor4()
pool_out = pool_2d(input, ds=(2, 2), ignore_border=True, mode='max')
method = 1

if method == 1:
    #indices = T.grad(None, wrt=input, known_grads={pool_out: T.ones_like(pool_out)})
    indices = MaxPoolGrad((2, 2), True)(input, pool_out, T.ones_like(pool_out))
    unpool = indices * pool_out.repeat(2, axis=2).repeat(2, axis=3)

elif method == 2:
    indices, pool_out = pool_2d(
        input, ds=(2, 2), ignore_border=True,
        mode='max')  #modifiy the code of max_pool_2d, to be completed

elif method == 3:
    pool_same_size = max_pool_2d_same_size(input, (2, 2))
    #indices = pool_same_size>0 #get 0/1 indicating non/argmax
    #unpool = T.set_subtensor(T.flatten(T.zeros_like(input))[T.flatten(indices)],T.flatten(pool_out)).reshape(input.shape)

    #indices = pool_same_size.nonzero() #get an array of the cordinates of argmax
    #unpool = T.set_subtensor(T.zeros_like(input)[indices],T.flatten(pool_out))
f_pool = theano.function([input], pool_out)
f_pool_out_same_size = theano.function([input], pool_out_same_size)
f_index = theano.function([input], indices)
f_unpool = theano.function([input], unpool)
a = np.arange(16).reshape(1, 1, 4, 4)

print f(a)