Skip to content
Snippets Groups Projects
Commit c8524de9 authored by Baptiste Bauvin's avatar Baptiste Bauvin
Browse files

Refactoring

parent bd9a80e9
No related branches found
No related tags found
No related merge requests found
../idea/
../multiview-machine-learning-omis.iml
\ No newline at end of file
......@@ -3,6 +3,7 @@
""" MultiClass Classification with MonoView """
# Import built-in modules
import pandas as pd
# Import sci-kit learn party modules
from sklearn.cross_validation import train_test_split
......
......@@ -126,7 +126,6 @@ CELL_DIMENSION = 5
NB_ORIENTATIONS = 8
NB_CLUSTERS = 12
MAXITER = 100
NB_CORES = 1
# Extract Feature from DB
hog_feat_desc,hog_f_extr_res = FeatExtraction.calcHOGParallel(nameDB, dfImages.values, CELL_DIMENSION, NB_ORIENTATIONS, NB_CLUSTERS, MAXITER, NB_CORES)
......
......@@ -3,6 +3,7 @@
import numpy as np
# TODO :
# Linear Weighted Fusion
# Bayesian Inference /!\ Statistically independant => ?
......@@ -15,13 +16,14 @@ import numpy as np
# Particle Filter ?
def linearWeightedFusion(toFuse, weights):
# Normalize weights ?
# weights = weights/float(max(weights))
weighted = np.array([np.array([feature*weights for (feature, weight) in zip(exampleToFuse, weights)]).flatten() for exampleToFuse in toFuse])
return fused
weighted = np.array(
[np.array([feature * weights for (feature, weight) in zip(exampleToFuse, weights)]).flatten() for
exampleToFuse in toFuse])
return weighted
if __name__ == '__main__':
\ No newline at end of file
pass
......@@ -5,6 +5,7 @@ import numpy as np
import sys
from sklearn.svm import SVC
# Our method in multiclass classification will be One-vs-One or One-vs-All
# classifiers, so if we can get the output of these classifiers, we are
# able to compute a score for each class in each mono-view classification
......@@ -17,18 +18,17 @@ from sklearn.svm import SVC
# weights : (nbFeature) array with the weights for each feature
def weightedLinear(monoViewDecisions, weights):
# Normalize weights ?
# weights = weights/float(max(weights))
fusedExamples = np.array([sum(np.array([featureScores * weight for weight, featureScores \
in zip(weights, exampleDecisions)])) for exampleDecisions in monoViewDecisions])
in zip(weights, exampleDecisions)])) for exampleDecisions in
monoViewDecisions])
# print fused
return np.array([np.argmax(fusedExample) for fusedExample in fusedExamples])
# The SVMClassifier is here used to find the right weights for linear fusion
def SVMForLinearFusionTrain(monoViewDecisions, labels):
SVMClassifier = SVC()
......@@ -41,7 +41,6 @@ def SVMForLinearFusionFuse(monoViewDecisions, SVMClassifier):
return labels
# For majority voting, we have a problem : we have 5 fetures and 101 classes
# on Calthech, so if each feature votes for one class, we can't find a good
# result
......@@ -66,8 +65,6 @@ def majorityVoting(monoViewDecisions, NB_CLASS):
return np.array([np.argmax(exampleVotes) for exampleVotes in votes])
# Main for testing
if __name__ == '__main__':
DATASET_LENGTH = 10
......@@ -77,20 +74,23 @@ if __name__ == '__main__':
LABELS = np.array([TRUE_CLASS for i in range(DATASET_LENGTH)])
LABELS[0] = 0
monoViewDecisionsEasy = np.array([np.array([np.zeros(NB_CLASS) for i in range(nbFeature)])for example in range(DATASET_LENGTH)])
monoViewDecisionsEasy = np.array(
[np.array([np.zeros(NB_CLASS) for i in range(nbFeature)]) for example in range(DATASET_LENGTH)])
for exampleDecisions in monoViewDecisionsEasy:
for decision in exampleDecisions:
decision[TRUE_CLASS] = 12
# print monoViewDecisionsEasy
monoViewDecisionsHard = np.array([np.array([np.zeros(NB_CLASS) for i in range(nbFeature)])for example in range(DATASET_LENGTH)])
monoViewDecisionsHard = np.array(
[np.array([np.zeros(NB_CLASS) for i in range(nbFeature)]) for example in range(DATASET_LENGTH)])
for exampleDecisions in monoViewDecisionsHard:
for decision in exampleDecisions:
decision[TRUE_CLASS] = 12
exampleDecisions[nbFeature - 2] = np.zeros(NB_CLASS) + 1400
exampleDecisions[nbFeature - 2][TRUE_CLASS] -= 110
monoViewDecisionsMajority = np.array([np.array([TRUE_CLASS,TRUE_CLASS,TRUE_CLASS,1,5]) for example in range(DATASET_LENGTH)])
monoViewDecisionsMajority = np.array(
[np.array([TRUE_CLASS, TRUE_CLASS, TRUE_CLASS, 1, 5]) for example in range(DATASET_LENGTH)])
monoViewDecisionsMajorityFail = np.array([np.array([1, 2, 3, 4, 5]) for example in range(DATASET_LENGTH)])
weights = np.random.rand(nbFeature)
......@@ -98,7 +98,6 @@ if __name__ == '__main__':
SVMClassifier = SVMForLinearFusionTrain(monoViewDecisionsMajority, LABELS)
print weightedLinear(monoViewDecisionsEasy, weights)
print weightedLinear(monoViewDecisionsHard, weights)
print SVMForLinearFusionFuse(monoViewDecisionsMajority, SVMClassifier)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment