{"record":{"id":"9551c08547f6e34e","repo":"open-mmlab/mmdetection","slug":"config-must-be-a-filename-or-config-object-but-go","errorCode":null,"errorMessage":"config must be a filename or Config object, but got {type(config)}","messagePattern":"config must be a filename or Config object, but got (.+?)","errorType":"validation","errorClass":"TypeError","httpStatus":null,"severity":"error","filePath":"mmdet/apis/inference.py","lineNumber":55,"sourceCode":"            :obj:`Path`, or the config object.\n        checkpoint (str, optional): Checkpoint path. If left as None, the model\n            will not load any weights.\n        palette (str): Color palette used for visualization. If palette\n            is stored in checkpoint, use checkpoint's palette first, otherwise\n            use externally passed palette. Currently, supports 'coco', 'voc',\n            'citys' and 'random'. Defaults to none.\n        device (str): The device where the anchors will be put on.\n            Defaults to cuda:0.\n        cfg_options (dict, optional): Options to override some settings in\n            the used config.\n\n    Returns:\n        nn.Module: The constructed detector.\n    \"\"\"\n    if isinstance(config, (str, Path)):\n        config = Config.fromfile(config)\n    elif not isinstance(config, Config):\n        raise TypeError('config must be a filename or Config object, '\n                        f'but got {type(config)}')\n    if cfg_options is not None:\n        config.merge_from_dict(cfg_options)\n    elif 'init_cfg' in config.model.backbone:\n        config.model.backbone.init_cfg = None\n\n    scope = config.get('default_scope', 'mmdet')\n    if scope is not None:\n        init_default_scope(config.get('default_scope', 'mmdet'))\n\n    model = MODELS.build(config.model)\n    model = revert_sync_batchnorm(model)\n    if checkpoint is None:\n        warnings.simplefilter('once')\n        warnings.warn('checkpoint is None, use COCO classes by default.')\n        model.dataset_meta = {'classes': get_classes('coco')}\n    else:\n        checkpoint = load_checkpoint(model, checkpoint, map_location='cpu')","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/open-mmlab/mmdetection/blob/cfd5d3a985b0249de009b67d04f37263e11cdf3d/mmdet/apis/inference.py#L37-L73","documentation":"init_detector only accepts a config as a str/Path filename or an already-built mmengine Config object. Passing anything else (dict, None, bytes, PosixPath-like objects not subclassing Path) raises this TypeError. Internally the function calls Config.fromfile on str/Path; dicts are deliberately not accepted because lazy imports and variables in config files require file context in some flows.","triggerScenarios":"Calling init_detector(cfg_dict, checkpoint) with a plain dict built in Python; passing None because the config path variable was never set; passing an opened file object or a ConfigDict obtained by other means.","commonSituations":"Programmatically modifying configs in scripts (users naturally build dicts), passing a pathlib2/other Path substitute, or a variable that is None due to an unset CLI arg.","solutions":["If you have a dict, convert it: from mmengine.utils import Config; cfg = Config.fromfile('base.py') then cfg.merge_from_dict(your_dict), or write the dict to a temp .py file and pass its path","Pass the config file path string directly: init_detector('configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py', 'ckpt.pth')","Pass an mmengine.config.Config object produced by Config.fromfile(path)","Check the variable is not None before calling (guard CLI arguments)"],"exampleFix":"# before\ncfg = dict(model=dict(type='FasterRCNN', ...))\nmodel = init_detector(cfg, 'ckpt.pth')  # TypeError\n# after\nfrom mmengine.config import Config\ncfg = Config.fromfile('configs/faster_rcnn/faster-rcnn_r50_fpn_1x_coco.py')\ncfg.merge_from_dict(dict(model=dict(backbone=dict(depth=101))))\nmodel = init_detector(cfg, 'ckpt.pth')","handlingStrategy":"type-guard","validationCode":"from pathlib import Path\nfrom mmengine.config import Config\nassert config is not None, 'config path was never set'\nif isinstance(config, (str, Path)):\n    config = Config.fromfile(config)\nassert isinstance(config, Config), f'expected str/Path/Config, got {type(config)}'","typeGuard":"from mmengine.config import Config\nfrom typing import Union\nimport pathlib\ndef is_valid_detector_config(x) -> bool:\n    return isinstance(x, (str, pathlib.Path, Config))","tryCatchPattern":"try:\n    model = init_detector(config, ckpt, device)\nexcept TypeError as e:\n    if 'config must be a filename' in str(e):\n        raise SystemExit('Pass a config file path or Config.fromfile(...) result, not a dict')\n    raise","preventionTips":["Normalize configs to Config objects at system entry points","Guard CLI args: if not args.config: parser.error('--config required')","Never hand init_detector a plain dict; write/merge through Config.fromfile"],"tags":["mmdetection","config","typeerror","init-detector"],"backgroundTag":"invalid-argument-type","analyzedSha":"cfd5d3a985b0249de009b67d04f37263e11cdf3d","analyzedAt":"2026-08-27T20:54:20.183Z","schemaVersion":2},"datasetVersion":"2026-08-28T00:17:15.603Z"}