Example #1
0
    def train(self, C=None, Gamma=None, ilog=None, callback=None):
        # Create the SVM
        self.svm = SVM(svm_type=TYPE_C_SVC)

        # Add training data
        for sub_id, tiles in self.training_data.iteritems():
            for tile in tiles:
                self.svm.addTraining(sub_id, tile)

        # Train the SVM
        if C != None and Gamma != None:
            self.svm.train(C_range=C,
                           G_range=Gamma,
                           verbose=True,
                           callback=callback)
        else:
            #Automatic
            self.svm.train(verbose=True, callback=callback)
Example #2
0
 def train(self, C = None, Gamma = None, ilog=None, callback=None):
     # Create the SVM
     self.svm = SVM(type=TYPE_C_SVC)
     
     # Add training data
     for id, tiles in self.training_data.iteritems():
         for tile in tiles:
             self.svm.addTraining(id,tile)
             
     # Train the SVM
     if C != None and Gamma != None:
         self.svm.train(C_range = C, G_range = Gamma, verbose=True,callback=callback)
     else:
         #Automatic
         self.svm.train(verbose=True,callback=callback)
Example #3
0
File: ml.py Project: mdqyy/pyvision
def testSVM():
    svm = SVM()

    for i in iris_training:
        svm.addTraining(labels[i], iris_data[i, :])

    svm.train(verbose=1)

    success = 0.0
    total = 0.0
    for i in iris_testing:
        c = svm.predict(iris_data[i, :])
        #print c, labels[i]
        if c == labels[i]:
            success += 1
        total += 1
    print "SVM Rate:", success / total
Example #4
0
def testSVM():
    svm = SVM()
        
    for i in iris_training:
        svm.addTraining(labels[i],iris_data[i,:])
        
    svm.train(verbose = 1)
    
    success = 0.0
    total = 0.0
    for i in iris_testing:
        c = svm.predict(iris_data[i,:])
        #print c, labels[i]
        if c == labels[i]:
            success += 1
        total += 1
    print "SVM Rate:",success/total
Example #5
0
    table = pv.Table()
    values = {0: [], 1: []}

    correct = 0
    total = 0
    for each in testing:
        label = clsfy.predict(each[1], ilog=ilog)
        total += 1
        if label == each[0]:
            correct += 1

    rate = float(correct) / total

    if ilog: ilog.table(table)
    return rate


if __name__ == "__main__":
    from pyvision.vector.SVM import SVM

    svm = SVM(kernel='LINEAR', random_seed=30)
    ilog = pv.ImageLog()
    print "SVM rate:", genderClassifier(svm, ilog=None)

    svm = SVM(kernel='RBF', random_seed=30)
    ilog = pv.ImageLog()
    print "SVM rate:", genderClassifier(svm, ilog=None)

    ilog.show()
Example #6
0
class SVMFaceRec:
    
    def __init__(self):
        self.tile_size = (128,160)
        self.leye = pv.Point(26.0,40.0)
        self.reye = pv.Point(102.0,40.0)
        self.norm_sigma = 8.0
        
        self.svm = None
        
        self.n_faces = 0
        self.n_labels = 0
        
        self.training_data = {}
        
            
    def preprocess(self,im,leye,reye,ilog=None):
        im = pv.Image(im.asPIL())
        affine = pv.AffineFromPoints(leye,reye,self.leye,self.reye,self.tile_size)
        tile = affine.transformImage(im)
        
        mat = tile.asMatrix2D()
        
        # High pass filter the image
        mat = mat - ndimage.gaussian_filter(mat,self.norm_sigma)
        
        # Value normalize the image.
        mat = mat - mat.mean()
        mat = mat / mat.std()
                
        tile = pv.Image(mat)
        
        return tile
        
    def addTraining(self,im,leye,reye,id,ilog=None):
        self.svm = None
        
        tile = self.preprocess(im,leye,reye,ilog)
        if not self.training_data.has_key(id):
            self.training_data[id] = []
        
        self.training_data[id].append(tile)

        self.n_labels = len(self.training_data)
        self.n_faces += 1
        
        
    def train(self, C = None, Gamma = None, ilog=None, callback=None):
        # Create the SVM
        self.svm = SVM(type=TYPE_C_SVC)
        
        # Add training data
        for id, tiles in self.training_data.iteritems():
            for tile in tiles:
                self.svm.addTraining(id,tile)
                
        # Train the SVM
        if C != None and Gamma != None:
            self.svm.train(C_range = C, G_range = Gamma, verbose=True,callback=callback)
        else:
            #Automatic
            self.svm.train(verbose=True,callback=callback)
            

        
    def predict(self,im,leye,reye):
        assert self.svm != None
        
        tile = self.preprocess(im,leye,reye)
        return self.svm.predict(tile)

    def score(self,im,leye,reye):
        tile = self.preprocess(im,leye,reye)
        return self.svm.predictSVMValues(tile)
    
    def reset(self):
        self.svm = None
        
        self.n_faces = 0
        self.n_labels = 0
        
        self.training_data = {}
        
    def isTrained(self):
        return self.svm != None
        
        
        
        
        
        
        
        
        
        
        
Example #7
0
 def runSVM(self):
     svm = SVM()
     print "I am in the SVM module now"   
Example #8
0
class SVMFaceRec:
    def __init__(self):
        self.tile_size = (128, 160)
        self.leye = pv.Point(26.0, 40.0)
        self.reye = pv.Point(102.0, 40.0)
        self.norm_sigma = 8.0

        self.svm = None

        self.n_faces = 0
        self.n_labels = 0

        self.training_data = {}

    def preprocess(self, im, leye, reye, ilog=None):
        im = pv.Image(im.asPIL())
        affine = pv.AffineFromPoints(leye, reye, self.leye, self.reye,
                                     self.tile_size)
        tile = affine.transformImage(im)

        mat = tile.asMatrix2D()

        # High pass filter the image
        mat = mat - ndimage.gaussian_filter(mat, self.norm_sigma)

        # Value normalize the image.
        mat = mat - mat.mean()
        mat = mat / mat.std()

        tile = pv.Image(mat)

        return tile

    def addTraining(self, im, leye, reye, sub_id, ilog=None):
        self.svm = None

        tile = self.preprocess(im, leye, reye, ilog)
        if not self.training_data.has_key(sub_id):
            self.training_data[sub_id] = []

        self.training_data[sub_id].append(tile)

        self.n_labels = len(self.training_data)
        self.n_faces += 1

    def train(self, C=None, Gamma=None, ilog=None, callback=None):
        # Create the SVM
        self.svm = SVM(svm_type=TYPE_C_SVC)

        # Add training data
        for sub_id, tiles in self.training_data.iteritems():
            for tile in tiles:
                self.svm.addTraining(sub_id, tile)

        # Train the SVM
        if C != None and Gamma != None:
            self.svm.train(C_range=C,
                           G_range=Gamma,
                           verbose=True,
                           callback=callback)
        else:
            #Automatic
            self.svm.train(verbose=True, callback=callback)

    def predict(self, im, leye, reye):
        assert self.svm != None

        tile = self.preprocess(im, leye, reye)
        return self.svm.predict(tile)

    def score(self, im, leye, reye):
        tile = self.preprocess(im, leye, reye)
        return self.svm.predictSVMValues(tile)

    def reset(self):
        self.svm = None

        self.n_faces = 0
        self.n_labels = 0

        self.training_data = {}

    def isTrained(self):
        return self.svm != None