MCPcopy Create free account
hub / github.com/VCIP-RGBD/DFormer / init_segmentor

Function init_segmentor

mmseg/apis/inference.py:12–39  ·  view source on GitHub ↗

Initialize a segmentor from config file. Args: config (str or :obj:`mmcv.Config`): Config file path or the config object. checkpoint (str, optional): Checkpoint path. If left as None, the model will not load any weights. device (str, optional) CPU

(config, checkpoint=None, device="cuda:0")

Source from the content-addressed store, hash-verified

10
11
12def init_segmentor(config, checkpoint=None, device="cuda:0"):
13 """Initialize a segmentor from config file.
14
15 Args:
16 config (str or :obj:`mmcv.Config`): Config file path or the config
17 object.
18 checkpoint (str, optional): Checkpoint path. If left as None, the model
19 will not load any weights.
20 device (str, optional) CPU/CUDA device option. Default 'cuda:0'.
21 Use 'cpu' for loading model on CPU.
22 Returns:
23 nn.Module: The constructed segmentor.
24 """
25 if isinstance(config, str):
26 config = mmcv.Config.fromfile(config)
27 elif not isinstance(config, mmcv.Config):
28 raise TypeError("config must be a filename or Config object, but got {}".format(type(config)))
29 config.model.pretrained = None
30 config.model.train_cfg = None
31 model = build_segmentor(config.model, test_cfg=config.get("test_cfg"))
32 if checkpoint is not None:
33 checkpoint = load_checkpoint(model, checkpoint, map_location="cpu")
34 model.CLASSES = checkpoint["meta"]["CLASSES"]
35 model.PALETTE = checkpoint["meta"]["PALETTE"]
36 model.cfg = config # save the config in the model for convenience
37 model.to(device)
38 model.eval()
39 return model
40
41
42class LoadImage:

Callers

nothing calls this directly

Calls 2

build_segmentorFunction · 0.90
formatMethod · 0.45

Tested by

no test coverage detected