ofxTSNE code

// h-----------------------------

#pragma once

#include "ofMain.h"
#include "ofxCcv.h"     // Another OpenCV
#include "ofxTSNE.h"    // T-SNE Plot
#include "ofxGui.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 mouseEntered(int x, int y);
        void mouseExited(int x, int y);
        void windowResized(int w, int h);
        void dragEvent(ofDragInfo dragInfo);
        void gotMessage(ofMessage msg);
       
    ofxCcv ccv;         // OpenCV var
    ofxTSNE tsne;       // tsne var
   
    vector<ofImage> images;     // image vectors
    vector<vector<double> > imagePoints;    // vector in vector
    vector<vector<float> > encodings;       // for encoding
   
    ofxPanel gui;               // control panel
    ofParameter<float> scale;       // control param for scale
    ofParameter<float> imageSize;   // control param for size
   
    int t;                      // Not used..
};

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

#include "ofApp.h"

//--------------------------------------------------------------
void ofApp::setup(){
   
    // load all the images
    ofLog() << "Gathering images...";
    ofDirectory dir;
    // all file in dir
    int nFiles = dir.listDir(ofToDataPath("images/"));
    // if files exist
    if(nFiles) {
        // for each nfiles
        for(int i=0; i<dir.size(); i++) {
            // each 20
            if (i % 20 == 0)    ofLog() << " - loading image "<<i<<" / "<<dir.size();
            string filePath = dir.getPath(i);   // filename + path
            images.push_back(ofImage());        // vector<ofImage> images;
            images.back().load(filePath);       // image load
        }
    }
   
    // setup ofxCcv
    // imagenet pretrained network loading
    ccv.setup("image-net-2012.sqlite3");
   
    // encode all of the images with ofxCcv
    ofLog() << "Encoding images...";
    // Convolution network feedforward
    for (int i=0; i<images.size(); i++) {
        if (i % 20 == 0)    ofLog() << " - encoding image "<<i<<" / "<<images.size();
        vector<float> encoding = ccv.encode(images[i], ccv.numLayers()-1);
        //vector<vector<float> > encodings;
        encodings.push_back(encoding);
    }
   
    // run t-SNE and load image points to imagePoints
    ofLog() << "Run t-SNE on images";

    imagePoints = tsne.run(encodings, 2, 25, 0.1, true);
    /* Perform t-SNE
     vector<vector<double> > run(vector<vector<float> > & data, int dims=2, double perplexity=30, double theta=0.5, bool normalize=true);
     */
   
    // make the images the same size
    for (int i=0; i<images.size(); i++) {
        images[i].resize(100 * images[i].getWidth() / images[i].getHeight(), 100);
    }
   
    // setup gui
    gui.setup();
    gui.add(scale.set("scale", 4.0, 0.0, 10.0));
    gui.add(imageSize.set("imageSize", 1.0, 0.0, 2.0));
}

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

//--------------------------------------------------------------
void ofApp::draw(){
    ofBackground(0);
   
    ofPushMatrix();     //좌표계 보존
    // translate by mouse move
    ofTranslate(-ofGetMouseX() * (scale - 0.5), -ofGetMouseY() * (scale - 0.5));
    // all position of images
    for (int i=0; i<imagePoints.size(); i++) {
        // scale control
        float x = ofMap(imagePoints[i][0], 0, 1, 0, scale * ofGetWidth());
        float y = ofMap(imagePoints[i][1], 0, 1, 0, scale * ofGetHeight());
        // size control
        images[i].draw(x, y, imageSize * images[i].getWidth(), imageSize * images[i].getHeight());
    }
    ofPopMatrix();      //좌표계 복원.
   
    gui.draw();
}

댓글

이 블로그의 인기 게시물

파이썬으로 Homomorphic Filtering 하기

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