Skip to content

Commit

Permalink
Workaround on YOLOTransform to support fixed size input (#109)
Browse files Browse the repository at this point in the history
* Make target in self.resize truly Optional

* Simplify using the Python3 idiom

* Cherrypicking from the upstream

* Support fixed size in self.transform

* [BC] Switch to the parameter size in YOLOModule

* Turn down the coco128 mAP in unit-test

* Update notebooks
  • Loading branch information
zhiqwang authored May 20, 2021
1 parent 97c8ab7 commit 66b8330
Show file tree
Hide file tree
Showing 7 changed files with 178 additions and 139 deletions.
36 changes: 17 additions & 19 deletions notebooks/export-onnx-inference-onnxruntime.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@
"metadata": {},
"outputs": [],
"source": [
"# img_one = get_image_from_url(\"https://gitee.com/zhiqwang/yolov5-rt-stack/raw/master/test/assets/bus.jpg\")\n",
"img_one = cv2.imread('../test/assets/bus.jpg')\n",
"img_one = get_image_from_url(\"https://gitee.com/zhiqwang/yolov5-rt-stack/raw/master/test/assets/bus.jpg\")\n",
"# img_one = cv2.imread('../test/assets/bus.jpg')\n",
"img_one = read_image_to_tensor(img_one, is_half=False)\n",
"img_one = img_one.to(device)\n",
"\n",
"# img_two = get_image_from_url(\"https://gitee.com/zhiqwang/yolov5-rt-stack/raw/master/test/assets/zidane.jpg\")\n",
"img_two = cv2.imread('../test/assets/zidane.jpg')\n",
"img_two = get_image_from_url(\"https://gitee.com/zhiqwang/yolov5-rt-stack/raw/master/test/assets/zidane.jpg\")\n",
"# img_two = cv2.imread('../test/assets/zidane.jpg')\n",
"img_two = read_image_to_tensor(img_two, is_half=False)\n",
"img_two = img_two.to(device)\n",
"\n",
Expand Down Expand Up @@ -102,8 +102,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 4.04 s, sys: 68 ms, total: 4.11 s\n",
"Wall time: 117 ms\n"
"CPU times: user 7.87 s, sys: 108 ms, total: 7.98 s\n",
"Wall time: 271 ms\n"
]
}
],
Expand All @@ -121,10 +121,10 @@
{
"data": {
"text/plain": [
"tensor([[ 53.9381, 389.6684, 238.1329, 898.0309],\n",
" [223.5657, 409.1237, 344.0657, 861.4382],\n",
" [ 19.7466, 226.0309, 795.1536, 762.1879],\n",
" [675.1501, 390.4896, 813.6778, 874.6315]])"
"tensor([[669.2656, 391.3025, 809.8663, 885.2344],\n",
" [ 54.0635, 397.8317, 235.9532, 901.3731],\n",
" [222.8834, 406.8119, 341.5572, 854.7792],\n",
" [ 18.6320, 232.9768, 810.9740, 760.1170]])"
]
},
"execution_count": 7,
Expand All @@ -144,7 +144,7 @@
{
"data": {
"text/plain": [
"tensor([0.8762, 0.8721, 0.8337, 0.8301])"
"tensor([0.8901, 0.8733, 0.8537, 0.7234])"
]
},
"execution_count": 8,
Expand All @@ -164,7 +164,7 @@
{
"data": {
"text/plain": [
"tensor([0, 0, 5, 0])"
"tensor([0, 0, 0, 5])"
]
},
"execution_count": 9,
Expand Down Expand Up @@ -224,8 +224,6 @@
" 'Automatically generated names will be applied to each dynamic axes of input {}'.format(key))\n",
"/usr/local/lib/python3.6/dist-packages/torch/onnx/utils.py:1112: UserWarning: No names were found for specified dynamic axes of provided input.Automatically generated names will be applied to each dynamic axes of input outputs\n",
" 'Automatically generated names will be applied to each dynamic axes of input {}'.format(key))\n",
"/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py:3123: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" dtype=torch.float32)).float())) for i in range(dim)]\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/anchor_utils.py:31: TracerWarning: torch.as_tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
" stride = torch.as_tensor([stride], dtype=dtype, device=device)\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/anchor_utils.py:50: TracerWarning: torch.as_tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
Expand All @@ -234,9 +232,9 @@
" shifts = shifts - torch.tensor(0.5, dtype=shifts.dtype, device=device)\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/box_head.py:362: TracerWarning: Converting a tensor to a Python index 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",
" for idx in range(batch_size): # image idx, image inference\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/transform.py:282: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/transform.py:290: TracerWarning: torch.tensor results are registered as constants in the trace. You can safely ignore this warning if you use this function to create tensors out of constant variables that would be the same every time you call this function. In any other case, this might cause the trace to be incorrect.\n",
" for s, s_orig in zip(new_size, original_size)\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/transform.py:282: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
"/data/wangzq/yolov5-rt-stack/yolort/models/transform.py:290: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).\n",
" for s, s_orig in zip(new_size, original_size)\n",
"/usr/local/lib/python3.6/dist-packages/torch/onnx/symbolic_opset9.py:2378: UserWarning: Exporting aten::index operator of advanced indexing in opset 11 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",
" \"If indices include negative values, the exported graph will produce incorrect results.\")\n",
Expand Down Expand Up @@ -357,7 +355,7 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Starting with onnx 1.8.1, onnxruntime 1.7.0...\n"
"Starting with onnx 1.9.0, onnxruntime 1.7.0...\n"
]
}
],
Expand Down Expand Up @@ -428,8 +426,8 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 2.5 s, sys: 16 ms, total: 2.51 s\n",
"Wall time: 77.6 ms\n"
"CPU times: user 5.03 s, sys: 164 ms, total: 5.19 s\n",
"Wall time: 157 ms\n"
]
}
],
Expand Down
Loading

0 comments on commit 66b8330

Please sign in to comment.