test_tflite_importer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  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. /*
  5. Test for TFLite models loading
  6. */
  7. #include "test_precomp.hpp"
  8. #include "npy_blob.hpp"
  9. #include <opencv2/dnn/layer.details.hpp> // CV_DNN_REGISTER_LAYER_CLASS
  10. #include <opencv2/dnn/utils/debug_utils.hpp>
  11. #include <opencv2/dnn/shape_utils.hpp>
  12. #ifdef OPENCV_TEST_DNN_TFLITE
  13. namespace opencv_test { namespace {
  14. using namespace cv;
  15. using namespace cv::dnn;
  16. class Test_TFLite : public DNNTestLayer {
  17. public:
  18. void testModel(Net& net, const std::string& modelName, const Mat& input, double l1 = 0, double lInf = 0);
  19. void testModel(const std::string& modelName, const Mat& input, double l1 = 0, double lInf = 0);
  20. void testModel(const std::string& modelName, const Size& inpSize, double l1 = 0, double lInf = 0);
  21. void testLayer(const std::string& modelName, double l1 = 0, double lInf = 0);
  22. };
  23. void testInputShapes(const Net& net, const std::vector<Mat>& inps) {
  24. std::vector<MatShape> inLayerShapes;
  25. std::vector<MatShape> outLayerShapes;
  26. net.getLayerShapes(MatShape(), 0, inLayerShapes, outLayerShapes);
  27. ASSERT_EQ(inLayerShapes.size(), inps.size());
  28. for (int i = 0; i < inps.size(); ++i) {
  29. ASSERT_EQ(inLayerShapes[i], shape(inps[i]));
  30. }
  31. }
  32. void Test_TFLite::testModel(Net& net, const std::string& modelName, const Mat& input, double l1, double lInf)
  33. {
  34. l1 = l1 ? l1 : default_l1;
  35. lInf = lInf ? lInf : default_lInf;
  36. net.setPreferableBackend(backend);
  37. net.setPreferableTarget(target);
  38. testInputShapes(net, {input});
  39. net.setInput(input);
  40. std::vector<String> outNames = net.getUnconnectedOutLayersNames();
  41. std::vector<Mat> outs;
  42. net.forward(outs, outNames);
  43. ASSERT_EQ(outs.size(), outNames.size());
  44. for (int i = 0; i < outNames.size(); ++i) {
  45. std::replace(outNames[i].begin(), outNames[i].end(), ':', '_');
  46. Mat ref = blobFromNPY(findDataFile(format("dnn/tflite/%s_out_%s.npy", modelName.c_str(), outNames[i].c_str())));
  47. // A workaround solution for the following cases due to inconsistent shape definitions.
  48. // The details please see: https://github.com/opencv/opencv/pull/25297#issuecomment-2039081369
  49. if (modelName == "face_landmark" || modelName == "selfie_segmentation") {
  50. ref = ref.reshape(1, 1);
  51. outs[i] = outs[i].reshape(1, 1);
  52. }
  53. normAssert(ref, outs[i], outNames[i].c_str(), l1, lInf);
  54. }
  55. }
  56. void Test_TFLite::testModel(const std::string& modelName, const Mat& input, double l1, double lInf)
  57. {
  58. Net net = readNet(findDataFile("dnn/tflite/" + modelName + ".tflite", false));
  59. testModel(net, modelName, input, l1, lInf);
  60. }
  61. void Test_TFLite::testModel(const std::string& modelName, const Size& inpSize, double l1, double lInf)
  62. {
  63. Mat input = imread(findDataFile("cv/shared/lena.png"));
  64. input = blobFromImage(input, 1.0 / 255, inpSize, 0, true);
  65. testModel(modelName, input, l1, lInf);
  66. }
  67. void Test_TFLite::testLayer(const std::string& modelName, double l1, double lInf)
  68. {
  69. Mat inp = blobFromNPY(findDataFile("dnn/tflite/" + modelName + "_inp.npy"));
  70. Net net = readNet(findDataFile("dnn/tflite/" + modelName + ".tflite"));
  71. testModel(net, modelName, inp, l1, lInf);
  72. }
  73. // https://google.github.io/mediapipe/solutions/face_mesh
  74. TEST_P(Test_TFLite, face_landmark)
  75. {
  76. if (backend == DNN_BACKEND_CUDA && target == DNN_TARGET_CUDA_FP16)
  77. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA_FP16);
  78. double l1 = 2.2e-5, lInf = 2e-4;
  79. if (target == DNN_TARGET_CPU_FP16 || target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD ||
  80. (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL))
  81. {
  82. l1 = 0.15;
  83. lInf = 0.82;
  84. }
  85. testModel("face_landmark", Size(192, 192), l1, lInf);
  86. }
  87. // https://google.github.io/mediapipe/solutions/face_detection
  88. TEST_P(Test_TFLite, face_detection_short_range)
  89. {
  90. double l1 = 0, lInf = 2e-4;
  91. if (target == DNN_TARGET_CPU_FP16 || target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD ||
  92. (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL))
  93. {
  94. l1 = 0.04;
  95. lInf = 0.8;
  96. }
  97. testModel("face_detection_short_range", Size(128, 128), l1, lInf);
  98. }
  99. // https://google.github.io/mediapipe/solutions/selfie_segmentation
  100. TEST_P(Test_TFLite, selfie_segmentation)
  101. {
  102. double l1 = 0, lInf = 0;
  103. if (target == DNN_TARGET_CPU_FP16 || target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16 || target == DNN_TARGET_MYRIAD ||
  104. (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL))
  105. {
  106. l1 = 0.01;
  107. lInf = 0.48;
  108. }
  109. testModel("selfie_segmentation", Size(256, 256), l1, lInf);
  110. }
  111. TEST_P(Test_TFLite, max_unpooling)
  112. {
  113. if (backend == DNN_BACKEND_CUDA)
  114. applyTestTag(CV_TEST_TAG_DNN_SKIP_CUDA);
  115. #if defined(INF_ENGINE_RELEASE) && INF_ENGINE_VER_MAJOR_LT(2022010000)
  116. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)
  117. applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_NGRAPH, CV_TEST_TAG_DNN_SKIP_IE_VERSION);
  118. #endif
  119. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target != DNN_TARGET_CPU) {
  120. if (target == DNN_TARGET_OPENCL_FP16) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL_FP16, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  121. if (target == DNN_TARGET_OPENCL) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_OPENCL, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  122. if (target == DNN_TARGET_MYRIAD) applyTestTag(CV_TEST_TAG_DNN_SKIP_IE_MYRIAD, CV_TEST_TAG_DNN_SKIP_IE_NGRAPH);
  123. }
  124. if (backend == DNN_BACKEND_OPENCV && target == DNN_TARGET_OPENCL_FP16)
  125. applyTestTag(CV_TEST_TAG_DNN_SKIP_OPENCL_FP16);
  126. // Due Max Unpoling is a numerically unstable operation and small difference between frameworks
  127. // might lead to positional difference of maximal elements in the tensor, this test checks
  128. // behavior of Max Unpooling layer only.
  129. Net net = readNet(findDataFile("dnn/tflite/hair_segmentation.tflite", false));
  130. net.setPreferableBackend(backend);
  131. net.setPreferableTarget(target);
  132. Mat input = imread(findDataFile("cv/shared/lena.png"));
  133. cvtColor(input, input, COLOR_BGR2RGBA);
  134. input = input.mul(Scalar(1, 1, 1, 0));
  135. input = blobFromImage(input, 1.0 / 255);
  136. testInputShapes(net, {input});
  137. net.setInput(input);
  138. std::vector<std::vector<Mat> > outs;
  139. net.forward(outs, {"p_re_lu_1", "max_pooling_with_argmax2d", "conv2d_86", "max_unpooling2d_2"});
  140. ASSERT_EQ(outs.size(), 4);
  141. ASSERT_EQ(outs[0].size(), 1);
  142. ASSERT_EQ(outs[1].size(), 2);
  143. ASSERT_EQ(outs[2].size(), 1);
  144. ASSERT_EQ(outs[3].size(), 1);
  145. Mat poolInp = outs[0][0];
  146. Mat poolOut = outs[1][0];
  147. Mat poolIds = outs[1][1];
  148. Mat unpoolInp = outs[2][0];
  149. Mat unpoolOut = outs[3][0];
  150. ASSERT_EQ(poolInp.size, unpoolOut.size);
  151. ASSERT_EQ(poolOut.size, poolIds.size);
  152. ASSERT_EQ(poolOut.size, unpoolInp.size);
  153. ASSERT_EQ(countNonZero(poolInp), poolInp.total());
  154. for (int c = 0; c < 32; ++c) {
  155. float *poolInpData = poolInp.ptr<float>(0, c);
  156. float *poolOutData = poolOut.ptr<float>(0, c);
  157. float *poolIdsData = poolIds.ptr<float>(0, c);
  158. float *unpoolInpData = unpoolInp.ptr<float>(0, c);
  159. float *unpoolOutData = unpoolOut.ptr<float>(0, c);
  160. for (int y = 0; y < 64; ++y) {
  161. for (int x = 0; x < 64; ++x) {
  162. int maxIdx = (y * 128 + x) * 2;
  163. std::vector<int> indices{maxIdx + 1, maxIdx + 128, maxIdx + 129};
  164. std::string errMsg = format("Channel %d, y: %d, x: %d", c, y, x);
  165. for (int idx : indices) {
  166. if (poolInpData[idx] > poolInpData[maxIdx]) {
  167. EXPECT_EQ(unpoolOutData[maxIdx], 0.0f) << errMsg;
  168. maxIdx = idx;
  169. }
  170. }
  171. EXPECT_EQ(poolInpData[maxIdx], poolOutData[y * 64 + x]) << errMsg;
  172. if (backend != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH) {
  173. EXPECT_EQ(poolIdsData[y * 64 + x], (float)maxIdx) << errMsg;
  174. }
  175. EXPECT_EQ(unpoolOutData[maxIdx], unpoolInpData[y * 64 + x]) << errMsg;
  176. }
  177. }
  178. }
  179. }
  180. TEST_P(Test_TFLite, EfficientDet_int8) {
  181. if (target != DNN_TARGET_CPU || (backend != DNN_BACKEND_OPENCV &&
  182. backend != DNN_BACKEND_TIMVX && backend != DNN_BACKEND_INFERENCE_ENGINE_NGRAPH)) {
  183. throw SkipTestException("Only OpenCV, TimVX and OpenVINO targets support INT8 on CPU");
  184. }
  185. Net net = readNet(findDataFile("dnn/tflite/coco_efficientdet_lite0_v1_1.0_quant_2021_09_06.tflite", false));
  186. net.setPreferableBackend(backend);
  187. net.setPreferableTarget(target);
  188. Mat img = imread(findDataFile("dnn/dog416.png"));
  189. Mat blob = blobFromImage(img, 1.0, Size(320, 320));
  190. net.setInput(blob);
  191. Mat out = net.forward();
  192. Mat_<float> ref({3, 7}, {
  193. 0, 7, 0.62890625, 0.6014542579650879, 0.13300055265426636, 0.8977657556533813, 0.292389452457428,
  194. 0, 17, 0.56640625, 0.15983937680721283, 0.35905322432518005, 0.5155506730079651, 0.9409466981887817,
  195. 0, 1, 0.5, 0.14357104897499084, 0.2240825891494751, 0.7183101177215576, 0.9140362739562988
  196. });
  197. normAssertDetections(ref, out, "", 0.5, 0.05, 0.1);
  198. }
  199. TEST_P(Test_TFLite, replicate_by_pack) {
  200. double l1 = 0, lInf = 0;
  201. if (backend == DNN_BACKEND_INFERENCE_ENGINE_NGRAPH && target == DNN_TARGET_OPENCL)
  202. {
  203. l1 = 4e-4;
  204. lInf = 2e-3;
  205. }
  206. testLayer("replicate_by_pack", l1, lInf);
  207. }
  208. TEST_P(Test_TFLite, split) {
  209. testLayer("split");
  210. }
  211. TEST_P(Test_TFLite, fully_connected) {
  212. if (backend == DNN_BACKEND_VKCOM)
  213. applyTestTag(CV_TEST_TAG_DNN_SKIP_VULKAN);
  214. testLayer("fully_connected");
  215. }
  216. TEST_P(Test_TFLite, permute) {
  217. testLayer("permutation_3d");
  218. // Temporarily disabled as TFLiteConverter produces a incorrect graph in this case
  219. //testLayer("permutation_4d_0123");
  220. testLayer("permutation_4d_0132");
  221. testLayer("permutation_4d_0213");
  222. testLayer("permutation_4d_0231");
  223. }
  224. TEST_P(Test_TFLite, global_average_pooling_2d) {
  225. testLayer("global_average_pooling_2d");
  226. }
  227. TEST_P(Test_TFLite, global_max_pooling_2d) {
  228. testLayer("global_max_pooling_2d");
  229. }
  230. TEST_P(Test_TFLite, leakyRelu) {
  231. testLayer("leakyRelu");
  232. }
  233. TEST_P(Test_TFLite, StridedSlice) {
  234. testLayer("strided_slice");
  235. }
  236. TEST_P(Test_TFLite, face_blendshapes)
  237. {
  238. Mat inp = blobFromNPY(findDataFile("dnn/tflite/face_blendshapes_inp.npy"));
  239. testModel("face_blendshapes", inp);
  240. }
  241. TEST_P(Test_TFLite, maximum)
  242. {
  243. Net net = readNetFromTFLite(findDataFile("dnn/tflite/maximum.tflite"));
  244. net.setPreferableBackend(backend);
  245. net.setPreferableTarget(target);
  246. Mat input_x = blobFromNPY(findDataFile("dnn/tflite/maximum_input_x.npy"));
  247. Mat input_y = blobFromNPY(findDataFile("dnn/tflite/maximum_input_y.npy"));
  248. net.setInput(input_x, "x");
  249. net.setInput(input_y, "y");
  250. Mat out = net.forward();
  251. Mat ref = blobFromNPY(findDataFile("dnn/tflite/maximum_output.npy"));
  252. double l1 = 1e-5;
  253. double lInf = 1e-4;
  254. if (target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16)
  255. {
  256. l1 = 1e-3;
  257. lInf = 1e-3;
  258. }
  259. normAssert(ref, out, "", l1, lInf);
  260. }
  261. TEST_P(Test_TFLite, minimum)
  262. {
  263. Net net = readNetFromTFLite(findDataFile("dnn/tflite/minimum.tflite"));
  264. net.setPreferableBackend(backend);
  265. net.setPreferableTarget(target);
  266. Mat input_x = blobFromNPY(findDataFile("dnn/tflite/minimum_input_x.npy"));
  267. Mat input_y = blobFromNPY(findDataFile("dnn/tflite/minimum_input_y.npy"));
  268. net.setInput(input_x, "x");
  269. net.setInput(input_y, "y");
  270. Mat out = net.forward();
  271. Mat ref = blobFromNPY(findDataFile("dnn/tflite/minimum_output.npy"));
  272. double l1 = 1e-5;
  273. double lInf = 1e-4;
  274. if (target == DNN_TARGET_CUDA_FP16 || target == DNN_TARGET_OPENCL_FP16)
  275. {
  276. l1 = 1e-3;
  277. lInf = 1e-3;
  278. }
  279. normAssert(ref, out, "", l1, lInf);
  280. }
  281. INSTANTIATE_TEST_CASE_P(/**/, Test_TFLite, dnnBackendsAndTargets());
  282. }} // namespace
  283. #endif // OPENCV_TEST_DNN_TFLITE