Exemplo n.º 1
0
    def setUp(self):
        self.op = FilterActs(module_stride=self.module_stride, openmp=False)
        self.op_omp = FilterActs(module_stride=self.module_stride, openmp=True)

        self.s_filters_list = [
            theano.shared(rand(fshape, self.dtype))
            for fshape in self.fshape_list
        ]
        self.s_images_list = [
            theano.shared(rand(ishape, self.dtype))
            for ishape in self.ishape_list
        ]
Exemplo n.º 2
0
    def alloc(cls,
            conf,
            image_shape,  # input dimensionality
            filters_hs_shape,       
            filters_irange,
            sigma,            
            seed = 8923402,            
            ):
 	rng = numpy.random.RandomState(seed)

        self = cls()
       
	n_images, n_channels, n_img_rows, n_img_cols = image_shape
        n_filters_hs_modules, n_filters_hs_per_modules, fcolors, n_filters_hs_rows, n_filters_hs_cols = filters_hs_shape        
        assert fcolors == n_channels        
        self.sigma = sigma
        self.v_shape = image_shape
        print 'v_shape'
	print self.v_shape
	self.filters_hs_shape = filters_hs_shape
        print 'self.filters_hs_shape'
        print self.filters_hs_shape
        self.out_conv_hs_shape = FilterActs.infer_shape_without_instance(self.v_shape,self.filters_hs_shape)        
        print 'self.out_conv_hs_shape'
        print self.out_conv_hs_shape
        conv_bias_hs_shape = (n_filters_hs_modules,n_filters_hs_per_modules)
        self.conv_bias_hs_shape = conv_bias_hs_shape
        print 'self.conv_bias_hs_shape'
        print self.conv_bias_hs_shape
        bias_v_shape = self.v_shape[1:]
        self.bias_v_shape = bias_v_shape
        print 'self.bias_v_shape'
        print self.bias_v_shape
        
                
        self.filters_hs = sharedX(rng.randn(*filters_hs_shape) * filters_irange , 'filters_hs')  
        
        #conv_bias_ival = rng.rand(*conv_bias_hs_shape)*2-1
        #conv_bias_ival *= conf['conv_bias_irange']
        #conv_bias_ival += conf['conv_bias0']
        conv_bias_ival = numpy.zeros(conv_bias_hs_shape)
	self.conv_bias_hs = sharedX(conv_bias_ival, name='conv_bias_hs')
	self.bias_v = sharedX(numpy.zeros(self.bias_v_shape), name='bias_v')       
        
        negsample_mask = numpy.zeros((n_channels,n_img_rows,n_img_cols),dtype=floatX)
 	negsample_mask[:,n_filters_hs_rows:n_img_rows-n_filters_hs_rows+1,n_filters_hs_cols:n_img_cols-n_filters_hs_cols+1] = 1
	self.negsample_mask = sharedX(negsample_mask,'negsample_mask')                
        
        self.conf = conf
        self._params = [self.filters_hs,
                self.conv_bias_hs,
                self.bias_v
                ]
        return self
Exemplo n.º 3
0
    def setUp(self):
        self.op = FilterActs(self.module_stride)

        for i in range(self.nbTests):
            self.s_images_list = [
                theano.shared(rand(ishape, self.dtype))
                for ishape in self.ishape_list
            ]
            self.s_filters_list = [
                theano.shared(rand(fshape, self.dtype))
                for fshape in self.fshape_list
            ]
