Exemplo n.º 1
0
 def computeLayer(self, layer):
   """
   @param layer: the output HmaxLayer to store results in.
   """
   LevelFilter.computeLayer(layer)
   
   #need to enable learning mode from UI (after S2 trained)
   #during training, need to pass in class labels
   
   vec = layer.array[:,0,0].tolist()
   
   if self.isLearning:
     if len(self.learned)>=100 and self.svmModel==None:
       #model = svm_train(y, x [, 'training_options'])
       self.svmModel = svmutil.svm_train(self.classes, self.learned)
       return
     self.classes.append(0)
     self.learned.append(vec)
   elif self.svmModel!=None:
     #p_labs, p_acc, p_vals = svm_predict(y, x, model [,'predicting_options'])
     pLabs, pAcc, pVals = svmutil.svm_predict([0], vec, self.svmModel)
     print "SVM Result: ", pLabs, pAcc, pVals
Exemplo n.º 2
0
    def computeLayer(self, layer):
        """
    Override the computeLayer from LevelFilter in order to continue
    with processing the learning or inferring using our SVM model.
    If the SVM is trained and we are inferring, then the SVM inference
    results are stored in the layer (which is assumed to be a LayerC2).
    The layers are able to render themselves onto a wx canvas for 
    inspection.
    @param layer: the output HmaxLayer to store results in.
    """
        LevelFilter.computeLayer(self, layer)

        #need to enable learning mode from UI (after S2 trained)
        #during training, need to pass in class labels
        vec = layer.array[:, 0, 0].tolist()  #contains vector of C2 maxes

        if self.isLearning and self.__svmModel == None:
            #add to count for how many of this class have been learned
            count = self.__classCounts.get(self.learningClass, 0)
            self.__classCounts[self.learningClass] = count + 1

            #copy base input image to use as example when showing SVM result
            if count == 0:
                layer.saveExampleImage(self.learningClass)

            self.classes.append(self.learningClass)
            self.learned.append(vec)
            #print "learned svm ",len(self.learned),self.learningClass
        elif self.__svmModel != None:
            #p_labs, p_acc, p_vals = svm_predict(y, x, model [,'predicting_options'])
            pLabs, pAcc, pVals = svmutil.svm_predict([0], [vec],
                                                     self.__svmModel, "-b 1")
            pVals = pVals[0]
            #sort ids in case SVM classIDs not consecutive
            ids = sorted(self.__classCounts.keys())
            layer.setAccuracyResult(sorted(zip(pVals, ids), reverse=True))
            if HMAX.DEBUG:
                print "SVM Result: ", pLabs, pAcc, pVals
 def computeLayer(self, layer):
   """
   Override the computeLayer from LevelFilter in order to continue
   with processing the learning or inferring using our SVM model.
   If the SVM is trained and we are inferring, then the SVM inference
   results are stored in the layer (which is assumed to be a LayerC2).
   The layers are able to render themselves onto a wx canvas for 
   inspection.
   @param layer: the output HmaxLayer to store results in.
   """
   LevelFilter.computeLayer(self, layer)
   
   #need to enable learning mode from UI (after S2 trained)
   #during training, need to pass in class labels
   vec = layer.array[:,0,0].tolist() #contains vector of C2 maxes
   
   if self.isLearning and self.__svmModel==None:
     #add to count for how many of this class have been learned
     count = self.__classCounts.get(self.learningClass, 0)
     self.__classCounts[self.learningClass] = count+1
     
     #copy base input image to use as example when showing SVM result
     if count==0:
       layer.saveExampleImage(self.learningClass)
     
     self.classes.append(self.learningClass)
     self.learned.append(vec)
     #print "learned svm ",len(self.learned),self.learningClass
   elif self.__svmModel!=None:
     #p_labs, p_acc, p_vals = svm_predict(y, x, model [,'predicting_options'])
     pLabs, pAcc, pVals = svmutil.svm_predict([0], [vec], self.__svmModel, "-b 1")
     pVals = pVals[0]
     #sort ids in case SVM classIDs not consecutive
     ids = sorted(self.__classCounts.keys())
     layer.setAccuracyResult(sorted(zip(pVals, ids),reverse=True))
     if HMAX.DEBUG:
       print "SVM Result: ", pLabs, pAcc, pVals