def __init__(self, prior_weights=[1.,0,0], name='constant1'): self.labelset = np.asarray(config.labelset) self.model_name = name self.force_recompute_prior = False self.params = { 'beta' : 50, # contrast parameter 'return_arguments' :['image','y'], # optimization parameter 'per_label': True, 'optim_solver':'unconstrained', 'rtol' : 1e-6, 'maxiter' : 2e3, } laplacian_type = 'std_b50' logger.info('laplacian type is: {}'.format(laplacian_type)) self.weight_functions = { 'std_b10' : lambda im: wflib.weight_std(im, beta=10), 'std_b50' : lambda im: wflib.weight_std(im, beta=50), 'std_b100' : lambda im: wflib.weight_std(im, beta=100), 'inv_b100o1' : lambda im: wflib.weight_inv(im, beta=100, offset=1), 'pdiff_r1b10': lambda im: wflib.weight_patch_diff(im, r0=1, beta=10), 'pdiff_r2b10': lambda im: wflib.weight_patch_diff(im, r0=2, beta=10), 'pdiff_r1b50' : lambda im: wflib.weight_patch_diff(im, r0=1, beta=50), } self.weight_function = self.weight_functions[laplacian_type] self.prior_models = [ prior_models.Constant, prior_models.Entropy_no_D, prior_models.Intensity, prior_models.Variance_no_D, prior_models.Variance_no_D_Cmap, ] self.prior_weights = prior_weights logger.info('Model name = {}, using prior weights={}'\ .format(self.model_name, self.prior_weights))
def __init__(self, use_parallel=True, **kwargs ): ## paths self.dir_reg = config.dir_reg self.dir_inf = config.dir_inf self.dir_svm = config.dir_svm self.training_vols = config.vols ## params self.force_recompute_prior = False self.use_parallel = use_parallel self.labelset = np.asarray(config.labelset) C = kwargs.pop('C',1.0) Cprime = kwargs.pop('Cprime',0.0) self.scale_only = kwargs.pop('scale_only', False) self.loss_type = kwargs.pop('loss_type', 'squareddiff') self.loss_factor = kwargs.pop('loss_factor', 1.) self.use_latent = kwargs.pop('use_latent', False) self.approx_aci = kwargs.pop('approx_aci', False) self.debug = kwargs.pop('debug', False) self.retrain = kwargs.pop('retrain', True) self.minimal_svm = kwargs.pop('minimal', False) self.one_iteration = kwargs.pop('one_iteration', False) self.start_script = kwargs.pop('start_script', '') self.use_mosek = kwargs.pop('use_mosek',True) #switch_loss = kwargs.pop('switch_loss', False) crop = kwargs.pop('crop','none') if crop=='none': self.crop = False else: self.crop = True ncrop = int(crop) self.slice_size = ncrop self.slice_step = ncrop ntrain = kwargs.pop('ntrain', 'all') if ntrain.isdigit(): n = int(ntrain) self.select_vol = slice(n,n+1) else: self.select_vol = slice(None) ## parameters for rw learning self.rwparams_svm = { 'labelset':self.labelset, # optimization 'rtol': 1e-6, 'maxiter': 1e3, 'per_label':True, 'optim_solver':'unconstrained', # contrained optim 'use_mosek': self.use_mosek, 'logbarrier_mu': 10, 'logbarrier_initial_t': 10, 'logbarrier_modified': False, 'logbarrier_maxiter': 10, 'newton_maxiter': 50, } ## parameters for svm api self.svm_api_params = { 'loss_type': self.loss_type, #'laplacian','squareddif', 'ideal', 'none' 'loss_factor': self.loss_factor, 'approx_aci': self.approx_aci, } ## parameters for rw inference self.rwparams_inf = { 'labelset':self.labelset, 'return_arguments':['image','y'], # optimization 'rtol': 1e-6, 'maxiter': 1e3, 'per_label':True, 'optim_solver':'unconstrained', } ## svm params self.svmparams = { 'C': C, 'Cprime': Cprime, 'nitermax': 100, 'epsilon': 1e-5, #'do_switch_loss': switch_loss, # latent 'latent_niter_max': 100, 'latent_epsilon': 1e-3, 'latent_use_parallel': self.use_parallel, } self.trainparams = { 'scale_only': self.scale_only, } ## weight functions if self.minimal_svm: self.hand_tuned_w = [1, 1e-2] self.weight_functions = {'std_b50': lambda im: wflib.weight_std(im, beta=50)} self.prior_models = {'constant': models.Constant} else: self.hand_tuned_w = [1.0, 0.0, 0.0, 0.0, 1e-2, 0.0, 0.0] self.weight_functions = { 'std_b10' : lambda im,i,j: wflib.weight_std(im,i,j, beta=10), 'std_b50' : lambda im,i,j: wflib.weight_std(im,i,j, beta=50), 'std_b100' : lambda im,i,j: wflib.weight_std(im,i,j, beta=100), 'inv_b100o1' : lambda im,i,j: wflib.weight_inv(im,i,j, beta=100, offset=1), } self.prior_models = { 'constant': models.Constant, 'entropy': models.Entropy_no_D, 'intensity': models.Intensity, } ## indices of w nlaplacian = len(self.weight_functions) nprior = len(self.prior_models) self.indices_laplacians = np.arange(nlaplacian) self.indices_priors = np.arange(nlaplacian,nlaplacian + nprior) ## compute the scale of psi #self.psi_scale = [1e4] * nlaplacian + [1e5] * nprior self.psi_scale = [1.0] * nlaplacian + [1.0] * nprior self.svmparams['psi_scale'] = self.psi_scale ## make arrays of function self.laplacian_functions = self.weight_functions.values() self.laplacian_names = self.weight_functions.keys() self.prior_functions = self.prior_models.values() self.prior_names = self.prior_models.keys() ## parallel ? if self.use_parallel: self.comm = mpi.COMM self.MPI_rank = mpi.RANK self.MPI_size = mpi.SIZE self.isroot = self.MPI_rank==0 if self.MPI_size==1: logger.warning('Found only one process. Not using parallel') self.use_parallel = False self.svmparams['use_parallel'] = self.use_parallel self.svmparams['latent_use_parallel'] = self.use_parallel else: self.isroot = True if self.isroot: logger.info('passed these command line arguments: {}'.format(str(sys.argv))) logger.info('using parallel?: {}'.format(use_parallel)) logger.info('using latent?: {}'.format(self.use_latent)) logger.info('train data: {}'.format(ntrain)) strkeys = ', '.join(self.laplacian_names) logger.info('laplacian functions (in order): {}'.format(strkeys)) strkeys = ', '.join(self.prior_names) logger.info('prior models (in order): {}'.format(strkeys)) logger.info('using loss type: {}'.format(self.loss_type)) logger.info('SVM parameters: {}'.format(self.svmparams)) logger.info('Computing one iteration at a time ?: {}'.format(self.one_iteration)) if self.debug: logger.info('debug mode, no saving') else: logger.info('writing svm output to: {}'.format(self.dir_svm))
def compute_features(test, train, y): im = io_analyze.load(config.dir_reg + test + train + 'reggray.hdr') nim = im/np.std(im) prior,mask = load_or_compute_prior_and_mask( test, force_recompute=False) seeds = (-1)*mask.astype(int) from rwsegment import rwsegment_prior_models as models from rwsegment import weight_functions as wflib rwparams = { 'labelset': np.asarray(config.labelset), } weight_functions = { 'std_b10' : lambda im: wflib.weight_std(im, beta=10), 'std_b50' : lambda im: wflib.weight_std(im, beta=50), 'std_b100' : lambda im: wflib.weight_std(im, beta=100), 'inv_b100o1' : lambda im: wflib.weight_inv(im, beta=100, offset=1), # 'pdiff_r1b10': lambda im: wflib.weight_patch_diff(im, r0=1, beta=10), # 'pdiff_r2b10': lambda im: wflib.weight_patch_diff(im, r0=2, beta=10), # 'pdiff_r1b50' : lambda im: wflib.weight_patch_diff(im, r0=1, beta=50), } prior_models = { 'constant': models.Constant, 'entropy': models.Entropy_no_D, 'intensity': models.Intensity, } ## indices of w nlaplacian = len(weight_functions) nprior = len(prior_models) indices_laplacians = np.arange(nlaplacian) indices_priors = np.arange(nlaplacian,nlaplacian + nprior) laplacian_functions = weight_functions.values() laplacian_names = weight_functions.keys() prior_functions = prior_models.values() prior_names = prior_models.keys() #from svm_rw_api import MetaAnchor #anchor_api = MetaAnchor( # prior, # prior_functions, # weights_priors, # image=im, # ) from rwsegment import rwsegment for fname in weight_functions: wf = weight_functions[fname] en_rw = rwsegment.energy_rw( nim, y, seeds=seeds, weight_function=wf, **rwparams ) print fname, en_rw for mname in prior_models: pm = prior_models[mname](prior,1., image=nim) en_anchor = rwsegment.energy_anchor( nim, y, pm, seeds=seeds, **rwparams ) print mname, en_anchor
def compute_objective(test, y, w): im = io_analyze.load(config.dir_reg + test + 'gray.hdr') nim = im/np.std(im) prior,mask = load_or_compute_prior_and_mask( test, force_recompute=False) seeds = (-1)*mask.astype(int) from rwsegment import rwsegment_prior_models as models from rwsegment import weight_functions as wflib rwparams = { 'labelset': np.asarray(config.labelset), # optimization 'rtol': 1e-6, 'maxiter': 1e3, 'per_label':True, 'optim_solver':'unconstrained', } weight_functions = { 'std_b10' : lambda im: wflib.weight_std(im, beta=10), 'std_b50' : lambda im: wflib.weight_std(im, beta=50), 'std_b100' : lambda im: wflib.weight_std(im, beta=100), 'inv_b100o1' : lambda im: wflib.weight_inv(im, beta=100, offset=1), # 'pdiff_r1b10': lambda im: wflib.weight_patch_diff(im, r0=1, beta=10), # 'pdiff_r2b10': lambda im: wflib.weight_patch_diff(im, r0=2, beta=10), # 'pdiff_r1b50' : lambda im: wflib.weight_patch_diff(im, r0=1, beta=50), } prior_models = { 'constant': models.Constant, 'entropy': models.Entropy_no_D, 'intensity': models.Intensity, } ## indices of w nlaplacian = len(weight_functions) nprior = len(prior_models) indices_laplacians = np.arange(nlaplacian) indices_priors = np.arange(nlaplacian,nlaplacian + nprior) laplacian_functions = weight_functions.values() laplacian_names = weight_functions.keys() prior_functions = prior_models.values() prior_names = prior_models.keys() weights_laplacians = np.asarray(w)[indices_laplacians] weights_priors = np.asarray(w)[indices_priors] def meta_weight_functions(im,_w): ''' meta weight function''' data = 0 for iwf,wf in enumerate(laplacian_functions): ij,_data = wf(im) data += _w[iwf]*_data return ij, data weight_function = lambda im: meta_weight_functions(im, weights_laplacians) from svm_rw_api import MetaAnchor anchor_api = MetaAnchor( prior, prior_functions, weights_priors, image=nim, ) from rwsegment import rwsegment en_rw = rwsegment.energy_rw( nim, y, seeds=seeds, weight_function=weight_function, **rwparams ) en_anchor = rwsegment.energy_anchor( nim, y, anchor_api, seeds=seeds, **rwparams ) obj = en_rw + en_anchor return obj
def compute_features(test, train, y): im = io_analyze.load(config.dir_reg + test + train + 'reggray.hdr') nim = im / np.std(im) prior, mask = load_or_compute_prior_and_mask(test, force_recompute=False) seeds = (-1) * mask.astype(int) from rwsegment import rwsegment_prior_models as models from rwsegment import weight_functions as wflib rwparams = { 'labelset': np.asarray(config.labelset), } weight_functions = { 'std_b10': lambda im: wflib.weight_std(im, beta=10), 'std_b50': lambda im: wflib.weight_std(im, beta=50), 'std_b100': lambda im: wflib.weight_std(im, beta=100), 'inv_b100o1': lambda im: wflib.weight_inv(im, beta=100, offset=1), # 'pdiff_r1b10': lambda im: wflib.weight_patch_diff(im, r0=1, beta=10), # 'pdiff_r2b10': lambda im: wflib.weight_patch_diff(im, r0=2, beta=10), # 'pdiff_r1b50' : lambda im: wflib.weight_patch_diff(im, r0=1, beta=50), } prior_models = { 'constant': models.Constant, 'entropy': models.Entropy_no_D, 'intensity': models.Intensity, } ## indices of w nlaplacian = len(weight_functions) nprior = len(prior_models) indices_laplacians = np.arange(nlaplacian) indices_priors = np.arange(nlaplacian, nlaplacian + nprior) laplacian_functions = weight_functions.values() laplacian_names = weight_functions.keys() prior_functions = prior_models.values() prior_names = prior_models.keys() #from svm_rw_api import MetaAnchor #anchor_api = MetaAnchor( # prior, # prior_functions, # weights_priors, # image=im, # ) from rwsegment import rwsegment for fname in weight_functions: wf = weight_functions[fname] en_rw = rwsegment.energy_rw(nim, y, seeds=seeds, weight_function=wf, **rwparams) print fname, en_rw for mname in prior_models: pm = prior_models[mname](prior, 1., image=nim) en_anchor = rwsegment.energy_anchor(nim, y, pm, seeds=seeds, **rwparams) print mname, en_anchor
def compute_objective(test, y, w): im = io_analyze.load(config.dir_reg + test + 'gray.hdr') nim = im / np.std(im) prior, mask = load_or_compute_prior_and_mask(test, force_recompute=False) seeds = (-1) * mask.astype(int) from rwsegment import rwsegment_prior_models as models from rwsegment import weight_functions as wflib rwparams = { 'labelset': np.asarray(config.labelset), # optimization 'rtol': 1e-6, 'maxiter': 1e3, 'per_label': True, 'optim_solver': 'unconstrained', } weight_functions = { 'std_b10': lambda im: wflib.weight_std(im, beta=10), 'std_b50': lambda im: wflib.weight_std(im, beta=50), 'std_b100': lambda im: wflib.weight_std(im, beta=100), 'inv_b100o1': lambda im: wflib.weight_inv(im, beta=100, offset=1), # 'pdiff_r1b10': lambda im: wflib.weight_patch_diff(im, r0=1, beta=10), # 'pdiff_r2b10': lambda im: wflib.weight_patch_diff(im, r0=2, beta=10), # 'pdiff_r1b50' : lambda im: wflib.weight_patch_diff(im, r0=1, beta=50), } prior_models = { 'constant': models.Constant, 'entropy': models.Entropy_no_D, 'intensity': models.Intensity, } ## indices of w nlaplacian = len(weight_functions) nprior = len(prior_models) indices_laplacians = np.arange(nlaplacian) indices_priors = np.arange(nlaplacian, nlaplacian + nprior) laplacian_functions = weight_functions.values() laplacian_names = weight_functions.keys() prior_functions = prior_models.values() prior_names = prior_models.keys() weights_laplacians = np.asarray(w)[indices_laplacians] weights_priors = np.asarray(w)[indices_priors] def meta_weight_functions(im, _w): ''' meta weight function''' data = 0 for iwf, wf in enumerate(laplacian_functions): ij, _data = wf(im) data += _w[iwf] * _data return ij, data weight_function = lambda im: meta_weight_functions(im, weights_laplacians) from svm_rw_api import MetaAnchor anchor_api = MetaAnchor( prior, prior_functions, weights_priors, image=nim, ) from rwsegment import rwsegment en_rw = rwsegment.energy_rw(nim, y, seeds=seeds, weight_function=weight_function, **rwparams) en_anchor = rwsegment.energy_anchor(nim, y, anchor_api, seeds=seeds, **rwparams) obj = en_rw + en_anchor return obj
def __init__(self, use_parallel=True, **kwargs): ## paths self.dir_reg = config.dir_reg self.dir_inf = config.dir_inf self.dir_svm = config.dir_svm self.training_vols = config.vols ## params self.force_recompute_prior = False self.use_parallel = use_parallel self.labelset = np.asarray(config.labelset) C = kwargs.pop('C', 1.0) Cprime = kwargs.pop('Cprime', 0.0) self.scale_only = kwargs.pop('scale_only', False) self.loss_type = kwargs.pop('loss_type', 'squareddiff') self.loss_factor = kwargs.pop('loss_factor', 1.) self.use_latent = kwargs.pop('use_latent', False) self.approx_aci = kwargs.pop('approx_aci', False) self.debug = kwargs.pop('debug', False) self.retrain = kwargs.pop('retrain', True) self.minimal_svm = kwargs.pop('minimal', False) self.one_iteration = kwargs.pop('one_iteration', False) self.start_script = kwargs.pop('start_script', '') self.use_mosek = kwargs.pop('use_mosek', True) #switch_loss = kwargs.pop('switch_loss', False) crop = kwargs.pop('crop', 'none') if crop == 'none': self.crop = False else: self.crop = True ncrop = int(crop) self.slice_size = ncrop self.slice_step = ncrop ntrain = kwargs.pop('ntrain', 'all') if ntrain.isdigit(): n = int(ntrain) self.select_vol = slice(n, n + 1) else: self.select_vol = slice(None) ## parameters for rw learning self.rwparams_svm = { 'labelset': self.labelset, # optimization 'rtol': 1e-6, 'maxiter': 1e3, 'per_label': True, 'optim_solver': 'unconstrained', # contrained optim 'use_mosek': self.use_mosek, 'logbarrier_mu': 10, 'logbarrier_initial_t': 10, 'logbarrier_modified': False, 'logbarrier_maxiter': 10, 'newton_maxiter': 50, } ## parameters for svm api self.svm_api_params = { 'loss_type': self.loss_type, #'laplacian','squareddif', 'ideal', 'none' 'loss_factor': self.loss_factor, 'approx_aci': self.approx_aci, } ## parameters for rw inference self.rwparams_inf = { 'labelset': self.labelset, 'return_arguments': ['image', 'y'], # optimization 'rtol': 1e-6, 'maxiter': 1e3, 'per_label': True, 'optim_solver': 'unconstrained', } ## svm params self.svmparams = { 'C': C, 'Cprime': Cprime, 'nitermax': 100, 'epsilon': 1e-5, #'do_switch_loss': switch_loss, # latent 'latent_niter_max': 100, 'latent_epsilon': 1e-3, 'latent_use_parallel': self.use_parallel, } self.trainparams = { 'scale_only': self.scale_only, } ## weight functions if self.minimal_svm: self.hand_tuned_w = [1, 1e-2] self.weight_functions = { 'std_b50': lambda im: wflib.weight_std(im, beta=50) } self.prior_models = {'constant': models.Constant} else: self.hand_tuned_w = [1.0, 0.0, 0.0, 0.0, 1e-2, 0.0, 0.0] self.weight_functions = { 'std_b10': lambda im, i, j: wflib.weight_std(im, i, j, beta=10), 'std_b50': lambda im, i, j: wflib.weight_std(im, i, j, beta=50), 'std_b100': lambda im, i, j: wflib.weight_std(im, i, j, beta=100), 'inv_b100o1': lambda im, i, j: wflib.weight_inv(im, i, j, beta=100, offset=1 ), } self.prior_models = { 'constant': models.Constant, 'entropy': models.Entropy_no_D, 'intensity': models.Intensity, } ## indices of w nlaplacian = len(self.weight_functions) nprior = len(self.prior_models) self.indices_laplacians = np.arange(nlaplacian) self.indices_priors = np.arange(nlaplacian, nlaplacian + nprior) ## compute the scale of psi #self.psi_scale = [1e4] * nlaplacian + [1e5] * nprior self.psi_scale = [1.0] * nlaplacian + [1.0] * nprior self.svmparams['psi_scale'] = self.psi_scale ## make arrays of function self.laplacian_functions = self.weight_functions.values() self.laplacian_names = self.weight_functions.keys() self.prior_functions = self.prior_models.values() self.prior_names = self.prior_models.keys() ## parallel ? if self.use_parallel: self.comm = mpi.COMM self.MPI_rank = mpi.RANK self.MPI_size = mpi.SIZE self.isroot = self.MPI_rank == 0 if self.MPI_size == 1: logger.warning('Found only one process. Not using parallel') self.use_parallel = False self.svmparams['use_parallel'] = self.use_parallel self.svmparams['latent_use_parallel'] = self.use_parallel else: self.isroot = True if self.isroot: logger.info('passed these command line arguments: {}'.format( str(sys.argv))) logger.info('using parallel?: {}'.format(use_parallel)) logger.info('using latent?: {}'.format(self.use_latent)) logger.info('train data: {}'.format(ntrain)) strkeys = ', '.join(self.laplacian_names) logger.info('laplacian functions (in order): {}'.format(strkeys)) strkeys = ', '.join(self.prior_names) logger.info('prior models (in order): {}'.format(strkeys)) logger.info('using loss type: {}'.format(self.loss_type)) logger.info('SVM parameters: {}'.format(self.svmparams)) logger.info('Computing one iteration at a time ?: {}'.format( self.one_iteration)) if self.debug: logger.info('debug mode, no saving') else: logger.info('writing svm output to: {}'.format(self.dir_svm))