Commit ee935371 authored by Jiri Borovec's avatar Jiri Borovec

added NMF

parent e8ffa515
......@@ -124,7 +124,8 @@ def create_args_parser(dict_params):
parser.add_argument('--nb_jobs', type=int, required=False, default=NB_THREADS,
help='number of processes running in parallel')
parser.add_argument('--method', type=str, required=False, nargs='+', default=None,
help='possible APD methods', choices=['PCA', 'ICA', 'DL', 'APDL'])
help='possible APD methods',
choices=['PCA', 'ICA', 'DL', 'NMF', 'APDL'])
return parser
......@@ -319,7 +320,7 @@ class ExperimentAPD(tl_expt.Experiment):
logging.debug('statistic: \n%s', repr(self.df_stat.describe()))
class ExperimentAPD_mp(ExperimentAPD):
class ExperimentAPD_parallel(ExperimentAPD):
"""
run the experiment in multiple threads
"""
......@@ -330,7 +331,7 @@ class ExperimentAPD_mp(ExperimentAPD):
:param dict_params: {str: ...}
:param nb_jobs: int
"""
super(ExperimentAPD_mp, self).__init__(dict_params)
super(ExperimentAPD_parallel, self).__init__(dict_params)
self.nb_jobs = nb_jobs
def _load_images(self):
......@@ -365,18 +366,19 @@ class ExperimentAPD_mp(ExperimentAPD):
def _perform_sequence(self):
""" perform sequence in multiprocessing pool """
logging.debug('perform_sequence in %i threads', self.nb_jobs)
logging.debug('perform_sequence in %i threads for %i values',
self.nb_jobs, len(self.iter_values))
# ISSUE with passing large date to processes so the images are saved
# and loaded in particular process again
# p_imgs = os.path.join(self.params.get('path_exp'), 'input_images.npz')
# np.savez(open(p_imgs, 'w'), imgs=self.imgs)
self.l_stat = []
tqdm_bar = tqdm.tqdm(total=len(self.iter_values))
# tqdm_bar = tqdm.tqdm(total=len(self.iter_values))
mproc_pool = mproc.Pool(self.nb_jobs)
for stat in mproc_pool.map(self._warp_perform_once, self.iter_values):
self.l_stat.append(stat)
tqdm_bar.update(1)
# tqdm_bar.update(1)
mproc_pool.close()
mproc_pool.join()
......
......@@ -21,7 +21,7 @@ import logging
import matplotlib
matplotlib.use('Agg')
import numpy as np
from sklearn.decomposition import SparsePCA, FastICA, DictionaryLearning
from sklearn.decomposition import SparsePCA, FastICA, DictionaryLearning, NMF
from skimage import segmentation
import tqdm
......@@ -184,15 +184,32 @@ class ExperimentDictLearn_base(ExperimentLinearCombineBase):
self.components = self.estimator.components_
class ExperimentFastICA(ExperimentFastICA_base, expt_apd.ExperimentAPD_mp):
class ExperimentNMF_base(ExperimentLinearCombineBase):
"""
http://scikit-learn.org/stable/modules/generated/sklearn.decomposition.DictionaryLearning.html
"""
def _estimate_linear_combination(self, imgs_vec):
self.estimator = NMF(n_components=self.params.get('nb_labels'),
max_iter=self.params.get('max_iter'),
init='random')
self.fit_result = self.estimator.fit_transform(imgs_vec)
self.components = self.estimator.components_
class ExperimentFastICA(ExperimentFastICA_base, expt_apd.ExperimentAPD_parallel):
pass
class ExperimentSparsePCA(ExperimentSparsePCA_base, expt_apd.ExperimentAPD_parallel):
pass
class ExperimentSparsePCA(ExperimentSparsePCA_base, expt_apd.ExperimentAPD_mp):
class ExperimentDictLearn(ExperimentDictLearn_base, expt_apd.ExperimentAPD_parallel):
pass
class ExperimentDictLearn(ExperimentDictLearn_base, expt_apd.ExperimentAPD_mp):
class ExperimentNMF(ExperimentNMF_base, expt_apd.ExperimentAPD_parallel):
pass
......@@ -201,6 +218,7 @@ METHODS = {
'PCA': ExperimentFastICA,
'ICA': ExperimentSparsePCA,
'DL': ExperimentDictLearn,
'NMF': ExperimentNMF,
'APDL': expt_apdl.ExperimentAPDL,
}
......@@ -209,6 +227,7 @@ METHODS_BASE = {
'PCA': ExperimentFastICA_base,
'ICA': ExperimentSparsePCA_base,
'DL': ExperimentDictLearn_base,
'NMF': ExperimentNMF_base,
'APDL': expt_apdl.ExperimentAPDL_base,
}
......
......@@ -189,7 +189,7 @@ class ExperimentAPDL_base(expt_apd.ExperimentAPD):
return stat
class ExperimentAPDL(ExperimentAPDL_base, expt_apd.ExperimentAPD_mp):
class ExperimentAPDL(ExperimentAPDL_base, expt_apd.ExperimentAPD_parallel):
"""
parallel version of APDL
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment