Repository: open-mmlab/mmdetection

Issue:
make `mmdet.apis.init_detector` work with pathlib
**Describe the feature**
make `mmdet.apis.init_detector` work with pathlib 
**Motivation**
Since mmcv works with pathlib, there is no reason to use str path only. (ref https://github.com/open-mmlab/mmcv/issues/3).
https://github.com/open-mmlab/mmdetection/blob/3e2693151add9b5d6db99b944da020cba837266b/mmdet/apis/inference.py#L31




Discussion:
and mmseg, mmcls:
https://github.com/open-mmlab/mmclassification/blob/5a67bc8a1aefd5493732b8f6ab10ee8c04839773/mmcls/apis/inference.py#L27

https://github.com/open-mmlab/mmsegmentation/blob/4bd32f4c3dec15b54e64197570edc3ab3fdcf164/mmseg/apis/inference.py#L25
Thanks for your valuable suggestion. we will plan to do this. It's also very appreciated if you would like to apply this function. Do you have a plan to make a pr in mmdetection?
Sorry, my schedule is full. I should have reported this weeks ago.

Resolving patch:
diff --git a/mmdet/apis/inference.py b/mmdet/apis/inference.py
--- a/mmdet/apis/inference.py
+++ b/mmdet/apis/inference.py
@@ -1,5 +1,6 @@
 # Copyright (c) OpenMMLab. All rights reserved.
 import warnings
+from pathlib import Path
 
 import mmcv
 import numpy as np
@@ -18,8 +19,8 @@ def init_detector(config, checkpoint=None, device='cuda:0', cfg_options=None):
     """Initialize a detector from config file.
 
     Args:
-        config (str or :obj:`mmcv.Config`): Config file path or the config
-            object.
+        config (str, :obj:`Path`, or :obj:`mmcv.Config`): Config file path,
+            :obj:`Path`, or the config object.
         checkpoint (str, optional): Checkpoint path. If left as None, the model
             will not load any weights.
         cfg_options (dict): Options to override some settings in the used
@@ -28,7 +29,7 @@ def init_detector(config, checkpoint=None, device='cuda:0', cfg_options=None):
     Returns:
         nn.Module: The constructed detector.
     """
-    if isinstance(config, str):
+    if isinstance(config, (str, Path)):
         config = mmcv.Config.fromfile(config)
     elif not isinstance(config, mmcv.Config):
         raise TypeError('config must be a filename or Config object, '
diff --git a/mmdet/models/detectors/kd_one_stage.py b/mmdet/models/detectors/kd_one_stage.py
--- a/mmdet/models/detectors/kd_one_stage.py
+++ b/mmdet/models/detectors/kd_one_stage.py
@@ -1,4 +1,6 @@
 # Copyright (c) OpenMMLab. All rights reserved.
+from pathlib import Path
+
 import mmcv
 import torch
 from mmcv.runner import load_checkpoint
@@ -34,7 +36,7 @@ def __init__(self,
                          pretrained)
         self.eval_teacher = eval_teacher
         # Build teacher model
-        if isinstance(teacher_config, str):
+        if isinstance(teacher_config, (str, Path)):
             teacher_config = mmcv.Config.fromfile(teacher_config)
         self.teacher_model = build_detector(teacher_config['model'])
         if teacher_ckpt is not None:


Test patch:
diff --git a/tests/test_runtime/test_apis.py b/tests/test_runtime/test_apis.py
new file mode 100644
--- /dev/null
+++ b/tests/test_runtime/test_apis.py
@@ -0,0 +1,32 @@
+import os
+from pathlib import Path
+
+import pytest
+
+from mmdet.apis import init_detector
+
+
+def test_init_detector():
+    project_dir = os.path.abspath(os.path.dirname(os.path.dirname(__file__)))
+    project_dir = os.path.join(project_dir, '..')
+
+    config_file = os.path.join(
+        project_dir, 'configs/mask_rcnn/mask_rcnn_r50_fpn_1x_coco.py')
+
+    # test init_detector with config_file: str and cfg_options
+    cfg_options = dict(
+        model=dict(
+            backbone=dict(
+                depth=18,
+                init_cfg=dict(
+                    type='Pretrained', checkpoint='torchvision://resnet18'))))
+    model = init_detector(config_file, device='cpu', cfg_options=cfg_options)
+
+    # test init_detector with :obj:`Path`
+    config_path_object = Path(config_file)
+    model = init_detector(config_path_object, device='cpu')
+
+    # test init_detector with undesirable type
+    with pytest.raises(TypeError):
+        config_list = [config_file]
+        model = init_detector(config_list)  # noqa: F841