Skip to content

Commit

Permalink
feat(exp,tools): support installation for pip (#1020)
Browse files Browse the repository at this point in the history
  • Loading branch information
FateScript committed Dec 23, 2021
1 parent dd5700c commit 12d0df1
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 34 deletions.
39 changes: 23 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,22 +120,23 @@ ln -s /path/to/your/COCO ./datasets/COCO
Step2. Reproduce our results on COCO by specifying -n:

```shell
python tools/train.py -n yolox-s -d 8 -b 64 --fp16 -o [--cache]
yolox-m
yolox-l
yolox-x
python -m yolox.tools.train -n yolox-s -d 8 -b 64 --fp16 -o [--cache]
yolox-m
yolox-l
yolox-x
```
* -d: number of gpu devices
* -b: total batch size, the recommended number for -b is num-gpu * 8
* --fp16: mixed precision training
* --cache: caching imgs into RAM to accelarate training, which need large system RAM.


When using -f, the above commands are equivalent to:
```shell
python tools/train.py -f exps/default/yolox_s.py -d 8 -b 64 --fp16 -o [--cache]
exps/default/yolox_m.py
exps/default/yolox_l.py
exps/default/yolox_x.py
python -m yolox.tools.train -f exps/default/yolox_s.py -d 8 -b 64 --fp16 -o [--cache]
exps/default/yolox_m.py
exps/default/yolox_l.py
exps/default/yolox_x.py
```

**Multi Machine Training**
Expand All @@ -154,6 +155,12 @@ On the second machine, run
python tools/train.py -n yolox-s -b 128 --dist-url tcp://123.123.123.123:12312 --num-machines 2 --machine-rank 1
```

**Others**
See more information with the following command:
```shell
python -m yolox.tools.train --help
```

</details>


Expand All @@ -163,21 +170,21 @@ python tools/train.py -n yolox-s -b 128 --dist-url tcp://123.123.123.123:12312 -
We support batch testing for fast evaluation:

```shell
python tools/eval.py -n yolox-s -c yolox_s.pth -b 64 -d 8 --conf 0.001 [--fp16] [--fuse]
yolox-m
yolox-l
yolox-x
python -m yolox.tools.eval -n yolox-s -c yolox_s.pth -b 64 -d 8 --conf 0.001 [--fp16] [--fuse]
yolox-m
yolox-l
yolox-x
```
* --fuse: fuse conv and bn
* -d: number of GPUs used for evaluation. DEFAULT: All GPUs available will be used.
* -b: total batch size across on all GPUs

To reproduce speed test, we use the following command:
```shell
python tools/eval.py -n yolox-s -c yolox_s.pth -b 1 -d 1 --conf 0.001 --fp16 --fuse
yolox-m
yolox-l
yolox-x
python -m yolox.tools.eval -n yolox-s -c yolox_s.pth -b 1 -d 1 --conf 0.001 --fp16 --fuse
yolox-m
yolox-l
yolox-x
```

</details>
Expand Down
File renamed without changes.
25 changes: 7 additions & 18 deletions yolox/exp/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,13 @@ def get_exp_by_file(exp_file):


def get_exp_by_name(exp_name):
import yolox

yolox_path = os.path.dirname(os.path.dirname(yolox.__file__))
filedict = {
"yolox-s": "yolox_s.py",
"yolox-m": "yolox_m.py",
"yolox-l": "yolox_l.py",
"yolox-x": "yolox_x.py",
"yolox-tiny": "yolox_tiny.py",
"yolox-nano": "nano.py",
"yolov3": "yolov3.py",
}
filename = filedict[exp_name]
exp_path = os.path.join(yolox_path, "exps", "default", filename)
return get_exp_by_file(exp_path)


def get_exp(exp_file, exp_name):
exp = exp_name.replace("-", "_") # convert string like "yolox-s" to "yolox_s"
module_name = ".".join(["yolox", "exp", "default", exp])
exp_object = importlib.import_module(module_name).Exp()
return exp_object


def get_exp(exp_file=None, exp_name=None):
"""
get Exp object by file or name. If exp_file and exp_name
are both provided, get Exp by exp_file.
Expand Down
28 changes: 28 additions & 0 deletions yolox/exp/default/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) 2014-2021 Megvii Inc. All rights reserved.

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

import importlib
import sys
from pathlib import Path

_EXP_PATH = Path(__file__).resolve().parent.parent.parent.parent / "exps" / "default"

if _EXP_PATH.is_dir():
# 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):

def find_spec(self, name, path, target=None):
if not name.startswith("yolox.exp.default"):
return
project_name = name.split(".")[-1] + ".py"
target_file = _EXP_PATH / project_name
if not target_file.is_file():
return
return importlib.util.spec_from_file_location(name, target_file)

sys.meta_path.append(_ExpFinder())
28 changes: 28 additions & 0 deletions yolox/tools/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#!/usr/bin/env python3
# -*- coding:utf-8 -*-
# Copyright (c) 2014-2021 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 pathlib import Path

_TOOLS_PATH = Path(__file__).resolve().parent.parent.parent / "tools"

if _TOOLS_PATH.is_dir():
# 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):

def find_spec(self, name, path, target=None):
if not name.startswith("yolox.tools."):
return
project_name = name.split(".")[-1] + ".py"
target_file = _TOOLS_PATH / project_name
if not target_file.is_file():
return
return importlib.util.spec_from_file_location(name, target_file)

sys.meta_path.append(_PathFinder())

0 comments on commit 12d0df1

Please sign in to comment.