sem_seg/test.py
def main() -> None:
    """Main function for model inference."""
    args = parse_args()

    assert args.format_only or args.show or args.show_dir, (
        "Please specify at least one operation (save/eval/format/show the "
        "results / save the results) with the argument '--format-only', "
        "'--show' or '--show-dir'"
    )

    cfg = mmcv.Config.fromfile(args.config)
    if cfg.load_from is None:
        cfg_name = os.path.split(args.config)[-1].replace(
            "_bdd100k.py", ".pth"
        )
        cfg.load_from = MODEL_SERVER + cfg_name
    if args.options is not None:
        cfg.merge_from_dict(args.options)
    # set cudnn_benchmark
    if cfg.get("cudnn_benchmark", False):
        torch.backends.cudnn.benchmark = True
    if args.aug_test:
        # hard code index
        cfg.data.test.pipeline[1].img_ratios = [
            0.5,
            0.75,
            1.0,
            1.25,
            1.5,
            1.75,
        ]
        cfg.data.test.pipeline[1].flip = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True

    # init distributed env first, since logger depends on the dist info.
    if args.launcher == "none":
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)

    # build the dataloader
    dataset = build_dataset(cfg.data.test)
    data_loader = build_dataloader(
        dataset,
        samples_per_gpu=1,
        workers_per_gpu=cfg.data.workers_per_gpu,
        dist=distributed,
        shuffle=False,
    )

    # build the model and load checkpoint
    cfg.model.train_cfg = None
    model = build_segmentor(cfg.model, test_cfg=cfg.get("test_cfg"))
    fp16_cfg = cfg.get("fp16", None)
    if fp16_cfg is not None:
        wrap_fp16_model(model)
    checkpoint = load_checkpoint(model, cfg.load_from, map_location="cpu")
    if "CLASSES" in checkpoint.get("meta", {}):
        model.CLASSES = checkpoint["meta"]["CLASSES"]
    else:
        print("'CLASSES' not found in meta, use dataset.CLASSES instead")
        model.CLASSES = dataset.CLASSES
    if "PALETTE" in checkpoint.get("meta", {}):
        model.PALETTE = checkpoint["meta"]["PALETTE"]
    else:
        print("'PALETTE' not found in meta, use dataset.PALETTE instead")
        model.PALETTE = dataset.PALETTE

    if not distributed:
        model = MMDataParallel(model, device_ids=[0])
        outputs = single_gpu_test(
            model,
            data_loader,
            args.show,
            args.show_dir,
            opacity=args.opacity,
        )
    else:
        model = MMDistributedDataParallel(
            model.cuda(),
            device_ids=[torch.cuda.current_device()],
            broadcast_buffers=False,
        )
        outputs = multi_gpu_test(
            model,
            data_loader,
            args.tmpdir,
            args.gpu_collect,
        )

    rank, _ = get_dist_info()
    if rank == 0:
        if args.format_only:
            dataset.format_results(outputs, args.format_dir)
Main function for model inference.
Main function for model inference.
Main function for model inference .
def main() -> None:
    args = parse_args()
    assert args.format_only or args.show or args.show_dir, (
        "Please specify at least one operation (save/eval/format/show the "
        "results / save the results) with the argument '--format-only', "
        "'--show' or '--show-dir'"
    )
    cfg = mmcv.Config.fromfile(args.config)
    if cfg.load_from is None:
        cfg_name = os.path.split(args.config)[-1].replace(
            "_bdd100k.py", ".pth"
        )
        cfg.load_from = MODEL_SERVER + cfg_name
    if args.options is not None:
        cfg.merge_from_dict(args.options)
    if cfg.get("cudnn_benchmark", False):
        torch.backends.cudnn.benchmark = True
    if args.aug_test:
        cfg.data.test.pipeline[1].img_ratios = [
            0.5,
            0.75,
            1.0,
            1.25,
            1.5,
            1.75,
        ]
        cfg.data.test.pipeline[1].flip = True
    cfg.model.pretrained = None
    cfg.data.test.test_mode = True
    if args.launcher == "none":
        distributed = False
    else:
        distributed = True
        init_dist(args.launcher, **cfg.dist_params)
    dataset = build_dataset(cfg.data.test)
    data_loader = build_dataloader(
        dataset,
        samples_per_gpu=1,
        workers_per_gpu=cfg.data.workers_per_gpu,
        dist=distributed,
        shuffle=False,
    )
    cfg.model.train_cfg = None
    model = build_segmentor(cfg.model, test_cfg=cfg.get("test_cfg"))
    fp16_cfg = cfg.get("fp16", None)
    if fp16_cfg is not None:
        wrap_fp16_model(model)
    checkpoint = load_checkpoint(model, cfg.load_from, map_location="cpu")
    if "CLASSES" in checkpoint.get("meta", {}):
        model.CLASSES = checkpoint["meta"]["CLASSES"]
    else:
        print("'CLASSES' not found in meta, use dataset.CLASSES instead")
        model.CLASSES = dataset.CLASSES
    if "PALETTE" in checkpoint.get("meta", {}):
        model.PALETTE = checkpoint["meta"]["PALETTE"]
    else:
        print("'PALETTE' not found in meta, use dataset.PALETTE instead")
        model.PALETTE = dataset.PALETTE
    if not distributed:
        model = MMDataParallel(model, device_ids=[0])
        outputs = single_gpu_test(
            model,
            data_loader,
            args.show,
            args.show_dir,
            opacity=args.opacity,
        )
    else:
        model = MMDistributedDataParallel(
            model.cuda(),
            device_ids=[torch.cuda.current_device()],
            broadcast_buffers=False,
        )
        outputs = multi_gpu_test(
            model,
            data_loader,
            args.tmpdir,
            args.gpu_collect,
        )
    rank, _ = get_dist_info()
    if rank == 0:
        if args.format_only:
            dataset.format_results(outputs, args.format_dir)
def main ( ) -> None : args = parse_args ( ) assert args . format_only or args . show or args . show_dir , ( "Please specify at least one operation (save/eval/format/show the " "results / save the results) with the argument '--format-only', " "'--show' or '--show-dir'" ) cfg = mmcv . Config . fromfile ( args . config ) if cfg . load_from is None : cfg_name = os . path . split ( args . config ) [ - 1 ] . replace ( "_bdd100k.py" , ".pth" ) cfg . load_from = MODEL_SERVER + cfg_name if args . options is not None : cfg . merge_from_dict ( args . options ) if cfg . get ( "cudnn_benchmark" , False ) : torch . backends . cudnn . benchmark = True if args . aug_test : cfg . data . test . pipeline [ 1 ] . img_ratios = [ 0.5 , 0.75 , 1.0 , 1.25 , 1.5 , 1.75 , ] cfg . data . test . pipeline [ 1 ] . flip = True cfg . model . pretrained = None cfg . data . test . test_mode = True if args . launcher == "none" : distributed = False else : distributed = True init_dist ( args . launcher , ** cfg . dist_params ) dataset = build_dataset ( cfg . data . test ) data_loader = build_dataloader ( dataset , samples_per_gpu = 1 , workers_per_gpu = cfg . data . workers_per_gpu , dist = distributed , shuffle = False , ) cfg . model . train_cfg = None model = build_segmentor ( cfg . model , test_cfg = cfg . get ( "test_cfg" ) ) fp16_cfg = cfg . get ( "fp16" , None ) if fp16_cfg is not None : wrap_fp16_model ( model ) checkpoint = load_checkpoint ( model , cfg . load_from , map_location = "cpu" ) if "CLASSES" in checkpoint . get ( "meta" , { } ) : model . CLASSES = checkpoint [ "meta" ] [ "CLASSES" ] else : print ( "'CLASSES' not found in meta, use dataset.CLASSES instead" ) model . CLASSES = dataset . CLASSES if "PALETTE" in checkpoint . get ( "meta" , { } ) : model . PALETTE = checkpoint [ "meta" ] [ "PALETTE" ] else : print ( "'PALETTE' not found in meta, use dataset.PALETTE instead" ) model . PALETTE = dataset . PALETTE if not distributed : model = MMDataParallel ( model , device_ids = [ 0 ] ) outputs = single_gpu_test ( model , data_loader , args . show , args . show_dir , opacity = args . opacity , ) else : model = MMDistributedDataParallel ( model . cuda ( ) , device_ids = [ torch . cuda . current_device ( ) ] , broadcast_buffers = False , ) outputs = multi_gpu_test ( model , data_loader , args . tmpdir , args . gpu_collect , ) rank , _ = get_dist_info ( ) if rank == 0 : if args . format_only : dataset . format_results ( outputs , args . format_dir )
Main function for model inference.
Main function for model inference .
"""Main function for model inference.""" # set cudnn_benchmark # hard code index # init distributed env first, since logger depends on the dist info. # build the dataloader # build the model and load checkpoint