Skip to content

Commit

Permalink
safer cmdline overrides
Browse files Browse the repository at this point in the history
Summary: Pull Request resolved: fairinternal/detectron2#534

Reviewed By: alexander-kirillov

Differential Revision: D27877352

Pulled By: ppwwyyxx

fbshipit-source-id: 03e6aa99385115014b02952aa9f97282ed152752
  • Loading branch information
ppwwyyxx authored and facebook-github-bot committed Apr 20, 2021
1 parent 16f6013 commit 2298ee5
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 21 deletions.
12 changes: 6 additions & 6 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -142,14 +142,14 @@ jobs:
# Refresh the key when dependencies should be updated (e.g. when pytorch releases)
- restore_cache:
keys:
- cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210304
- cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210404

- <<: *install_linux_dep

- save_cache:
paths:
- ~/venv
key: cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210304
key: cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210404

- <<: *install_detectron2
- <<: *run_unittests
Expand All @@ -166,15 +166,15 @@ jobs:
# Cache the directory that contains python and dependencies
- restore_cache:
keys:
- cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210304
- cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210404

- <<: *install_python
- <<: *install_linux_dep

- save_cache:
paths:
- /opt/circleci/.pyenv
key: cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210304
key: cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210404

- <<: *install_detectron2
- <<: *run_unittests
Expand All @@ -190,7 +190,7 @@ jobs:
# Cache the env directory that contains dependencies
- restore_cache:
keys:
- cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210304
- cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210404

- run:
name: Install Dependencies
Expand All @@ -205,7 +205,7 @@ jobs:
- save_cache:
paths:
- env
key: cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210304
key: cache-{{ arch }}-<< parameters.pytorch_version >>-{{ .Branch }}-20210404

- <<: *install_detectron2
# TODO: unittest fails for now
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/workflow.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
path: |
${{ env.pythonLocation }}/lib/python3.6/site-packages
~/.torch
key: ${{ runner.os }}-torch${{ matrix.torch }}-${{ hashFiles('setup.py') }}-20200709
key: ${{ runner.os }}-torch${{ matrix.torch }}-${{ hashFiles('setup.py') }}-20210420

- name: Install dependencies
run: |
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<img src=".github/Detectron2-Logo-Horz.svg" width="300" >

Detectron2 is Facebook AI Research's next generation software system
that implements state-of-the-art detection and segmentation algorithms.
It is a ground-up rewrite of the previous version,
[Detectron](https://github.com/facebookresearch/Detectron/),
and it originates from [maskrcnn-benchmark](https://github.com/facebookresearch/maskrcnn-benchmark/).
Detectron2 is Facebook AI Research's next generation library
that provides state-of-the-art detection and segmentation algorithms.
It is the successor of
[Detectron](https://github.com/facebookresearch/Detectron/)
and [maskrcnn-benchmark](https://github.com/facebookresearch/maskrcnn-benchmark/).
It supports a number of computer vision research projects and production applications in Facebook.

<div align="center">
<img src="https://user-images.githubusercontent.com/1381301/66535560-d3422200-eace-11e9-9123-5535d469db19.png"/>
Expand Down
22 changes: 19 additions & 3 deletions detectron2/config/lazy.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,14 +278,30 @@ def apply_overrides(cfg, overrides: List[str]):
Returns:
the cfg object
"""

def safe_update(cfg, key, value):
parts = key.split(".")
for idx in range(1, len(parts)):
prefix = ".".join(parts[:idx])
v = OmegaConf.select(cfg, prefix, default=None)
if v is None:
break
if not OmegaConf.is_config(v):
raise KeyError(
f"Trying to update key {key}, but {prefix} "
f"is not a config, but has type {type(v)}."
)
OmegaConf.update(cfg, key, value, merge=True)

from hydra.core.override_parser.overrides_parser import OverridesParser

parser = OverridesParser.create()
overrides = parser.parse_overrides(overrides)
for o in overrides:
key = o.key_or_group
value = o.value()
# TODO seems nice to support this
assert not o.is_delete(), "deletion is not a supported override"
OmegaConf.update(cfg, key, value, merge=True)
if o.is_delete():
# TODO support this
raise NotImplementedError("deletion is not yet a supported override")
safe_update(cfg, key, value)
return cfg
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ def get_model_zoo_configs() -> List[str]:
# NOTE: when updating fvcore/iopath version, make sure fvcore depends
# on the same version of iopath.
"fvcore>=0.1.5,<0.1.6", # required like this to make it pip installable
"iopath>=0.1.7,<0.1.8",
"iopath>=0.1.7,<0.1.9",
"pycocotools>=2.0.2", # corresponds to https://github.com/ppwwyyxx/cocoapi
"future", # used by caffe2
"pydot", # used to save caffe2 SVGs
Expand Down
5 changes: 5 additions & 0 deletions tests/config/test_lazy_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,8 @@ def test_overrides(self):
LazyConfig.apply_overrides(cfg, ["lazyobj.x=123", 'dir1b_dict.a="123"'])
self.assertEqual(cfg.dir1b_dict.a, "123")
self.assertEqual(cfg.lazyobj.x, 123)

def test_invalid_overrides(self):
cfg = LazyConfig.load(self.root_filename)
with self.assertRaises(KeyError):
LazyConfig.apply_overrides(cfg, ["lazyobj.x.xxx=123"])
11 changes: 6 additions & 5 deletions tools/lazyconfig_train_net.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,12 @@


def do_test(cfg, model):
ret = inference_on_dataset(
model, instantiate(cfg.dataloader.test), instantiate(cfg.dataloader.evaluator)
)
print_csv_format(ret)
return ret
if "evaluator" in cfg.dataloader:
ret = inference_on_dataset(
model, instantiate(cfg.dataloader.test), instantiate(cfg.dataloader.evaluator)
)
print_csv_format(ret)
return ret


def do_train(args, cfg):
Expand Down

0 comments on commit 2298ee5

Please sign in to comment.