prompt: We are currently solving the following issue within our repository. Here is the issue text:
  --- BEGIN ISSUE ---
  Allow passing a Config object to the MMDetection models
  ## 🚀 Feature
  **Is your feature request related to a problem? Please describe.**
  In the current version, to update the loss_weight for example, We have to duplicate an existing config .py file and make the changes in the new file, and pass it the model method. The latter expect a config file_path
  
  
  **Describe the solution you'd like**
   I think it would be better to pass the MMDetection config object instead to the model method. By doing so, we could let the users populate the config object and update any field they wish to change, without creating and updating external files, like this: 
  
  ```
  cfg = Config.fromfile(cfg_filepath)
  cfg.model.bbox_head.loss_cls.loss_weight = 0.8
  cfg.model.bbox_head.loss_bbox.loss_weight = 2
  ```
  
  
  **Additional context**
  There is a discussion in our Discord forum around this issue:
  https://discordapp.com/channels/735877944085446747/780951884683935744/811650062706540644
  
  
  --- END ISSUE ---
  
  Below are some code segments, each from a relevant file. One or more of these files may contain bugs.
  --- BEGIN FILES ---
  Path: `icevision/models/mmdet/common/bbox/two_stage/model.py`
  Content:
  ```
  1 __all__ = ["model"]
  2 
  3 from icevision.imports import *
  4 from mmcv import Config
  5 from mmdet.models import build_detector
  6 from mmcv.runner import load_checkpoint
  7 
  8 
  9 def model(
  10     cfg_path: Union[str, Path],
  11     num_classes: int,
  12     weights_path: Optional[Union[str, Path]] = None,
  13 ) -> nn.Module:
  14     cfg = Config.fromfile(str(cfg_path))
  15     cfg.model.roi_head.bbox_head.num_classes = num_classes - 1
  16     if weights_path is not None:
  17         cfg.model.pretrained = None
  18 
  19     _model = build_detector(cfg.model, cfg.get("train_cfg"), cfg.get("test_cfg"))
  20 
  21     if weights_path is not None:
  22         load_checkpoint(_model, str(weights_path))
  23 
  24     return _model
  25
  ```
  
  Path: `icevision/models/mmdet/common/bbox/single_stage/model.py`
  Content:
  ```
  1 __all__ = ["model"]
  2 
  3 from icevision.imports import *
  4 from mmcv import Config
  5 from mmdet.models import build_detector
  6 from mmcv.runner import load_checkpoint
  7 
  8 
  9 def model(
  10     cfg_path: Union[str, Path],
  11     num_classes: int,
  12     weights_path: Optional[Union[str, Path]] = None,
  13 ) -> nn.Module:
  14     cfg = Config.fromfile(str(cfg_path))
  15     cfg.model.bbox_head.num_classes = num_classes - 1
  16     if weights_path is not None:
  17         cfg.model.pretrained = None
  18 
  19     _model = build_detector(cfg.model, cfg.get("train_cfg"), cfg.get("test_cfg"))
  20 
  21     if weights_path is not None:
  22         load_checkpoint(_model, str(weights_path))
  23 
  24     return _model
  25
  ```
  
  Path: `icevision/models/mmdet/common/mask/two_stage/model.py`
  Content:
  ```
  1 __all__ = ["model"]
  2 
  3 from icevision.imports import *
  4 from mmcv import Config
  5 from mmdet.models import build_detector
  6 from mmcv.runner import load_checkpoint
  7 
  8 
  9 def model(
  10     cfg_path: Union[str, Path],
  11     num_classes: int,
  12     weights_path: Optional[Union[str, Path]] = None,
  13 ) -> nn.Module:
  14     cfg = Config.fromfile(str(cfg_path))
  15     cfg.model.roi_head.bbox_head.num_classes = num_classes - 1
  16     cfg.model.roi_head.mask_head.num_classes = num_classes - 1
  17     if weights_path is not None:
  18         cfg.model.pretrained = None
  19 
  20     _model = build_detector(cfg.model, cfg.get("train_cfg"), cfg.get("test_cfg"))
  21 
  22     if weights_path is not None:
  23         load_checkpoint(_model, str(weights_path))
  24 
  25     return _model
  26
  ```
  --- END FILES ---
  
  Please first localize the bug based on the issue statement, and then generate a patch according to the `git diff` format fenced by three backticks.
  
  Here is an example:
  
  ```diff
  diff --git a/examples/server_async.py b/examples/server_async.py
  --- a/examples/server_async.py
  +++ b/examples/server_async.py
  @@ -313,4 +313,4 @@
   
   
   if __name__ == "__main__":
  -    asyncio.run(run_async_server("."), debug=True)
  +    asyncio.run(run_async_server(), debug=True)
  diff --git a/examples/server_sync.py b/examples/server_sync.py
  --- a/examples/server_sync.py
  +++ b/examples/server_sync.py
  @@ -313,5 +313,5 @@
   
   
   if __name__ == "__main__":
  -    server = run_sync_server(".")
  +    server = run_sync_server()
       server.shutdown()
  
  ```
  
