Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include "stdafx.h"
- //dlib - http://dlib.net/
- #include <dlib/matrix.h>
- #include <dlib/dnn.h>
- #include <dlib/opencv.h>
- #include <dlib/image_io.h>
- using namespace dlib;
- //opencv - https://opencv.org/
- #include <opencv2/opencv.hpp>
- using namespace std;
- using namespace cv;
- using namespace cv::ml;
- struct space
- {
- int x01, y01, x02, y02, x03, y03, x04, y04, occup;
- };
- int load_parking_geometry(const char *filename, space *spaces);
- void extract_space(space *spaces, Mat in_mat, std::vector<Mat> &vector_images);
- void draw_detection(space *spaces, Mat &frame);
- void evaluation(fstream &detectorOutputFile, fstream &groundTruthFile);
- void train_parking_dlib();
- void test_parking();
- void test_parking_dlib();
- void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData);
- int spaces_num = 56;
- cv::Size space_size(80, 80);
- //LeNet
- using net_type = loss_binary_log<
- fc<1,
- relu<fc<84,
- relu<fc<120,
- max_pool<2, 2, 2, 2, relu<con<16, 5, 5, 1, 1,
- max_pool<2, 2, 2, 2, relu<con<6, 5, 5, 1, 1,
- input<matrix<bgr_pixel>>
- >>>>>>>>>>>>;
- //AlexNet
- /*using net_type = loss_binary_log<
- fc<1,
- relu<fc<4096,
- relu<fc<4096,
- max_pool<3, 3, 2, 2,
- relu<con<256, 3, 3, 1, 1,
- relu<con<384, 3, 3, 1, 1,
- relu<con<384, 3, 3, 1, 1,
- max_pool<3, 3, 2, 2,
- relu<con<256, 5, 5, 1, 1,
- max_pool<3, 3, 2, 2,
- relu<con<96, 11, 11, 4, 4,
- input<matrix<bgr_pixel>>
- >>>>>>>>>>>>>>>>>>>;*/
- template <typename SUBNET> using i3a_1 = relu<con<64, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i3a_2 = relu<con<128, 3, 3, 1, 1, relu<con<96, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i3a_3 = relu<con<32, 5, 5, 1, 1, relu<con<16, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i3a_4 = relu<con<32, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_3a = inception4<i3a_1, i3a_2, i3a_3, i3a_4, SUBNET>;
- template <typename SUBNET> using i3b_1 = relu<con<128, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i3b_2 = relu<con<192, 3, 3, 1, 1, relu<con<128, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i3b_3 = relu<con<96, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i3b_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_3b = inception4<i3b_1, i3b_2, i3b_3, i3b_4, SUBNET>;
- template <typename SUBNET> using i4a_1 = relu<con<192, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i4a_2 = relu<con<208, 3, 3, 1, 1, relu<con<96, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4a_3 = relu<con<48, 5, 5, 1, 1, relu<con<16, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4a_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_4a = inception4<i4a_1, i4a_2, i4a_3, i4a_4, SUBNET>;
- template <typename SUBNET> using i4b_1 = relu<con<160, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i4b_2 = relu<con<224, 3, 3, 1, 1, relu<con<112, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4b_3 = relu<con<64, 5, 5, 1, 1, relu<con<24, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4b_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_4b = inception4<i4b_1, i4b_2, i4b_3, i4b_4, SUBNET>;
- template <typename SUBNET> using i4c_1 = relu<con<128, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i4c_2 = relu<con<256, 3, 3, 1, 1, relu<con<128, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4c_3 = relu<con<64, 5, 5, 1, 1, relu<con<24, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4c_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_4c = inception4<i4c_1, i4c_2, i4c_3, i4c_4, SUBNET>;
- template <typename SUBNET> using i4d_1 = relu<con<112, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i4d_2 = relu<con<288, 3, 3, 1, 1, relu<con<144, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4d_3 = relu<con<64, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4d_4 = relu<con<64, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_4d = inception4<i4d_1, i4d_2, i4d_3, i4d_4, SUBNET>;
- template <typename SUBNET> using i4e_1 = relu<con<256, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i4e_2 = relu<con<320, 3, 3, 1, 1, relu<con<160, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4e_3 = relu<con<128, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i4e_4 = relu<con<128, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_4e = inception4<i4e_1, i4e_2, i4e_3, i4e_4, SUBNET>;
- template <typename SUBNET> using i5a_1 = relu<con<256, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i5a_2 = relu<con<320, 3, 3, 1, 1, relu<con<160, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i5a_3 = relu<con<128, 5, 5, 1, 1, relu<con<32, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i5a_4 = relu<con<128, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_5a = inception4<i5a_1, i5a_2, i5a_3, i5a_4, SUBNET>;
- template <typename SUBNET> using i5b_1 = relu<con<384, 1, 1, 1, 1, SUBNET>>;
- template <typename SUBNET> using i5b_2 = relu<con<384, 3, 3, 1, 1, relu<con<192, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i5b_3 = relu<con<128, 5, 5, 1, 1, relu<con<48, 1, 1, 1, 1, SUBNET>>>>;
- template <typename SUBNET> using i5b_4 = relu<con<128, 1, 1, 1, 1, max_pool<3, 3, 1, 1, SUBNET>>>;
- template <typename SUBNET> using incept_5b = inception4<i5b_1, i5b_2, i5b_3, i5b_4, SUBNET>;
- /*using net_type =
- loss_binary_log<
- fc<1,
- dropout<avg_pool<7, 7, 1, 1,
- incept_5b<incept_5b<
- max_pool<3, 3, 2, 2,
- incept_4e<incept_4d<incept_4c<incept_4b<incept_4a<
- max_pool<3, 3, 2, 2,
- incept_3b<incept_3a<
- max_pool<3, 3, 2, 2, relu<con<192, 3, 3, 1, 1,
- relu<con<64, 1, 1, 1, 1,
- max_pool<3, 3, 2, 2,
- relu<con<64, 7, 7, 2, 2,
- input<matrix<bgr_pixel>>
- >>>>>>>>>>>>>>>>>>>>>>>;*/
- using net_type2 =
- loss_binary_log<
- fc<1,
- dlib::multiply<avg_pool<7, 7, 1, 1,
- incept_5b<incept_5b<
- max_pool<3, 3, 2, 2,
- incept_4e<incept_4d<incept_4c<incept_4b<incept_4a<
- max_pool<3, 3, 2, 2,
- incept_3b<incept_3a<
- max_pool<3, 3, 2, 2,
- relu<con<192, 3, 3, 1, 1,
- relu<con<64, 1, 1, 1, 1,
- max_pool<3, 3, 2, 2,
- relu<con<64, 7, 7, 2, 2,
- input<matrix<bgr_pixel>>
- >>>>>>>>>>>>>>>>>>>>>>>;
- int main(int argc, char** argv)
- {
- cout << "Test OpenCV Start" << endl;
- test_parking();
- cout << "Test OpenCV End" << endl << endl;
- cout << "Train Dlib Start" << endl;
- train_parking_dlib();
- cout << "Train Dlib End" << endl << endl;
- cout << "Test Dlib Start" << endl;
- test_parking_dlib();
- cout << "Test Dlib End" << endl;
- }
- void train_parking_dlib()
- {
- std::vector<matrix<bgr_pixel>> training_images;
- matrix<bgr_pixel> image;
- std::vector<float> training_labels;
- fstream train_file("train_images_dlib.txt");
- string train_path;
- while (train_file >> train_path)
- {
- load_png(image, train_path);
- matrix<bgr_pixel> impom(80, 80);
- resize_image(image, impom);
- training_images.push_back(move(impom));
- float label = -1.0f;
- if (train_path.find("full") != std::string::npos) label = 1.0f;
- training_labels.push_back(label);
- }
- cout << "Train images: " << training_images.size() << endl;
- cout << "Train labels: " << training_labels.size() << endl;
- double lr = 0.0001;
- int batch_size = 16;
- net_type net;
- dnn_trainer<net_type> trainer(net);
- trainer.set_learning_rate(lr);
- trainer.set_learning_rate_shrink_factor(0.1);
- trainer.set_iterations_without_progress_threshold(1000000);
- trainer.set_test_iterations_without_progress_threshold(1000000);
- trainer.set_mini_batch_size(batch_size);
- /*trainer.set_max_num_epochs(40);
- trainer.be_verbose();
- trainer.train(training_images, training_labels);
- */
- trainer.be_quiet();
- std::vector<matrix<bgr_pixel>> validation_images;
- std::vector<float> validation_labels;
- double val = 0.1;
- int val_segment;
- int es_max_epochs = 5;
- double es_min_test_loss = 999;
- bool train_flag = true;
- std::vector<matrix<bgr_pixel>> validation_mini_batch_samples;
- std::vector<float> validation_mini_batch_labels;
- std::vector<matrix<bgr_pixel>> mini_batch_samples;
- std::vector<float> mini_batch_labels;
- val_segment = training_images.size() * val;
- for (int j = 0; j < val_segment; j++)
- {
- validation_images.push_back(training_images.back());
- training_images.pop_back();
- validation_labels.push_back(training_labels.back());
- training_labels.pop_back();
- }
- std::vector<matrix<bgr_pixel>> training_images_copy;
- std::vector<float> training_labels_copy;
- std::vector<matrix<bgr_pixel>> validation_images_copy;
- std::vector<float> validation_labels_copy;
- dlib::rand rnd(time(0));
- int epoch_step_calls = 0;
- int epoch_counter = 1;
- int es_counter = 0;
- while (train_flag)
- {
- training_images_copy = training_images;
- training_labels_copy = training_labels;
- int iterations = training_images_copy.size();
- for (int i = 0; i < iterations; i++) {
- int a = std::rand() % iterations;
- int b = std::rand() % iterations;
- matrix<bgr_pixel> pom = training_images_copy[a];
- training_images_copy[a] = training_images_copy[b];
- training_images_copy[b] = pom;
- float pom2 = training_labels_copy[a];
- training_labels_copy[a] = training_labels_copy[b];
- training_labels_copy[b] = pom2;
- }
- for (int i = 1; i <= iterations; i++)
- {
- mini_batch_samples.push_back(training_images_copy.back());
- training_images_copy.pop_back();
- mini_batch_labels.push_back(training_labels_copy.back());
- training_labels_copy.pop_back();
- if (mini_batch_samples.size() % batch_size == 0 || i == iterations)
- {
- trainer.train_one_step(mini_batch_samples, mini_batch_labels);
- mini_batch_samples.clear();
- mini_batch_labels.clear();
- }
- }
- validation_images_copy = validation_images;
- validation_labels_copy = validation_labels;
- iterations = validation_images_copy.size();
- for (int i = 0; i < iterations; i++) {
- int a = std::rand() % iterations;
- int b = std::rand() % iterations;
- matrix<bgr_pixel> pom = validation_images_copy[a];
- validation_images_copy[a] = validation_images_copy[b];
- validation_images_copy[b] = pom;
- float pom2 = validation_labels_copy[a];
- validation_labels_copy[a] = validation_labels_copy[b];
- validation_labels_copy[b] = pom2;
- }
- /*test_loss = trainer.get_net().compute_loss(validation_images.begin(), validation_images.end(), validation_labels.begin());
- double train_loss = trainer.get_average_loss();
- trainer.clear_average_loss();
- if (test_loss < min_test_loss)
- {
- min_test_loss = test_loss;
- epochs_without_progress = 0;
- }
- else
- epochs_without_progress++;
- if (epochs_without_progress == ES_max_epochs)
- {
- train_flag = false;
- cout << "Early stopping" << endl;
- }
- cout << epoch_counter++ << ". Epoch - training set loss error(epoch average): " << train_loss << " - validation set loss error(end of epoch): " << test_loss << " - epochs without progress: "
- << epochs_without_progress << " - lr: " << trainer.get_learning_rate() << endl;
- */
- for (int i = 1; i <= iterations; i++)
- {
- validation_mini_batch_samples.push_back(validation_images_copy.back());
- validation_images_copy.pop_back();
- validation_mini_batch_labels.push_back(validation_labels_copy.back());
- validation_labels_copy.pop_back();
- if (validation_mini_batch_samples.size() % batch_size == 0 || i == iterations)
- {
- trainer.test_one_step(validation_mini_batch_samples, validation_mini_batch_labels);
- epoch_step_calls++;
- validation_mini_batch_samples.clear();
- validation_mini_batch_labels.clear();
- }
- }
- double train_loss = trainer.get_average_loss();
- double test_loss = trainer.get_average_test_loss();
- trainer.clear_average_loss();
- int epochs_without_progress = round((float)trainer.get_test_steps_without_progress() / (float)epoch_step_calls) - 1;
- if (epochs_without_progress == -1)
- epochs_without_progress = 0;
- if (epochs_without_progress >= 5)
- {
- train_flag = false;
- cout << "Dlib Early stopping" << endl;
- }
- cout << epoch_counter++ << ". Epoch - training set loss error(epoch average): " << train_loss << " - validation set loss error(moving average): " << test_loss << " - epochs without progress: "
- << epochs_without_progress << " - test step calls: " << trainer.get_test_one_step_calls() << " - lr: " << trainer.get_learning_rate() << endl;
- epoch_step_calls = 0;
- }
- net.clean();
- serialize("GoogLeNet.dat") << net;
- }
- void test_parking_dlib()
- {
- space *spaces = new space[spaces_num];
- load_parking_geometry("parking_map.txt", spaces);
- fstream test_file("test_images.txt");
- ofstream out_label_file("out_prediction.txt");
- string test_path;
- net_type2 net2;
- deserialize("GoogLeNet.dat") >> net2;
- while (test_file >> test_path)
- {
- Mat frame = imread(test_path, 1);
- Mat draw_frame = frame.clone();
- //cvtColor(frame, frame, COLOR_BGR2GRAY);
- std::vector<Mat> test_images;
- extract_space(spaces, frame, test_images);
- int colNum = 0;
- for (int i = 0; i < test_images.size(); i++)
- {
- Mat pFrame = test_images[i];
- Mat test_image;
- resize(pFrame, test_image, Size(80, 80));
- cv_image<bgr_pixel> cimg(test_image);
- matrix<bgr_pixel> dlibFrame = dlib::mat(cimg);
- float predict_label2 = net2(dlibFrame);
- int predict_label;
- if (predict_label2 > 0)
- predict_label = 1;
- else if (predict_label2 <= 0)
- predict_label = 0;
- out_label_file << predict_label << endl;
- spaces[i].occup = predict_label;
- //imshow("original", pFrame);
- //imshow("test input", test_image);
- //waitKey(2);
- }
- //draw detection
- draw_detection(spaces, draw_frame);
- namedWindow("draw_frame", 0);
- imshow("draw_frame", draw_frame);
- waitKey(2);
- }
- //evaluation
- fstream detector_file("out_prediction.txt");
- fstream groundtruth_file("groundtruth.txt");
- evaluation(detector_file, groundtruth_file);
- }
- void test_parking()
- {
- space *spaces = new space[spaces_num];
- load_parking_geometry("parking_map.txt", spaces);
- fstream test_file("test_images.txt");
- ofstream out_label_file("out_prediction.txt");
- string test_path;
- while (test_file >> test_path)
- {
- //cout << "test_path: " << test_path << endl;
- Mat frame, gradX, gradY, blur;
- frame = imread(test_path, 1);
- Mat draw_frame = frame.clone();
- cvtColor(frame, frame, COLOR_BGR2GRAY);
- std::vector<Mat> test_images;
- extract_space(spaces, frame, test_images);
- int colNum = 0;
- int numEdgePixels;
- for (int i = 0; i < test_images.size(); i++)
- {
- frame = test_images[i];
- Mat gradX, gradY, blur, abs_grad_x, abs_grad_y, test_image;
- medianBlur(frame, blur, 9);
- Sobel(frame, gradX, CV_8U, 1, 0, 3);
- Sobel(frame, gradY, CV_8U, 0, 1, 3);
- convertScaleAbs(gradX, abs_grad_x);
- convertScaleAbs(gradY, abs_grad_y);
- addWeighted(abs_grad_x, 0.5, abs_grad_y, 0.5, 0, test_image);
- numEdgePixels = 0;
- for (int j = 0; j < test_image.rows; j++)
- for (int k = 0; k < test_image.cols; k++)
- {
- if (test_image.at<uchar>(j, k) > 100)
- numEdgePixels++;
- }
- //cout << numEdgePixels << endl;
- //imshow("blur", blur);
- //imshow("grad", test_image);
- int predict_label = 0;
- if (numEdgePixels > 140)
- predict_label = 1;
- out_label_file << predict_label << endl;
- spaces[i].occup = predict_label;
- //imshow("test_img", test_images[i]);
- //waitKey(2);
- }
- //draw detection
- draw_detection(spaces, draw_frame);
- namedWindow("draw_frame", 0);
- imshow("draw_frame", draw_frame);
- waitKey(2);
- }
- //evaluation
- fstream detector_file("out_prediction.txt");
- fstream groundtruth_file("groundtruth.txt");
- evaluation(detector_file, groundtruth_file);
- }
- int load_parking_geometry(const char *filename, space *spaces)
- {
- FILE *file = fopen(filename, "r");
- if (file == NULL) return -1;
- int ps_count, i, count;
- count = fscanf(file, "%d\n", &ps_count); // read count of polygons
- for (i = 0; i < ps_count; i++) {
- int p = 0;
- int poly_size;
- count = fscanf(file, "%d->", &poly_size); // read count of polygon vertexes
- int *row = new int[poly_size * 2];
- int j;
- for (j = 0; j < poly_size; j++) {
- int x, y;
- count = fscanf(file, "%d,%d;", &x, &y); // read vertex coordinates
- row[p] = x;
- row[p + 1] = y;
- p = p + 2;
- }
- spaces[i].x01 = row[0];
- spaces[i].y01 = row[1];
- spaces[i].x02 = row[2];
- spaces[i].y02 = row[3];
- spaces[i].x03 = row[4];
- spaces[i].y03 = row[5];
- spaces[i].x04 = row[6];
- spaces[i].y04 = row[7];
- //printf("}\n");
- free(row);
- count = fscanf(file, "\n"); // read end of line
- }
- fclose(file);
- return 1;
- }
- void extract_space(space *spaces, Mat in_mat, std::vector<Mat> &vector_images)
- {
- for (int x = 0; x < spaces_num; x++)
- {
- Mat src_mat(4, 2, CV_32F);
- Mat out_mat(space_size, CV_8U, 1);
- src_mat.at<float>(0, 0) = spaces[x].x01;
- src_mat.at<float>(0, 1) = spaces[x].y01;
- src_mat.at<float>(1, 0) = spaces[x].x02;
- src_mat.at<float>(1, 1) = spaces[x].y02;
- src_mat.at<float>(2, 0) = spaces[x].x03;
- src_mat.at<float>(2, 1) = spaces[x].y03;
- src_mat.at<float>(3, 0) = spaces[x].x04;
- src_mat.at<float>(3, 1) = spaces[x].y04;
- Mat dest_mat(4, 2, CV_32F);
- dest_mat.at<float>(0, 0) = 0;
- dest_mat.at<float>(0, 1) = 0;
- dest_mat.at<float>(1, 0) = out_mat.cols;
- dest_mat.at<float>(1, 1) = 0;
- dest_mat.at<float>(2, 0) = out_mat.cols;
- dest_mat.at<float>(2, 1) = out_mat.rows;
- dest_mat.at<float>(3, 0) = 0;
- dest_mat.at<float>(3, 1) = out_mat.rows;
- Mat H = findHomography(src_mat, dest_mat, 0);
- warpPerspective(in_mat, out_mat, H, space_size);
- //imshow("out_mat", out_mat);
- //waitKey(0);
- vector_images.push_back(out_mat);
- }
- }
- void draw_detection(space *spaces, Mat &frame)
- {
- int sx, sy;
- for (int i = 0; i < spaces_num; i++)
- {
- Point pt1, pt2;
- pt1.x = spaces[i].x01;
- pt1.y = spaces[i].y01;
- pt2.x = spaces[i].x03;
- pt2.y = spaces[i].y03;
- sx = (pt1.x + pt2.x) / 2;
- sy = (pt1.y + pt2.y) / 2;
- if (spaces[i].occup)
- {
- circle(frame, Point(sx, sy - 25), 12, CV_RGB(255, 0, 0), -1);
- }
- else
- {
- circle(frame, Point(sx, sy - 25), 12, CV_RGB(0, 255, 0), -1);
- }
- }
- }
- void evaluation(fstream &detectorOutputFile, fstream &groundTruthFile)
- {
- int detectorLine, groundTruthLine;
- int falsePositives = 0;
- int falseNegatives = 0;
- int truePositives = 0;
- int trueNegatives = 0;
- while (true)
- {
- if (!(detectorOutputFile >> detectorLine)) break;
- groundTruthFile >> groundTruthLine;
- int detect = detectorLine;
- int ground = groundTruthLine;
- //false positives
- if ((detect == 1) && (ground == 0))
- {
- falsePositives++;
- }
- //false negatives
- if ((detect == 0) && (ground == 1))
- {
- falseNegatives++;
- }
- //true positives
- if ((detect == 1) && (ground == 1))
- {
- truePositives++;
- }
- //true negatives
- if ((detect == 0) && (ground == 0))
- {
- trueNegatives++;
- }
- }
- cout << "falsePositives " << falsePositives << endl;
- cout << "falseNegatives " << falseNegatives << endl;
- cout << "truePositives " << truePositives << endl;
- cout << "trueNegatives " << trueNegatives << endl;
- float acc = (float)(truePositives + trueNegatives) / (float)(truePositives + trueNegatives + falsePositives + falseNegatives);
- cout << "Accuracy " << acc << endl;
- }
- void convert_to_ml(const std::vector< cv::Mat > & train_samples, cv::Mat& trainData)
- {
- //--Convert data
- const int rows = (int)train_samples.size();
- const int cols = (int)std::max(train_samples[0].cols, train_samples[0].rows);
- cv::Mat tmp(1, cols, CV_32FC1); //< used for transposition if needed
- trainData = cv::Mat(rows, cols, CV_32FC1);
- std::vector< Mat >::const_iterator itr = train_samples.begin();
- std::vector< Mat >::const_iterator end = train_samples.end();
- for (int i = 0; itr != end; ++itr, ++i)
- {
- CV_Assert(itr->cols == 1 ||
- itr->rows == 1);
- if (itr->cols == 1)
- {
- transpose(*(itr), tmp);
- tmp.copyTo(trainData.row(i));
- }
- else if (itr->rows == 1)
- {
- itr->copyTo(trainData.row(i));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement