コード例 #1
0
ファイル: gabor_fitting.py プロジェクト: afcarl/mca-genmodel
def pgabor_fit(RFs, comm=MPI.COMM_WORLD):
    """
    Parralel gabor-fit the supplied receptive fields.

       *RFs* is expected to be of shape (num_rfs, rf_size, rf_size) and be the same 
       on all involved MPI ranks.
    """
    num_RF, gabor_size, _ = RFs.shape
    gabor_shape = (gabor_size, gabor_size)

    my_errors = np.zeros(num_RF)  # Error per datapoint
    my_params = np.zeros([num_RF, 8])  # 8 gabor params per datapoint

    # Iterate over all RFs with comm.size stride.
    for i in xrange(comm.rank, num_RF, comm.size):
        dlog.progress("Gabor fitting %d of %d" % (i, num_RF), i / num_RF)
        this_RF = RFs[i]

        this_params = gabor_fit(this_RF)
        this_error = np.sum(
            np.abs(this_RF - gabor_kern(this_params, gabor_shape)))

        my_params[i, :] = this_params
        my_errors[i] = this_error

    # Aggregate results
    params = np.empty_like(my_params)
    errors = np.empty_like(my_errors)

    comm.Allreduce([my_params, MPI.DOUBLE], [params, MPI.DOUBLE])
    comm.Allreduce([my_errors, MPI.DOUBLE], [errors, MPI.DOUBLE])
    # Filter results that do not seem useful
    #params, errors = fit_conv(params, errors, gabor_shape[0])

    return params, errors
コード例 #2
0
ファイル: __init__.py プロジェクト: afcarl/mca-genmodel
    def run(self, verbose=False):
        """ Run a complete cooling-cycle 

        When *verbose* is True a progress message is printed for every step
        via dlog.progress(...)
        """
        model = self.model
        anneal = self.anneal
        my_data = self.data
        model_params = self.lparams

        while not anneal.finished:
            # Progress message
            if verbose:
                dlog.progress("EM step %d of %d" % (anneal['step']+1, anneal['max_step']), anneal['position'])

            # Do E and M step
            new_model_params = model.step(anneal, model_params, my_data)
            
            # Calculate the gain so that dynamic annealing schemes can be implemented
            gain = model.gain(model_params, new_model_params)

            anneal.next(gain)
            if anneal.accept:
                model_params = new_model_params

        self.lparams = model_params
コード例 #3
0
def pgabor_fit(RFs, comm=MPI.COMM_WORLD):
    """
    Parralel gabor-fit the supplied receptive fields.

       *RFs* is expected to be of shape (num_rfs, rf_size, rf_size) and be the same 
       on all involved MPI ranks.
    """
    num_RF, gabor_size, _ = RFs.shape
    gabor_shape = (gabor_size, gabor_size)

    my_errors = np.zeros(num_RF)        # Error per datapoint 
    my_params = np.zeros([num_RF, 8])   # 8 gabor params per datapoint
    
    # Iterate over all RFs with comm.size stride. 
    for i in xrange(comm.rank, num_RF, comm.size):
        dlog.progress("Gabor fitting %d of %d" % (i, num_RF), i/num_RF)
        this_RF = RFs[i]

        this_params = gabor_fit(this_RF)
        this_error = np.sum(np.abs(this_RF-gabor_kern(this_params, gabor_shape)))

        my_params[i, :] = this_params
        my_errors[i] = this_error

    # Aggregate results
    params = np.empty_like(my_params)
    errors = np.empty_like(my_errors)

    comm.Allreduce([my_params, MPI.DOUBLE], [params, MPI.DOUBLE])
    comm.Allreduce([my_errors, MPI.DOUBLE], [errors, MPI.DOUBLE])
    # Filter results that do not seem useful
    #params, errors = fit_conv(params, errors, gabor_shape[0])

    return params, errors
コード例 #4
0
ファイル: dog_fitting.py プロジェクト: afcarl/mca-genmodel
def pdog_fit(RFs, comm=MPI.COMM_WORLD):
    """
    Fit DoGs to the supplied receptive fields.
    *RFs* is expected to be of shape (num_rfs, rf_size, rf_size)
    Returns the minimizing parameters, the min square error, and the actual (rotated) sigma values
    """
    num_RF, gabor_size, _ = RFs.shape
    gabor_shape = (gabor_size, gabor_size)

    my_errors = np.zeros(num_RF)  # Error per datapoint
    my_params = np.zeros([num_RF, 8])  # 8 gabor params per datapoint
    my_sigmas = np.zeros([num_RF, 2])

    # Iterate over all RFs with comm.size stride.
    #for i in xrange(comm.rank, comm.size, comm.size):
    for i in xrange(comm.rank, num_RF, comm.size):
        dlog.progress("DoG fitting %d of %d" % (i, num_RF), i / num_RF)
        this_RF = RFs[i]

        these_params, this_error, this_sigma = dog_fit(this_RF)
        this_error = np.sum(
            np.abs(this_RF - dog_kern(these_params, this_RF.shape)))

        my_params[i, :] = these_params
        my_errors[i] = this_error
        my_sigmas[i, :] = this_sigma

    # Aggregate results
    params = np.empty_like(my_params)
    errors = np.empty_like(my_errors)
    sigmas = np.empty_like(my_sigmas)

    comm.Allreduce([my_params, MPI.DOUBLE], [params, MPI.DOUBLE])
    comm.Allreduce([my_errors, MPI.DOUBLE], [errors, MPI.DOUBLE])
    comm.Allreduce([my_sigmas, MPI.DOUBLE], [sigmas, MPI.DOUBLE])

    return [params, errors, sigmas]
