Example #1
0
def maxpool(data, factor, getPos=True):
	"""
	Return max pooled data and the pooled pixel positions.

	Args:
	-----
		data: An N x k x m x n array.
		factor: Pooling factor.

	Returns:
	--------
		An N x k x (m/factor) x (n/factor), N x k x m x n arrays.
	"""
	_data = np.asarray(data, dtype='float32')
	x = tn.ftensor4('x')
	f = thn.function([], max_pool(x, factor, True), givens={x: shared(_data)})
	g = thn.function([], max_pool_same(x, factor)/x, givens={x: shared(_data + 0.0000000001)})

	pooled = f()
	if not getPos:
		return pooled

	positions = g()
	positions[np.where(np.isnan(positions))] = 0
	return pooled, positions
Example #2
0
def maxpool(data, factor, getPos=True):
    """
	Return max pooled data and the pooled pixel positions.

	Args:
	-----
		data: An N x k x m x n array.
		factor: Pooling factor.

	Returns:
	--------
		An N x k x (m/factor) x (n/factor), N x k x m x n arrays.
	"""
    _data = np.asarray(data, dtype='float32')
    x = tn.ftensor4('x')
    f = thn.function([], max_pool(x, factor, True), givens={x: shared(_data)})
    g = thn.function([],
                     max_pool_same(x, factor) / x,
                     givens={x: shared(_data + 0.0000000001)})

    pooled = f()
    if not getPos:
        return pooled

    positions = g()
    positions[np.where(np.isnan(positions))] = 0
    return pooled, positions
Example #3
0
 def get_nonlin_output(self):
     rval = max_pool(self.X, self.pool_shape) 
     ''', 
     self.pool_stride, 
     [self.X.shape[2], self.X.shape[3]])
     '''
     #rval = T.switch(rval > 0., rval, 0.)
     #rval = T.maximum(rval, 0.)
     rval = self.nonlin.apply(rval)
     return rval
Example #4
0
    def initialize_x_space(self,rng):
        """
        This function initializes the coding space and dimmensions
        
        X is how I generally call the sparse code variables. 
        Thus, X_space has its dimmensions

        """
        dummy_batch_size = self.mlp.batch_size

        if dummy_batch_size is None:
            dummy_batch_size = self.batch_size
        dummy_detector =\
                sharedX(self.detector_space.get_origin_batch(dummy_batch_size))
        
        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = max_pool(dummy_detector,
                                   self.pool_shape)
                '''
                                   pool_stride=self.pool_stride,
                                   image_shape=self.detector_space)
                '''
            elif self.pool_type == 'mean':
                dummy_p = mean_pool(dummy_detector,
                                    self.pool_shape)
                '''
                                    pool_stride=self.pool_stride,
                                    image_shape=self.detector_shape)
                '''
            dummy_p = dummy_p.eval()
            self.x_space = Conv2DSpace(shape=[dummy_p.shape[2],
                                              dummy_p.shape[3]],
                                            num_channels=
                                                self.output_channels,
                                            axes=('b', 'c', 0, 1))
        else:
            dummy_detector = dummy_detector.eval()
            self.x_space = Conv2DSpace(shape=[dummy_detector.shape[2],
                                            dummy_detector.shape[3]],
                                            num_channels=self.output_channels,
                                            axes=('b', 'c', 0, 1))
        
        X = rng.normal(0, .001, size=(dummy_batch_size,
                                     self.output_channels,
                                     self.detector_space.shape[0],
                                     self.detector_space.shape[1]))
        
        self.X = sharedX(X, self.layer_name+'_X')

        logger.info('Code space: {0}'.format(self.x_space.shape))
Example #5
0
def sparse_pool(input_image, stride=2, poolsize=(2, 2), mode='max'):
    pooled_array = []
    counter = 0
    for offset_x in xrange(stride):
        for offset_y in xrange(stride):
            pooled_array += [
                max_pool(input_image[:, :, offset_x::stride, offset_y::stride],
                         poolsize,
                         st=(1, 1),
                         mode=mode,
                         padding=(0, 0),
                         ignore_border=True)
            ]
            counter += 1

    # Concatenate pooled image to create one big image
    running_concat = []
    for it in xrange(stride):
        running_concat += [
            T.concatenate(pooled_array[stride * it:stride * (it + 1)], axis=3)
        ]
    concatenated_image = T.concatenate(running_concat, axis=2)

    pooled_output_array = []

    for it in xrange(counter + 1):
        pooled_output_array += [T.tensor4()]

    pooled_output_array[0] = concatenated_image

    counter = 0
    for offset_x in xrange(stride):
        for offset_y in xrange(stride):
            pooled_output_array[counter + 1] = T.set_subtensor(
                pooled_output_array[counter][:, :, offset_x::stride,
                                             offset_y::stride],
                pooled_array[counter])
            counter += 1

    return pooled_output_array[counter]
