Repository: airctic/icevision

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



Discussion:

Resolving patch:
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:


Test patch: