Feng Wang
commited on
Commit
·
e9faa7e
1
Parent(s):
d9f51c5
refactor(YOLOX): refactor datasets and add demo_utils
Browse files- datasets/README.md +24 -0
- demo/ONNXRuntime/onnx_inference.py +10 -6
- demo/OpenVINO/python/demo_utils.py +0 -86
- demo/OpenVINO/python/openvino_inference.py +4 -2
- setup.cfg +1 -1
- yolox/data/dataloading.py +2 -1
- yolox/utils/__init__.py +1 -0
- {demo/ONNXRuntime → yolox/utils}/demo_utils.py +7 -1
datasets/README.md
ADDED
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Prepare datasets
|
2 |
+
|
3 |
+
If you have a dataset directory, you could use os environment variable named `YOLOX_DATADIR`. Under this directory, YOLOX will look for datasets in the structure described below, if needed.
|
4 |
+
```
|
5 |
+
$YOLOX_DATADIR/
|
6 |
+
COCO/
|
7 |
+
```
|
8 |
+
You can set the location for builtin datasets by
|
9 |
+
```shell
|
10 |
+
export YOLOX_DATADIR=/path/to/your/datasets
|
11 |
+
```
|
12 |
+
If `YOLOX_DATADIR` is not set, the default value of dataset directory is `./datasets` relative to your current working directory.
|
13 |
+
|
14 |
+
## Expected dataset structure for [COCO detection](https://cocodataset.org/#download):
|
15 |
+
|
16 |
+
```
|
17 |
+
COCO/
|
18 |
+
annotations/
|
19 |
+
instances_{train,val}2017.json
|
20 |
+
{train,val}2017/
|
21 |
+
# image files that are mentioned in the corresponding json
|
22 |
+
```
|
23 |
+
|
24 |
+
You can use the 2014 version of the dataset as well.
|
demo/ONNXRuntime/onnx_inference.py
CHANGED
@@ -1,14 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
import cv2
|
2 |
import numpy as np
|
3 |
|
|
|
|
|
4 |
from yolox.data.data_augment import preproc as preprocess
|
5 |
from yolox.data.datasets import COCO_CLASSES
|
6 |
-
from yolox.utils
|
7 |
-
|
8 |
-
import argparse
|
9 |
-
import onnxruntime
|
10 |
-
import os
|
11 |
-
from demo_utils import mkdir, multiclass_nms, postprocess
|
12 |
|
13 |
|
14 |
def make_parser():
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding: utf-8 -*-
|
3 |
+
# Copyright (c) Megvii, Inc. and its affiliates.
|
4 |
+
|
5 |
+
import argparse
|
6 |
+
import os
|
7 |
+
|
8 |
import cv2
|
9 |
import numpy as np
|
10 |
|
11 |
+
import onnxruntime
|
12 |
+
|
13 |
from yolox.data.data_augment import preproc as preprocess
|
14 |
from yolox.data.datasets import COCO_CLASSES
|
15 |
+
from yolox.utils import mkdir, multiclass_nms, postprocess, vis
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
|
18 |
def make_parser():
|
demo/OpenVINO/python/demo_utils.py
DELETED
@@ -1,86 +0,0 @@
|
|
1 |
-
import numpy as np
|
2 |
-
|
3 |
-
import os
|
4 |
-
|
5 |
-
|
6 |
-
def mkdir(path):
|
7 |
-
if not os.path.exists(path):
|
8 |
-
os.makedirs(path)
|
9 |
-
|
10 |
-
|
11 |
-
def nms(boxes, scores, nms_thr):
|
12 |
-
"""Single class NMS implemented in Numpy."""
|
13 |
-
x1 = boxes[:, 0]
|
14 |
-
y1 = boxes[:, 1]
|
15 |
-
x2 = boxes[:, 2]
|
16 |
-
y2 = boxes[:, 3]
|
17 |
-
|
18 |
-
areas = (x2 - x1 + 1) * (y2 - y1 + 1)
|
19 |
-
order = scores.argsort()[::-1]
|
20 |
-
|
21 |
-
keep = []
|
22 |
-
while order.size > 0:
|
23 |
-
i = order[0]
|
24 |
-
keep.append(i)
|
25 |
-
xx1 = np.maximum(x1[i], x1[order[1:]])
|
26 |
-
yy1 = np.maximum(y1[i], y1[order[1:]])
|
27 |
-
xx2 = np.minimum(x2[i], x2[order[1:]])
|
28 |
-
yy2 = np.minimum(y2[i], y2[order[1:]])
|
29 |
-
|
30 |
-
w = np.maximum(0.0, xx2 - xx1 + 1)
|
31 |
-
h = np.maximum(0.0, yy2 - yy1 + 1)
|
32 |
-
inter = w * h
|
33 |
-
ovr = inter / (areas[i] + areas[order[1:]] - inter)
|
34 |
-
|
35 |
-
inds = np.where(ovr <= nms_thr)[0]
|
36 |
-
order = order[inds + 1]
|
37 |
-
|
38 |
-
return keep
|
39 |
-
|
40 |
-
|
41 |
-
def multiclass_nms(boxes, scores, nms_thr, score_thr):
|
42 |
-
"""Multiclass NMS implemented in Numpy"""
|
43 |
-
final_dets = []
|
44 |
-
num_classes = scores.shape[1]
|
45 |
-
for cls_ind in range(num_classes):
|
46 |
-
cls_scores = scores[:, cls_ind]
|
47 |
-
valid_score_mask = cls_scores > score_thr
|
48 |
-
if valid_score_mask.sum() == 0:
|
49 |
-
continue
|
50 |
-
else:
|
51 |
-
valid_scores = cls_scores[valid_score_mask]
|
52 |
-
valid_boxes = boxes[valid_score_mask]
|
53 |
-
keep = nms(valid_boxes, valid_scores, nms_thr)
|
54 |
-
if len(keep) > 0:
|
55 |
-
cls_inds = np.ones((len(keep), 1)) * cls_ind
|
56 |
-
dets = np.concatenate([valid_boxes[keep], valid_scores[keep, None], cls_inds], 1)
|
57 |
-
final_dets.append(dets)
|
58 |
-
return np.concatenate(final_dets, 0)
|
59 |
-
|
60 |
-
|
61 |
-
def postprocess(outputs, img_size, p6=False):
|
62 |
-
|
63 |
-
grids = []
|
64 |
-
expanded_strides = []
|
65 |
-
|
66 |
-
if not p6:
|
67 |
-
strides = [8, 16, 32]
|
68 |
-
else:
|
69 |
-
strides = [8, 16, 32, 64]
|
70 |
-
|
71 |
-
hsizes = [img_size[0]//stride for stride in strides]
|
72 |
-
wsizes = [img_size[1]//stride for stride in strides]
|
73 |
-
|
74 |
-
for hsize, wsize, stride in zip(hsizes, wsizes, strides):
|
75 |
-
xv, yv = np.meshgrid(np.arange(hsize), np.arange(wsize))
|
76 |
-
grid = np.stack((xv, yv), 2).reshape(1, -1, 2)
|
77 |
-
grids.append(grid)
|
78 |
-
shape = grid.shape[:2]
|
79 |
-
expanded_strides.append(np.full((*shape, 1), stride))
|
80 |
-
|
81 |
-
grids = np.concatenate(grids, 1)
|
82 |
-
expanded_strides = np.concatenate(expanded_strides, 1)
|
83 |
-
outputs[..., :2] = (outputs[..., :2] + grids) * expanded_strides
|
84 |
-
outputs[..., 2:4] = np.exp(outputs[..., 2:4]) * expanded_strides
|
85 |
-
|
86 |
-
return outputs
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
demo/OpenVINO/python/openvino_inference.py
CHANGED
@@ -2,6 +2,8 @@
|
|
2 |
# -*- coding: utf-8 -*-
|
3 |
# Copyright (C) 2018-2021 Intel Corporation
|
4 |
# SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
5 |
import argparse
|
6 |
import logging as log
|
7 |
import os
|
@@ -10,11 +12,11 @@ import sys
|
|
10 |
import cv2
|
11 |
import numpy as np
|
12 |
|
13 |
-
from demo_utils import mkdir, multiclass_nms, postprocess
|
14 |
from openvino.inference_engine import IECore
|
|
|
15 |
from yolox.data.data_augment import preproc as preprocess
|
16 |
from yolox.data.datasets import COCO_CLASSES
|
17 |
-
from yolox.utils
|
18 |
|
19 |
|
20 |
def parse_args() -> argparse.Namespace:
|
|
|
2 |
# -*- coding: utf-8 -*-
|
3 |
# Copyright (C) 2018-2021 Intel Corporation
|
4 |
# SPDX-License-Identifier: Apache-2.0
|
5 |
+
# Copyright (c) Megvii, Inc. and its affiliates.
|
6 |
+
|
7 |
import argparse
|
8 |
import logging as log
|
9 |
import os
|
|
|
12 |
import cv2
|
13 |
import numpy as np
|
14 |
|
|
|
15 |
from openvino.inference_engine import IECore
|
16 |
+
|
17 |
from yolox.data.data_augment import preproc as preprocess
|
18 |
from yolox.data.datasets import COCO_CLASSES
|
19 |
+
from yolox.utils import mkdir, multiclass_nms, postprocess, vis
|
20 |
|
21 |
|
22 |
def parse_args() -> argparse.Namespace:
|
setup.cfg
CHANGED
@@ -6,7 +6,7 @@ known_standard_library = setuptools
|
|
6 |
known_third_party = tqdm,loguru
|
7 |
known_data_processing = cv2,numpy,scipy,PIL,matplotlib,scikit_image
|
8 |
known_datasets = pycocotools
|
9 |
-
known_deeplearning = torch,torchvision,caffe2,onnx,apex,timm,thop,torch2trt,tensorrt
|
10 |
known_myself = yolox
|
11 |
sections = FUTURE,STDLIB,THIRDPARTY,data_processing,datasets,deeplearning,myself,FIRSTPARTY,LOCALFOLDER
|
12 |
no_lines_before=STDLIB,THIRDPARTY,datasets
|
|
|
6 |
known_third_party = tqdm,loguru
|
7 |
known_data_processing = cv2,numpy,scipy,PIL,matplotlib,scikit_image
|
8 |
known_datasets = pycocotools
|
9 |
+
known_deeplearning = torch,torchvision,caffe2,onnx,apex,timm,thop,torch2trt,tensorrt,openvino,onnxruntime
|
10 |
known_myself = yolox
|
11 |
sections = FUTURE,STDLIB,THIRDPARTY,data_processing,datasets,deeplearning,myself,FIRSTPARTY,LOCALFOLDER
|
12 |
no_lines_before=STDLIB,THIRDPARTY,datasets
|
yolox/data/dataloading.py
CHANGED
@@ -20,7 +20,8 @@ def get_yolox_datadir():
|
|
20 |
yolox_datadir = os.getenv("YOLOX_DATADIR", None)
|
21 |
if yolox_datadir is None:
|
22 |
import yolox
|
23 |
-
|
|
|
24 |
return yolox_datadir
|
25 |
|
26 |
|
|
|
20 |
yolox_datadir = os.getenv("YOLOX_DATADIR", None)
|
21 |
if yolox_datadir is None:
|
22 |
import yolox
|
23 |
+
yolox_path = os.path.dirname(os.path.dirname(yolox.__file__))
|
24 |
+
yolox_datadir = os.path.join(yolox_path, "datasets")
|
25 |
return yolox_datadir
|
26 |
|
27 |
|
yolox/utils/__init__.py
CHANGED
@@ -5,6 +5,7 @@
|
|
5 |
from .allreduce_norm import *
|
6 |
from .boxes import *
|
7 |
from .checkpoint import load_ckpt, save_checkpoint
|
|
|
8 |
from .dist import *
|
9 |
from .ema import ModelEMA
|
10 |
from .logger import setup_logger
|
|
|
5 |
from .allreduce_norm import *
|
6 |
from .boxes import *
|
7 |
from .checkpoint import load_ckpt, save_checkpoint
|
8 |
+
from .demo_utils import *
|
9 |
from .dist import *
|
10 |
from .ema import ModelEMA
|
11 |
from .logger import setup_logger
|
{demo/ONNXRuntime → yolox/utils}/demo_utils.py
RENAMED
@@ -1,7 +1,13 @@
|
|
1 |
-
|
|
|
|
|
2 |
|
3 |
import os
|
4 |
|
|
|
|
|
|
|
|
|
5 |
|
6 |
def mkdir(path):
|
7 |
if not os.path.exists(path):
|
|
|
1 |
+
#!/usr/bin/env python3
|
2 |
+
# -*- coding:utf-8 -*-
|
3 |
+
# Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
|
4 |
|
5 |
import os
|
6 |
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
__all__ = ["mkdir", "nms", "multiclass_nms", "postprocess"]
|
10 |
+
|
11 |
|
12 |
def mkdir(path):
|
13 |
if not os.path.exists(path):
|