perf_recurrent.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. namespace opencv_test {
  6. struct LstmParams {
  7. // Batch size
  8. int nrSamples;
  9. // Size of the input vector
  10. int inputSize;
  11. // Size of the internal state vector
  12. int hiddenSize;
  13. // Number of timesteps for the LSTM
  14. int nrSteps;
  15. };
  16. static inline void PrintTo(const LstmParams& params, ::std::ostream* os) {
  17. (*os) << "BATCH=" << params.nrSamples
  18. << ", IN=" << params.inputSize
  19. << ", HIDDEN=" << params.hiddenSize
  20. << ", TS=" << params.nrSteps;
  21. }
  22. static const LstmParams testLstmConfigs[] = {
  23. {1, 192, 192, 100},
  24. {1, 1024, 192, 100},
  25. {1, 64, 192, 100},
  26. {1, 192, 512, 100},
  27. {64, 192, 192, 2},
  28. {64, 1024, 192, 2},
  29. {64, 64, 192, 2},
  30. {64, 192, 512, 2},
  31. {128, 192, 192, 2},
  32. {128, 1024, 192, 2},
  33. {128, 64, 192, 2},
  34. {128, 192, 512, 2}
  35. };
  36. class Layer_LSTM : public TestBaseWithParam<LstmParams> {};
  37. PERF_TEST_P_(Layer_LSTM, lstm) {
  38. const LstmParams& params = GetParam();
  39. LayerParams lp;
  40. lp.type = "LSTM";
  41. lp.name = "testLstm";
  42. lp.set("produce_cell_output", false);
  43. lp.set("use_timestamp_dim", true);
  44. Mat weightH(params.hiddenSize * 4, params.hiddenSize, CV_32FC1, cv::Scalar(0));
  45. Mat weightX(params.hiddenSize * 4, params.inputSize, CV_32FC1, cv::Scalar(0));
  46. Mat bias(params.hiddenSize * 4, 1, CV_32FC1, cv::Scalar(0));
  47. Mat hInternal(params.nrSteps, params.hiddenSize, CV_32FC1, cv::Scalar(0));
  48. Mat cInternal(params.nrSteps, params.hiddenSize, CV_32FC1, cv::Scalar(0));
  49. lp.blobs.push_back(weightH);
  50. lp.blobs.push_back(weightX);
  51. lp.blobs.push_back(bias);
  52. lp.blobs.push_back(hInternal);
  53. lp.blobs.push_back(cInternal);
  54. std::vector<int> inputDims;
  55. inputDims.push_back(params.nrSamples);
  56. inputDims.push_back(params.nrSteps);
  57. inputDims.push_back(params.inputSize);
  58. Mat input(inputDims.size(), inputDims.data(), CV_32FC1);
  59. input = cv::Scalar(0);
  60. Net net;
  61. net.addLayerToPrev(lp.name, lp.type, lp);
  62. net.setInput(input);
  63. // Warm up
  64. std::vector<Mat> outputs(2);
  65. net.forward(outputs, "testLstm");
  66. TEST_CYCLE()
  67. {
  68. net.forward(outputs, "testLstm");
  69. }
  70. SANITY_CHECK_NOTHING();
  71. }
  72. INSTANTIATE_TEST_CASE_P(/**/, Layer_LSTM, testing::ValuesIn(testLstmConfigs));
  73. } // namespace