コード例 #5
0
def pdog_fit(RFs, comm=MPI.COMM_WORLD):
    """
    Fit DoGs to the supplied receptive fields.
    *RFs* is expected to be of shape (num_rfs, rf_size, rf_size)
    Returns the minimizing parameters, the min square error, and the actual (rotated) sigma values
    """
    num_RF, gabor_size, _ = RFs.shape
    gabor_shape = (gabor_size, gabor_size)

    my_errors = np.zeros(num_RF)  # Error per datapoint
    my_params = np.zeros([num_RF, 8])  # 8 gabor params per datapoint
    my_sigmas = np.zeros([num_RF, 2])

    # Iterate over all RFs with comm.size stride.
    # for i in xrange(comm.rank, comm.size, comm.size):
    for i in xrange(comm.rank, num_RF, comm.size):
        dlog.progress("DoG fitting %d of %d" % (i, num_RF), i / num_RF)
        this_RF = RFs[i]

        these_params, this_error, this_sigma = dog_fit(this_RF)
        this_error = np.sum(np.abs(this_RF - dog_kern(these_params, this_RF.shape)))

        my_params[i, :] = these_params
        my_errors[i] = this_error
        my_sigmas[i, :] = this_sigma

    # Aggregate results
    params = np.empty_like(my_params)
    errors = np.empty_like(my_errors)
    sigmas = np.empty_like(my_sigmas)

    comm.Allreduce([my_params, MPI.DOUBLE], [params, MPI.DOUBLE])
    comm.Allreduce([my_errors, MPI.DOUBLE], [errors, MPI.DOUBLE])
    comm.Allreduce([my_sigmas, MPI.DOUBLE], [sigmas, MPI.DOUBLE])

    return [params, errors, sigmas]
