perf_convolution3d.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. #include "perf_precomp.hpp"
  5. #include <opencv2/dnn/shape_utils.hpp>
  6. namespace opencv_test {
  7. struct Conv3DParam_t {
  8. int kernel[3];
  9. struct BlobShape { int dims[5]; } shapeIn;
  10. int outCN;
  11. int groups;
  12. int stride[3];
  13. int dilation[3];
  14. int pad[6];
  15. const char* padMode;
  16. bool hasBias;
  17. double declared_flops;
  18. };
  19. // Details: #12142
  20. static const Conv3DParam_t testConvolution3DConfigs[] = {
  21. {{3, 3, 3}, {{1, 6, 10, 38, 50}}, 6, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "VALID", true, 26956800.},
  22. {{3, 3, 3}, {{1, 2, 19, 19, 19}}, 2, 2, {2, 2, 2}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "", true, 218000.},
  23. {{3, 3, 3}, {{1, 2, 25, 19, 19}}, 2, 2, {1, 2, 2}, {1, 1, 1}, {2, 2, 2, 2, 2, 2}, "SAME", false, 545000.},
  24. {{3, 3, 3}, {{1, 11, 9, 150, 200}}, 11, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "VALID", true, 1342562760.},
  25. {{3, 3, 3}, {{1, 10, 98, 10, 10}}, 10, 1, {1, 1, 1}, {1, 1, 1}, {1, 0, 1, 1, 0,1}, "SAME", false, 53018000.},
  26. {{5, 5, 5}, {{1, 6, 19, 19, 19}}, 6, 2, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", false, 30395250.},
  27. {{5, 5, 5}, {{1, 4, 50, 19, 19}}, 4, 1, {2, 2, 2}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "VALID", false, 5893888.},
  28. {{5, 5, 5}, {{1, 3, 75, 75, 100}}, 3, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "SAME", true, 1267312500.},
  29. {{5, 5, 5}, {{1, 2, 21, 75, 100}}, 2, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", true, 116103744.},
  30. {{5, 5, 5}, {{1, 4, 40, 75, 75}}, 4, 1, {2, 2, 2}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", false, 93405312.},
  31. {{7, 7, 7}, {{1, 6, 15, 19, 19}}, 6, 1, {2, 1, 1}, {1, 1, 1}, {3, 3, 3, 3, 3, 3}, "SAME", true, 71339376.},
  32. {{7, 7, 7}, {{1, 2, 38, 38, 38}}, 2, 1, {1, 2, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", false, 44990464.},
  33. {{1, 1, 1}, {{1, 4, 9, 10, 10}}, 4, 1, {1, 1, 2}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "VALID", false, 16200.},
  34. {{3, 1, 4}, {{1, 14, 5, 10, 10}}, 14, 1, {1, 1, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "SAME", false, 2359000.},
  35. {{1, 1, 1}, {{1, 8, 1, 10, 10}}, 8, 8, {1, 1, 1}, {1, 1, 1}, {1, 1, 1, 1, 1, 1}, "", true, 58752.},
  36. {{3, 4, 2}, {{1, 4, 8, 10, 10}}, 4, 4, {1, 2, 1}, {1, 1, 1}, {0, 0, 0, 0, 0, 0}, "", true, 166752.}
  37. };
  38. struct Conv3DParamID
  39. {
  40. enum {
  41. CONV_0 = 0,
  42. CONV_100 = 16,
  43. CONV_LAST = sizeof(testConvolution3DConfigs) / sizeof(testConvolution3DConfigs[0])
  44. };
  45. int val_;
  46. Conv3DParamID(int val = 0) : val_(val) {}
  47. operator int() const { return val_; }
  48. static ::testing::internal::ParamGenerator<Conv3DParamID> all()
  49. {
  50. #if 0
  51. enum { NUM = (int)CONV_LAST };
  52. #else
  53. enum { NUM = (int)CONV_100 };
  54. #endif
  55. Conv3DParamID v_[NUM]; for (int i = 0; i < NUM; ++i) { v_[i] = Conv3DParamID(i); } // reduce generated code size
  56. return ::testing::ValuesIn(v_, v_ + NUM);
  57. }
  58. };
  59. static inline void PrintTo(const Conv3DParamID& v, std::ostream* os)
  60. {
  61. CV_Assert((int)v >= 0); CV_Assert((int)v < Conv3DParamID::CONV_LAST);
  62. const Conv3DParam_t& p = testConvolution3DConfigs[(int)v];
  63. *os << "GFLOPS=" << cv::format("%.3f", p.declared_flops * 1e-9)
  64. << ", K=[" << p.kernel[0] << " x " << p.kernel[1] << " x " << p.kernel[2] << "]"
  65. << ", IN={" << p.shapeIn.dims[0] << ", " << p.shapeIn.dims[1] << ", " << p.shapeIn.dims[2] << ", " << p.shapeIn.dims[3] << ", " << p.shapeIn.dims[4] << "}"
  66. << ", OCN=" << p.outCN;
  67. if (p.groups > 1)
  68. *os << ", G=" << p.groups;
  69. if (p.stride[0] * p.stride[1] * p.stride[2] != 1)
  70. *os << ", S=[" << p.stride[0] << " x " << p.stride[1] << " x " << p.stride[2] << "]";
  71. if (p.dilation[0] * p.dilation[1] * p.dilation[2] != 1)
  72. *os << ", D=[" << p.dilation[0] << " x " << p.dilation[1] << " x " << p.dilation[2] << "]";
  73. if (p.pad[0] != 0 && p.pad[1] != 0 && p.pad[2] != 0 &&
  74. p.pad[3] != 0 && p.pad[4] != 0 && p.pad[5] != 0)
  75. *os << ", P=(" << p.pad[0] << ", " << p.pad[3] << ") x ("
  76. << p.pad[1] << ", " << p.pad[4] << ") x ("
  77. << p.pad[2] << ", " << p.pad[5] << ")";
  78. if (!((std::string)p.padMode).empty())
  79. *os << ", PM=" << ((std::string)p.padMode);
  80. if (p.hasBias)
  81. *os << ", BIAS";
  82. }
  83. typedef tuple<Conv3DParamID, tuple<Backend, Target> > Conv3DTestParam_t;
  84. typedef TestBaseWithParam<Conv3DTestParam_t> Conv3D;
  85. PERF_TEST_P_(Conv3D, conv3d)
  86. {
  87. int test_id = (int)get<0>(GetParam());
  88. ASSERT_GE(test_id, 0); ASSERT_LT(test_id, Conv3DParamID::CONV_LAST);
  89. const Conv3DParam_t& params = testConvolution3DConfigs[test_id];
  90. double declared_flops = params.declared_flops;
  91. DictValue kernel = DictValue::arrayInt(&params.kernel[0], 3);
  92. DictValue stride = DictValue::arrayInt(&params.stride[0], 3);
  93. DictValue pad = DictValue::arrayInt(&params.pad[0], 6);
  94. DictValue dilation = DictValue::arrayInt(&params.dilation[0], 3);
  95. MatShape inputShape = MatShape(params.shapeIn.dims, params.shapeIn.dims + 5);
  96. int outChannels = params.outCN;
  97. int groups = params.groups;
  98. std::string padMode(params.padMode);
  99. bool hasBias = params.hasBias;
  100. Backend backendId = get<0>(get<1>(GetParam()));
  101. Target targetId = get<1>(get<1>(GetParam()));
  102. if (targetId != DNN_TARGET_CPU && backendId != DNN_BACKEND_CUDA)
  103. throw SkipTestException("Only CPU and CUDA is supported");
  104. int inChannels = inputShape[1];
  105. int sz[] = {outChannels, inChannels / groups, params.kernel[0], params.kernel[1], params.kernel[2]};
  106. Mat weights(5, &sz[0], CV_32F);
  107. randu(weights, -1.0f, 1.0f);
  108. LayerParams lp;
  109. lp.set("kernel_size", kernel);
  110. lp.set("pad", pad);
  111. if (!padMode.empty())
  112. lp.set("pad_mode", padMode);
  113. lp.set("stride", stride);
  114. lp.set("dilation", dilation);
  115. lp.set("num_output", outChannels);
  116. lp.set("group", groups);
  117. lp.set("bias_term", hasBias);
  118. lp.type = "Convolution";
  119. lp.name = "testLayer";
  120. lp.blobs.push_back(weights);
  121. if (hasBias)
  122. {
  123. Mat bias(1, outChannels, CV_32F);
  124. randu(bias, -1.0f, 1.0f);
  125. lp.blobs.push_back(bias);
  126. }
  127. int inpSz[] = {1, inChannels, inputShape[2], inputShape[3], inputShape[4]};
  128. Mat input(5, &inpSz[0], CV_32F);
  129. randu(input, -1.0f, 1.0f);
  130. Net net;
  131. net.addLayerToPrev(lp.name, lp.type, lp);
  132. net.setInput(input);
  133. net.setPreferableBackend(backendId);
  134. net.setPreferableTarget(targetId);
  135. Mat output = net.forward();
  136. MatShape netInputShape = shape(input);
  137. size_t weightsMemory = 0, blobsMemory = 0;
  138. net.getMemoryConsumption(netInputShape, weightsMemory, blobsMemory);
  139. int64 flops = net.getFLOPS(netInputShape);
  140. CV_Assert(flops > 0);
  141. std::cout
  142. << "IN=" << divUp(input.total() * input.elemSize(), 1u<<10) << " Kb " << netInputShape
  143. << " OUT=" << divUp(output.total() * output.elemSize(), 1u<<10) << " Kb " << shape(output)
  144. << " Weights(parameters): " << divUp(weightsMemory, 1u<<10) << " Kb"
  145. << " MFLOPS=" << flops * 1e-6 << std::endl;
  146. TEST_CYCLE()
  147. {
  148. Mat res = net.forward();
  149. }
  150. EXPECT_NEAR(flops, declared_flops, declared_flops * 1e-6);
  151. SANITY_CHECK_NOTHING();
  152. }
  153. INSTANTIATE_TEST_CASE_P(/**/, Conv3D, Combine(
  154. Conv3DParamID::all(),
  155. dnnBackendsAndTargets(false, false) // defined in ../test/test_common.hpp
  156. ));
  157. } // namespace