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.

instance.ipynb 488 kB

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "code",
  5. "execution_count": null,
  6. "id": "failing-secret",
  7. "metadata": {},
  8. "outputs": [],
  9. "source": [
  10. "import matplotlib.pyplot as plt\n",
  11. "import torch\n",
  12. "import cv2\n",
  13. "import yaml\n",
  14. "from torchvision import transforms\n",
  15. "import numpy as np\n",
  16. "\n",
  17. "from utils.datasets import letterbox\n",
  18. "from utils.general import non_max_suppression_mask_conf\n",
  19. "\n",
  20. "from detectron2.modeling.poolers import ROIPooler\n",
  21. "from detectron2.structures import Boxes\n",
  22. "from detectron2.utils.memory import retry_if_cuda_oom\n",
  23. "from detectron2.layers import paste_masks_in_image\n"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": null,
  29. "id": "sixth-universe",
  30. "metadata": {},
  31. "outputs": [],
  32. "source": [
  33. "device = torch.device(\"cuda:0\" if torch.cuda.is_available() else \"cpu\")\n",
  34. "with open('data/hyp.scratch.mask.yaml') as f:\n",
  35. " hyp = yaml.load(f, Loader=yaml.FullLoader)\n",
  36. "weigths = torch.load('yolov7-mask.pt')\n",
  37. "model = weigths['model']\n",
  38. "model = model.half().to(device)\n",
  39. "_ = model.eval()"
  40. ]
  41. },
  42. {
  43. "cell_type": "code",
  44. "execution_count": null,
  45. "id": "respected-source",
  46. "metadata": {},
  47. "outputs": [],
  48. "source": [
  49. "image = cv2.imread('./horses.jpg') # 504x378 image\n",
  50. "image = letterbox(image, 640, stride=64, auto=True)[0]\n",
  51. "image_ = image.copy()\n",
  52. "image = transforms.ToTensor()(image)\n",
  53. "image = torch.tensor(np.array([image.numpy()]))\n",
  54. "image = image.to(device)\n",
  55. "image = image.half()\n",
  56. "\n",
  57. "output = model(image)"
  58. ]
  59. },
  60. {
  61. "cell_type": "code",
  62. "execution_count": null,
  63. "id": "cognitive-writer",
  64. "metadata": {},
  65. "outputs": [],
  66. "source": [
  67. "inf_out, train_out, attn, mask_iou, bases, sem_output = output['test'], output['bbox_and_cls'], output['attn'], output['mask_iou'], output['bases'], output['sem']"
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "execution_count": null,
  73. "id": "dedicated-helmet",
  74. "metadata": {},
  75. "outputs": [],
  76. "source": [
  77. "bases = torch.cat([bases, sem_output], dim=1)\n",
  78. "nb, _, height, width = image.shape\n",
  79. "names = model.names\n",
  80. "pooler_scale = model.pooler_scale\n",
  81. "pooler = ROIPooler(output_size=hyp['mask_resolution'], scales=(pooler_scale,), sampling_ratio=1, pooler_type='ROIAlignV2', canonical_level=2)"
  82. ]
  83. },
  84. {
  85. "cell_type": "code",
  86. "execution_count": null,
  87. "id": "martial-burner",
  88. "metadata": {},
  89. "outputs": [],
  90. "source": [
  91. "output, output_mask, output_mask_score, output_ac, output_ab = non_max_suppression_mask_conf(inf_out, attn, bases, pooler, hyp, conf_thres=0.25, iou_thres=0.65, merge=False, mask_iou=None)"
  92. ]
  93. },
  94. {
  95. "cell_type": "code",
  96. "execution_count": null,
  97. "id": "further-conditions",
  98. "metadata": {},
  99. "outputs": [],
  100. "source": [
  101. "pred, pred_masks = output[0], output_mask[0]\n",
  102. "base = bases[0]\n",
  103. "bboxes = Boxes(pred[:, :4])\n",
  104. "original_pred_masks = pred_masks.view(-1, hyp['mask_resolution'], hyp['mask_resolution'])\n",
  105. "pred_masks = retry_if_cuda_oom(paste_masks_in_image)( original_pred_masks, bboxes, (height, width), threshold=0.5)\n",
  106. "pred_masks_np = pred_masks.detach().cpu().numpy()\n",
  107. "pred_cls = pred[:, 5].detach().cpu().numpy()\n",
  108. "pred_conf = pred[:, 4].detach().cpu().numpy()\n",
  109. "nimg = image[0].permute(1, 2, 0) * 255\n",
  110. "nimg = nimg.cpu().numpy().astype(np.uint8)\n",
  111. "nimg = cv2.cvtColor(nimg, cv2.COLOR_RGB2BGR)\n",
  112. "nbboxes = bboxes.tensor.detach().cpu().numpy().astype(np.int)\n",
  113. "pnimg = nimg.copy()"
  114. ]
  115. },
  116. {
  117. "cell_type": "code",
  118. "execution_count": null,
  119. "id": "grand-thickness",
  120. "metadata": {},
  121. "outputs": [],
  122. "source": [
  123. "for one_mask, bbox, cls, conf in zip(pred_masks_np, nbboxes, pred_cls, pred_conf):\n",
  124. " if conf < 0.25:\n",
  125. " continue\n",
  126. " color = [np.random.randint(255), np.random.randint(255), np.random.randint(255)]\n",
  127. " \n",
  128. " \n",
  129. " pnimg[one_mask] = pnimg[one_mask] * 0.5 + np.array(color, dtype=np.uint8) * 0.5\n",
  130. " pnimg = cv2.rectangle(pnimg, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color, 2)\n",
  131. " #label = '%s %.3f' % (names[int(cls)], conf)\n",
  132. " #t_size = cv2.getTextSize(label, 0, fontScale=0.5, thickness=1)[0]\n",
  133. " #c2 = bbox[0] + t_size[0], bbox[1] - t_size[1] - 3\n",
  134. " #pnimg = cv2.rectangle(pnimg, (bbox[0], bbox[1]), c2, color, -1, cv2.LINE_AA) # filled\n",
  135. " #pnimg = cv2.putText(pnimg, label, (bbox[0], bbox[1] - 2), 0, 0.5, [255, 255, 255], thickness=1, lineType=cv2.LINE_AA) \n",
  136. " "
  137. ]
  138. },
  139. {
  140. "cell_type": "code",
  141. "execution_count": 9,
  142. "id": "allied-drama",
  143. "metadata": {},
  144. "outputs": [
  145. {
  146. "data": {

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