model_analysis.cpp 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Software License Agreement (BSD License)
  3. *
  4. * Copyright (c) 2009, Willow Garage, Inc.
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * * Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * * Redistributions in binary form must reproduce the above
  14. * copyright notice, this list of conditions and the following
  15. * disclaimer in the documentation and/or other materials provided
  16. * with the distribution.
  17. * * Neither the name of Willow Garage, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived
  19. * from this software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  28. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  29. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. */
  35. /**
  36. * @file sphereview_3dobj_demo.cpp
  37. * @brief Generating training data for CNN with triplet loss.
  38. * @author Yida Wang
  39. */
  40. #include <iostream>
  41. #include "opencv2/imgproc.hpp"
  42. #include "opencv2/cnn_3dobj.hpp"
  43. using namespace cv;
  44. using namespace cv::cnn_3dobj;
  45. int main(int argc, char** argv)
  46. {
  47. const String keys = "{help | | this demo will have an analysis on the trained model, it will print information about whether the model is suit for set different classes apart and also discriminant on object pose at the same time.}"
  48. "{caffemodel | ../../testdata/cv/3d_triplet_iter_30000.caffemodel | caffe model for feature exrtaction.}"
  49. "{network_forIMG | ../../testdata/cv/3d_triplet_testIMG.prototxt | Network definition file used for extracting feature from a single image and making a classification}"
  50. "{mean_file | no | The mean file generated by Caffe from all gallery images, this could be used for mean value substraction from all images. If you want to use the mean file, you can set this as ../data/images_mean/triplet_mean.binaryproto.}"
  51. "{target_img | ../data/images_all/4_78.png | Path of image in reference.}"
  52. "{ref_img1 | ../data/images_all/4_79.png | Path of closest image.}"
  53. "{ref_img2 | ../data/images_all/4_87.png | Path of less closer image in the same class with reference image.}"
  54. "{ref_img3 | ../data/images_all/3_78.png | Path of image with the same pose in another class.}"
  55. "{feature_blob | feat | Name of layer which will represent as the feature, in this network, ip1 or feat is well.}"
  56. "{device | CPU | device}"
  57. "{dev_id | 0 | dev_id}";
  58. /* Get parameters from comand line. */
  59. cv::CommandLineParser parser(argc, argv, keys);
  60. parser.about("Demo for object data classification and pose estimation");
  61. if (parser.has("help"))
  62. {
  63. parser.printMessage();
  64. return 0;
  65. }
  66. String caffemodel = parser.get<String>("caffemodel");
  67. String network_forIMG = parser.get<String>("network_forIMG");
  68. String mean_file = parser.get<String>("mean_file");
  69. String target_img = parser.get<String>("target_img");
  70. String ref_img1 = parser.get<String>("ref_img1");
  71. String ref_img2 = parser.get<String>("ref_img2");
  72. String ref_img3 = parser.get<String>("ref_img3");
  73. String feature_blob = parser.get<String>("feature_blob");
  74. String device = parser.get<String>("device");
  75. int dev_id = parser.get<int>("dev_id");
  76. std::vector<String> ref_img;
  77. /* Sample which is most closest in pose to reference image
  78. *and also the same class.
  79. */
  80. ref_img.push_back(ref_img1);
  81. /* Sample which is less closest in pose to reference image
  82. *and also the same class.
  83. */
  84. ref_img.push_back(ref_img2);
  85. /* Sample which is very close in pose to reference image
  86. *but not the same class.
  87. */
  88. ref_img.push_back(ref_img3);
  89. /* Initialize a net work with Device. */
  90. cv::cnn_3dobj::descriptorExtractor descriptor(device, dev_id);
  91. /* Load net with the caffe trained net work parameter and structure. */
  92. if (strcmp(mean_file.c_str(), "no") == 0)
  93. descriptor.loadNet(network_forIMG, caffemodel);
  94. else
  95. descriptor.loadNet(network_forIMG, caffemodel, mean_file);
  96. cv::Mat img_base = cv::imread(target_img, -1);
  97. if (img_base.empty())
  98. {
  99. printf("could not read reference image %s\n, make sure the path of images are set properly.", target_img.c_str());
  100. }
  101. std::vector<cv::Mat> img;
  102. for (unsigned int i = 0; i < ref_img.size(); i++)
  103. {
  104. img.push_back(cv::imread(ref_img[i], -1));
  105. if (img[i].empty()) {
  106. printf("could not read reference image %s\n, make sure the path of images are set properly.", ref_img[i].c_str());
  107. }
  108. }
  109. cv::Mat feature_test;
  110. descriptor.extract(img_base, feature_test, feature_blob);
  111. if (feature_test.empty()) {
  112. printf("could not extract feature from test image which is read into cv::Mat.");
  113. }
  114. cv::Mat feature_reference;
  115. descriptor.extract(img, feature_reference, feature_blob);
  116. if (feature_reference.empty()) {
  117. printf("could not extract feature from reference images which is already stored in vector<cv::Mat>.");
  118. }
  119. std::vector<float> matches;
  120. for (int i = 0; i < feature_reference.rows; i++)
  121. {
  122. cv::Mat distance = feature_test-feature_reference.row(i);
  123. matches.push_back(cv::norm(distance));
  124. }
  125. bool pose_pass = false;
  126. bool class_pass = false;
  127. /* Have comparations on the distance between reference image and 3 other images
  128. *distance between closest sample and reference image should be smallest and
  129. *distance between sample in another class and reference image should be largest.
  130. */
  131. if (matches[0] < matches[1] && matches[0] < matches[2])
  132. pose_pass = true;
  133. if (matches[1] < matches[2])
  134. class_pass = true;
  135. if (!pose_pass)
  136. {
  137. printf("\n =========== Model %s ========== \nIs not trained properly that the similar pose could not be tell from a cluster of features.\n", caffemodel.c_str());
  138. }
  139. else if (!class_pass)
  140. {
  141. printf("\n =========== Model %s ========== \nIs not trained properly that feature from the same class is not discriminant from the one of another class with similar pose.\n", caffemodel.c_str());
  142. }
  143. else
  144. {
  145. printf("\n =========== Model %s ========== \nSuits for setting different classes apart and also discriminant on object pose at the same time.\n", caffemodel.c_str());
  146. }
  147. return 0;
  148. }