test_googlenet.cpp 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  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. if (targetId == DNN_TARGET_CPU_FP16)
  58. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  59. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  60. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  61. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  62. net.setPreferableTarget(targetId);
  63. if (targetId == DNN_TARGET_OPENCL)
  64. {
  65. // Initialize network for a single image in the batch but test with batch size=2.
  66. Mat inp = Mat(224, 224, CV_8UC3);
  67. randu(inp, -1, 1);
  68. net.setInput(blobFromImage(inp));
  69. net.forward();
  70. }
  71. std::vector<Mat> inpMats;
  72. inpMats.push_back( imread(_tf("googlenet_0.png")) );
  73. inpMats.push_back( imread(_tf("googlenet_1.png")) );
  74. ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
  75. net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
  76. Mat out = net.forward("prob");
  77. Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
  78. normAssert(out, ref);
  79. }
  80. TEST_P(Reproducibility_GoogLeNet, IntermediateBlobs)
  81. {
  82. const int targetId = GetParam();
  83. if (targetId == DNN_TARGET_OPENCL_FP16)
  84. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  85. if (targetId == DNN_TARGET_CPU_FP16)
  86. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  87. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  88. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  89. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  90. net.setPreferableTarget(targetId);
  91. std::vector<String> blobsNames;
  92. blobsNames.push_back("conv1/7x7_s2");
  93. blobsNames.push_back("conv1/relu_7x7");
  94. blobsNames.push_back("inception_4c/1x1");
  95. blobsNames.push_back("inception_4c/relu_1x1");
  96. std::vector<Mat> outs;
  97. Mat in = blobFromImage(imread(_tf("googlenet_0.png")), 1.0f, Size(), Scalar(), false);
  98. net.setInput(in, "data");
  99. net.forward(outs, blobsNames);
  100. CV_Assert(outs.size() == blobsNames.size());
  101. for (size_t i = 0; i < blobsNames.size(); i++)
  102. {
  103. std::string filename = blobsNames[i];
  104. std::replace( filename.begin(), filename.end(), '/', '#');
  105. Mat ref = blobFromNPY(_tf("googlenet_" + filename + ".npy"));
  106. normAssert(outs[i], ref, "", 1E-4, 1E-2);
  107. }
  108. }
  109. TEST_P(Reproducibility_GoogLeNet, SeveralCalls)
  110. {
  111. const int targetId = GetParam();
  112. if (targetId == DNN_TARGET_OPENCL_FP16)
  113. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  114. if (targetId == DNN_TARGET_CPU_FP16)
  115. applyTestTag(CV_TEST_TAG_DNN_SKIP_CPU_FP16);
  116. Net net = readNetFromCaffe(findDataFile("dnn/bvlc_googlenet.prototxt"),
  117. findDataFile("dnn/bvlc_googlenet.caffemodel", false));
  118. net.setPreferableBackend(DNN_BACKEND_OPENCV);
  119. net.setPreferableTarget(targetId);
  120. std::vector<Mat> inpMats;
  121. inpMats.push_back( imread(_tf("googlenet_0.png")) );
  122. inpMats.push_back( imread(_tf("googlenet_1.png")) );
  123. ASSERT_TRUE(!inpMats[0].empty() && !inpMats[1].empty());
  124. net.setInput(blobFromImages(inpMats, 1.0f, Size(), Scalar(), false), "data");
  125. Mat out = net.forward();
  126. Mat ref = blobFromNPY(_tf("googlenet_prob.npy"));
  127. normAssert(out, ref);
  128. std::vector<String> blobsNames;
  129. blobsNames.push_back("conv1/7x7_s2");
  130. std::vector<Mat> outs;
  131. Mat in = blobFromImage(inpMats[0], 1.0f, Size(), Scalar(), false);
  132. net.setInput(in, "data");
  133. net.forward(outs, blobsNames);
  134. CV_Assert(outs.size() == blobsNames.size());
  135. ref = blobFromNPY(_tf("googlenet_conv1#7x7_s2.npy"));
  136. normAssert(outs[0], ref, "", 1E-4, 1E-2);
  137. }
  138. INSTANTIATE_TEST_CASE_P(/**/, Reproducibility_GoogLeNet,
  139. testing::ValuesIn(getAvailableTargets(DNN_BACKEND_OPENCV)));
  140. }} // namespace