test_dnn_superres.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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 "test_precomp.hpp"
  5. namespace opencv_test { namespace {
  6. const std::string DNN_SUPERRES_DIR = "dnn_superres";
  7. const std::string IMAGE_FILENAME = "butterfly.png";
  8. /****************************************************************************************\
  9. * Test single output models *
  10. \****************************************************************************************/
  11. void runSingleModel(std::string algorithm, int scale, std::string model_filename)
  12. {
  13. SCOPED_TRACE(algorithm);
  14. Ptr <DnnSuperResImpl> dnn_sr = makePtr<DnnSuperResImpl>();
  15. std::string path = cvtest::findDataFile(DNN_SUPERRES_DIR + "/" + IMAGE_FILENAME);
  16. Mat img = imread(path);
  17. ASSERT_FALSE(img.empty()) << "Test image can't be loaded: " << path;
  18. std::string pb_path = cvtest::findDataFile(DNN_SUPERRES_DIR + "/" + model_filename);
  19. dnn_sr->readModel(pb_path);
  20. dnn_sr->setModel(algorithm, scale);
  21. ASSERT_EQ(scale, dnn_sr->getScale());
  22. ASSERT_EQ(algorithm, dnn_sr->getAlgorithm());
  23. Mat result;
  24. dnn_sr->upsample(img, result);
  25. ASSERT_FALSE(result.empty()) << "Could not perform upsampling for scale algorithm " << algorithm << " and scale factor " << scale;
  26. int new_cols = img.cols * scale;
  27. int new_rows = img.rows * scale;
  28. ASSERT_EQ(new_cols, result.cols);
  29. ASSERT_EQ(new_rows, result.rows);
  30. }
  31. TEST(CV_DnnSuperResSingleOutputTest, accuracy_espcn_2)
  32. {
  33. runSingleModel("espcn", 2, "ESPCN_x2.pb");
  34. }
  35. TEST(CV_DnnSuperResSingleOutputTest, accuracy_fsrcnn_2)
  36. {
  37. runSingleModel("fsrcnn", 2, "FSRCNN_x2.pb");
  38. }
  39. TEST(CV_DnnSuperResSingleOutputTest, accuracy_fsrcnn_3)
  40. {
  41. runSingleModel("fsrcnn", 3, "FSRCNN_x3.pb");
  42. }
  43. /****************************************************************************************\
  44. * Test multi output models *
  45. \****************************************************************************************/
  46. void runMultiModel(std::string algorithm, int scale, std::string model_filename,
  47. std::vector<int> scales, std::vector<String> node_names)
  48. {
  49. SCOPED_TRACE(algorithm);
  50. Ptr <DnnSuperResImpl> dnn_sr = makePtr<DnnSuperResImpl>();
  51. std::string path = cvtest::findDataFile(DNN_SUPERRES_DIR + "/" + IMAGE_FILENAME);
  52. Mat img = imread(path);
  53. ASSERT_FALSE(img.empty()) << "Test image can't be loaded: " << path;
  54. std::string pb_path = cvtest::findDataFile(DNN_SUPERRES_DIR + "/" + model_filename);
  55. dnn_sr->readModel(pb_path);
  56. dnn_sr->setModel(algorithm, scale);
  57. ASSERT_EQ(scale, dnn_sr->getScale());
  58. ASSERT_EQ(algorithm, dnn_sr->getAlgorithm());
  59. std::vector<Mat> outputs;
  60. dnn_sr->upsampleMultioutput(img, outputs, scales, node_names);
  61. for(unsigned int i = 0; i < outputs.size(); i++)
  62. {
  63. SCOPED_TRACE(cv::format("i=%d scale[i]=%d", i, scales[i]));
  64. ASSERT_FALSE(outputs[i].empty());
  65. int new_cols = img.cols * scales[i];
  66. int new_rows = img.rows * scales[i];
  67. EXPECT_EQ(new_cols, outputs[i].cols);
  68. EXPECT_EQ(new_rows, outputs[i].rows);
  69. }
  70. }
  71. TEST(CV_DnnSuperResMultiOutputTest, accuracy)
  72. {
  73. //LAPSRN
  74. //x4
  75. std::vector<String> names_4x {"NCHW_output_2x", "NCHW_output_4x"};
  76. std::vector<int> scales_4x {2, 4};
  77. runMultiModel("lapsrn", 4, "LapSRN_x4.pb", scales_4x, names_4x);
  78. }
  79. }}