Exemplo n.º 4
0
    def alloc(cls,
            conf,
            image_shape,  # input dimensionality
            filters_hs_shape,       
            filters_irange,
            v_prec,
            v_prec_lower_limit, #should be parameter of the training algo            
            seed = 8923402            
            ):
 	print 'alloc rbm'
        rng = numpy.random.RandomState(seed)

        self = cls()
       
	n_images, n_channels, n_img_rows, n_img_cols = image_shape
        n_filters_hs_modules, n_filters_hs_per_modules, fcolors, n_filters_hs_rows, n_filters_hs_cols = filters_hs_shape        
        assert fcolors == n_channels        
        self.v_shape = image_shape
        print 'v_shape'
	print self.v_shape
	self.filters_hs_shape = filters_hs_shape
        print 'self.filters_hs_shape'
        print self.filters_hs_shape
        self.out_conv_hs_shape = FilterActs.infer_shape_without_instance(self.v_shape,self.filters_hs_shape)        
        print 'self.out_conv_hs_shape'
        print self.out_conv_hs_shape
        conv_bias_hs_shape = self.out_conv_hs_shape[1:]
        #conv_bias_hs_shape = (n_filters_hs_modules, n_filters_hs_per_modules) 
        self.conv_bias_hs_shape = conv_bias_hs_shape
        print 'self.conv_bias_hs_shape'
        print self.conv_bias_hs_shape
        self.v_prec = sharedX(numpy.zeros((n_channels, n_img_rows, n_img_cols))+v_prec, 'var_v_prec')
        self.v_prec_fast = sharedX(numpy.zeros((n_channels, n_img_rows, n_img_cols))+conf['v_prec_lower_limit'], 'var_v_prec_fast')
        #self.v_prec = sharedX(v_prec, 'var_v_prec')        
        #self.v_prec_fast = sharedX(0.0, 'var_v_prec_fast')        
        self.v_prec_lower_limit = sharedX(v_prec_lower_limit, 'v_prec_lower_limit')
        self.v_prec_fast_lower_limit = sharedX(conf['v_prec_fast_lower_limit'], 'v_prec_fast_lower_limit')
        
        self.filters_hs = sharedX(rng.randn(*filters_hs_shape) * filters_irange , 'filters_hs')  
        self.filters_hs_fast = sharedX(numpy.zeros(filters_hs_shape), 'filters_hs_fast') 
        
	self.conv_bias_hs = sharedX(numpy.zeros(self.conv_bias_hs_shape), name='conv_bias_hs')
        self.conv_bias_hs_fast = sharedX(numpy.zeros(self.conv_bias_hs_shape), name='conv_bias_hs_fast')
        
        conv_mu_ival = numpy.zeros(conv_bias_hs_shape,dtype=floatX) + conf['conv_mu0']
	self.conv_mu = sharedX(conv_mu_ival, name='conv_mu')
	self.conv_mu_fast = sharedX(numpy.zeros(conv_bias_hs_shape,dtype=floatX), name='conv_mu_fast')
        
	if conf['alpha_logdomain']:
            conv_alpha_ival = numpy.zeros(conv_bias_hs_shape,dtype=floatX) + numpy.log(conf['conv_alpha0'])
	    self.conv_alpha = sharedX(conv_alpha_ival,'conv_alpha')
	    conv_alpha_ival_fast = numpy.zeros(conv_bias_hs_shape,dtype=floatX)
	    #conv_alpha_ival_fast = numpy.zeros(conv_bias_hs_shape,dtype=floatX) + numpy.log(conf['alpha_min'])
	    self.conv_alpha_fast = sharedX(conv_alpha_ival_fast, name='conv_alpha_fast')
	else:
            self.conv_alpha = sharedX(
                    numpy.zeros(conv_bias_hs_shape)+conf['conv_alpha0'],
                    'conv_alpha')
            self.conv_alpha_fast = sharedX(
                    numpy.zeros(conv_bias_hs_shape), name='conv_alpha_fast')        
            #self.conv_alpha_fast = sharedX(
            #        numpy.zeros(conv_bias_hs_shape)+conf['alpha_min'], name='conv_alpha_fast')        
 
        if conf['lambda_logdomain']:
            self.conv_lambda = sharedX(
                    numpy.zeros(self.filters_hs_shape)
                        + numpy.log(conf['lambda0']),
                    name='conv_lambda')
            self.conv_lambda_fast = sharedX(
                    numpy.zeros(self.filters_hs_shape),
                    name='conv_lambda_fast')        
            #self.conv_lambda_fast = sharedX(
            #        numpy.zeros(self.filters_hs_shape)
            #            + numpy.log(conf['lambda_min']),
            #        name='conv_lambda_fast')         
        else:
            self.conv_lambda = sharedX(
                    numpy.zeros(self.filters_hs_shape)
                        + (conf['lambda0']),
                    name='conv_lambda')
            self.conv_lambda_fast = sharedX(
                    numpy.zeros(self.filters_hs_shape),
                    name='conv_lambda_fast')        
            #self.conv_lambda_fast = sharedX(
            #        numpy.zeros(self.filters_hs_shape)
            #            + (conf['lambda_min']),
            #        name='conv_lambda_fast')        

        negsample_mask = numpy.zeros((n_channels,n_img_rows,n_img_cols),dtype=floatX)
 	negsample_mask[:,n_filters_hs_rows:n_img_rows-n_filters_hs_rows+1,n_filters_hs_cols:n_img_cols-n_filters_hs_cols+1] = 1
	self.negsample_mask = sharedX(negsample_mask,'negsample_mask')                
        
        self.conf = conf
        self._params = [self.v_prec,
                self.filters_hs,
                self.conv_bias_hs,
                self.conv_mu, 
                self.conv_alpha,
                self.conv_lambda
                ]
        self._params_fast = [self.v_prec_fast,
                self.filters_hs_fast,
                self.conv_bias_hs_fast,
                self.conv_mu_fast,
                self.conv_alpha_fast,
                self.conv_lambda_fast
	        ]
                    
        return self