golden_diff: diff --git a/icevision/models/mmdet/common/bbox/single_stage/model.py b/icevision/models/mmdet/common/bbox/single_stage/model.py
  --- a/icevision/models/mmdet/common/bbox/single_stage/model.py
  +++ b/icevision/models/mmdet/common/bbox/single_stage/model.py
  @@ -7,11 +7,16 @@
   
   
   def model(
  -    cfg_path: Union[str, Path],
  +    cfg: Union[str, Path, Config],
       num_classes: int,
       weights_path: Optional[Union[str, Path]] = None,
   ) -> nn.Module:
  -    cfg = Config.fromfile(str(cfg_path))
  +
  +    # if `cfg` argument is a path (str, Path) create an Config object from the file
  +    # otherwise cfg should be already an Config object
  +    if isinstance(cfg, (str, Path)):
  +        cfg = Config.fromfile(str(cfg))
  +
       cfg.model.bbox_head.num_classes = num_classes - 1
       if weights_path is not None:
           cfg.model.pretrained = None
  diff --git a/icevision/models/mmdet/common/bbox/two_stage/model.py b/icevision/models/mmdet/common/bbox/two_stage/model.py
  --- a/icevision/models/mmdet/common/bbox/two_stage/model.py
  +++ b/icevision/models/mmdet/common/bbox/two_stage/model.py
  @@ -7,11 +7,16 @@
   
   
   def model(
  -    cfg_path: Union[str, Path],
  +    cfg: Union[str, Path, Config],
       num_classes: int,
       weights_path: Optional[Union[str, Path]] = None,
   ) -> nn.Module:
  -    cfg = Config.fromfile(str(cfg_path))
  +
  +    # if `cfg` argument is a path (str, Path) create an Config object from the file
  +    # otherwise cfg should be already an Config object
  +    if isinstance(cfg, (str, Path)):
  +        cfg = Config.fromfile(str(cfg))
  +
       cfg.model.roi_head.bbox_head.num_classes = num_classes - 1
       if weights_path is not None:
           cfg.model.pretrained = None
  diff --git a/icevision/models/mmdet/common/mask/two_stage/model.py b/icevision/models/mmdet/common/mask/two_stage/model.py
  --- a/icevision/models/mmdet/common/mask/two_stage/model.py
  +++ b/icevision/models/mmdet/common/mask/two_stage/model.py
  @@ -7,11 +7,16 @@
   
   
   def model(
  -    cfg_path: Union[str, Path],
  +    cfg: Union[str, Path, Config],
       num_classes: int,
       weights_path: Optional[Union[str, Path]] = None,
   ) -> nn.Module:
  -    cfg = Config.fromfile(str(cfg_path))
  +
  +    # if `cfg` argument is a path (str, Path) create an Config object from the file
  +    # otherwise cfg should be already an Config object
  +    if isinstance(cfg, (str, Path)):
  +        cfg = Config.fromfile(str(cfg))
  +
       cfg.model.roi_head.bbox_head.num_classes = num_classes - 1
       cfg.model.roi_head.mask_head.num_classes = num_classes - 1
       if weights_path is not None:
  
