diff --git a/Code/MonoMutliViewClassifiers/ExecClassif.py b/Code/MonoMutliViewClassifiers/ExecClassif.py index 5aaf18c5bf1f444c0c3ebeb9501949ddebe9556e..d79229431aee4ce2feeeb356c5959165d7b9b37b 100644 --- a/Code/MonoMutliViewClassifiers/ExecClassif.py +++ b/Code/MonoMutliViewClassifiers/ExecClassif.py @@ -499,6 +499,8 @@ if metrics == [[""]]: metricsNames = [name for _, name, isPackage in pkgutil.iter_modules(['Metrics']) if not isPackage and name!="log_loss"] metrics = [[metricName] for metricName in metricsNames] + metrics[0]=metrics[1] + metrics[1]=["accuracy_score"] for metricIndex, metric in enumerate(metrics): if len(metric)==1: metrics[metricIndex]=[metric[0], None] diff --git a/Code/MonoMutliViewClassifiers/MonoviewClassifiers/Adaboost.py b/Code/MonoMutliViewClassifiers/MonoviewClassifiers/Adaboost.py index 02c0560086b4313ed138783e3174f492e9226c6b..86bf631b9f24f511bee5860b4d84dcdf6b3dfec1 100644 --- a/Code/MonoMutliViewClassifiers/MonoviewClassifiers/Adaboost.py +++ b/Code/MonoMutliViewClassifiers/MonoviewClassifiers/Adaboost.py @@ -22,7 +22,6 @@ def fit(DATASET, CLASS_LABELS, NB_CORES=1,**kwargs): def gridSearch(X_train, y_train, nbFolds=4, metric=["accuracy_score", None], nIter=30, nbCores=1): - pipeline = Pipeline([('classifier', AdaBoostClassifier())]) param= {"classifier__n_estimators": randint(1, 15), diff --git a/Code/MonoMutliViewClassifiers/Multiview/ExecMultiview.py b/Code/MonoMutliViewClassifiers/Multiview/ExecMultiview.py index 4e482a3399e57a2613e5120e9b1806026c608e98..25e9a8f9b88a6585a2629b458ee640f7589522d3 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/ExecMultiview.py +++ b/Code/MonoMutliViewClassifiers/Multiview/ExecMultiview.py @@ -62,8 +62,8 @@ def ExecMultiview(directory, DATASET, name, learningRate, nbFolds, nbCores, data classifiersIterations = [] classifierPackage = globals()[CL_type] # Permet d'appeler un module avec une string classifierModule = getattr(classifierPackage, CL_type) - classifierClass = getattr(classifierModule, CL_type) - classifierGridSearch = getattr(classifierModule, "gridSearch_hdf5") + # classifierClass = getattr(classifierModule, CL_type) + # classifierGridSearch = getattr(classifierModule, "gridSearch_hdf5") analysisModule = getattr(classifierPackage, "analyzeResults") diff --git a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/EarlyFusionPackage/WeightedLinear.py b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/EarlyFusionPackage/WeightedLinear.py index 97c0ac4ac70692a7c929e98e2ef97736e37514bf..d0c1fbf2d8718a8f61f303058ec3ea701d48df15 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/EarlyFusionPackage/WeightedLinear.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/EarlyFusionPackage/WeightedLinear.py @@ -14,25 +14,25 @@ def genParamsSets(classificationKWARGS, nIter=1): return paramsSets -def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): - if type(viewsIndices)==type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - bestScore = 0.0 - bestConfig = None - if classificationKWARGS["fusionMethodConfig"][0] is not None: - for i in range(nIter): - randomWeightsArray = np.random.random_sample(nbView) - normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) - classificationKWARGS["fusionMethodConfig"][0] = normalizedArray - classifier = WeightedLinear(1, **classificationKWARGS) - classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) - if accuracy > bestScore: - bestScore = accuracy - bestConfig = normalizedArray - return [bestConfig] +# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): +# if type(viewsIndices)==type(None): +# viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) +# nbView = len(viewsIndices) +# bestScore = 0.0 +# bestConfig = None +# if classificationKWARGS["fusionMethodConfig"][0] is not None: +# for i in range(nIter): +# randomWeightsArray = np.random.random_sample(nbView) +# normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) +# classificationKWARGS["fusionMethodConfig"][0] = normalizedArray +# classifier = WeightedLinear(1, **classificationKWARGS) +# classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) +# if accuracy > bestScore: +# bestScore = accuracy +# bestConfig = normalizedArray +# return [bestConfig] class WeightedLinear(EarlyFusionClassifier): diff --git a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/BayesianInference.py b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/BayesianInference.py index de6b0193eeb06171f543e608d5e82e8b4b7bb681..88628f66e93bf891f543d7ffad51359a3131516e 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/BayesianInference.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/BayesianInference.py @@ -13,26 +13,26 @@ def genParamsSets(classificationKWARGS, nIter=1): normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) paramsSets.append([normalizedArray]) return paramsSets - -def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): - if type(viewsIndices)==type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - bestScore = 0.0 - bestConfig = None - if classificationKWARGS["fusionMethodConfig"][0] is not None: - for i in range(nIter): - randomWeightsArray = np.random.random_sample(nbView) - normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) - classificationKWARGS["fusionMethodConfig"][0] = normalizedArray - classifier = BayesianInference(1, **classificationKWARGS) - classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) - if accuracy > bestScore: - bestScore = accuracy - bestConfig = normalizedArray - return [bestConfig] +# +# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): +# if type(viewsIndices)==type(None): +# viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) +# nbView = len(viewsIndices) +# bestScore = 0.0 +# bestConfig = None +# if classificationKWARGS["fusionMethodConfig"][0] is not None: +# for i in range(nIter): +# randomWeightsArray = np.random.random_sample(nbView) +# normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) +# classificationKWARGS["fusionMethodConfig"][0] = normalizedArray +# classifier = BayesianInference(1, **classificationKWARGS) +# classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) +# if accuracy > bestScore: +# bestScore = accuracy +# bestConfig = normalizedArray +# return [bestConfig] class BayesianInference(LateFusionClassifier): diff --git a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/MajorityVoting.py b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/MajorityVoting.py index 2ec709517d1011846d38fa2a3194e987b1fd506d..9ea51595c60483f8ace89bc959c9e49debc761e6 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/MajorityVoting.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/MajorityVoting.py @@ -14,25 +14,25 @@ def genParamsSets(classificationKWARGS, nIter=1): paramsSets.append([normalizedArray]) return paramsSets -def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): - if type(viewsIndices)==type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - bestScore = 0.0 - bestConfig = None - if classificationKWARGS["fusionMethodConfig"][0] is not None: - for i in range(nIter): - randomWeightsArray = np.random.random_sample(nbView) - normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) - classificationKWARGS["fusionMethodConfig"][0] = normalizedArray - classifier = MajorityVoting(1, **classificationKWARGS) - classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) - if accuracy > bestScore: - bestScore = accuracy - bestConfig = normalizedArray - return [bestConfig] +# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): +# if type(viewsIndices)==type(None): +# viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) +# nbView = len(viewsIndices) +# bestScore = 0.0 +# bestConfig = None +# if classificationKWARGS["fusionMethodConfig"][0] is not None: +# for i in range(nIter): +# randomWeightsArray = np.random.random_sample(nbView) +# normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) +# classificationKWARGS["fusionMethodConfig"][0] = normalizedArray +# classifier = MajorityVoting(1, **classificationKWARGS) +# classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) +# if accuracy > bestScore: +# bestScore = accuracy +# bestConfig = normalizedArray +# return [bestConfig] class MajorityVoting(LateFusionClassifier): diff --git a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SCMForLinear.py b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SCMForLinear.py index 73671d32459bd3734638c3cf214c9d5f821410e8..6af655646d903c356d6ad77f0a406329795d2c73 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SCMForLinear.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SCMForLinear.py @@ -26,28 +26,28 @@ def genParamsSets(classificationKWARGS, nIter=1): return paramsSets -def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): - if type(viewsIndices)==type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - bestScore = 0.0 - bestConfig = None - for i in range(nIter): - max_attributes = random.randint(1, 20) - p = random.random() - model = random.choice(["conjunction", "disjunction"]) - order = random.randint(1,nbView) - randomWeightsArray = np.random.random_sample(nbView) - normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) - classificationKWARGS["fusionMethodConfig"][0] = [p, max_attributes, model, order] - classifier = SCMForLinear(1, **classificationKWARGS) - classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) - if accuracy > bestScore: - bestScore = accuracy - bestConfig = [p, max_attributes, model, order] - return [bestConfig] +# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): +# if type(viewsIndices)==type(None): +# viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) +# nbView = len(viewsIndices) +# bestScore = 0.0 +# bestConfig = None +# for i in range(nIter): +# max_attributes = random.randint(1, 20) +# p = random.random() +# model = random.choice(["conjunction", "disjunction"]) +# order = random.randint(1,nbView) +# randomWeightsArray = np.random.random_sample(nbView) +# normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) +# classificationKWARGS["fusionMethodConfig"][0] = [p, max_attributes, model, order] +# classifier = SCMForLinear(1, **classificationKWARGS) +# classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) +# if accuracy > bestScore: +# bestScore = accuracy +# bestConfig = [p, max_attributes, model, order] +# return [bestConfig] class SCMForLinear(LateFusionClassifier): diff --git a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SVMForLinear.py b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SVMForLinear.py index 066cf3d1470a2c94e16388356417edf7e367d770..894dc7fe7f359a7e1ba1378d8c00e6c2dbe57f9b 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SVMForLinear.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/SVMForLinear.py @@ -13,8 +13,8 @@ def genParamsSets(classificationKWARGS, nIter=1): paramsSets.append([]) return paramsSets -def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): - return None +# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): +# return None class SVMForLinear(LateFusionClassifier): diff --git a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/WeightedLinear.py b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/WeightedLinear.py index c4a1c60bcfb3a5d512d4e8dce6be6257c303d50b..caf01b88326dfaed6b3540cd2a785866b6bdbcc9 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/WeightedLinear.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Fusion/Methods/LateFusionPackage/WeightedLinear.py @@ -15,25 +15,25 @@ def genParamsSets(classificationKWARGS, nIter=1): return paramsSets -def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): - if type(viewsIndices)==type(None): - viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) - nbView = len(viewsIndices) - bestScore = 0.0 - bestConfig = None - if classificationKWARGS["fusionMethodConfig"][0] is not None: - for i in range(nIter): - randomWeightsArray = np.random.random_sample(nbView) - normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) - classificationKWARGS["fusionMethodConfig"][0] = normalizedArray - classifier = WeightedLinear(1, **classificationKWARGS) - classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) - accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) - if accuracy > bestScore: - bestScore = accuracy - bestConfig = normalizedArray - return [bestConfig] +# def gridSearch(DATASET, classificationKWARGS, trainIndices, nIter=30, viewsIndices=None): +# if type(viewsIndices)==type(None): +# viewsIndices = np.arange(DATASET.get("Metadata").attrs["nbView"]) +# nbView = len(viewsIndices) +# bestScore = 0.0 +# bestConfig = None +# if classificationKWARGS["fusionMethodConfig"][0] is not None: +# for i in range(nIter): +# randomWeightsArray = np.random.random_sample(nbView) +# normalizedArray = randomWeightsArray/np.sum(randomWeightsArray) +# classificationKWARGS["fusionMethodConfig"][0] = normalizedArray +# classifier = WeightedLinear(1, **classificationKWARGS) +# classifier.fit_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# predictedLabels = classifier.predict_hdf5(DATASET, trainIndices, viewsIndices=viewsIndices) +# accuracy = accuracy_score(DATASET.get("Labels")[trainIndices], predictedLabels) +# if accuracy > bestScore: +# bestScore = accuracy +# bestConfig = normalizedArray +# return [bestConfig] class WeightedLinear(LateFusionClassifier): diff --git a/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Classifiers/DecisionTree.py b/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Classifiers/DecisionTree.py index fdf6519bf0799e8581274b4bce6761f1ab025be2..0cc9ceaa5543e9f6ca7f4ea07b8668ec4c031791 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Classifiers/DecisionTree.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Classifiers/DecisionTree.py @@ -4,7 +4,9 @@ import numpy as np from ModifiedMulticlass import OneVsRestClassifier from SubSampling import subSample import logging -# Add weights +# Add weights + +import Metrics def DecisionTree(data, labels, arg, weights): depth = int(arg[0]) @@ -37,6 +39,17 @@ def gridSearch(data, labels, metric="accuracy_score"): bestResults = [] classifier = tree.DecisionTreeClassifier(max_depth=1) preliminary_accuracies = np.zeros(50) + # metricModule = getattr(Metrics, metric[0]) + # if metric[1]!=None: + # metricKWARGS = dict((index, metricConfig) for index, metricConfig in enumerate(metric[1])) + # else: + # metricKWARGS = {} + # if metricModule.getConfig()[-14]=="h": + # baseScore = -1000.0 + # isBetter = "higher" + # else: + # baseScore = 1000.0 + # isBetter = "lower" for i in range(50): subSampledData, subSampledLabels, subSampledWeights = subSample(data, labels, 0.05) classifier.fit(subSampledData, subSampledLabels) @@ -112,7 +125,7 @@ def getBestSetting(bestSettings, bestResults): diffTo52 = abs(0.55-result) bestResult = result bestSettingsIndex = resultIndex - logging.debug("\t\tInfo:\t Best Reslut : "+str(result)) + logging.debug("\t\tInfo:\t Best Result : "+str(result)) return map(lambda p: round(p, 4), bestSettings[bestSettingsIndex]) # return map(round(,4), bestSettings[bestSettingsIndex]) \ No newline at end of file diff --git a/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Mumbo.py b/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Mumbo.py index b4c7d5a01c456b768e78293a402cd64167685195..1d12dc86def9631dc977229b323bd19e7a09cc39 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Mumbo.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Mumbo/Mumbo.py @@ -37,8 +37,8 @@ def trainWeakClassifier(classifierName, monoviewDataset, CLASS_LABELS, def trainWeakClassifier_hdf5(classifierName, monoviewDataset, CLASS_LABELS, DATASET_LENGTH, - viewIndice, classifier_config, viewName, iterIndex, costMatrices): - weights = computeWeights(DATASET_LENGTH, iterIndex, viewIndice, CLASS_LABELS, costMatrices) + viewIndice, classifier_config, viewName, iterIndex, costMatrices, classifierIndex): + weights = computeWeights(DATASET_LENGTH, iterIndex, classifierIndex, CLASS_LABELS, costMatrices) classifierModule = globals()[classifierName] # Permet d'appeler une fonction avec une string classifierMethod = getattr(classifierModule, classifierName) classifier, classes, isBad, averageAccuracy = classifierMethod(monoviewDataset, CLASS_LABELS, classifier_config, weights) @@ -68,17 +68,23 @@ class Mumbo: self.classifiersNames = kwargs["classifiersNames"] self.classifiersConfigs = kwargs["classifiersConfigs"] nbView = kwargs["nbView"] + self.nbCores = NB_CORES + self.iterIndex = 0 self.edges = np.zeros((self.maxIter, nbView)) self.alphas = np.zeros((self.maxIter, nbView)) self.generalAlphas = np.zeros(self.maxIter) - self.nbCores = NB_CORES - self.iterIndex = 0 self.bestClassifiers = [] - self.bestViews = np.zeros(self.maxIter, dtype=int) + self.bestViews = np.zeros(self.maxIter, dtype=int)-1 self.averageAccuracies = np.zeros((self.maxIter, nbView)) self.iterAccuracies = np.zeros(self.maxIter) def initDataDependant(self, datasetLength, nbView, nbClass, labels): + self.edges = np.zeros((self.maxIter, nbView)) + self.alphas = np.zeros((self.maxIter, nbView)) + self.generalAlphas = np.zeros(self.maxIter) + self.bestClassifiers = [] + self.bestViews = np.zeros(self.maxIter, dtype=int)-1 + self.averageAccuracies = np.zeros((self.maxIter, nbView)) self.costMatrices = np.array([ np.array([ np.array([ @@ -160,7 +166,6 @@ class Mumbo: self.iterIndex += 1 - def predict_hdf5(self, DATASET, usedIndices=None, viewsIndices=None): NB_CLASS = DATASET.get("Metadata").attrs["nbClass"] if usedIndices == None: @@ -176,8 +181,11 @@ class Mumbo: for labelIndex, exampleIndex in enumerate(usedIndices): votes = np.zeros(NB_CLASS) for classifier, alpha, view in zip(self.bestClassifiers, self.alphas, self.bestViews): - data = getV(DATASET, int(view), exampleIndex) - votes[int(classifier.predict(np.array([data])))] += alpha[viewDict[view]] + if view != -1: + data = getV(DATASET, int(view), exampleIndex) + votes[int(classifier.predict(np.array([data])))] += alpha[viewDict[view]] + else: + pass predictedLabels[labelIndex] = np.argmax(votes) else: predictedLabels = [] @@ -192,7 +200,6 @@ class Mumbo: predictedProbas = np.zeros((DATASET_LENGTH, NB_CLASS)) for labelIndex, exampleIndex in enumerate(usedIndices): - votes = np.zeros(NB_CLASS) for classifier, alpha, view in zip(self.bestClassifiers, self.alphas, self.bestViews): data = getV(DATASET, int(view), exampleIndex) predictedProbas[labelIndex, int(classifier.predict(np.array([data])))] += alpha[view] @@ -242,13 +249,13 @@ class Mumbo: classifiersNames = self.classifiersNames iterIndex = self.iterIndex trainedClassifiersAndLabels = Parallel(n_jobs=NB_JOBS)( - delayed(trainWeakClassifier_hdf5)(classifiersNames[viewIndex], + delayed(trainWeakClassifier_hdf5)(classifiersNames[classifierIndex], getV(DATASET,viewIndex,trainIndices), DATASET.get("Labels")[trainIndices], DATASET_LENGTH, - viewIndex, classifiersConfigs[viewIndex], - DATASET.get("View"+str(viewIndex)).attrs["name"], iterIndex, costMatrices) - for viewIndex in viewIndices) + viewIndex, classifiersConfigs[classifierIndex], + DATASET.get("View"+str(viewIndex)).attrs["name"], iterIndex, costMatrices, classifierIndex) + for classifierIndex, viewIndex in enumerate(viewIndices)) for viewFakeIndex, (classifier, labelsArray, isBad, averageAccuracy) in enumerate(trainedClassifiersAndLabels): self.averageAccuracies[self.iterIndex, viewFakeIndex] = averageAccuracy @@ -370,44 +377,44 @@ class Mumbo: = -1 * np.sum(np.exp(self.generalFs[self.iterIndex, exampleIndice] - self.generalFs[self.iterIndex, exampleIndice, classe])) - def fit(self, DATASET, CLASS_LABELS, **kwargs): - # Initialization - DATASET_LENGTH = len(CLASS_LABELS) - NB_VIEW = len(DATASET) - NB_CLASS = len(set(CLASS_LABELS)) - bestViews = np.zeros(self.maxIter) - bestClassifiers = [] - - # Learning - for i in range(self.maxIter): - logging.debug('\t\tStart:\t Iteration ' + str(self.iterIndex + 1)) - classifiers, predictedLabels, areBad = self.trainWeakClassifiers(DATASET, CLASS_LABELS, NB_CLASS, - DATASET_LENGTH, NB_VIEW) - if areBad.all(): - logging.warning("All bad for iteration " + str(self.iterIndex)) - - self.predictions[self.iterIndex] = predictedLabels - - for viewIndice in range(NB_VIEW): - self.computeEdge(viewIndice, DATASET_LENGTH, CLASS_LABELS) - if areBad[viewIndice]: - self.alphas[self.iterIndex, viewIndice] = 0. - else: - self.alphas[self.iterIndex, viewIndice] = self.computeAlpha(self.edges[self.iterIndex, - viewIndice]) - self.updateDs(CLASS_LABELS, NB_VIEW, DATASET_LENGTH) - self.updateFs(NB_VIEW, DATASET_LENGTH, NB_CLASS) - - self.updateCostmatrices(NB_VIEW, DATASET_LENGTH, NB_CLASS, CLASS_LABELS) - bestView, edge = self.chooseView(NB_VIEW, CLASS_LABELS, DATASET_LENGTH) - self.bestViews[self.iterIndex] = bestView - if areBad.all(): - self.generalAlphas[self.iterIndex] = 0. - else: - self.generalAlphas[self.iterIndex] = self.computeAlpha(edge) - self.bestClassifiers.append(classifiers[bestView]) - self.updateGeneralFs(DATASET_LENGTH, NB_CLASS, bestView) - self.updateGeneralCostMatrix(DATASET_LENGTH, NB_CLASS, CLASS_LABELS) + # def fit(self, DATASET, CLASS_LABELS, **kwargs): + # # Initialization + # DATASET_LENGTH = len(CLASS_LABELS) + # NB_VIEW = len(DATASET) + # NB_CLASS = len(set(CLASS_LABELS)) + # bestViews = np.zeros(self.maxIter) + # bestClassifiers = [] + # + # # Learning + # for i in range(self.maxIter): + # logging.debug('\t\tStart:\t Iteration ' + str(self.iterIndex + 1)) + # classifiers, predictedLabels, areBad = self.trainWeakClassifiers(DATASET, CLASS_LABELS, NB_CLASS, + # DATASET_LENGTH, NB_VIEW) + # if areBad.all(): + # logging.warning("All bad for iteration " + str(self.iterIndex)) + # + # self.predictions[self.iterIndex] = predictedLabels + # + # for viewIndice in range(NB_VIEW): + # self.computeEdge(viewIndice, DATASET_LENGTH, CLASS_LABELS) + # if areBad[viewIndice]: + # self.alphas[self.iterIndex, viewIndice] = 0. + # else: + # self.alphas[self.iterIndex, viewIndice] = self.computeAlpha(self.edges[self.iterIndex, + # viewIndice]) + # self.updateDs(CLASS_LABELS, NB_VIEW, DATASET_LENGTH) + # self.updateFs(NB_VIEW, DATASET_LENGTH, NB_CLASS) + # + # self.updateCostmatrices(NB_VIEW, DATASET_LENGTH, NB_CLASS, CLASS_LABELS) + # bestView, edge, bestFakeView = self.chooseView(NB_VIEW, CLASS_LABELS, DATASET_LENGTH) + # self.bestViews[self.iterIndex] = bestView + # if areBad.all(): + # self.generalAlphas[self.iterIndex] = 0. + # else: + # self.generalAlphas[self.iterIndex] = self.computeAlpha(edge) + # self.bestClassifiers.append(classifiers[bestView]) + # self.updateGeneralFs(DATASET_LENGTH, NB_CLASS, bestView) + # self.updateGeneralCostMatrix(DATASET_LENGTH, NB_CLASS, CLASS_LABELS) def predict(self, DATASET, NB_CLASS=2): @@ -439,7 +446,7 @@ class Mumbo: return np.transpose(predictedLabels) - def classifyMumbobyIter_hdf5(self, DATASET, usedIndices=None, NB_CLASS=2): + def classifyMumbobyIter_hdf5(self, DATASET, fakeViewsIndicesDict, usedIndices=None, NB_CLASS=2): if usedIndices == None: usedIndices = range(DATASET.get("Metadata").attrs["datasetLength"]) if usedIndices: @@ -452,7 +459,7 @@ class Mumbo: for usedExampleIndex, exampleIndex in enumerate(usedIndices): data = np.array([np.array(getV(DATASET,int(view), exampleIndex))]) - votesByIter[usedExampleIndex, int(classifier.predict(data))] += alpha[view] + votesByIter[usedExampleIndex, int(classifier.predict(data))] += alpha[fakeViewsIndicesDict[view]] votes[usedExampleIndex] = votes[usedExampleIndex] + np.array(votesByIter[usedExampleIndex]) predictedLabels[usedExampleIndex, iterIndex] = np.argmax(votes[usedExampleIndex]) else: diff --git a/Code/MonoMutliViewClassifiers/Multiview/Mumbo/analyzeResults.py b/Code/MonoMutliViewClassifiers/Multiview/Mumbo/analyzeResults.py index d4811a12d80516f1a503eaaf310db03747f62a79..bb74ecb7e98e972247528f27a2b45f07380922d1 100644 --- a/Code/MonoMutliViewClassifiers/Multiview/Mumbo/analyzeResults.py +++ b/Code/MonoMutliViewClassifiers/Multiview/Mumbo/analyzeResults.py @@ -140,6 +140,7 @@ def getReport(classifiersIterations, CLASS_LABELS, iterationValidationIndices, D DATASET_LENGTH = DATASET.get("Metadata").attrs["datasetLength"] NB_CLASS = DATASET.get("Metadata").attrs["nbClass"] metricModule = getattr(Metrics, metric[0]) + fakeViewsIndicesDict = dict((viewIndex, fakeViewIndex) for viewIndex, fakeViewIndex in zip(viewIndices, range(nbView))) if metric[1]!=None: metricKWARGS = dict((index, metricConfig) for index, metricConfig in enumerate(metric[1])) else: @@ -165,9 +166,9 @@ def getReport(classifiersIterations, CLASS_LABELS, iterationValidationIndices, D meanAverageAccuraciesIterations.append(np.mean(mumboClassifier.averageAccuracies, axis=0)) viewsStatsIteration[statIterIndex, :] = np.array([float(list(mumboClassifier.bestViews).count(viewIndex))/ len(mumboClassifier.bestViews)for viewIndex in range(nbView)]) - PredictedTrainLabelsByIter = mumboClassifier.classifyMumbobyIter_hdf5(DATASET, usedIndices=learningIndices, + PredictedTrainLabelsByIter = mumboClassifier.classifyMumbobyIter_hdf5(DATASET, fakeViewsIndicesDict, usedIndices=learningIndices, NB_CLASS=NB_CLASS) - PredictedTestLabelsByIter = mumboClassifier.classifyMumbobyIter_hdf5(DATASET, usedIndices=validationIndices, + PredictedTestLabelsByIter = mumboClassifier.classifyMumbobyIter_hdf5(DATASET, fakeViewsIndicesDict, usedIndices=validationIndices, NB_CLASS=NB_CLASS) scoresByIter = np.zeros((len(PredictedTestLabelsByIter),2)) for iterIndex,(iterPredictedTrainLabels, iterPredictedTestLabels) in enumerate(zip(PredictedTrainLabelsByIter, PredictedTestLabelsByIter)):