test_gapi_infer.py 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. #!/usr/bin/env python
  2. import numpy as np
  3. import cv2 as cv
  4. import os
  5. import sys
  6. import unittest
  7. from tests_common import NewOpenCVTests
  8. try:
  9. if sys.version_info[:2] < (3, 0):
  10. raise unittest.SkipTest('Python 2.x is not supported')
  11. class test_gapi_infer(NewOpenCVTests):
  12. def infer_reference_network(self, model_path, weights_path, img):
  13. net = cv.dnn.readNetFromModelOptimizer(model_path, weights_path)
  14. net.setPreferableBackend(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE)
  15. net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
  16. blob = cv.dnn.blobFromImage(img)
  17. net.setInput(blob)
  18. return net.forward(net.getUnconnectedOutLayersNames())
  19. def make_roi(self, img, roi):
  20. return img[roi[1]:roi[1] + roi[3], roi[0]:roi[0] + roi[2], ...]
  21. def test_age_gender_infer(self):
  22. # NB: Check IE
  23. if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
  24. return
  25. root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013'
  26. model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  27. weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  28. device_id = 'CPU'
  29. img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  30. img = cv.resize(cv.imread(img_path), (62,62))
  31. # OpenCV DNN
  32. dnn_age, dnn_gender = self.infer_reference_network(model_path, weights_path, img)
  33. # OpenCV G-API
  34. g_in = cv.GMat()
  35. inputs = cv.GInferInputs()
  36. inputs.setInput('data', g_in)
  37. outputs = cv.gapi.infer("net", inputs)
  38. age_g = outputs.at("age_conv3")
  39. gender_g = outputs.at("prob")
  40. comp = cv.GComputation(cv.GIn(g_in), cv.GOut(age_g, gender_g))
  41. pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
  42. gapi_age, gapi_gender = comp.apply(cv.gin(img), args=cv.gapi.compile_args(cv.gapi.networks(pp)))
  43. # Check
  44. self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF))
  45. self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
  46. def test_age_gender_infer_roi(self):
  47. # NB: Check IE
  48. if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
  49. return
  50. root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013'
  51. model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  52. weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  53. device_id = 'CPU'
  54. img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  55. img = cv.imread(img_path)
  56. roi = (10, 10, 62, 62)
  57. # OpenCV DNN
  58. dnn_age, dnn_gender = self.infer_reference_network(model_path,
  59. weights_path,
  60. self.make_roi(img, roi))
  61. # OpenCV G-API
  62. g_in = cv.GMat()
  63. g_roi = cv.GOpaqueT(cv.gapi.CV_RECT)
  64. inputs = cv.GInferInputs()
  65. inputs.setInput('data', g_in)
  66. outputs = cv.gapi.infer("net", g_roi, inputs)
  67. age_g = outputs.at("age_conv3")
  68. gender_g = outputs.at("prob")
  69. comp = cv.GComputation(cv.GIn(g_in, g_roi), cv.GOut(age_g, gender_g))
  70. pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
  71. gapi_age, gapi_gender = comp.apply(cv.gin(img, roi), args=cv.gapi.compile_args(cv.gapi.networks(pp)))
  72. # Check
  73. self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF))
  74. self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
  75. def test_age_gender_infer_roi_list(self):
  76. # NB: Check IE
  77. if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
  78. return
  79. root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013'
  80. model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  81. weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  82. device_id = 'CPU'
  83. rois = [(10, 15, 62, 62), (23, 50, 62, 62), (14, 100, 62, 62), (80, 50, 62, 62)]
  84. img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  85. img = cv.imread(img_path)
  86. # OpenCV DNN
  87. dnn_age_list = []
  88. dnn_gender_list = []
  89. for roi in rois:
  90. age, gender = self.infer_reference_network(model_path,
  91. weights_path,
  92. self.make_roi(img, roi))
  93. dnn_age_list.append(age)
  94. dnn_gender_list.append(gender)
  95. # OpenCV G-API
  96. g_in = cv.GMat()
  97. g_rois = cv.GArrayT(cv.gapi.CV_RECT)
  98. inputs = cv.GInferInputs()
  99. inputs.setInput('data', g_in)
  100. outputs = cv.gapi.infer("net", g_rois, inputs)
  101. age_g = outputs.at("age_conv3")
  102. gender_g = outputs.at("prob")
  103. comp = cv.GComputation(cv.GIn(g_in, g_rois), cv.GOut(age_g, gender_g))
  104. pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
  105. gapi_age_list, gapi_gender_list = comp.apply(cv.gin(img, rois),
  106. args=cv.gapi.compile_args(cv.gapi.networks(pp)))
  107. # Check
  108. for gapi_age, gapi_gender, dnn_age, dnn_gender in zip(gapi_age_list,
  109. gapi_gender_list,
  110. dnn_age_list,
  111. dnn_gender_list):
  112. self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF))
  113. self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
  114. def test_age_gender_infer2_roi(self):
  115. # NB: Check IE
  116. if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
  117. return
  118. root_path = '/omz_intel_models/intel/age-gender-recognition-retail-0013/FP32/age-gender-recognition-retail-0013'
  119. model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  120. weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  121. device_id = 'CPU'
  122. rois = [(10, 15, 62, 62), (23, 50, 62, 62), (14, 100, 62, 62), (80, 50, 62, 62)]
  123. img_path = self.find_file('cv/face/david2.jpg', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  124. img = cv.imread(img_path)
  125. # OpenCV DNN
  126. dnn_age_list = []
  127. dnn_gender_list = []
  128. for roi in rois:
  129. age, gender = self.infer_reference_network(model_path,
  130. weights_path,
  131. self.make_roi(img, roi))
  132. dnn_age_list.append(age)
  133. dnn_gender_list.append(gender)
  134. # OpenCV G-API
  135. g_in = cv.GMat()
  136. g_rois = cv.GArrayT(cv.gapi.CV_RECT)
  137. inputs = cv.GInferListInputs()
  138. inputs.setInput('data', g_rois)
  139. outputs = cv.gapi.infer2("net", g_in, inputs)
  140. age_g = outputs.at("age_conv3")
  141. gender_g = outputs.at("prob")
  142. comp = cv.GComputation(cv.GIn(g_in, g_rois), cv.GOut(age_g, gender_g))
  143. pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
  144. gapi_age_list, gapi_gender_list = comp.apply(cv.gin(img, rois),
  145. args=cv.gapi.compile_args(cv.gapi.networks(pp)))
  146. # Check
  147. for gapi_age, gapi_gender, dnn_age, dnn_gender in zip(gapi_age_list,
  148. gapi_gender_list,
  149. dnn_age_list,
  150. dnn_gender_list):
  151. self.assertEqual(0.0, cv.norm(dnn_gender, gapi_gender, cv.NORM_INF))
  152. self.assertEqual(0.0, cv.norm(dnn_age, gapi_age, cv.NORM_INF))
  153. def test_person_detection_retail_0013(self):
  154. # NB: Check IE
  155. if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
  156. return
  157. root_path = '/omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013'
  158. model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  159. weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  160. img_path = self.find_file('gpu/lbpcascade/er.png', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  161. device_id = 'CPU'
  162. img = cv.resize(cv.imread(img_path), (544, 320))
  163. # OpenCV DNN
  164. net = cv.dnn.readNetFromModelOptimizer(model_path, weights_path)
  165. net.setPreferableBackend(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE)
  166. net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
  167. blob = cv.dnn.blobFromImage(img)
  168. def parseSSD(detections, size):
  169. h, w = size
  170. bboxes = []
  171. detections = detections.reshape(-1, 7)
  172. for sample_id, class_id, confidence, xmin, ymin, xmax, ymax in detections:
  173. if confidence >= 0.5:
  174. x = int(xmin * w)
  175. y = int(ymin * h)
  176. width = int(xmax * w - x)
  177. height = int(ymax * h - y)
  178. bboxes.append((x, y, width, height))
  179. return bboxes
  180. net.setInput(blob)
  181. dnn_detections = net.forward()
  182. dnn_boxes = parseSSD(np.array(dnn_detections), img.shape[:2])
  183. # OpenCV G-API
  184. g_in = cv.GMat()
  185. inputs = cv.GInferInputs()
  186. inputs.setInput('data', g_in)
  187. g_sz = cv.gapi.streaming.size(g_in)
  188. outputs = cv.gapi.infer("net", inputs)
  189. detections = outputs.at("detection_out")
  190. bboxes = cv.gapi.parseSSD(detections, g_sz, 0.5, False, False)
  191. comp = cv.GComputation(cv.GIn(g_in), cv.GOut(bboxes))
  192. pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
  193. gapi_boxes = comp.apply(cv.gin(img.astype(np.float32)),
  194. args=cv.gapi.compile_args(cv.gapi.networks(pp)))
  195. # Comparison
  196. self.assertEqual(0.0, cv.norm(np.array(dnn_boxes).flatten(),
  197. np.array(gapi_boxes).flatten(),
  198. cv.NORM_INF))
  199. def test_person_detection_retail_0013(self):
  200. # NB: Check IE
  201. if not cv.dnn.DNN_TARGET_CPU in cv.dnn.getAvailableTargets(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE):
  202. return
  203. root_path = '/omz_intel_models/intel/person-detection-retail-0013/FP32/person-detection-retail-0013'
  204. model_path = self.find_file(root_path + '.xml', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  205. weights_path = self.find_file(root_path + '.bin', [os.environ.get('OPENCV_DNN_TEST_DATA_PATH')])
  206. img_path = self.find_file('gpu/lbpcascade/er.png', [os.environ.get('OPENCV_TEST_DATA_PATH')])
  207. device_id = 'CPU'
  208. img = cv.resize(cv.imread(img_path), (544, 320))
  209. # OpenCV DNN
  210. net = cv.dnn.readNetFromModelOptimizer(model_path, weights_path)
  211. net.setPreferableBackend(cv.dnn.DNN_BACKEND_INFERENCE_ENGINE)
  212. net.setPreferableTarget(cv.dnn.DNN_TARGET_CPU)
  213. blob = cv.dnn.blobFromImage(img)
  214. def parseSSD(detections, size):
  215. h, w = size
  216. bboxes = []
  217. detections = detections.reshape(-1, 7)
  218. for sample_id, class_id, confidence, xmin, ymin, xmax, ymax in detections:
  219. if confidence >= 0.5:
  220. x = int(xmin * w)
  221. y = int(ymin * h)
  222. width = int(xmax * w - x)
  223. height = int(ymax * h - y)
  224. bboxes.append((x, y, width, height))
  225. return bboxes
  226. net.setInput(blob)
  227. dnn_detections = net.forward()
  228. dnn_boxes = parseSSD(np.array(dnn_detections), img.shape[:2])
  229. # OpenCV G-API
  230. g_in = cv.GMat()
  231. inputs = cv.GInferInputs()
  232. inputs.setInput('data', g_in)
  233. g_sz = cv.gapi.streaming.size(g_in)
  234. outputs = cv.gapi.infer("net", inputs)
  235. detections = outputs.at("detection_out")
  236. bboxes = cv.gapi.parseSSD(detections, g_sz, 0.5, False, False)
  237. comp = cv.GComputation(cv.GIn(g_in), cv.GOut(bboxes))
  238. pp = cv.gapi.ie.params("net", model_path, weights_path, device_id)
  239. gapi_boxes = comp.apply(cv.gin(img.astype(np.float32)),
  240. args=cv.gapi.compile_args(cv.gapi.networks(pp)))
  241. # Comparison
  242. self.assertEqual(0.0, cv.norm(np.array(dnn_boxes).flatten(),
  243. np.array(gapi_boxes).flatten(),
  244. cv.NORM_INF))
  245. except unittest.SkipTest as e:
  246. message = str(e)
  247. class TestSkip(unittest.TestCase):
  248. def setUp(self):
  249. self.skipTest('Skip tests: ' + message)
  250. def test_skip():
  251. pass
  252. pass
  253. if __name__ == '__main__':
  254. NewOpenCVTests.bootstrap()