verification_info: {"golden_diff": "diff --git a/icevision/models/mmdet/common/bbox/single_stage/model.py b/icevision/models/mmdet/common/bbox/single_stage/model.py\n--- a/icevision/models/mmdet/common/bbox/single_stage/model.py\n+++ b/icevision/models/mmdet/common/bbox/single_stage/model.py\n@@ -7,11 +7,16 @@\n \n \n def model(\n-    cfg_path: Union[str, Path],\n+    cfg: Union[str, Path, Config],\n     num_classes: int,\n     weights_path: Optional[Union[str, Path]] = None,\n ) -> nn.Module:\n-    cfg = Config.fromfile(str(cfg_path))\n+\n+    # if `cfg` argument is a path (str, Path) create an Config object from the file\n+    # otherwise cfg should be already an Config object\n+    if isinstance(cfg, (str, Path)):\n+        cfg = Config.fromfile(str(cfg))\n+\n     cfg.model.bbox_head.num_classes = num_classes - 1\n     if weights_path is not None:\n         cfg.model.pretrained = None\ndiff --git a/icevision/models/mmdet/common/bbox/two_stage/model.py b/icevision/models/mmdet/common/bbox/two_stage/model.py\n--- a/icevision/models/mmdet/common/bbox/two_stage/model.py\n+++ b/icevision/models/mmdet/common/bbox/two_stage/model.py\n@@ -7,11 +7,16 @@\n \n \n def model(\n-    cfg_path: Union[str, Path],\n+    cfg: Union[str, Path, Config],\n     num_classes: int,\n     weights_path: Optional[Union[str, Path]] = None,\n ) -> nn.Module:\n-    cfg = Config.fromfile(str(cfg_path))\n+\n+    # if `cfg` argument is a path (str, Path) create an Config object from the file\n+    # otherwise cfg should be already an Config object\n+    if isinstance(cfg, (str, Path)):\n+        cfg = Config.fromfile(str(cfg))\n+\n     cfg.model.roi_head.bbox_head.num_classes = num_classes - 1\n     if weights_path is not None:\n         cfg.model.pretrained = None\ndiff --git a/icevision/models/mmdet/common/mask/two_stage/model.py b/icevision/models/mmdet/common/mask/two_stage/model.py\n--- a/icevision/models/mmdet/common/mask/two_stage/model.py\n+++ b/icevision/models/mmdet/common/mask/two_stage/model.py\n@@ -7,11 +7,16 @@\n \n \n def model(\n-    cfg_path: Union[str, Path],\n+    cfg: Union[str, Path, Config],\n     num_classes: int,\n     weights_path: Optional[Union[str, Path]] = None,\n ) -> nn.Module:\n-    cfg = Config.fromfile(str(cfg_path))\n+\n+    # if `cfg` argument is a path (str, Path) create an Config object from the file\n+    # otherwise cfg should be already an Config object\n+    if isinstance(cfg, (str, Path)):\n+        cfg = Config.fromfile(str(cfg))\n+\n     cfg.model.roi_head.bbox_head.num_classes = num_classes - 1\n     cfg.model.roi_head.mask_head.num_classes = num_classes - 1\n     if weights_path is not None:\n", "issue": "Allow passing a Config object to the MMDetection models\n## \ud83d\ude80 Feature\r\n**Is your feature request related to a problem? Please describe.**\r\nIn the current version, to update the loss_weight for example, We have to duplicate an existing config .py file and make the changes in the new file, and pass it the model method. The latter expect a config file_path\r\n\r\n\r\n**Describe the solution you'd like**\r\n I think it would be better to pass the MMDetection config object instead to the model method. By doing so, we could let the users populate the config object and update any field they wish to change, without creating and updating external files, like this: \r\n\r\n```\r\ncfg = Config.fromfile(cfg_filepath)\r\ncfg.model.bbox_head.loss_cls.loss_weight = 0.8\r\ncfg.model.bbox_head.loss_bbox.loss_weight = 2\r\n```\r\n\r\n\r\n**Additional context**\r\nThere is a discussion in our Discord forum around this issue:\r\nhttps://discordapp.com/channels/735877944085446747/780951884683935744/811650062706540644\r\n\n", "before_files": [{"content": "__all__ = [\"model\"]\n\nfrom icevision.imports import *\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\n\n\ndef model(\n    cfg_path: Union[str, Path],\n    num_classes: int,\n    weights_path: Optional[Union[str, Path]] = None,\n) -> nn.Module:\n    cfg = Config.fromfile(str(cfg_path))\n    cfg.model.roi_head.bbox_head.num_classes = num_classes - 1\n    if weights_path is not None:\n        cfg.model.pretrained = None\n\n    _model = build_detector(cfg.model, cfg.get(\"train_cfg\"), cfg.get(\"test_cfg\"))\n\n    if weights_path is not None:\n        load_checkpoint(_model, str(weights_path))\n\n    return _model\n", "path": "icevision/models/mmdet/common/bbox/two_stage/model.py"}, {"content": "__all__ = [\"model\"]\n\nfrom icevision.imports import *\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\n\n\ndef model(\n    cfg_path: Union[str, Path],\n    num_classes: int,\n    weights_path: Optional[Union[str, Path]] = None,\n) -> nn.Module:\n    cfg = Config.fromfile(str(cfg_path))\n    cfg.model.bbox_head.num_classes = num_classes - 1\n    if weights_path is not None:\n        cfg.model.pretrained = None\n\n    _model = build_detector(cfg.model, cfg.get(\"train_cfg\"), cfg.get(\"test_cfg\"))\n\n    if weights_path is not None:\n        load_checkpoint(_model, str(weights_path))\n\n    return _model\n", "path": "icevision/models/mmdet/common/bbox/single_stage/model.py"}, {"content": "__all__ = [\"model\"]\n\nfrom icevision.imports import *\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\n\n\ndef model(\n    cfg_path: Union[str, Path],\n    num_classes: int,\n    weights_path: Optional[Union[str, Path]] = None,\n) -> nn.Module:\n    cfg = Config.fromfile(str(cfg_path))\n    cfg.model.roi_head.bbox_head.num_classes = num_classes - 1\n    cfg.model.roi_head.mask_head.num_classes = num_classes - 1\n    if weights_path is not None:\n        cfg.model.pretrained = None\n\n    _model = build_detector(cfg.model, cfg.get(\"train_cfg\"), cfg.get(\"test_cfg\"))\n\n    if weights_path is not None:\n        load_checkpoint(_model, str(weights_path))\n\n    return _model\n", "path": "icevision/models/mmdet/common/mask/two_stage/model.py"}], "after_files": [{"content": "__all__ = [\"model\"]\n\nfrom icevision.imports import *\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\n\n\ndef model(\n    cfg: Union[str, Path, Config],\n    num_classes: int,\n    weights_path: Optional[Union[str, Path]] = None,\n) -> nn.Module:\n\n    # if `cfg` argument is a path (str, Path) create an Config object from the file\n    # otherwise cfg should be already an Config object\n    if isinstance(cfg, (str, Path)):\n        cfg = Config.fromfile(str(cfg))\n\n    cfg.model.roi_head.bbox_head.num_classes = num_classes - 1\n    if weights_path is not None:\n        cfg.model.pretrained = None\n\n    _model = build_detector(cfg.model, cfg.get(\"train_cfg\"), cfg.get(\"test_cfg\"))\n\n    if weights_path is not None:\n        load_checkpoint(_model, str(weights_path))\n\n    return _model\n", "path": "icevision/models/mmdet/common/bbox/two_stage/model.py"}, {"content": "__all__ = [\"model\"]\n\nfrom icevision.imports import *\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\n\n\ndef model(\n    cfg: Union[str, Path, Config],\n    num_classes: int,\n    weights_path: Optional[Union[str, Path]] = None,\n) -> nn.Module:\n\n    # if `cfg` argument is a path (str, Path) create an Config object from the file\n    # otherwise cfg should be already an Config object\n    if isinstance(cfg, (str, Path)):\n        cfg = Config.fromfile(str(cfg))\n\n    cfg.model.bbox_head.num_classes = num_classes - 1\n    if weights_path is not None:\n        cfg.model.pretrained = None\n\n    _model = build_detector(cfg.model, cfg.get(\"train_cfg\"), cfg.get(\"test_cfg\"))\n\n    if weights_path is not None:\n        load_checkpoint(_model, str(weights_path))\n\n    return _model\n", "path": "icevision/models/mmdet/common/bbox/single_stage/model.py"}, {"content": "__all__ = [\"model\"]\n\nfrom icevision.imports import *\nfrom mmcv import Config\nfrom mmdet.models import build_detector\nfrom mmcv.runner import load_checkpoint\n\n\ndef model(\n    cfg: Union[str, Path, Config],\n    num_classes: int,\n    weights_path: Optional[Union[str, Path]] = None,\n) -> nn.Module:\n\n    # if `cfg` argument is a path (str, Path) create an Config object from the file\n    # otherwise cfg should be already an Config object\n    if isinstance(cfg, (str, Path)):\n        cfg = Config.fromfile(str(cfg))\n\n    cfg.model.roi_head.bbox_head.num_classes = num_classes - 1\n    cfg.model.roi_head.mask_head.num_classes = num_classes - 1\n    if weights_path is not None:\n        cfg.model.pretrained = None\n\n    _model = build_detector(cfg.model, cfg.get(\"train_cfg\"), cfg.get(\"test_cfg\"))\n\n    if weights_path is not None:\n        load_checkpoint(_model, str(weights_path))\n\n    return _model\n", "path": "icevision/models/mmdet/common/mask/two_stage/model.py"}]}