Beispiel #1
0
    assert (
        cfg.test_imagedir is not None
    ), "Imagedir needs to be specified either via commandline argument (--imagedir) or config (test_imagedir)."

    if not hasattr(cfg, "num_classes"):  # infer number of classes
        with open(cfg.annotations) as f:
            NUM_CLASSES = len(json.load(f)["categories"])
        cfg.num_classes = NUM_CLASSES

    pathlib.Path(args.outdir).mkdir(exist_ok=True, parents=True)
    save_config(cfg, args.outdir)
    print(cfg)

    print("Initializing model from checkpoint {}".format(args.checkpoint))
    checkpoint = torch.load(args.checkpoint, map_location="cpu")
    model = Model.from_config(cfg)
    missing_keys, unexpected_keys = model.load_state_dict(
        checkpoint['model_state_dict'], strict=False)

    assert not missing_keys, "Checkpoint is missing keys required to initialize the model: {}".format(
        missing_keys)
    if len(unexpected_keys):
        print(
            "Checkpoint contains unexpected keys that were not used to initialize the model: "
        )
        print(unexpected_keys)

    model.to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))

    test(model,
         cfg.test_annotations,
    assert (
        cfg.test_imagedir is not None
    ), "Imagedir needs to be specified either via commandline argument (--imagedir) or config (test_imagedir)."

    if not hasattr(cfg, "num_classes"):  # infer number of classes
        with open(cfg.annotations) as f:
            NUM_CLASSES = len(json.load(f)["categories"])
        cfg.num_classes = NUM_CLASSES

    pathlib.Path(args.outdir).mkdir(exist_ok=True, parents=True)
    save_config(cfg, args.outdir)
    print(cfg)

    print("Initializing model from checkpoint {}".format(args.checkpoint))
    checkpoint = torch.load(args.checkpoint, map_location="cpu")
    model = Model.from_config(cfg, extended_output=True)
    missing_keys, unexpected_keys = model.load_state_dict(
        checkpoint['model_state_dict'], strict=False)

    assert not missing_keys, "Checkpoint is missing keys required to initialize the model: {}".format(
        missing_keys)
    if len(unexpected_keys):
        print(
            "Checkpoint contains unexpected keys that were not used to initialize the model: "
        )
        print(unexpected_keys)

    model.to(torch.device("cuda:0" if torch.cuda.is_available() else "cpu"))

    evaluate_uncertainty(model,
                         cfg.test_annotations,