test_googlenet.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*M///////////////////////////////////////////////////////////////////////////////////////
  2. //
  3. // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
  4. //
  5. // By downloading, copying, installing or using the software you agree to this license.
  6. // If you do not agree to this license, do not download, install,
  7. // copy or use the software.
  8. //
  9. //
  10. // License Agreement
  11. // For Open Source Computer Vision Library
  12. //
  13. // Copyright (C) 2013, OpenCV Foundation, all rights reserved.
  14. // Third party copyrights are property of their respective owners.
  15. //
  16. // Redistribution and use in source and binary forms, with or without modification,
  17. // are permitted provided that the following conditions are met:
  18. //
  19. // * Redistribution's of source code must retain the above copyright notice,
  20. // this list of conditions and the following disclaimer.
  21. //
  22. // * Redistribution's in binary form must reproduce the above copyright notice,
  23. // this list of conditions and the following disclaimer in the documentation
  24. // and/or other materials provided with the distribution.
  25. //
  26. // * The name of the copyright holders may not be used to endorse or promote products
  27. // derived from this software without specific prior written permission.
  28. //
  29. // This software is provided by the copyright holders and contributors "as is" and
  30. // any express or implied warranties, including, but not limited to, the implied
  31. // warranties of merchantability and fitness for a particular purpose are disclaimed.
  32. // In no event shall the Intel Corporation or contributors be liable for any direct,
  33. // indirect, incidental, special, exemplary, or consequential damages
  34. // (including, but not limited to, procurement of substitute goods or services;
  35. // loss of use, data, or profits; or business interruption) however caused
  36. // and on any theory of liability, whether in contract, strict liability,
  37. // or tort (including negligence or otherwise) arising in any way out of
  38. // the use of this software, even if advised of the possibility of such damage.
  39. //
  40. //M*/
  41. #include "test_precomp.hpp"
  42. #include "npy_blob.hpp"
  43. #include <opencv2/core/ocl.hpp>
  44. #include <opencv2/ts/ocl_test.hpp>
  45. namespace opencv_test { namespace {
  46. template<typename TString>
  47. static std::string _tf(TString filename)
  48. {
  49. return (getOpenCVExtraDir() + "/dnn/") + filename;
  50. }
  51. typedef testing::TestWithParam<Target> Reproducibility_GoogLeNet;
  52. TEST_P(Reproducibility_GoogLeNet, Batching)
  53. {
  54. const int targetId = GetParam();
  55. if (targetId == DNN_TARGET_OPENCL_FP16)
  56. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  57. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  58. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  59. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  60. net.setPreferableTarget(targetId);
  61. if (targetId == DNN_TARGET_OPENCL)
  62. {
  63. // Initialize network for a single image in the batch but test with batch size=2.
  64. Mat inp = Mat(224, 224, CV_8UC3);
  65. randu(inp, -1, 1);
  66. net.setInput(blobFromImage(inp));
  67. net.forward();
  68. }
  69. std::vector<Mat> inpMats;
  70. inpMats.push_back( imread(_tf("googlenet_0.png")) );
  71. inpMats.push_back( imread(_tf("googlenet_1.png")) );
  72. ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
  73. net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
  74. Mat out = net.forward("prob");
  75. Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
  76. normAssert(out, ref);
  77. }
  78. TEST_P(Reproducibility_GoogLeNet, IntermediateBlobs)
  79. {
  80. const int targetId = GetParam();
  81. if (targetId == DNN_TARGET_OPENCL_FP16)
  82. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  83. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  84. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  85. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  86. net.setPreferableTarget(targetId);
  87. std::vector<String> blobsNames;
  88. blobsNames.push_back("conv1/7x7_s2");
  89. blobsNames.push_back("conv1/relu_7x7");
  90. blobsNames.push_back("inception_4c/1x1");
  91. blobsNames.push_back("inception_4c/relu_1x1");
  92. std::vector<Mat> outs;
  93. Mat in = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(), Scalar(), false);
  94. net.setInput(in, "data");
  95. net.forward(outs, blobsNames);
  96. CV_Assert(outs.size() == blobsNames.size());
  97. for (size_t i = 0; i < blobsNames.size(); i++)
  98. {
  99. std::string filename = blobsNames[i];
  100. std::replace( filename.begin(), filename.end(), '/', '#');
  101. Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));
  102. normAssert(outs[i], ref, "", 1E-4, 1E-2);
  103. }
  104. }
  105. TEST_P(Reproducibility_GoogLeNet, SeveralCalls)
  106. {
  107. const int targetId = GetParam();
  108. if (targetId == DNN_TARGET_OPENCL_FP16)
  109. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  110. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  111. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  112. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  113. net.setPreferableTarget(targetId);
  114. std::vector<Mat> inpMats;
  115. inpMats.push_back( imread(_tf("googlenet_0.png")) );
  116. inpMats.push_back( imread(_tf("googlenet_1.png")) );
  117. ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
  118. net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
  119. Mat out = net.forward();
  120. Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
  121. normAssert(out, ref);
  122. std::vector<String> blobsNames;
  123. blobsNames.push_back("conv1/7x7_s2");
  124. std::vector<Mat> outs;
  125. Mat in = blobFromImage(inpMats[0], 1.0f, Size(), Scalar(), false);
  126. net.setInput(in, "data");
  127. net.forward(outs, blobsNames);
  128. CV_Assert(outs.size() == blobsNames.size());
  129. ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));
  130. normAssert(outs[0], ref, "", 1E-4, 1E-2);
  131. }
  132. INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_GoogLeNet,
  133. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
  134. }} // namespace