Spaces:
Sleeping
Sleeping
🚀 [Update] the code with Vec2Box in deploy part
Browse files
examples/notebook_inference.ipynb
CHANGED
|
@@ -10,7 +10,11 @@
|
|
| 10 |
"from hydra import compose, initialize\n",
|
| 11 |
"from PIL import Image \n",
|
| 12 |
"\n",
|
| 13 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
]
|
| 15 |
},
|
| 16 |
{
|
|
@@ -23,9 +27,8 @@
|
|
| 23 |
"CONFIG_NAME = \"config\"\n",
|
| 24 |
"\n",
|
| 25 |
"DEVICE = 'cuda:0'\n",
|
| 26 |
-
"
|
| 27 |
-
"\n",
|
| 28 |
-
"WEIGHT_PATH = '../weights/v9-cnw.pt' \n",
|
| 29 |
"IMAGE_PATH = '../demo/images/inference/image.png'\n",
|
| 30 |
"\n",
|
| 31 |
"custom_logger()\n",
|
|
@@ -40,9 +43,10 @@
|
|
| 40 |
"outputs": [],
|
| 41 |
"source": [
|
| 42 |
"with initialize(config_path=CONFIG_PATH, version_base=None, job_name=\"notebook_job\"):\n",
|
| 43 |
-
" cfg: Config = compose(config_name=CONFIG_NAME, overrides=[\"task=inference\", f\"task.data.source={IMAGE_PATH}\"])\n",
|
| 44 |
-
" model = create_model(cfg.model, WEIGHT_PATH).to(device)\n",
|
| 45 |
-
" transform = AugmentationComposer([], cfg.image_size)"
|
|
|
|
| 46 |
]
|
| 47 |
},
|
| 48 |
{
|
|
@@ -63,7 +67,9 @@
|
|
| 63 |
"source": [
|
| 64 |
"with torch.no_grad():\n",
|
| 65 |
" predict = model(image)\n",
|
| 66 |
-
"
|
|
|
|
|
|
|
| 67 |
"draw_bboxes(image, predict_box, save_path='../demo/images/output/', idx2label=cfg.class_list)"
|
| 68 |
]
|
| 69 |
},
|
|
|
|
| 10 |
"from hydra import compose, initialize\n",
|
| 11 |
"from PIL import Image \n",
|
| 12 |
"\n",
|
| 13 |
+
"# Ensure that the necessary repository is cloned and installed. You may need to run: \n",
|
| 14 |
+
"# git clone git@github.com:WongKinYiu/YOLO.git\n",
|
| 15 |
+
"# cd YOLO \n",
|
| 16 |
+
"# pip install .\n",
|
| 17 |
+
"from yolo import AugmentationComposer, bbox_nms, Config, create_model, custom_logger, draw_bboxes, Vec2Box"
|
| 18 |
]
|
| 19 |
},
|
| 20 |
{
|
|
|
|
| 27 |
"CONFIG_NAME = \"config\"\n",
|
| 28 |
"\n",
|
| 29 |
"DEVICE = 'cuda:0'\n",
|
| 30 |
+
"CLASS_NUM = 80\n",
|
| 31 |
+
"WEIGHT_PATH = '../weights/v9-c.pt' \n",
|
|
|
|
| 32 |
"IMAGE_PATH = '../demo/images/inference/image.png'\n",
|
| 33 |
"\n",
|
| 34 |
"custom_logger()\n",
|
|
|
|
| 43 |
"outputs": [],
|
| 44 |
"source": [
|
| 45 |
"with initialize(config_path=CONFIG_PATH, version_base=None, job_name=\"notebook_job\"):\n",
|
| 46 |
+
" cfg: Config = compose(config_name=CONFIG_NAME, overrides=[\"task=inference\", f\"task.data.source={IMAGE_PATH}\", \"model=v9-c-deploy\"])\n",
|
| 47 |
+
" model = create_model(cfg.model, class_num=CLASS_NUM, weight_path=WEIGHT_PATH).to(device)\n",
|
| 48 |
+
" transform = AugmentationComposer([], cfg.image_size)\n",
|
| 49 |
+
" vec2box = Vec2Box(model, cfg.image_size, device)"
|
| 50 |
]
|
| 51 |
},
|
| 52 |
{
|
|
|
|
| 67 |
"source": [
|
| 68 |
"with torch.no_grad():\n",
|
| 69 |
" predict = model(image)\n",
|
| 70 |
+
" predict = vec2box(predict[\"Main\"])\n",
|
| 71 |
+
"\n",
|
| 72 |
+
"predict_box = bbox_nms(predict[0], predict[2], cfg.task.nms)\n",
|
| 73 |
"draw_bboxes(image, predict_box, save_path='../demo/images/output/', idx2label=cfg.class_list)"
|
| 74 |
]
|
| 75 |
},
|
yolo/config/model/v9-c-deploy.yaml
ADDED
|
@@ -0,0 +1,77 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
anchor:
|
| 2 |
+
reg_max: 16
|
| 3 |
+
|
| 4 |
+
model:
|
| 5 |
+
backbone:
|
| 6 |
+
- Conv:
|
| 7 |
+
args: {out_channels: 64, kernel_size: 3, stride: 2}
|
| 8 |
+
source: 0
|
| 9 |
+
- Conv:
|
| 10 |
+
args: {out_channels: 128, kernel_size: 3, stride: 2}
|
| 11 |
+
- RepNCSPELAN:
|
| 12 |
+
args: {out_channels: 256, part_channels: 128}
|
| 13 |
+
|
| 14 |
+
- ADown:
|
| 15 |
+
args: {out_channels: 256}
|
| 16 |
+
- RepNCSPELAN:
|
| 17 |
+
args: {out_channels: 512, part_channels: 256}
|
| 18 |
+
tags: B3
|
| 19 |
+
|
| 20 |
+
- ADown:
|
| 21 |
+
args: {out_channels: 512}
|
| 22 |
+
- RepNCSPELAN:
|
| 23 |
+
args: {out_channels: 512, part_channels: 512}
|
| 24 |
+
tags: B4
|
| 25 |
+
|
| 26 |
+
- ADown:
|
| 27 |
+
args: {out_channels: 512}
|
| 28 |
+
- RepNCSPELAN:
|
| 29 |
+
args: {out_channels: 512, part_channels: 512}
|
| 30 |
+
tags: B5
|
| 31 |
+
|
| 32 |
+
neck:
|
| 33 |
+
- SPPELAN:
|
| 34 |
+
args: {out_channels: 512}
|
| 35 |
+
tags: N3
|
| 36 |
+
|
| 37 |
+
- UpSample:
|
| 38 |
+
args: {scale_factor: 2, mode: nearest}
|
| 39 |
+
- Concat:
|
| 40 |
+
source: [-1, B4]
|
| 41 |
+
- RepNCSPELAN:
|
| 42 |
+
args: {out_channels: 512, part_channels: 512}
|
| 43 |
+
tags: N4
|
| 44 |
+
|
| 45 |
+
- UpSample:
|
| 46 |
+
args: {scale_factor: 2, mode: nearest}
|
| 47 |
+
- Concat:
|
| 48 |
+
source: [-1, B3]
|
| 49 |
+
|
| 50 |
+
head:
|
| 51 |
+
- RepNCSPELAN:
|
| 52 |
+
args: {out_channels: 256, part_channels: 256}
|
| 53 |
+
tags: P3
|
| 54 |
+
|
| 55 |
+
- ADown:
|
| 56 |
+
args: {out_channels: 256}
|
| 57 |
+
- Concat:
|
| 58 |
+
source: [-1, N4]
|
| 59 |
+
- RepNCSPELAN:
|
| 60 |
+
args: {out_channels: 512, part_channels: 512}
|
| 61 |
+
tags: P4
|
| 62 |
+
|
| 63 |
+
- ADown:
|
| 64 |
+
args: {out_channels: 512}
|
| 65 |
+
- Concat:
|
| 66 |
+
source: [-1, N3]
|
| 67 |
+
- RepNCSPELAN:
|
| 68 |
+
args: {out_channels: 512, part_channels: 512}
|
| 69 |
+
tags: P5
|
| 70 |
+
|
| 71 |
+
detection:
|
| 72 |
+
- MultiheadDetection:
|
| 73 |
+
source: [P3, P4, P5]
|
| 74 |
+
tags: Main
|
| 75 |
+
args:
|
| 76 |
+
reg_max: ${model.anchor.reg_max}
|
| 77 |
+
output: True
|
yolo/utils/deploy_utils.py
CHANGED
|
@@ -35,8 +35,13 @@ class FastModelLoader:
|
|
| 35 |
|
| 36 |
def onnx_forward(self: InferenceSession, x: Tensor):
|
| 37 |
x = {self.get_inputs()[0].name: x.cpu().numpy()}
|
| 38 |
-
|
| 39 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 40 |
|
| 41 |
InferenceSession.__call__ = onnx_forward
|
| 42 |
try:
|
|
|
|
| 35 |
|
| 36 |
def onnx_forward(self: InferenceSession, x: Tensor):
|
| 37 |
x = {self.get_inputs()[0].name: x.cpu().numpy()}
|
| 38 |
+
model_outputs, layer_output = [], []
|
| 39 |
+
for idx, predict in enumerate(self.run(None, x)):
|
| 40 |
+
layer_output.append(torch.from_numpy(predict))
|
| 41 |
+
if idx % 3 == 2:
|
| 42 |
+
model_outputs.append(layer_output)
|
| 43 |
+
layer_output = []
|
| 44 |
+
return {"Main": model_outputs}
|
| 45 |
|
| 46 |
InferenceSession.__call__ = onnx_forward
|
| 47 |
try:
|