Exemplo n.º 5
0
def Toncv(image,filters,module_stride=1):
    op = FilterActs(module_stride)
    return op(image,filters)
Exemplo n.º 6
0
 def test_op_eq(self):
     assert FilterActs(1) == FilterActs(1)
     assert not (FilterActs(1) != FilterActs(1))
     assert (FilterActs(2) != FilterActs(1))
     assert FilterActs(1) != None
Exemplo n.º 7
0
    def alloc(cls,
            conf,
            image_shape,  # input dimensionality
            filters_hs_shape,       
            filters_irange,
            v_prec,
            v_prec_lower_limit, #should be parameter of the training algo            
            seed = 8923402            
            ):
 	print 'alloc rbm'
        rng = numpy.random.RandomState(seed)

        self = cls()
       
	n_images, n_channels, n_img_rows, n_img_cols = image_shape
        n_filters_hs_modules, n_filters_hs_per_modules, fcolors, n_filters_hs_rows, n_filters_hs_cols = filters_hs_shape        
        assert fcolors == n_channels        
        self.v_shape = image_shape
        print 'v_shape'
	print self.v_shape
	self.filters_hs_shape = filters_hs_shape
        print 'self.filters_hs_shape'
        print self.filters_hs_shape
        self.out_conv_hs_shape = FilterActs.infer_shape_without_instance(self.v_shape,self.filters_hs_shape)        
        print 'self.out_conv_hs_shape'
        print self.out_conv_hs_shape
        #conv_bias_hs_shape = self.out_conv_hs_shape[1:]
        conv_bias_hs_shape = (n_filters_hs_modules, n_filters_hs_per_modules) 
        self.conv_bias_hs_shape = conv_bias_hs_shape
        print 'self.conv_bias_hs_shape'
        print self.conv_bias_hs_shape
        self.v_prec = sharedX(numpy.zeros((n_channels, n_img_rows, n_img_cols))+v_prec, 'var_v_prec')
        self.v_prec_lower_limit = sharedX(v_prec_lower_limit, 'v_prec_lower_limit')
        #a = self.v_prec.broadcastable
        #b = self.v_prec_lower_limit.broadcastable
        #print a,b
        
        self.filters_hs = sharedX(rng.randn(*filters_hs_shape) * filters_irange , 'filters_hs')  
        #a = self.filters_hs.broadcastable
        #print a

        #conv_bias_ival = rng.rand(*conv_bias_hs_shape)*2-1
        #conv_bias_ival *= conf['conv_bias_irange']
        #conv_bias_ival += conf['conv_bias0']
	#self.conv_bias_hs = sharedX(conv_bias_ival, name='conv_bias_hs')
	self.conv_bias_hs = sharedX(numpy.zeros(self.conv_bias_hs_shape), name='conv_bias_hs')
                
        conv_mu_ival = numpy.zeros(conv_bias_hs_shape,dtype=floatX) + conf['conv_mu0']
	self.conv_mu = sharedX(conv_mu_ival, 'conv_mu')
        
	if conf['alpha_logdomain']:
            conv_alpha_ival = numpy.zeros(conv_bias_hs_shape,dtype=floatX) + numpy.log(conf['conv_alpha0'])
	    self.conv_alpha = sharedX(conv_alpha_ival,'conv_alpha')
	else:
            self.conv_alpha = sharedX(
                    numpy.zeros(conv_bias_hs_shape)+conf['conv_alpha0'],
                    'conv_alpha')
 
        if conf['lambda_logdomain']:
            self.conv_lambda = sharedX(
                    numpy.zeros(self.filters_hs_shape)
                        + numpy.log(conf['lambda0']),
                    name='conv_lambda')
        else:
            self.conv_lambda = sharedX(
                    numpy.zeros(self.filters_hs_shape)
                        + (conf['lambda0']),
                    name='conv_lambda')

        negsample_mask = numpy.zeros((n_channels,n_img_rows,n_img_cols),dtype=floatX)
 	negsample_mask[:,n_filters_hs_rows:n_img_rows-n_filters_hs_rows+1,n_filters_hs_cols:n_img_cols-n_filters_hs_cols+1] = 1
	self.negsample_mask = sharedX(negsample_mask,'negsample_mask')                
        
        self.conf = conf
        self._params = [self.v_prec,
                self.filters_hs,
                self.conv_bias_hs,
                self.conv_mu, 
                self.conv_alpha,
                self.conv_lambda
                ]
        return self
