Created
October 28, 2014 07:44
-
-
Save nuttt/7fccf6e068577f743846 to your computer and use it in GitHub Desktop.
svm
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
| #include <opencv2/core/core.hpp> | |
| #include <opencv2/highgui/highgui.hpp> | |
| #include <opencv2/imgproc/imgproc.hpp> | |
| #include <opencv2/ml/ml.hpp> | |
| #include <iostream> | |
| #include <stdio.h> | |
| #include <stdlib.h> | |
| #include <string> | |
| #include <vector> | |
| #include <algorithm> | |
| using namespace cv; | |
| using namespace std; | |
| class TrainingSet { | |
| public: | |
| Mat data; | |
| int label; | |
| TrainingSet(Mat data, int label) { | |
| this->data = data; | |
| this->label = label; | |
| } | |
| }; | |
| int writeTrainingSet() { | |
| Mat image; | |
| int fileName; | |
| String fullFileName; | |
| vector<Mat> trainingData; | |
| vector<int> label; | |
| Mat train; | |
| for(int folderName=0;folderName<=9;folderName++){ | |
| fileName = 1; | |
| while(true){ | |
| fullFileName = "/Users/nuttt/MNIST/train/"+to_string(folderName)+"/"+to_string(fileName)+".jpg"; | |
| image = imread(fullFileName, CV_LOAD_IMAGE_GRAYSCALE); | |
| cout << fullFileName << endl; | |
| if(!image.data){ | |
| cout << "Could not open or find the image" << std::endl ; | |
| break; | |
| } | |
| Mat tmp = image.reshape(0,1); | |
| trainingData.push_back(tmp); | |
| label.push_back(folderName); | |
| fileName++; | |
| // break; | |
| } | |
| // break; | |
| } | |
| FileStorage fs("/Users/nuttt/MNIST/train.yml.gz", FileStorage::WRITE); | |
| fs << "trainingData" << trainingData; | |
| fs << "trainingLabel" << label; | |
| fs.release(); | |
| return 0; | |
| } | |
| int writeTestSet() { | |
| Mat image; | |
| int fileName; | |
| String fullFileName; | |
| vector<Mat> trainingData; | |
| vector<int> label; | |
| for(int folderName=0;folderName<=9;folderName++){ | |
| fileName = 1; | |
| while(true){ | |
| fullFileName = "/Users/nuttt/MNIST/test/"+to_string(folderName)+"/"+to_string(fileName)+".jpg"; | |
| image = imread(fullFileName, CV_LOAD_IMAGE_GRAYSCALE); | |
| cout << fullFileName << endl; | |
| if(!image.data){ | |
| cout << "Could not open or find the image" << std::endl ; | |
| break; | |
| } | |
| trainingData.push_back(image.reshape(0,1)); | |
| label.push_back(folderName); | |
| fileName++; | |
| // break; | |
| } | |
| // break; | |
| } | |
| FileStorage fs("/Users/nuttt/MNIST/test.yml.gz", FileStorage::WRITE); | |
| fs << "testData" << trainingData; | |
| fs << "testLabel" << label; | |
| fs.release(); | |
| return 0; | |
| } | |
| vector<TrainingSet> readTrainingSet() { | |
| FileStorage fs("/Users/nuttt/MNIST/train.yml.gz", FileStorage::READ); | |
| vector<TrainingSet> output; | |
| vector<Mat> data; | |
| vector<int> label; | |
| fs["trainingData"] >> data; | |
| fs["trainingLabel"] >> label; | |
| for(int i=0; i<label.size(); i++){ | |
| output.push_back(TrainingSet(data[i], label[i])); | |
| } | |
| fs.release(); | |
| return output; | |
| } | |
| vector<TrainingSet> readTestSet() { | |
| FileStorage fs("/Users/nuttt/MNIST/test.yml.gz", FileStorage::READ); | |
| vector<TrainingSet> output; | |
| vector<Mat> data; | |
| vector<int> label; | |
| fs["testData"] >> data; | |
| fs["testLabel"] >> label; | |
| for(int i=0; i<label.size(); i++){ | |
| output.push_back(TrainingSet(data[i], label[i])); | |
| } | |
| fs.release(); | |
| return output; | |
| } | |
| int predict(vector<TrainingSet> trainingData, Mat testData){ | |
| vector<pair<double, int> > distance; | |
| int count[10] = {0,0,0,0,0,0,0,0,0,0}; | |
| double d; | |
| Mat test = testData.reshape(0, 1); | |
| for(int i=0; i<trainingData.size(); i++){ | |
| d = norm(trainingData[i].data, test); | |
| distance.push_back(pair<double, int> (d, trainingData[i].label)); | |
| } | |
| sort(distance.begin(), distance.end()); | |
| for(int i=0; i<10; i++){ | |
| count[distance[i].second]++; | |
| } | |
| int maxx = count[0]; | |
| int maxID = 0; | |
| for(int i=0; i<10; i++){ | |
| if(count[i] > maxx){ | |
| maxx = count[i]; | |
| maxID = i; | |
| } | |
| } | |
| return maxID; | |
| } | |
| void trainSVM() { | |
| vector<TrainingSet> trainingData; | |
| trainingData = readTrainingSet(); | |
| CvSVM svm; | |
| Mat train, label; | |
| for(int i = 0; i < trainingData.size(); i++) { | |
| train.push_back(trainingData[i].data); | |
| label.push_back(trainingData[i].label); | |
| } | |
| train.convertTo(train, CV_32F); | |
| CvSVMParams params; | |
| params.svm_type = CvSVM::C_SVC; | |
| params.kernel_type = CvSVM::LINEAR; | |
| cout << "Training SVM" << endl; | |
| svm.train(train, label, Mat(), Mat(), params); | |
| svm.save("/Users/nuttt/MNIST/svm_linear.gz"); | |
| } | |
| int main(int, char**) { | |
| cout << "Start" << endl; | |
| //trainSVM(); | |
| vector<TrainingSet> testData; | |
| testData = readTestSet(); | |
| CvSVM svm; | |
| svm.load("/Users/nuttt/MNIST/svm_linear.gz"); | |
| Mat result, input; | |
| int result_count[10][10]; //[actual][predict] | |
| for (int i=0; i<10; i++) { | |
| for (int j=0; j<10; j++) { | |
| result_count[i][j] = 0; | |
| } | |
| } | |
| for(int i=0; i<testData.size();i++) { | |
| testData[i].data.convertTo(input, CV_32F); | |
| int response = svm.predict(input); | |
| cout << testData[i].label << ": " << response << endl; | |
| result_count[response][testData[i].label]++; | |
| } | |
| for (int i=0; i<10; i++) { | |
| for (int j=0; j<10; j++) { | |
| printf("%d\t", result_count[i][j]); | |
| } | |
| printf("\n"); | |
| } | |
| return 0; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment