Guest User

Untitled

a guest
Mar 23rd, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.03 KB | None | 0 0
  1. #include<iostream>
  2. #include<opencv2/opencv.hpp>
  3. std::vector<std::string> GetTrainFiles(std::string filename) {
  4. std::vector<std::string> ret;
  5. std::fstream fin(filename, std::ios::in);
  6. if (fin.is_open() == false) {
  7. std::cout << "Incorrect file path" << std::endl;
  8. exit(1);
  9. }
  10. while (fin.eof() == false) {
  11. std::string line;
  12. std::getline(fin, line);
  13. if (line.length() == 0) {
  14. break;
  15. }
  16. ret.push_back(line);
  17. }
  18. fin.close();
  19. return ret;
  20. }
  21. std::vector<float> IOU(std::pair<float, float> val, std::vector<std::pair<float, float>> centroids) {
  22. std::vector<float> similarities;
  23. for (size_t i = 0; i < centroids.size(); i++) {
  24. float s = 0.0F;
  25. float& w = val.first;
  26. float& h = val.second;
  27. float& cw = centroids[i].first;
  28. float& ch = centroids[i].second;
  29. if (cw > w && ch > h) {
  30. s = (w*h) / (cw*ch);
  31. } else if (cw > w && ch <= h) {
  32. s = w*ch / (w*h + (cw - w)*ch);
  33. } else if (cw <= w && ch >= h) {
  34. s = cw*h / (w*h + cw*(ch - h));
  35. } else {
  36. s = (cw*ch) / (w*h);
  37. }
  38. similarities.push_back(1.0F-s);
  39. }
  40. return similarities;
  41. }
  42. std::vector<std::pair<float, float>> KMeans(std::vector<std::pair<float, float>> X, std::vector<std::pair<float, float>> centroids){
  43. std::vector<std::pair<float, float>> ret;
  44. size_t N = X.size();
  45. size_t K = centroids.size();
  46. std::vector<std::vector<float>> D;
  47. std::vector<int> prev_assignments;
  48. while (true) {
  49. for (int i = 0; i < N; i++) {
  50. std::vector<float> d = IOU(X[i], centroids);
  51. D.push_back(d);
  52. }
  53. std::vector<int> assignments;
  54. for (int i = 0; i < N; i++) {
  55. int a = static_cast<int>(std::min_element(D[i].begin(), D[i].end()) - D[i].begin());
  56. assignments.push_back(a);
  57. }
  58. if (assignments == prev_assignments) {
  59. ret = centroids;
  60. break;
  61. }
  62. std::vector<std::pair<float, float>> centroid_sums;
  63. centroid_sums.assign(K, std::pair<float,float>(0.0F,0.0F));
  64. for (int i = 0; i < N; i++) {
  65. centroid_sums[assignments[i]].first += X[i].first;
  66. centroid_sums[assignments[i]].second += X[i].second;
  67. }
  68. for (int i = 0; i < K; i++) {
  69. size_t cnt = std::max(std::count(assignments.begin(), assignments.end(), i), 1LL);
  70. centroids[i].first = centroid_sums[i].first / cnt;
  71. centroids[i].second = centroid_sums[i].second / cnt;
  72. }
  73. prev_assignments = assignments;
  74. }
  75.  
  76. std::sort(ret.begin(), ret.end());
  77. return ret;
  78. }
  79.  
  80. int main() {
  81. const int NUM_CLUSTERS = 7;
  82. const int WH = 416;
  83. std::string train_file = "train.txt";
  84. std::vector<std::string> files = GetTrainFiles(train_file);
  85. std::vector<std::pair<float, float>> annotation_dims;
  86. for (auto&file : files) {
  87. file=file.substr(0, file.find_last_of('.')) + ".txt";
  88. std::fstream fin(file, std::ios::in);
  89. if (fin.is_open() == false) {
  90. std::cerr << "No files" << std::endl;
  91. exit(1);
  92. }
  93. while (fin.eof()==false) {
  94. std::string line;
  95. std::getline(fin, line);
  96. if (line.length() == 0)break;
  97. std::istringstream iss;
  98. iss.str(line);
  99. float c, x, y, w, h;
  100. iss >> c >> x >> y >> w >> h;
  101. annotation_dims.push_back(std::make_pair(w, h));
  102. }
  103. fin.close();
  104. }
  105. std::sort(annotation_dims.begin(), annotation_dims.end(), [](std::pair<float, float> a, std::pair<float, float> b)->bool {
  106. return a.first*a.second < b.first*b.second;
  107. });
  108. std::vector<std::pair<float, float>> centroids;
  109.  
  110. for (int i = 0; i < NUM_CLUSTERS; i++) {
  111. centroids.push_back(annotation_dims[annotation_dims.size()/(NUM_CLUSTERS+2)*(i+1)]);
  112. }
  113. centroids = KMeans(annotation_dims, centroids);
  114.  
  115. for (auto&e : centroids) {
  116. e.first *= WH / 32;
  117. e.second *= WH / 32;
  118. }
  119. std::cout.precision(3);
  120. for (int i = 0; i < centroids.size()-1; i++) {
  121. std::cout << centroids[i].first << "," << centroids[i].second << ", ";
  122. }
  123. std::cout << centroids.back().first << "," << centroids.back().second << std::endl;
  124.  
  125. cv::Mat img = cv::Mat::zeros(WH, WH, CV_8UC3) + cv::Scalar(255, 255, 255);
  126.  
  127. for (int i = 0; i < centroids.size(); i++) {
  128. cv::rectangle(img, cv::Rect(10 + i * 5, 10 + i * 5, centroids[i].first*32, centroids[i].second*32), cv::Scalar(rand() % 255, rand() % 255, rand() % 255), 2);
  129. }
  130.  
  131. cv::imshow("img", img);
  132. cv::waitKey();
  133. cv::destroyAllWindows();
  134. return 0;
  135. }
Add Comment
Please, Sign In to add comment