コード例 #6
0
ファイル: preproc-dog.py プロジェクト: afcarl/mca-genmodel
    # Create output file
    tbl_out = AutoTable(out_fname + ".h5")

    # Size magic
    left = (oversize // 2) - (size // 2)
    right = left + size

    #============================================================
    # Start to do some real work
    batch_size = 1000

    dog = DoG(1., 3., 9)
    for n in xrange(0, N_patches):
        if n % batch_size == 0:
            dlog.progress("Preprocessing...", n / N_patches)

        P = in_oversized[n, :, :]
        P_ = convolve2d(P, dog, 'same')
        P_ = P_[left:right, left:right]

        # Normalize and mean-free
        if options.mf:
            P_ -= P_.mean()
        if options.norm:
            P_max = max(P_.max(), -P_.min())
            P_ /= (P_max + 1e-5)
        if options.varnorm:
            P_var = np.var(P_)
            P_ /= (np.sqrt(P_var) + 1e-5)
コード例 #7
0
    # Create output file
    tbl_out = AutoTable(out_fname+".h5")

    # Size magic
    left = (oversize // 2)-(size //2)
    right = left + size
    
    #============================================================
    # Start to do some real work
    batch_size = 1000
    
    dog = DoG(1., 3., 9)
    for n in xrange(0, N_patches):
        if n % batch_size == 0:
            dlog.progress("Preprocessing...", n/N_patches)

        P = in_oversized[n,:,:]
        P_ = convolve2d(P, dog, 'same')
        P_ = P_[left:right, left:right]

        # Normalize and mean-free
        if options.mf:
            P_ -= P_.mean()
        if options.norm:
            P_max = max(P_.max(), -P_.min())
            P_ /= (P_max+1e-5)
        if options.varnorm:
            P_var = np.var(P_)
            P_ /= (np.sqrt(P_var)+1e-5)
    
コード例 #8
0
ファイル: run-em.py プロジェクト: afcarl/mca-genmodel
from pulp.utils.autotable import AutoTable
from pulp.utils import accel

import pulp.utils.tracing as tracing

from pulp.analyze.gabor_fitting import pgabor_fit
from pulp.analyze.dog_fitting import pdog_fit

comm = MPI.COMM_WORLD

#===============================================================================
# Main

paramfile = sys.argv[1]

dlog.progress("%s" % ' '.join(sys.argv))
dlog.progress("Running %d parallel processes" % comm.size)
dlog.progress("Using accelerted functions: %s" % accel.backend)
dlog.progress("Reading paramfile %s" % paramfile)

# Some default parameter values
#TODO: Agree upon default values...
enable_tracing = True
partial_a = 1.
partial_b = 1.
partial_c = 1.

# Read paramfile
execfile(paramfile)

#=================== Create output path and files ==============================
コード例 #9
0
ファイル: preproc-zca.py プロジェクト: afcarl/mca-genmodel
    print "Output file:  %s" % out_fname
    print "# of patches: %d" % N_patches
    print "Patch size :  %d x %d" % (size, size)

    # Create output file
    tbl_out = AutoTable(out_fname + ".h5")

    # Internal parameters
    batch_size = 1000
    epsilon = 1e-3
    D = size**2
    dim = D

    #============================================================
    # Start to do some real work
    dlog.progress("Loading patches...")

    P = in_patches[:N_patches, :, :].reshape(N_patches, -1)
    P_mean = P.mean(axis=0)

    # Create covariance matrix
    cov_mat = np.zeros((D, D))
    for n in xrange(0, N_patches):
        if n % batch_size == 0:
            dlog.progress("Creating covariance matrix (%d)" % n, n / N_patches)

        cov_mat += np.outer(P[n] - P_mean, P[n] - P_mean)
    cov_mat /= N_patches

    # Eigenvalue decomposition
    dlog.progress("Computing principal components...")
コード例 #10
0
    print "Output file:  %s" % out_fname
    print "# of patches: %d" % N_patches
    print "Patch size :  %d x %d" % (size, size)

    # Create output file
    tbl_out = AutoTable(out_fname+".h5")

    # Internal parameters
    batch_size = 1000
    epsilon = 1e-3
    D = size**2
    dim = D
    
    #============================================================
    # Start to do some real work
    dlog.progress("Loading patches...")

    P = in_patches[:N_patches,:,:].reshape(N_patches, -1)
    P_mean = P.mean(axis=0)

    # Create covariance matrix
    cov_mat = np.zeros((D, D))
    for n in xrange(0, N_patches):
        if n % batch_size == 0:
            dlog.progress("Creating covariance matrix (%d)" % n, n/N_patches)

        cov_mat += np.outer( P[n]-P_mean, P[n]-P_mean )
    cov_mat /= N_patches


    # Eigenvalue decomposition
コード例 #11
0
ファイル: run-em.py プロジェクト: jbornschein/mca-genmodel
from pulp.utils import accel

import pulp.utils.tracing as tracing

from pulp.analyze.gabor_fitting import pgabor_fit
from pulp.analyze.dog_fitting import pdog_fit

comm = MPI.COMM_WORLD


#===============================================================================
# Main

paramfile = sys.argv[1]

dlog.progress("%s" % ' '.join(sys.argv) )
dlog.progress("Running %d parallel processes" % comm.size) 
dlog.progress("Using accelerted functions: %s" % accel.backend)
dlog.progress("Reading paramfile %s" % paramfile)
    
# Some default parameter values
#TODO: Agree upon default values... 
enable_tracing = True
partial_a = 1.  
partial_b = 1.
partial_c = 1.

# Read paramfile
execfile(paramfile)

#=================== Create output path and files ==============================
コード例 #12
0
    N_patches = 1000000
    min_var = 0.0001

    out_fname = "patches-%d" % size
    out_tbl = AutoTable(out_fname+".h5")

    images_h5 = tables.openFile(images_fname, "r")
    images = images_h5.root.images

    N_images = images.shape[0]
    #ppi = (N_patches // N_images // 10) + 1
    ppi = 4
    
    for n in xrange(N_patches):
        if n % 1000 == 0:
            dlog.progress("Extracting patch %d" % n, n/N_patches)
        if n % ppi == 0:
            while True:
                img = images[np.random.randint(N_images)]
                img = img / img.max()
                oversized_batch = pri.extract_patches_from_single_image(img, (oversize, oversize), ppi)
                patches_batch = oversized_batch[:, (size//2):(size//2+size), (size//2):(size//2+size)]

                variance = np.var( patches_batch.reshape([ppi, -1] ), axis=1)
                if np.alltrue(variance > min_var):
                    break

        out_tbl.append('oversized', oversized_batch[n%ppi])
        out_tbl.append('patches', patches_batch[n%ppi])

out_tbl.close()
コード例 #13
0
    N_patches = 1000000
    min_var = 0.0001

    out_fname = "patches-%d" % size
    out_tbl = AutoTable(out_fname + ".h5")

    images_h5 = tables.openFile(images_fname, "r")
    images = images_h5.root.images

    N_images = images.shape[0]
    #ppi = (N_patches // N_images // 10) + 1
    ppi = 4

    for n in xrange(N_patches):
        if n % 1000 == 0:
            dlog.progress("Extracting patch %d" % n, n / N_patches)
        if n % ppi == 0:
            while True:
                img = images[np.random.randint(N_images)]
                img = img / img.max()
                oversized_batch = pri.extract_patches_from_single_image(
                    img, (oversize, oversize), ppi)
                patches_batch = oversized_batch[:,
                                                (size // 2):(size // 2 + size),
                                                (size // 2):(size // 2 + size)]

                variance = np.var(patches_batch.reshape([ppi, -1]), axis=1)
                if np.alltrue(variance > min_var):
                    break

        out_tbl.append('oversized', oversized_batch[n % ppi])