{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "52dd366d-8533-4092-855c-7978f7d396ba",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Python version: 3.8.13 (default, Mar 28 2022, 11:38:47) \n",
      "[GCC 7.5.0], sys.version_info(major=3, minor=8, micro=13, releaselevel='final', serial=0) \n",
      "Pytorch version: 1.12.0+cu116 \n"
     ]
    }
   ],
   "source": [
    "import sys\n",
    "import torch\n",
    "print(f\"Python version: {sys.version}, {sys.version_info} \")\n",
    "print(f\"Pytorch version: {torch.__version__} \")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "1c455423-ff75-4bd1-9b49-6e9826440c58",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Thu Jul 28 16:34:36 2022       \n",
      "+-----------------------------------------------------------------------------+\n",
      "| NVIDIA-SMI 515.48.07    Driver Version: 515.48.07    CUDA Version: 11.7     |\n",
      "|-------------------------------+----------------------+----------------------+\n",
      "| GPU  Name        Persistence-M| Bus-Id        Disp.A | Volatile Uncorr. ECC |\n",
      "| Fan  Temp  Perf  Pwr:Usage/Cap|         Memory-Usage | GPU-Util  Compute M. |\n",
      "|                               |                      |               MIG M. |\n",
      "|===============================+======================+======================|\n",
      "|   0  NVIDIA GeForce ...  Off  | 00000000:01:00.0 Off |                  N/A |\n",
      "| N/A   42C    P8    16W /  N/A |     13MiB /  8192MiB |      0%      Default |\n",
      "|                               |                      |                  N/A |\n",
      "+-------------------------------+----------------------+----------------------+\n",
      "                                                                               \n",
      "+-----------------------------------------------------------------------------+\n",
      "| Processes:                                                                  |\n",
      "|  GPU   GI   CI        PID   Type   Process name                  GPU Memory |\n",
      "|        ID   ID                                                   Usage      |\n",
      "|=============================================================================|\n",
      "|    0   N/A  N/A      1463      G   /usr/lib/xorg/Xorg                  4MiB |\n",
      "|    0   N/A  N/A      2517      G   /usr/lib/xorg/Xorg                  4MiB |\n",
      "+-----------------------------------------------------------------------------+\n"
     ]
    }
   ],
   "source": [
    "!nvidia-smi"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "1c9bdd45-f5fd-4865-b060-4cca4333ce65",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "--2022-07-28 16:30:52--  https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt\n",
      "Resolving github.com (github.com)... 20.205.243.166\n",
      "Connecting to github.com (github.com)|20.205.243.166|:443... connected.\n",
      "HTTP request sent, awaiting response... 302 Found\n",
      "Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/ba7d01ee-125a-4134-8864-fa1abcbf94d5?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220728%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220728T083052Z&X-Amz-Expires=300&X-Amz-Signature=81396357980573956af703230d72bdb09561f4dc2b2d8f18e599c3a89649f1de&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=511187726&response-content-disposition=attachment%3B%20filename%3Dyolov7-tiny.pt&response-content-type=application%2Foctet-stream [following]\n",
      "--2022-07-28 16:30:53--  https://objects.githubusercontent.com/github-production-release-asset-2e65be/511187726/ba7d01ee-125a-4134-8864-fa1abcbf94d5?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20220728%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20220728T083052Z&X-Amz-Expires=300&X-Amz-Signature=81396357980573956af703230d72bdb09561f4dc2b2d8f18e599c3a89649f1de&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=511187726&response-content-disposition=attachment%3B%20filename%3Dyolov7-tiny.pt&response-content-type=application%2Foctet-stream\n",
      "Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.109.133, 185.199.108.133, ...\n",
      "Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... connected.\n",
      "HTTP request sent, awaiting response... 200 OK\n",
      "Length: 12639769 (12M) [application/octet-stream]\n",
      "Saving to: ‘yolov7-tiny.pt’\n",
      "\n",
      "yolov7-tiny.pt      100%[===================>]  12.05M   226KB/s    in 63s     \n",
      "\n",
      "2022-07-28 16:31:57 (195 KB/s) - ‘yolov7-tiny.pt’ saved [12639769/12639769]\n",
      "\n"
     ]
    }
   ],
   "source": [
    "!wget https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7-tiny.pt"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "57cea8f5-72bb-453e-a97a-af59a11231de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Namespace(batch_size=1, conf_thres=0.35, device='cpu', dynamic=False, dynamic_batch=True, end2end=True, grid=True, img_size=[640, 640], include_nms=False, iou_thres=0.65, max_wh=7680, simplify=True, topk_all=100, weights='./yolov7-tiny.pt')\n",
      "YOLOR 🚀 v0.1-74-gd77092b torch 1.12.0+cu116 CPU\n",
      "\n",
      "Fusing layers... \n",
      "Model Summary: 200 layers, 6219709 parameters, 6219709 gradients\n",
      "/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",
      "  return _VF.meshgrid(tensors, **kwargs)  # type: ignore[attr-defined]\n",
      "\n",
      "Starting TorchScript export with torch 1.12.0+cu116...\n",
      "/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",
      "  if self.grid[i].shape[2:4] != x[i].shape[2:4]:\n",
      "TorchScript export success, saved as ./yolov7-tiny.torchscript.pt\n",
      "\n",
      "Starting ONNX export with onnx 1.12.0...\n",
      "onnxruntime\n",
      "/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",
      "  return self._grad\n",
      "/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",
      "  warnings.warn(\n",
      "\n",
      "Starting to simplify ONNX...\n",
      "ONNX export success, saved as ./yolov7-tiny.onnx\n",
      "CoreML export failure: No module named 'coremltools'\n",
      "\n",
      "Export complete (2.81s). Visualize with https://github.com/lutzroeder/netron.\n",
      "builder.py\t    README.md\t      w2onnx.py\n",
      "cfg\t\t    requirements.txt  weights\n",
      "coco\t\t    runs\t      YOLO-TensorRT8\n",
      "data\t\t    scripts\t      YOLOv7-Dynamic-Batch.ipynb\n",
      "detect.py\t    test.py\t      yolov7-tiny-batch16.onnx\n",
      "export.py\t    tools\t      yolov7-tiny-batch16.plan\n",
      "figure\t\t    traced_model.pt   yolov7-tiny-batch1.onnx\n",
      "hubconf.py\t    train_aux.py      yolov7-tiny-batch1.plan\n",
      "inference\t    train.py\t      yolov7-tiny-batch32.onnx\n",
      "LICENSE.md\t    usage.md\t      yolov7-tiny-batch32.plan\n",
      "models\t\t    utils\t      yolov7-tiny.onnx\n",
      "onnx_nms_ort.ipynb  Valid_RGB\t      yolov7-tiny.pt\n",
      "paper\t\t    Valid_RGB.zip     yolov7-tiny.torchscript.pt\n"
     ]
    }
   ],
   "source": [
    "# export ONNX model for onnxruntime\n",
    "!python export.py --weights ./yolov7-tiny.pt --grid --end2end --simplify \\\n",
    "    --topk-all 100 --iou-thres 0.65 --conf-thres 0.35 \\\n",
    "    --img-size 640 640 \\\n",
    "    --dynamic-batch \\\n",
    "    --max-wh 7680\n",
    "!ls"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "6ec4c01e-dac9-417e-b4cf-7c6440e274e9",
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2\n",
    "import time\n",
    "import random\n",
    "import numpy as np\n",
    "import onnxruntime as ort\n",
    "from PIL import Image\n",
    "from pathlib import Path\n",
    "from collections import OrderedDict,namedtuple"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "06a9a121-40a2-4eb6-8a79-94894a01915a",
   "metadata": {},
   "outputs": [],
   "source": [
    "cuda = True\n",
    "w = \"yolov7-tiny.onnx\"\n",
    "imgList = [cv2.imread('inference/images/horses.jpg'),\n",
    "           cv2.imread('inference/images/bus.jpg'),\n",
    "           cv2.imread('inference/images/zidane.jpg'),\n",
    "           cv2.imread('inference/images/image1.jpg'),\n",
    "           cv2.imread('inference/images/image2.jpg'),\n",
    "           cv2.imread('inference/images/image3.jpg')]\n",
    "imgList*=6\n",
    "imgList = imgList[:32]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "007a7721-c49d-4713-94c6-4a57790acabd",
   "metadata": {},
   "outputs": [],
   "source": [
    "providers = ['CUDAExecutionProvider', 'CPUExecutionProvider'] if cuda else ['CPUExecutionProvider']\n",
    "session = ort.InferenceSession(w, providers=providers)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "fdf1c66b-37bf-4c94-9005-2338331cf73d",
   "metadata": {},
   "outputs": [],
   "source": [
    "names = ['person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus', 'train', 'truck', 'boat', 'traffic light', \n",
    "         'fire hydrant', 'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse', 'sheep', 'cow', \n",
    "         'elephant', 'bear', 'zebra', 'giraffe', 'backpack', 'umbrella', 'handbag', 'tie', 'suitcase', 'frisbee', \n",
    "         'skis', 'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove', 'skateboard', 'surfboard', \n",
    "         'tennis racket', 'bottle', 'wine glass', 'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', \n",
    "         'sandwich', 'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake', 'chair', 'couch', \n",
    "         'potted plant', 'bed', 'dining table', 'toilet', 'tv', 'laptop', 'mouse', 'remote', 'keyboard', 'cell phone', \n",
    "         'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'book', 'clock', 'vase', 'scissors', 'teddy bear', \n",
    "         'hair drier', 'toothbrush']\n",
    "colors = {name:[random.randint(0, 255) for _ in range(3)] for i,name in enumerate(names)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "bf8215aa-918e-4c5a-b67b-70b5c3f1ba15",
   "metadata": {},
   "outputs": [],
   "source": [
    "def letterbox(im, new_shape=(640, 640), color=(114, 114, 114), auto=True, scaleup=True, stride=32):\n",
    "    # Resize and pad image while meeting stride-multiple constraints\n",
    "    shape = im.shape[:2]  # current shape [height, width]\n",
    "    if isinstance(new_shape, int):\n",
    "        new_shape = (new_shape, new_shape)\n",
    "\n",
    "    # Scale ratio (new / old)\n",
    "    r = min(new_shape[0] / shape[0], new_shape[1] / shape[1])\n",
    "    if not scaleup:  # only scale down, do not scale up (for better val mAP)\n",
    "        r = min(r, 1.0)\n",
    "\n",
    "    # Compute padding\n",
    "    new_unpad = int(round(shape[1] * r)), int(round(shape[0] * r))\n",
    "    dw, dh = new_shape[1] - new_unpad[0], new_shape[0] - new_unpad[1]  # wh padding\n",
    "\n",
    "    if auto:  # minimum rectangle\n",
    "        dw, dh = np.mod(dw, stride), np.mod(dh, stride)  # wh padding\n",
    "\n",
    "    dw /= 2  # divide padding into 2 sides\n",
    "    dh /= 2\n",
    "\n",
    "    if shape[::-1] != new_unpad:  # resize\n",
    "        im = cv2.resize(im, new_unpad, interpolation=cv2.INTER_LINEAR)\n",
    "    top, bottom = int(round(dh - 0.1)), int(round(dh + 0.1))\n",
    "    left, right = int(round(dw - 0.1)), int(round(dw + 0.1))\n",
    "    im = cv2.copyMakeBorder(im, top, bottom, left, right, cv2.BORDER_CONSTANT, value=color)  # add border\n",
    "    return im, r, (dw, dh)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "b9ce7a13-31b8-4a35-bd8d-4f0debd46480",
   "metadata": {},
   "outputs": [],
   "source": [
    "origin_RGB = []\n",
    "resize_data = []\n",
    "for img in imgList:\n",
    "  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n",
    "  origin_RGB.append(img)\n",
    "  image = img.copy()\n",
    "  image, ratio, dwdh = letterbox(image, auto=False)\n",
    "  image = image.transpose((2, 0, 1))\n",
    "  image = np.expand_dims(image, 0)\n",
    "  image = np.ascontiguousarray(image)\n",
    "  im = image.astype(np.float32)\n",
    "  resize_data.append((im,ratio,dwdh))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b1cae709-f145-4c63-b846-8edd6716f06b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(32, 3, 640, 640)"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "np_batch = np.concatenate([data[0] for data in resize_data])\n",
    "np_batch.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "c382a4d2-b37a-40be-9618-653419319fde",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['output']"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "outname = [i.name for i in session.get_outputs()]\n",
    "outname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "b448209b-3b92-4a48-9a55-134590e717d5",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['images']"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "inname = [i.name for i in session.get_inputs()]\n",
    "inname"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "ef8bc01f-a7c6-47e0-93ed-42f41f631fee",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0.0000000e+00,  3.6190948e+02,  2.8389893e+02,  4.9353003e+02,\n",
       "          3.9562683e+02,  1.7000000e+01,  9.2383915e-01],\n",
       "        [ 0.0000000e+00, -1.0330048e+00,  2.6461920e+02,  2.6221228e+02,\n",
       "          4.4826050e+02,  1.7000000e+01,  9.2104465e-01],\n",
       "        [ 0.0000000e+00,  2.1545255e+02,  2.7048724e+02,  3.5087741e+02,\n",
       "          4.1111517e+02,  1.7000000e+01,  7.6392365e-01],\n",
       "        [ 0.0000000e+00, -9.6838379e-01,  2.6136188e+02,  1.2927969e+02,\n",
       "          3.3445026e+02,  1.7000000e+01,  6.9170636e-01],\n",
       "        [ 0.0000000e+00,  3.0596286e+02,  2.8082364e+02,  3.7849179e+02,\n",
       "          3.7234799e+02,  1.7000000e+01,  4.6678147e-01]], dtype=float32)]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# batch 1 infer\n",
    "im = np.ascontiguousarray(np_batch[0:1,...]/255)\n",
    "out = session.run(outname,{'images':im})\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "d0376a85-ec36-41d3-9067-ec5a8ec5a231",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0.0000000e+00,  3.6191144e+02,  2.8390021e+02,  4.9352777e+02,\n",
       "          3.9562689e+02,  1.7000000e+01,  9.2384642e-01],\n",
       "        [ 0.0000000e+00, -1.0345917e+00,  2.6461029e+02,  2.6221442e+02,\n",
       "          4.4825989e+02,  1.7000000e+01,  9.2106003e-01],\n",
       "        [ 0.0000000e+00,  2.1546349e+02,  2.7049829e+02,  3.5087964e+02,\n",
       "          4.1111389e+02,  1.7000000e+01,  7.6392013e-01],\n",
       "        [ 0.0000000e+00, -9.6724701e-01,  2.6136130e+02,  1.2928046e+02,\n",
       "          3.3446219e+02,  1.7000000e+01,  6.9178230e-01],\n",
       "        [ 0.0000000e+00,  3.0596533e+02,  2.8082968e+02,  3.7849182e+02,\n",
       "          3.7233865e+02,  1.7000000e+01,  4.6649921e-01],\n",
       "        [ 1.0000000e+00,  2.1281133e+02,  2.4224355e+02,  2.8546884e+02,\n",
       "          5.1077960e+02,  0.0000000e+00,  8.9588410e-01],\n",
       "        [ 1.0000000e+00,  1.1204082e+02,  2.3507364e+02,  2.2090996e+02,\n",
       "          5.3113416e+02,  0.0000000e+00,  8.5693455e-01],\n",
       "        [ 1.0000000e+00,  4.7646179e+02,  2.3513080e+02,  5.6024109e+02,\n",
       "          5.1906622e+02,  0.0000000e+00,  8.5674554e-01],\n",
       "        [ 1.0000000e+00,  9.0671051e+01,  1.3539163e+02,  5.3778564e+02,\n",
       "          4.4476132e+02,  5.0000000e+00,  8.3615136e-01],\n",
       "        [ 2.0000000e+00,  3.7307474e+02,  1.6083412e+02,  5.7743860e+02,\n",
       "          4.9584790e+02,  0.0000000e+00,  7.9307902e-01],\n",
       "        [ 2.0000000e+00,  2.1895935e+02,  3.5587909e+02,  2.6999200e+02,\n",
       "          4.9702692e+02,  2.7000000e+01,  5.8485562e-01],\n",
       "        [ 2.0000000e+00,  6.5187561e+01,  2.4052054e+02,  4.7870172e+02,\n",
       "          4.9418930e+02,  0.0000000e+00,  5.7696176e-01],\n",
       "        [ 3.0000000e+00,  5.3027496e+00,  6.2176086e+01,  3.7205682e+02,\n",
       "          5.8482434e+02,  0.0000000e+00,  8.7524450e-01],\n",
       "        [ 3.0000000e+00,  2.6052777e+02,  1.4254730e+02,  6.3039929e+02,\n",
       "          5.8130194e+02,  0.0000000e+00,  8.1194180e-01]], dtype=float32)]"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# batch 4 infer\n",
    "im = np.ascontiguousarray(np_batch[0:4,...]/255)\n",
    "out = session.run(outname,{'images':im})\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "c0a50aee-fa52-4b6e-aa92-bbb1f12d5652",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[array([[ 0.0000000e+00,  3.6191074e+02,  2.8389581e+02,  4.9353012e+02,\n",
       "          3.9562750e+02,  1.7000000e+01,  9.2385465e-01],\n",
       "        [ 0.0000000e+00, -1.0335236e+00,  2.6461029e+02,  2.6221289e+02,\n",
       "          4.4826691e+02,  1.7000000e+01,  9.2106324e-01],\n",
       "        [ 0.0000000e+00,  2.1546918e+02,  2.7049741e+02,  3.5089246e+02,\n",
       "          4.1111246e+02,  1.7000000e+01,  7.6381278e-01],\n",
       "        [ 0.0000000e+00, -9.6646881e-01,  2.6135922e+02,  1.2928015e+02,\n",
       "          3.3445938e+02,  1.7000000e+01,  6.9177818e-01],\n",
       "        [ 0.0000000e+00,  3.0595779e+02,  2.8082639e+02,  3.7848483e+02,\n",
       "          3.7233902e+02,  1.7000000e+01,  4.6637228e-01],\n",
       "        [ 1.0000000e+00,  2.1281424e+02,  2.4223289e+02,  2.8546671e+02,\n",
       "          5.1078430e+02,  0.0000000e+00,  8.9587235e-01],\n",
       "        [ 1.0000000e+00,  4.7646426e+02,  2.3512268e+02,  5.6024048e+02,\n",
       "          5.1906110e+02,  0.0000000e+00,  8.5685480e-01],\n",
       "        [ 1.0000000e+00,  1.1203514e+02,  2.3505554e+02,  2.2091090e+02,\n",
       "          5.3114307e+02,  0.0000000e+00,  8.5680377e-01],\n",
       "        [ 1.0000000e+00,  9.0665085e+01,  1.3541930e+02,  5.3778650e+02,\n",
       "          4.4475671e+02,  5.0000000e+00,  8.3618683e-01],\n",
       "        [ 2.0000000e+00,  3.7307629e+02,  1.6083318e+02,  5.7743591e+02,\n",
       "          4.9584601e+02,  0.0000000e+00,  7.9310930e-01],\n",
       "        [ 2.0000000e+00,  2.1895792e+02,  3.5587857e+02,  2.6999387e+02,\n",
       "          4.9703476e+02,  2.7000000e+01,  5.8482271e-01],\n",
       "        [ 2.0000000e+00,  6.5209000e+01,  2.4051682e+02,  4.7865540e+02,\n",
       "          4.9418790e+02,  0.0000000e+00,  5.7698834e-01],\n",
       "        [ 3.0000000e+00,  5.2892609e+00,  6.2162445e+01,  3.7209552e+02,\n",
       "          5.8483594e+02,  0.0000000e+00,  8.7545133e-01],\n",
       "        [ 3.0000000e+00,  2.6051691e+02,  1.4254585e+02,  6.3040662e+02,\n",
       "          5.8132233e+02,  0.0000000e+00,  8.1180179e-01],\n",
       "        [ 4.0000000e+00,  4.2224957e+02,  1.0267369e+02,  6.3170001e+02,\n",
       "          5.5488086e+02,  0.0000000e+00,  9.4197059e-01],\n",
       "        [ 4.0000000e+00,  4.9545387e+01,  2.7456857e+02,  1.8946579e+02,\n",
       "          4.1091318e+02,  3.2000000e+01,  9.3334389e-01],\n",
       "        [ 4.0000000e+00,  2.6599213e+01,  1.0745810e+02,  4.5867126e+02,\n",
       "          5.5153650e+02,  0.0000000e+00,  8.2260609e-01],\n",
       "        [ 5.0000000e+00,  1.9983047e+02,  3.6107239e+01,  5.2495667e+02,\n",
       "          4.8967474e+02,  1.7000000e+01,  8.4681529e-01],\n",
       "        [ 5.0000000e+00,  1.1123685e+02,  3.1816605e+02,  3.7944864e+02,\n",
       "          5.6111890e+02,  1.6000000e+01,  6.9891703e-01]], dtype=float32)]"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# batch 6 infer\n",
    "im = np.ascontiguousarray(np_batch[0:6,...]/255)\n",
    "out = session.run(outname,{'images':im})\n",
    "out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "2a72d2fd-14dd-42cf-b807-3e8a82b971d7",
   "metadata": {
    "tags": []
   },
   "outputs": [],
   "source": [
    "# batch 32 infer\n",
    "im = np.ascontiguousarray(np_batch/255)\n",
    "out = session.run(outname,{'images':im})[0]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "f3ca9301-ba52-4a8c-9ae0-55b28be8a904",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i,(batch_id,x0,y0,x1,y1,cls_id,score) in enumerate(out):\n",
    "    if batch_id >= 6:\n",
    "        break\n",
    "    image = origin_RGB[int(batch_id)]\n",
    "    ratio,dwdh = resize_data[int(batch_id)][1:]\n",
    "    box = np.array([x0,y0,x1,y1])\n",
    "    box -= np.array(dwdh*2)\n",
    "    box /= ratio\n",
    "    box = box.round().astype(np.int32).tolist()\n",
    "    cls_id = int(cls_id)\n",
    "    score = round(float(score),3)\n",
    "    name = names[cls_id]\n",
    "    color = colors[name]\n",
    "    name += ' '+str(score)\n",
    "    cv2.rectangle(image,box[:2],box[2:],color,2)\n",
    "    cv2.putText(image,name,(box[0], box[1] - 2),cv2.FONT_HERSHEY_SIMPLEX,0.75,[225, 255, 255],thickness=2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "ff5ce6a4-4fd9-4804-9afa-e8e8a3e20b41",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 739488 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=773x512>"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(origin_RGB[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "d13ed2df-ceb8-46c8-8bfc-aa7ff3750f03",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 2073000 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=810x1080>"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(origin_RGB[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "b4449198-3c2b-41d6-9a23-de7accf73d82",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 1425280 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=1280x720>"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(origin_RGB[2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "4faf6e6e-afb5-4c97-82c3-aeffdc9aba9e",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 506972 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=640x536>"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(origin_RGB[3])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "27485468-2e69-4aaf-8089-ba0134a1b26f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 534240 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=640x480>"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(origin_RGB[4])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "ef743cc3-7ae9-495c-ab24-2ac25967ec5c",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "[encoded data, 623860 characters]\n",
      "text/plain": [
       "<PIL.Image.Image image mode=RGB size=431x640>"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "Image.fromarray(origin_RGB[5])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c8dad466-7aa2-4ba4-81f1-0d8f57268081",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:torch] *",
   "language": "python",
   "name": "conda-env-torch-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.13"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