Example #6
0
File: cnn.py Project: ev0/Cnn
	def feedf(self, data):
		"""
		Pool features within a given receptive from the input data.

		Args:
		-----
			data: An N x k x m1 x n1 array of input plains.

		Returns:
		-------
			A N x k x m2 x n2 array of output plains.
		"""
		if self.type == 'max':
			_data = np.asarray(data, dtype='float32')
			x = tn.ftensor4('x')
			f = thn.function([], max_pool(x, self.factor), givens={x: shared(_data)})
			g = thn.function([], max_pool_same(x, self.factor)/x, givens={x: shared(_data + 0.0000000001)})
			self.grad = g()
			self.grad[np.where(np.isnan(self.grad))] = 0
			return f()
		else:
			return downsample(data, (1, 1, self.factor[0], self.factor[1]))
Example #7
0
    def initialize_x_space(self,rng):
        """
        This function initializes the coding space and dimmensions
        
        X is how I generally call the sparse code variables. 
        Thus, X_space has its dimmensions

        """
        dummy_batch_size = self.mlp.batch_size

        if dummy_batch_size is None:
            dummy_batch_size = self.batch_size
        dummy_detector =\
                sharedX(self.detector_space.get_origin_batch(dummy_batch_size))
        
        if self.pool_type is not None:
            assert self.pool_type in ['max', 'mean']
            if self.pool_type == 'max':
                dummy_p = max_pool(dummy_detector,
                                   self.pool_shape)
                ''',
                                   pool_stride=self.pool_stride,
                                   image_shape=self.detector_space.shape)
                '''
            elif self.pool_type == 'mean':
                dummy_p = mean_pool(dummy_detector,
                                    self.pool_shape)
                ''',
                                    pool_stride=self.pool_stride,
                                    image_shape=self.detector_shape.shape)
                '''
            dummy_p = dummy_p.eval()
            self.x_space = Conv2DSpace(shape=[dummy_p.shape[2],
                                              dummy_p.shape[3]],
                                            num_channels=self.output_channels,
                                            axes=('b', 'c', 0, 1))
        else:
            dummy_detector = dummy_detector.eval()
            self.x_space = Conv2DSpace(shape=[dummy_detector.shape[2],
                                            dummy_detector.shape[3]],
                                            num_channels=self.output_channels,
                                            axes=('b', 'c', 0, 1))
        
        X = rng.normal(0, .001, size=(dummy_batch_size,
                                     self.output_channels,
                                     self.detector_space.shape[0],
                                     self.detector_space.shape[1]))
        
        self.dim_x = self.output_channels * self.detector_space.shape[0] \
                      * self.detector_space.shape[1]

        self.X = sharedX(X, self.layer_name+'_X')
        
        S0 = rng.normal(0, .001, size=(dummy_batch_size,
                                      self.input_channels,
                                      self.input_space.components[0].shape[0],
                                      self.input_space.components[0].shape[1]))
        
        self.S0 = sharedX(S0, self.layer_name+'_S0')

        S1 = rng.normal(0, .001, 
                size=(dummy_batch_size, self.dim))

        self.S1 = sharedX(S1, self.layer_name+'_S1')
        
        # This is the statistic that comes from the layer above
        top_flow = rng.binomial(1, .1, size=(dummy_batch_size,
                                            self.output_channels,
                                            self.x_space.shape[0],
                                            self.x_space.shape[0]))

        self.top_flow = sharedX(top_flow, self.layer_name+'_top_flow')
                                      
        logger.info('Code space: {0}'.format(self.x_space.shape))
Example #8
0
 def get_nonlin_output(self, state_below):
     rval = max_pool(self.X, self.pool_shape)
     rval = self.nonlin.apply(rval)
     return rval