Skip to content

Commit

Permalink
Refactor configuration & CLI (#2805)
Browse files Browse the repository at this point in the history
* Initial CLI commit

* Align core module's arguments

* Fix jsonargparse to recognize Trainer arguments

* Refactor configuration files

* Replace monkey-patching to contextmanager

* Disable SAM intg test temporarily
  • Loading branch information
harimkang authored Jan 23, 2024
1 parent 6c35bf7 commit 6b8b82e
Show file tree
Hide file tree
Showing 193 changed files with 4,426 additions and 4,803 deletions.
14 changes: 3 additions & 11 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,14 @@ classifiers = [
dependencies = [
"datumaro==1.6.0rc1",
"omegaconf",
"hydra-core",
"rich",
"jsonargparse==4.27.1",
"psutil", # Mem cache needs system checks
"ftfy",
"regex",
"importlib_resources"
"importlib_resources",
"docstring_parser", # CLI help-formatter
"rich_argparse", # CLI help-formatter
]

[project.optional-dependencies]
Expand Down Expand Up @@ -245,21 +246,12 @@ exclude = [
"tests/assets",

# it will be cleaned up later
"src/otx/core/engine/utils/*",
"src/otx/algo/classification/backbones/*",
"for_developers/helpers.py",

# Ruff complains it but don't know how to fix since it literally showed no useful logs.
# https://github.com/openvinotoolkit/training_extensions/actions/runs/7176557723/job/19541622452?pr=2718#step:5:170
"tests/regression/*.py",

# Temporarily disable it. This will be fixed in the next PR.
# [TODO] harimkang
"src/otx/core/utils/instantiators.py",
"src/otx/core/utils/logging_utils.py",
"src/otx/core/utils/pylogger.py",
"src/otx/core/utils/rich_utils.py",
"src/otx/core/utils/utils.py",
]

# Same as Black.
Expand Down
2 changes: 1 addition & 1 deletion src/otx/algo/detection/atss.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class ATSS(MMDetCompatibleModel):
"""ATSS Model."""

def __init__(self, num_classes: int, variant: Literal["mobilenetv2", "f50_fpn", "resnext101"]) -> None:
def __init__(self, num_classes: int, variant: Literal["mobilenetv2", "r50_fpn", "resnext101"]) -> None:
model_name = f"atss_{variant}"
config = read_mmconfig(model_name=model_name)
super().__init__(num_classes=num_classes, config=config)
2 changes: 1 addition & 1 deletion src/otx/algo/segmentation/litehrnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
class LiteHRNet(MMSegCompatibleModel):
"""LiteHRNet Model."""

def __init__(self, num_classes: int, variant: Literal["18", "s", "x"]) -> None:
def __init__(self, num_classes: int, variant: Literal["18", 18, "s", "x"]) -> None:
model_name = f"litehrnet_{variant}"
config = read_mmconfig(model_name=model_name)
super().__init__(num_classes=num_classes, config=config)
70 changes: 1 addition & 69 deletions src/otx/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,75 +3,7 @@

"""CLI entrypoints."""

from __future__ import annotations

from jsonargparse import ArgumentParser

from otx import __version__


class OTXCLI:
"""OTX CLI entrypoint."""

def __init__(self) -> None:
"""Initialize OTX CLI."""
self.parser = self.init_parser()
self.setup_subcommands()
self.config = self.parser.parse_args()

self.run()

def init_parser(self, **kwargs) -> ArgumentParser:
"""Initialize the argument parser for the OTX CLI.
Args:
**kwargs: Additional keyword arguments to pass to the ArgumentParser constructor.
Returns:
ArgumentParser: The initialized argument parser.
"""
parser = ArgumentParser(description="OpenVINO Training-Extension command line tool", env_prefix="otx")
parser.add_argument(
"-V",
"--version",
action="version",
version=f"%(prog)s {__version__}",
help="Display OTX version number.",
)
return parser

def setup_subcommands(self) -> None:
"""Setup subcommands for the OTX CLI."""
parser_subcommands = self.parser.add_subcommands()
from otx.cli.install import add_install_parser

add_install_parser(parser_subcommands)

# otx train parser
train_parser = ArgumentParser()
train_parser.add_argument("overrides", help="overrides values", default=[], nargs="+")
parser_subcommands.add_subcommand("train", train_parser, help="Training subcommand for OTX")

# otx test parser
test_parser = ArgumentParser()
test_parser.add_argument("overrides", help="overrides values", default=[], nargs="+")
parser_subcommands.add_subcommand("test", test_parser, help="Testing subcommand for OTX")

def run(self) -> None:
"""Run the OTX CLI."""
subcommand = self.config["subcommand"]
if subcommand == "install":
from otx.cli.install import otx_install

otx_install(**self.config["install"])
elif subcommand == "train":
from otx.cli.train import otx_train

otx_train(**self.config["train"])
elif subcommand == "test":
from otx.cli.test import otx_test

otx_test(**self.config["test"])
from .cli import OTXCLI


def main() -> None:
Expand Down
Loading

0 comments on commit 6b8b82e

Please sign in to comment.