1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
|
void svm(cv::Mat& trainingData, cv::Mat& trainingClasses, cv::Mat& testData, cv::Mat& testClasses, int size)
{
CvSVMParams param = CvSVMParams();
param.svm_type = CvSVM::C_SVC;
param.kernel_type = CvSVM::RBF; //CvSVM::RBF, CvSVM::LINEAR ... the best choice
param.degree = 0; // for poly
param.gamma = 20; // for poly/rbf/sigmoid
param.coef0 = 0; // for poly/sigmoid
/*Parameters in the generalized SVM optimization problem */
param.C = 7; // for CV_SVM_C_SVC, CV_SVM_EPS_SVR and CV_SVM_NU_SVR
param.nu = 1.0; // for CV_SVM_NU_SVC, CV_SVM_ONE_CLASS, and CV_SVM_NU_SVR
param.p = 0.0; // for CV_SVM_EPS_SVR
param.class_weights = NULL; // for CV_SVM_C_SVC
param.term_crit.type = CV_TERMCRIT_ITER +CV_TERMCRIT_EPS;
param.term_crit.max_iter = 1000; // maximum number of iteration in training
param.term_crit.epsilon = 1e-6; // epsilon is the error to stop training
/* SVM training (use train auto for OpenCV>=2.0)
The method trains the SVM model. It follows the conventions of the generic train method with the following limitations: only the CV _ ROW _ SAMPLE data layout is supported,
the input variables are all ordered, the output variables can be either categorical ( _params.svm_type=CvSVM::C_SVC or _params.svm_type=CvSVM::NU_SVC ),
or ordered ( _params.svm_type=CvSVM::EPS_SVR or _params.svm_type=CvSVM::NU_SVR ), or not required at all ( _params.svm_type=CvSVM::ONE_CLASS ), missing measurements are not supported.
All the other parameters are gathered in CvSVMParams structure.
*/
CvSVM SVM;
SVM.train_auto(trainingData, trainingClasses, cv::Mat(), cv::Mat(), param); // PLANTAGE
// Mat predicted(testClasses.rows, 1, CV_32F);
/*
for(int i = 0; i < testData.rows; i++)
{
Mat sample = testData.row(i);
//float x = sample.at<float>(0,0);
//float y = sample.at<float>(0,1);
float result = svm.predict(sample);
cout << "result= " << result << "\n";
}
*/
/*
cout << "Accuracy_{SVM} = " << evaluate(predicted, testClasses) << endl;
plot_binary(testData, predicted, "Predictions SVM", size);
// plot support vectors
if(plotSupportVectors)
{
Mat plot_sv(size, size, CV_8UC3);
plot_sv.setTo(cv::Scalar(255.0, 255.0, 255.0));
int svec_count = svm.get_support_vector_count();
for(int vecNum = 0; vecNum < svec_count; vecNum++)
{
const float* vec = svm.get_support_vector(vecNum);
circle(plot_sv, Point(vec[0]*size, vec[1]*size), 3 , CV_RGB(0, 0, 0));
}
imshow("Support Vectors", plot_sv);
}
*/
}
int main(int argc, char** argv )
{
if (argc == 6)
{
//argv[1] = file: ad_cranfield.data
//argv[2] = line1_train;
//argv[3] = line2_train;
//argv[4] = line1_test;
//argv[5] = line2_test;
FILE* frandom = NULL;
char* line1_train = argv[2];
char* line2_train = argv[3];
char* line1_test = argv[4];
char* line2_test = argv[5];
// get min/max line numbers
int min_line_train = min(atoi(line1_train), atoi(line2_train)); // atoi convert string to integer, ex: "5" => 5
int max_line_train = max(atoi(line1_train), atoi(line2_train));
int min_line_test = min(atoi(line1_test), atoi(line2_test));
int max_line_test = max(atoi(line1_test), atoi(line2_test));
int NUMBER_OF_TRAINING_SAMPLES = max_line_train - min_line_train;
int NUMBER_OF_TESTING_SAMPLES = max_line_test - min_line_test;
cout << "NUMBER_OF_TRAINING_SAMPLES = " << NUMBER_OF_TRAINING_SAMPLES << "\n";
cout << "NUMBER_OF_TESTING_SAMPLES = " << NUMBER_OF_TESTING_SAMPLES << "\n";
FILE* f = fopen( argv[1], "r" );
if( !f )
{
printf("ERROR: cannot read file \n");
return 0; // all not OK
}
FILE* ftrain = f;
FILE* ftest = f;
frandom = f;
randomize (f, frandom, "frandom");
selectlines(line1_train, line2_train, frandom, ftrain, "fww");
selectlines(line1_test, line2_test, frandom, ftest, "fww");
// define training data storage matrices (one for attribute examples, one for classifications)
Mat training_data = Mat(NUMBER_OF_TRAINING_SAMPLES, ATTRIBUTES_PER_SAMPLE, CV_32FC1); // CV_32FC1 constant which assumes float type and train and res matrices just reinterpret bits of your int and doubles as floats.
Mat training_classifications = Mat(NUMBER_OF_TRAINING_SAMPLES, 1, CV_32FC1); // 1.0 = class 1 = ad and 0.0 = class 2 = nonad
//define testing data storage matrices
Mat testing_data = Mat(NUMBER_OF_TESTING_SAMPLES, ATTRIBUTES_PER_SAMPLE, CV_32FC1);
Mat testing_classifications = Mat(NUMBER_OF_TESTING_SAMPLES, 1, CV_32FC1); // 1.0 = class 1 = ad and 0.0 = class 2 = nonad
// define all the attributes as numerical (** not needed for all ML techniques **)
Mat var_type = Mat(ATTRIBUTES_PER_SAMPLE + 1, 1, CV_8U );
var_type.setTo(Scalar(CV_VAR_NUMERICAL)); // all inputs are numerical
// this is a classification problem so reset the last (+1) output
// var_type element to CV_VAR_CATEGORICAL (** not needed for all ML techniques **)
var_type.at<uchar>(ATTRIBUTES_PER_SAMPLE, 0) = CV_VAR_CATEGORICAL;
// load training and testing data sets
if (read_data_from_csv(ftrain, training_data, training_classifications, NUMBER_OF_TRAINING_SAMPLES) && read_data_from_csv(ftest, testing_data, testing_classifications, NUMBER_OF_TESTING_SAMPLES))
{
//*********************************************************************
// DO YOUR ML HERE
//*********************************************************************
// all matrix memory freed by destructors
// all OK : main returns 0
svm(training_data, training_classifications, testing_data, testing_classifications, 100 );
return 0;
}
else
{
// not OK : main returns -1
return -1;
}
}
else
{
return -1;
}
}
/******************************************************************************/ |
Partager