You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

end2end_onnxruntime.ipynb 518 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "04eee3aa-c235-4bf7-a4cf-b6b2d08b0626",
  6. "metadata": {},
  7. "source": [
  8. "# Export onnx for onnxruntime end2end detect !!!"
  9. ]
  10. },
  11. {
  12. "cell_type": "code",
  13. "execution_count": 1,
  14. "id": "936876aa-146a-4100-b41e-36d4a13feb2c",
  15. "metadata": {},
  16. "outputs": [
  17. {
  18. "name": "stdout",
  19. "output_type": "stream",
  20. "text": [
  21. "Namespace(batch_size=1, conf_thres=0.35, device='cpu', dynamic=False, end2end=True, grid=True, img_size=[640, 640], include_nms=False, iou_thres=0.65, max_wh=640, simplify=True, topk_all=100, weights='weights/yolov7-d6.pt')\n",
  22. "YOLOR 🚀 v0.1-59-g33a9e01 torch 1.12.0+cu116 CPU\n",
  23. "\n",
  24. "Fusing layers... \n",
  25. "Model Summary: 539 layers, 133757052 parameters, 133757052 gradients\n",
  26. "/home/ubuntu/miniconda3/envs/torch/lib/python3.8/site-packages/torch/functional.py:478: UserWarning: torch.meshgrid: in an upcoming release, it will be required to pass the indexing argument. (Triggered internally at ../aten/src/ATen/native/TensorShape.cpp:2894.)\n",
  27. " return _VF.meshgrid(tensors, **kwargs) # type: ignore[attr-defined]\n",
  28. "\n",
  29. "Starting TorchScript export with torch 1.12.0+cu116...\n",
  30. "/home/ubuntu/work/yolo/yolov7/models/yolo.py:51: TracerWarning: Converting a tensor to a Python boolean might cause the trace to be incorrect. We can't record the data flow of Python values, so this value will be treated as a constant in the future. This means that the trace might not generalize to other inputs!\n",
  31. " if self.grid[i].shape[2:4] != x[i].shape[2:4]:\n",
  32. "TorchScript export success, saved as weights/yolov7-d6.torchscript.pt\n",
  33. "\n",
  34. "Starting ONNX export with onnx 1.12.0...\n",
  35. "onnxruntime\n",
  36. "/home/ubuntu/miniconda3/envs/torch/lib/python3.8/site-packages/torch/_tensor.py:1083: UserWarning: The .grad attribute of a Tensor that is not a leaf Tensor is being accessed. Its .grad attribute won't be populated during autograd.backward(). If you indeed want the .grad field to be populated for a non-leaf Tensor, use .retain_grad() on the non-leaf Tensor. If you access the non-leaf Tensor by mistake, make sure you access the leaf Tensor instead. See github.com/pytorch/pytorch/pull/30531 for more informations. (Triggered internally at aten/src/ATen/core/TensorBody.h:477.)\n",
  37. " return self._grad\n",
  38. "/home/ubuntu/miniconda3/envs/torch/lib/python3.8/site-packages/torch/onnx/symbolic_opset9.py:4182: UserWarning: Exporting aten::index operator of advanced indexing in opset 12 is achieved by combination of multiple ONNX operators, including Reshape, Transpose, Concat, and Gather. If indices include negative values, the exported graph will produce incorrect results.\n",
  39. " warnings.warn(\n",
  40. "\n",
  41. "Starting to simplify ONNX...\n",
  42. "ONNX export success, saved as weights/yolov7-d6.onnx\n",
  43. "CoreML export failure: No module named 'coremltools'\n",
  44. "\n",
  45. "Export complete (18.38s). Visualize with https://github.com/lutzroeder/netron.\n"
  46. ]
  47. }
  48. ],
  49. "source": [
  50. "# export onnx first\n",
  51. "!python export.py --weights weights/yolov7-d6.pt \\\n",
  52. " --grid --end2end --simplify \\\n",
  53. " --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 \\\n",
  54. " --max-wh 640 # For onnxruntime, you need to specify this value as an integer, when it is 0 it means agnostic NMS, \n",
  55. " # otherwise it is non-agnostic NMS"
  56. ]
  57. },
  58. {
  59. "cell_type": "code",
  60. "execution_count": 2,
  61. "id": "6ec4c01e-dac9-417e-b4cf-7c6440e274e9",
  62. "metadata": {},
  63. "outputs": [],
  64. "source": [
  65. "import cv2\n",
  66. "import time\n",
  67. "import requests\n",
  68. "import random\n",
  69. "import numpy as np\n",
  70. "import onnxruntime as ort\n",
  71. "from PIL import Image\n",
  72. "from pathlib import Path\n",
  73. "from collections import OrderedDict,namedtuple"
  74. ]
  75. },
  76. {
  77. "cell_type": "code",
  78. "execution_count": 3,
  79. "id": "06a9a121-40a2-4eb6-8a79-94894a01915a",
  80. "metadata": {},
  81. "outputs": [],
  82. "source": [
  83. "cuda = False\n",
  84. "w = \"weights/yolov7-d6.onnx\""
  85. ]
  86. },
  87. {
  88. "cell_type": "code",
  89. "execution_count": 4,
  90. "id": "007a7721-c49d-4713-94c6-4a57790acabd",
  91. "metadata": {},
  92. "outputs": [],
  93. "source": [
  94. "providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']\n",
  95. "session = ort.InferenceSession(w, providers=providers)"
  96. ]
  97. },
  98. {
  99. "cell_type": "code",
  100. "execution_count": 5,
  101. "id": "6c7a8ce1-5026-4870-8705-61399c6b7609",
  102. "metadata": {},
  103. "outputs": [],
  104. "source": [
  105. "def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleup=True, stride=32):\n",
  106. " # Resize and pad image while meeting stride-multiple constraints\n",
  107. " shape = im.shape[:2] # current shape [height, width]\n",
  108. " if isinstance(new_shape, int):\n",
  109. " new_shape = (new_shape, new_shape)\n",
  110. "\n",
  111. " # Scale ratio (new / old)\n",
  112. " r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])\n",
  113. " if not scaleup: # only scale down, do not scale up (for better val mAP)\n",
  114. " r = min(r, 1.0)\n",
  115. "\n",
  116. " # Compute padding\n",
  117. " new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))\n",
  118. " dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1] # wh padding\n",
  119. "\n",
  120. " if auto: # minimum rectangle\n",
  121. " dw, dh = np.mod(dw, stride), np.mod(dh, stride) # wh padding\n",
  122. "\n",
  123. " dw /= 2 # divide padding into 2 sides\n",
  124. " dh /= 2\n",
  125. "\n",
  126. " if shape[::-1] != new_unpad: # resize\n",
  127. " im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)\n",
  128. " top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))\n",
  129. " left, right = int(round(dw - 0.1)), int(round(dw + 0.1))\n",
  130. " im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color) # add border\n",
  131. " return im, r, (dw, dh)"
  132. ]
  133. },
  134. {
  135. "cell_type": "code",
  136. "execution_count": 6,
  137. "id": "fdf1c66b-37bf-4c94-9005-2338331cf73d",
  138. "metadata": {},
  139. "outputs": [],
  140. "source": [
  141. "names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', \n",
  142. " 'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', \n",
  143. " 'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', \n",
  144. " 'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', \n",
  145. " 'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', \n",
  146. " 'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', \n",
  147. " 'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', \n",
  148. " 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', \n",
  149. " 'hair drier', 'toothbrush']\n",
  150. "colors = {name:[random.randint(0, 255) for _ in range(3)] for i,name in enumerate(names)}"
  151. ]
  152. },
  153. {
  154. "cell_type": "code",
  155. "execution_count": 7,
  156. "id": "b9ce7a13-31b8-4a35-bd8d-4f0debd46480",
  157. "metadata": {},
  158. "outputs": [
  159. {
  160. "data": {
  161. "text/plain": [
  162. "(1, 3, 640, 640)"
  163. ]
  164. },
  165. "execution_count": 7,
  166. "metadata": {},
  167. "output_type": "execute_result"
  168. }
  169. ],
  170. "source": [
  171. "url = 'https://oneflow-static.oss-cn-beijing.aliyuncs.com/tripleMu/image1.jpg'\n",
  172. "file = requests.get(url)\n",
  173. "img = cv2.imdecode(np.frombuffer(file.content, np.uint8), 1)\n",
  174. "img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
  175. "\n",
  176. "image = img.copy()\n",
  177. "image, ratio, dwdh = letterbox(image, auto=False)\n",
  178. "image = image.transpose((2, 0, 1))\n",
  179. "image = np.expand_dims(image, 0)\n",
  180. "image = np.ascontiguousarray(image)\n",
  181. "\n",
  182. "im = image.astype(np.float32)\n",
  183. "im /= 255\n",
  184. "im.shape"
  185. ]
  186. },
  187. {
  188. "cell_type": "code",
  189. "execution_count": 8,
  190. "id": "c382a4d2-b37a-40be-9618-653419319fde",
  191. "metadata": {},
  192. "outputs": [
  193. {
  194. "data": {
  195. "text/plain": [
  196. "['output']"
  197. ]
  198. },
  199. "execution_count": 8,
  200. "metadata": {},
  201. "output_type": "execute_result"
  202. }
  203. ],
  204. "source": [
  205. "outname = [i.name for i in session.get_outputs()]\n",
  206. "outname"
  207. ]
  208. },
  209. {
  210. "cell_type": "code",
  211. "execution_count": 9,
  212. "id": "b448209b-3b92-4a48-9a55-134590e717d5",
  213. "metadata": {},
  214. "outputs": [
  215. {
  216. "data": {
  217. "text/plain": [
  218. "['images']"
  219. ]
  220. },
  221. "execution_count": 9,
  222. "metadata": {},
  223. "output_type": "execute_result"
  224. }
  225. ],
  226. "source": [
  227. "inname = [i.name for i in session.get_inputs()]\n",
  228. "inname"
  229. ]
  230. },
  231. {
  232. "cell_type": "code",
  233. "execution_count": 10,
  234. "id": "ef8bc01f-a7c6-47e0-93ed-42f41f631fee",
  235. "metadata": {},
  236. "outputs": [],
  237. "source": [
  238. "inp = {inname[0]:im}"
  239. ]
  240. },
  241. {
  242. "cell_type": "code",
  243. "execution_count": 11,
  244. "id": "c0a50aee-fa52-4b6e-aa92-bbb1f12d5652",
  245. "metadata": {},
  246. "outputs": [],
  247. "source": [
  248. "outputs = session.run(outname, inp)[0]"
  249. ]
  250. },
  251. {
  252. "cell_type": "code",
  253. "execution_count": 12,
  254. "id": "9d7d69af-bcd4-45e6-8a53-0a49b6d7c586",
  255. "metadata": {},
  256. "outputs": [
  257. {
  258. "data": {
  259. "text/plain": [
  260. "array([[0.0000000e+00, 2.4857600e+02, 1.2993985e+02, 6.2988782e+02,\n",
  261. " 5.8161853e+02, 0.0000000e+00, 9.6787709e-01],\n",
  262. " [0.0000000e+00, 2.4103851e+00, 5.6784058e+01, 3.9849658e+02,\n",
  263. " 5.8172839e+02, 0.0000000e+00, 9.6493036e-01],\n",
  264. " [0.0000000e+00, 1.9663359e+02, 2.5155685e+02, 2.5495015e+02,\n",
  265. " 4.7303458e+02, 2.7000000e+01, 8.8971210e-01],\n",
  266. " [0.0000000e+00, 2.5648334e+02, 2.5891159e+02, 2.9980090e+02,\n",
  267. " 2.9901724e+02, 5.5000000e+01, 5.1295620e-01]], dtype=float32)"
  268. ]
  269. },
  270. "execution_count": 12,
  271. "metadata": {},
  272. "output_type": "execute_result"
  273. }
  274. ],
  275. "source": [
  276. "outputs"
  277. ]
  278. },
  279. {
  280. "cell_type": "code",
  281. "execution_count": 13,
  282. "id": "f4385c28-1b6c-4c61-a876-fd155d7df915",
  283. "metadata": {},
  284. "outputs": [],
  285. "source": [
  286. "ori_images = [img.copy()]"
  287. ]
  288. },
  289. {
  290. "cell_type": "code",
  291. "execution_count": 14,
  292. "id": "d30904c5-1089-4a2a-a464-d446235ee9fc",
  293. "metadata": {},
  294. "outputs": [],
  295. "source": [
  296. "for i,(batch_id,x0,y0,x1,y1,cls_id,score) in enumerate(outputs):\n",
  297. " image = ori_images[int(batch_id)]\n",
  298. " box = np.array([x0,y0,x1,y1])\n",
  299. " box -= np.array(dwdh*2)\n",
  300. " box /= ratio\n",
  301. " box = box.round().astype(np.int32).tolist()\n",
  302. " cls_id = int(cls_id)\n",
  303. " score = round(float(score),3)\n",
  304. " name = names[cls_id]\n",
  305. " color = colors[name]\n",
  306. " name += ' '+str(score)\n",
  307. " cv2.rectangle(image,box[:2],box[2:],color,2)\n",
  308. " cv2.putText(image,name,(box[0], box[1] - 2),cv2.FONT_HERSHEY_SIMPLEX,0.75,[225, 255, 255],thickness=2) "
  309. ]
  310. },
  311. {
  312. "cell_type": "code",
  313. "execution_count": 15,
  314. "id": "b4449198-3c2b-41d6-9a23-de7accf73d82",
  315. "metadata": {},
  316. "outputs": [
  317. {
  318. "data": {

随着人工智能和大数据的发展,任一方面对自动化工具有着一定的需求,在当下疫情防控期间,使用mindspore来实现yolo模型来进行目标检测及语义分割,对视频或图片都可以进行口罩佩戴检测和行人社交距离检测,来对公共场所的疫情防控来实行自动化管理。