Skip to content

Commit

Permalink
fix(yolox): some bugs due to upgrade (#1602)
Browse files Browse the repository at this point in the history
  • Loading branch information
FateScript committed Feb 2, 2023
1 parent 5c110a2 commit f15f193
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 31 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -219,3 +219,10 @@ events.out.tfevents*

# vim
.vim

# OS generated files
.DS_Store
.DS_Store?
.Trashes
ehthumbs.db
Thumbs.db
20 changes: 15 additions & 5 deletions demo/ONNXRuntime/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,18 @@

This doc introduces how to convert your pytorch model into onnx, and how to run an onnxruntime demo to verify your convertion.

### Download ONNX models.
### Step1: Install onnxruntime

run the following command to install onnxruntime:
```shell
pip install onnxruntime
```

### Step2: Get ONNX models

Users might download our pre-generated ONNX models or convert their own models to ONNX.

#### Download ONNX models.

| Model | Parameters | GFLOPs | Test Size | mAP | Weights |
|:------| :----: | :----: | :---: | :---: | :---: |
Expand All @@ -14,8 +25,7 @@ This doc introduces how to convert your pytorch model into onnx, and how to run
| YOLOX-Darknet53| 63.72M | 185.3 | 640x640 |48.0 | [github](https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_darknet.onnx) |
| YOLOX-X | 99.1M | 281.9 | 640x640 |51.5 | [github](https://github.com/Megvii-BaseDetection/YOLOX/releases/download/0.1.1rc0/yolox_x.onnx) |


### Convert Your Model to ONNX
#### Convert Your Model to ONNX

First, you should move to <YOLOX_HOME> by:
```shell
Expand All @@ -38,7 +48,7 @@ Notes:
dummy_input = torch.randn(1, 3, exp.test_size[0], exp.test_size[1])
```

2. Convert a standard YOLOX model by -f. When using -f, the above command is equivalent to:
1. Convert a standard YOLOX model by -f. When using -f, the above command is equivalent to:

```shell
python3 tools/export_onnx.py --output-name yolox_s.onnx -f exps/default/yolox_s.py -c yolox_s.pth
Expand All @@ -50,7 +60,7 @@ python3 tools/export_onnx.py --output-name yolox_s.onnx -f exps/default/yolox_s.
python3 tools/export_onnx.py --output-name your_yolox.onnx -f exps/your_dir/your_yolox.py -c your_yolox.pth
```

### ONNXRuntime Demo
### Step3: ONNXRuntime Demo

Step1.
```shell
Expand Down
8 changes: 1 addition & 7 deletions demo/ONNXRuntime/onnx_inference.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (c) Megvii, Inc. and its affiliates.

import argparse
Expand Down Expand Up @@ -51,11 +50,6 @@ def make_parser():
default="640,640",
help="Specify an input shape for inference.",
)
parser.add_argument(
"--with_p6",
action="store_true",
help="Whether your model uses p6 in FPN/PAN.",
)
return parser


Expand All @@ -70,7 +64,7 @@ def make_parser():

ort_inputs = {session.get_inputs()[0].name: img[None, :, :, :]}
output = session.run(None, ort_inputs)
predictions = demo_postprocess(output[0], input_shape, p6=args.with_p6)[0]
predictions = demo_postprocess(output[0], input_shape)[0]

boxes = predictions[:, :4]
scores = predictions[:, 4:5] * predictions[:, 5:]
Expand Down
2 changes: 1 addition & 1 deletion demo/OpenVINO/python/openvino_inference.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def main():
# ---------------------------Step 8. Process output--------------------------------------------------------------------
res = res[out_blob]

predictions = demo_postprocess(res, (h, w), p6=False)[0]
predictions = demo_postprocess(res, (h, w))[0]

boxes = predictions[:, :4]
scores = predictions[:, 4, None] * predictions[:, 5:]
Expand Down
6 changes: 3 additions & 3 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ thop
ninja
tabulate
psutil
tensorboard

# verified versions
# pycocotools corresponds to https://github.com/ppwwyyxx/cocoapi
pycocotools>=2.0.2
onnx==1.8.1
onnxruntime==1.8.0
onnx-simplifier==0.4.1
onnx>=1.13.0
onnx-simplifier==0.4.10
2 changes: 1 addition & 1 deletion yolox/evaluators/voc_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ def voc_eval(
for imagename in imagenames:
R = [obj for obj in recs[imagename] if obj["name"] == classname]
bbox = np.array([x["bbox"] for x in R])
difficult = np.array([x["difficult"] for x in R]).astype(np.bool)
difficult = np.array([x["difficult"] for x in R]).astype(bool)
det = [False] * len(R)
npos = npos + sum(~difficult)
class_recs[imagename] = {"bbox": bbox, "difficult": difficult, "det": det}
Expand Down
6 changes: 3 additions & 3 deletions yolox/exp/default/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

# This file is used for package installation and find default exp file

import importlib
import sys
from importlib import abc, util
from pathlib import Path

_EXP_PATH = Path(__file__).resolve().parent.parent.parent.parent / "exps" / "default"
Expand All @@ -14,7 +14,7 @@
# This is true only for in-place installation (pip install -e, setup.py develop),
# where setup(package_dir=) does not work: https://github.com/pypa/setuptools/issues/230

class _ExpFinder(importlib.abc.MetaPathFinder):
class _ExpFinder(abc.MetaPathFinder):

def find_spec(self, name, path, target=None):
if not name.startswith("yolox.exp.default"):
Expand All @@ -23,6 +23,6 @@ def find_spec(self, name, path, target=None):
target_file = _EXP_PATH / project_name
if not target_file.is_file():
return
return importlib.util.spec_from_file_location(name, target_file)
return util.spec_from_file_location(name, target_file)

sys.meta_path.append(_ExpFinder())
7 changes: 3 additions & 4 deletions yolox/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) Megvii Inc. All rights reserved.

# This file is used for package installation. Script of train/eval/export will be available.

import importlib
import sys
from importlib import abc, util
from pathlib import Path

_TOOLS_PATH = Path(__file__).resolve().parent.parent.parent / "tools"
Expand All @@ -14,7 +13,7 @@
# This is true only for in-place installation (pip install -e, setup.py develop),
# where setup(package_dir=) does not work: https://github.com/pypa/setuptools/issues/230

class _PathFinder(importlib.abc.MetaPathFinder):
class _PathFinder(abc.MetaPathFinder):

def find_spec(self, name, path, target=None):
if not name.startswith("yolox.tools."):
Expand All @@ -23,6 +22,6 @@ def find_spec(self, name, path, target=None):
target_file = _TOOLS_PATH / project_name
if not target_file.is_file():
return
return importlib.util.spec_from_file_location(name, target_file)
return util.spec_from_file_location(name, target_file)

sys.meta_path.append(_PathFinder())
8 changes: 1 addition & 7 deletions yolox/utils/demo_utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) Megvii Inc. All rights reserved.

import os
Expand Down Expand Up @@ -97,14 +96,9 @@ def multiclass_nms_class_agnostic(boxes, scores, nms_thr, score_thr):


def demo_postprocess(outputs, img_size, p6=False):

grids = []
expanded_strides = []

if not p6:
strides = [8, 16, 32]
else:
strides = [8, 16, 32, 64]
strides = [8, 16, 32] if not p6 else [8, 16, 32, 64]

hsizes = [img_size[0] // stride for stride in strides]
wsizes = [img_size[1] // stride for stride in strides]
Expand Down

0 comments on commit f15f193

Please sign in to comment.