perf_layer.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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 Layer_Slice : public TestBaseWithParam<tuple<Backend, Target> >
  8. {
  9. template<int DIMS>
  10. void test_slice(const int* inputShape, const int* begin, const int* end)
  11. {
  12. int backendId = get<0>(GetParam());
  13. int targetId = get<1>(GetParam());
  14. Mat input(DIMS, inputShape, CV_32FC1, Scalar::all(0));
  15. for (int i = 0; i < (int)input.total(); ++i)
  16. input.ptr<float>()[i] = (float)(i & 4095);
  17. std::vector<Range> range(DIMS);
  18. for (int i = 0; i < DIMS; ++i)
  19. range[i] = Range(begin[i], end[i]);
  20. Net net;
  21. LayerParams lp;
  22. lp.type = "Slice";
  23. lp.name = "testLayer";
  24. lp.set("begin", DictValue::arrayInt<int*>((int*)&begin[0], DIMS));
  25. lp.set("end", DictValue::arrayInt<int*>((int*)&end[0], DIMS));
  26. net.addLayerToPrev(lp.name, lp.type, lp);
  27. // warmup
  28. {
  29. net.setInput(input);
  30. net.setPreferableBackend(backendId);
  31. net.setPreferableTarget(targetId);
  32. Mat out = net.forward();
  33. EXPECT_GT(cv::norm(out, NORM_INF), 0);
  34. #if 0
  35. //normAssert(out, input(range));
  36. cout << input(range).clone().reshape(1, 1) << endl;
  37. cout << out.reshape(1, 1) << endl;
  38. #endif
  39. }
  40. TEST_CYCLE()
  41. {
  42. Mat res = net.forward();
  43. }
  44. SANITY_CHECK_NOTHING();
  45. }
  46. };
  47. PERF_TEST_P_(Layer_Slice, YOLOv4_tiny_1)
  48. {
  49. const int inputShape[4] = {1, 64, 104, 104};
  50. const int begin[] = {0, 32, 0, 0};
  51. const int end[] = {1, 64, 104, 104};
  52. test_slice<4>(inputShape, begin, end);
  53. }
  54. PERF_TEST_P_(Layer_Slice, YOLOv4_tiny_2)
  55. {
  56. const int inputShape[4] = {1, 128, 52, 52};
  57. const int begin[] = {0, 64, 0, 0};
  58. const int end[] = {1, 128, 52, 52};
  59. test_slice<4>(inputShape, begin, end);
  60. }
  61. PERF_TEST_P_(Layer_Slice, YOLOv4_tiny_3)
  62. {
  63. const int inputShape[4] = {1, 256, 26, 26};
  64. const int begin[] = {0, 128, 0, 0};
  65. const int end[] = {1, 256, 26, 26};
  66. test_slice<4>(inputShape, begin, end);
  67. }
  68. PERF_TEST_P_(Layer_Slice, FastNeuralStyle_eccv16)
  69. {
  70. const int inputShape[4] = {1, 128, 80, 100};
  71. const int begin[] = {0, 0, 2, 2};
  72. const int end[] = {1, 128, 76, 96};
  73. test_slice<4>(inputShape, begin, end);
  74. }
  75. INSTANTIATE_TEST_CASE_P(/**/, Layer_Slice, dnnBackendsAndTargets(false, false));
  76. } // namespace