ofx SVM Classification

// h-----------------------------
#pragma once

#include "ofMain.h"
#include "ofxLearn.h"           // 딥러닝 패키지

class ofApp : public ofBaseApp{
public:
    void setup();
    void update();
    void draw();
   
    void keyPressed(int key);
    void keyReleased(int key);
    void mouseMoved(int x, int y);
    void mouseDragged(int x, int y, int button);
    void mousePressed(int x, int y, int button);
    void mouseReleased(int x, int y, int button);
    void windowResized(int w, int h);
    void dragEvent(ofDragInfo dragInfo);
    void gotMessage(ofMessage msg);
   
    ofxLearnSVM classifier;         // 써포트 벡터 머신
    vector<vector<double> > trainingExamples;   // 학습 데이타
    vector<int> trainingLabels;                 // 학습 레이브
};

// cpp ----------------------------------------

#include "ofApp.h"

//--------------------------------------------------------------
//???: 초기화, 학습쌍 만들기, 학습
void ofApp::setup() {
    ofSetLogLevel(OF_LOG_VERBOSE);
   
    // add 5000 samples to training set
    for (int i=0; i<5000; i++)
    {
        // our samples have two features: x, and y,
        // which are bound between (0, 1).
        // note: your feature values don't need to be between 0, 1
        // but best practice is to pre-normalize because it's faster
        // and ensures parity of feature influences

        vector<double> sample;
        sample.push_back(ofRandom(1));
        sample.push_back(ofRandom(1));

        // our label contains 3 possible classes, which roughly
        // correspond to the distance from the center of the screen
        // with some noise thrown in
        int label;
        float distFromCenter = ofDist(sample[0], sample[1], 0.5, 0.5);
        if (distFromCenter < ofRandom(0.1, 0.25)) {
            label = 1;
        }
        else if (distFromCenter < ofRandom(0.15, 0.45)) {
            label = 2;
        }
        else {
            label = 3;
        }
       
        // save our samples
        trainingExamples.push_back(sample);
        trainingLabels.push_back(label);
       
        // add sample to our classifier
        classifier.addSample(sample, label);
    }
   
    classifier.train();
   
    // or you can make a grid parameter search to find the
    // best parameters for training. this takes much longer
    // but should be more accurate.
    //classifier.trainWithGridParameterSearch()
}

//--------------------------------------------------------------
void ofApp::update(){
}

//--------------------------------------------------------------
//???: 그리기, 학습쌍들 그리기, 마우스 위치에 클래스 큰원 동적 표시
void ofApp::draw() {
   
    // draw training set
    ofBackground(255);
    for (int i=0; i<trainingExamples.size(); i++) {
        vector<double> trainingExample = trainingExamples[i];
        int trainingLabel = trainingLabels[i];
        if (trainingLabel == 1) {
            ofSetColor(255, 0, 0);
        }
        else if (trainingLabel == 2) {
            ofSetColor(0, 255, 0);
        }
        else if (trainingLabel == 3) {
            ofSetColor(0, 0, 255);
        }
        ofCircle(trainingExample[0] * ofGetWidth(), trainingExample[1] * ofGetHeight(), 5);
    }

    // classify a new sample
    vector<double> sample;
    sample.push_back((double)ofGetMouseX()/ofGetWidth());
    sample.push_back((double)ofGetMouseY()/ofGetHeight());

    int label = classifier.predict(sample);
   
    if (label == 1) {
        ofSetColor(255, 0, 0);
    }
    else if (label == 2) {
        ofSetColor(0, 255, 0);
    }
    else if (label == 3) {
        ofSetColor(0, 0, 255);
    }
    ofCircle(ofGetMouseX(), ofGetMouseY(), ofMap(sin(0.1*ofGetFrameNum()), -1, 1, 5, 35));
    ofSetColor(0);
    ofDrawBitmapString("class "+ofToString(label), ofGetMouseX()-25, ofGetMouseY());

}



댓글

이 블로그의 인기 게시물

파이썬으로 Homomorphic Filtering 하기

파이썬으로 2D FFT/iFFT 하기: numpy 버전