Last active
December 25, 2015 02:19
-
-
Save alfredplpl/6901609 to your computer and use it in GitHub Desktop.
This is a class of Bag-of-Features by GMM
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#see also: http://scikit-learn.org/stable/modules/generated/sklearn.mixture.GMM.html | |
import numpy as np | |
from sklearn import mixture,preprocessing | |
class BagOfFeaturesGMM: | |
"""This is a class of Bag-of-Features by GMM """ | |
codebookSize=0 | |
classifier=None | |
def __init__(self, codebookSize): | |
self.codebookSize=codebookSize | |
def train(self,features,iterMax=100): | |
# construct a GMM classifier | |
gmm = mixture.GMM(n_components=self.codebookSize,n_iter=iterMax) | |
# train the classifier | |
self.classifier = gmm.fit(features) | |
def makeHistograms(self, feature): | |
histogram=np.zeros(self.codebookSize) | |
if self.classifier==None : | |
raise Exception("You need train this instance.") | |
results=self.classifier.predict(feature) | |
for idx in results: | |
idx=int(idx) | |
histogram[idx]=histogram[idx]+1 | |
histogram=preprocessing.normalize([histogram], norm='l2')[0] | |
return histogram |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment