From d8e35e1842c39973a17cb8f5aa3ab566bcd49f4e Mon Sep 17 00:00:00 2001 From: Rinat Shigapov Date: Mon, 1 Oct 2018 15:56:21 +0000 Subject: [PATCH 1/3] support modeling helpers defined in external packages --- detectron/modeling/model_builder.py | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/detectron/modeling/model_builder.py b/detectron/modeling/model_builder.py index 25ab21770..157d47c3b 100644 --- a/detectron/modeling/model_builder.py +++ b/detectron/modeling/model_builder.py @@ -145,7 +145,13 @@ def get_func(func_name): return globals()[parts[0]] # Otherwise, assume we're referencing a module under modeling module_name = 'detectron.modeling.' + '.'.join(parts[:-1]) - module = importlib.import_module(module_name) + try: + module = importlib.import_module(module_name) + except ImportError: + # Finally check if we're referencing a module from the environment + module_name = '.'.join(parts[:-1]) + module = importlib.import_module(module_name) + logger.debug('Using function %s from the environment', func_name) return getattr(module, parts[-1]) except Exception: logger.error('Failed to find function: {}'.format(func_name)) From 56480b9188d345b0f06039b977f2b01fea381937 Mon Sep 17 00:00:00 2001 From: Rinat Shigapov Date: Mon, 1 Oct 2018 15:57:24 +0000 Subject: [PATCH 2/3] add support to override DetectionModelHelper class --- detectron/core/config.py | 5 +++++ detectron/modeling/model_builder.py | 11 ++++++++++- 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/detectron/core/config.py b/detectron/core/config.py index 1f836e0e8..3a5e151de 100644 --- a/detectron/core/config.py +++ b/detectron/core/config.py @@ -440,6 +440,11 @@ # (e.g., 'generalized_rcnn', 'mask_rcnn', ...) __C.MODEL.TYPE = b'' +# Detection model helper class to use +# +# Allows to apply custom DetectionModelHelper implementation +__C.MODEL.MODEL_HELPER_CLASS = b'detectron.modeling.detector.DetectionModelHelper' + # The backbone conv body to use # The string must match a function that is imported in modeling.model_builder # (e.g., 'FPN.add_fpn_ResNet101_conv5_body' to specify a ResNet-101-FPN diff --git a/detectron/modeling/model_builder.py b/detectron/modeling/model_builder.py index 157d47c3b..90678817f 100644 --- a/detectron/modeling/model_builder.py +++ b/detectron/modeling/model_builder.py @@ -113,7 +113,16 @@ def create(model_type_func, train=False, gpu_id=0): targeted to a specific GPU by specifying gpu_id. This is used by optimizer.build_data_parallel_model() during test time. """ - model = DetectionModelHelper( + parts = cfg.MODEL.MODEL_HELPER_CLASS.split('.') + try: + module_name = '.'join(parts[:-1]) + module = importlib.import_module(module_name) + model_helper_class = getattr(module, parts[-1]) + except (IndexError, ImportError, AttributeError): + logger.error('Failed to find model helper: %s', model_helper_class) + raise + + model = model_helper_class( name=model_type_func, train=train, num_classes=cfg.MODEL.NUM_CLASSES, From 6f121a6a6d2d69f58e593e79042e05db0ee5c7a4 Mon Sep 17 00:00:00 2001 From: Rinat Shigapov Date: Wed, 10 Oct 2018 14:53:32 +0000 Subject: [PATCH 3/3] Fixed infer_simple test tools/infer_simple.py works with python3.7 and caffe2 installed with pytorch --- detectron/core/config.py | 2 +- detectron/modeling/model_builder.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/detectron/core/config.py b/detectron/core/config.py index a505730b5..7a12e652b 100644 --- a/detectron/core/config.py +++ b/detectron/core/config.py @@ -444,7 +444,7 @@ # Detection model helper class to use # # Allows to apply custom DetectionModelHelper implementation -__C.MODEL.MODEL_HELPER_CLASS = b'detectron.modeling.detector.DetectionModelHelper' +__C.MODEL.MODEL_HELPER_CLASS = 'detectron.modeling.detector.DetectionModelHelper' # The backbone conv body to use # The string must match a function that is imported in modeling.model_builder diff --git a/detectron/modeling/model_builder.py b/detectron/modeling/model_builder.py index 90678817f..b3f181123 100644 --- a/detectron/modeling/model_builder.py +++ b/detectron/modeling/model_builder.py @@ -115,7 +115,7 @@ def create(model_type_func, train=False, gpu_id=0): """ parts = cfg.MODEL.MODEL_HELPER_CLASS.split('.') try: - module_name = '.'join(parts[:-1]) + module_name = '.'.join(parts[:-1]) module = importlib.import_module(module_name) model_helper_class = getattr(module, parts[-1]) except (IndexError, ImportError, AttributeError):