Exemplo n.º 8
0
def main_sample(filename, algo='Gibbs', rng=777888, burn_in=50001, save_interval=5000, n_files=10, sampling_for_v=False):
    rbm = cPickle.load(open(filename))
    n_samples = 128
    rbm.v_shape = (n_samples,1,120,120)
    rbm.out_conv_hs_shape = FilterActs.infer_shape_without_instance(rbm.v_shape,rbm.filters_hs_shape)
    rbm.v_prec = sharedX(numpy.zeros(rbm.v_shape[1:])+rbm.v_prec.get_value(borrow=True).mean(), 'var_v_prec')
    if algo == 'Gibbs':
        sampler = Gibbs.alloc(rbm, rng)
        new_particles  = rbm.gibbs_step_for_v(
                        sampler.particles, sampler.s_rng,
                        border_mask=False, sampling_for_v=sampling_for_v,
                        With_fast=False)
        new_particles = tensor.clip(new_particles,
                rbm.conf['particles_min'],
                rbm.conf['particles_max'])
        fn = theano.function([], [],
                updates={sampler.particles: new_particles})
        particles = sampler.particles
    elif algo == 'HMC':
        print "WARNING THIS PROBABLY DOESNT WORK"
        # still need to figure out how to get the clipping into
        # the iterations of mcmc
        sampler = HMC(rbm, rbm.conf['batchsize'], rng)
        ups = sampler.updates()
        ups[sampler.positions] = tensor.clip(ups[sampler.positions],
                rbm.conf['particles_min'],
                rbm.conf['particles_max'])
        fn = theano.function([], [], updates=ups)
        particles = sampler.positions
    
    B_texture = Brodatz('../../../Brodatz/D6.gif', patch_shape=(1,98,98), 
                         noise_concelling=0.0, seed=3322 ,batchdata_size=1, rescale=1.0, rescale_size=2)
    shp = B_texture.test_img.shape
    img = numpy.zeros((1,)+shp)
    temp_img = numpy.asarray(B_texture.test_img, dtype='uint8')
    img[0,] = temp_img
    Image.fromarray(temp_img,'L').save('test_img.png')    
    for i in xrange(burn_in):
	if i% 100 ==0:
	    print i	
        #savename = '%s_Large_sample_burn_%04i.png'%(filename,i)        	
	#tmp = particles.get_value(borrow=True)[0,0,11:363,11:363]
	#w = numpy.asarray(255 * (tmp - tmp.min()) / (tmp.max() - tmp.min() + 1e-6), dtype='uint8')
	#Image.fromarray(w,'L').save(savename)		
	savename = '%s_sample_burn_%04i.png'%(filename,i)
	if i % 1000 == 0 and i!=0:
	    print 'saving'
            Image.fromarray(
                tile_conv_weights(
                    particles.get_value(borrow=True)[:,:,11:110,11:110],
                    flip=False,scale_each=True),
                'L').save(savename)	
            samples = particles.get_value(borrow=True)[:,:,11:110,11:110]
            for samples_index in xrange(n_samples):
                temp_samples = samples[samples_index,]
                #temp_samples = numpy.asarray(255 * (temp_samples - temp_samples.min()) / \
                #                   (temp_samples.max() - temp_samples.min() + 1e-6), dtype='uint8')
                samples[samples_index,]= temp_samples
            CC = CrossCorrelation(img,samples,
                       window_size=19, n_patches_of_samples=1)
	    aaa = CC.TSS()
	    print aaa.mean(),aaa.std()
        fn()   
    
    """
Exemplo n.º 9
0
    def testMainOpSpeed(self):
        def do_time(output, mode=theano.Mode(linker='c')):
            f = theano.function([], output, mode=mode)
            t0 = time.time()
            [f() for i in range(self.n_calls)]
            t1 = time.time()
            return t1 - t0

        for i in range(self.nbTests):
            print "image shape", self.ishape_list[i]
            print "filter shape", self.fshape_list[i]
            # Generate theano functions to run the op in python and in C
            output = self.op(self.s_images_list[i], self.s_filters_list[i])
            output_omp = self.op_omp(self.s_images_list[i],
                                     self.s_filters_list[i])
            output_fcols = FilterActs(
                module_stride=self.module_stride,
                openmp=False,
                fcols=self.fshape_list[i][-1])(self.s_images_list[i],
                                               self.s_filters_list[i])
            output_fcols_omp = FilterActs(
                module_stride=self.module_stride,
                openmp=True,
                fcols=self.fshape_list[i][-1])(self.s_images_list[i],
                                               self.s_filters_list[i])
            output_frows_fcols = FilterActs(
                module_stride=self.module_stride,
                openmp=False,
                fcols=self.fshape_list[i][-1],
                frows=self.fshape_list[i][-2])(self.s_images_list[i],
                                               self.s_filters_list[i])
            output_frows_fcols_omp = FilterActs(
                module_stride=self.module_stride,
                openmp=True,
                fcols=self.fshape_list[i][-1],
                frows=self.fshape_list[i][-2])(self.s_images_list[i],
                                               self.s_filters_list[i])

            # Run the OP in python
            py_t = do_time(output, mode=theano.Mode(linker='py'))
            print "py", py_t

            # Run the OP in C
            c_t = do_time(output, mode=theano.Mode(linker='c|py'))
            print "c|py", c_t, "speed up", py_t / c_t

            # Run the OP in C with fcols
            c_t_fcols = do_time(output_fcols)
            print "c fcols", c_t_fcols, "speed up", py_t / c_t_fcols

            # Run the OP in C with fcols, frows
            c_t_frows_fcols = do_time(output_frows_fcols)
            print "c frows_fcols", c_t_frows_fcols, "speed up", py_t / c_t_frows_fcols

            # Run the Op in C with openmp
            if theano.config.openmp:
                c_omp_t = do_time(output_omp)
                print "omp c", c_omp_t, "speed up python", py_t / c_omp_t, "speed up c", c_t / c_omp_t

                c_omp_fcols_t = do_time(output_fcols_omp)
                print "omp c fcols", c_omp_fcols_t, "speed up python", py_t / c_omp_fcols_t, "speed up c fcols", c_t_fcols / c_omp_fcols_t

                c_omp_frows_fcols_t = do_time(output_frows_fcols_omp)
                print "omp c fcols", c_omp_frows_fcols_t, "speed up python", py_t / c_omp_frows_fcols_t, "speed up c frows_fcols", c_t_frows_fcols / c_omp_frows_fcols_t