Ejemplo n.º 1
0
    parser = argparse.ArgumentParser()
    parser.add_argument('--path', type=str, help='model checkpoints path')
    parser.add_argument('--weights',
                        type=str,
                        default='./checkpoints/1/model/3-regular.pth',
                        help='the weights file you want to test')  # 修改点
    args = parser.parse_args()
    config_path = os.path.join(args.path, 'config.yml')

    # load config file
    config = Config(config_path)

    os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(e) for e in config.GPU)
    if torch.cuda.is_available():
        config.DEVICE = torch.device("cuda")
        print('\nGPU IS AVAILABLE')
        torch.backends.cudnn.benchmark = True
    else:
        config.DEVICE = torch.device("cpu")

    net = VGG16().to(config.DEVICE)

    test_set = ImageFolder(config.TEST_PATH, transform=test_tf)
    test_data = torch.utils.data.DataLoader(test_set,
                                            batch_size=config.BATCH_SIZE,
                                            shuffle=True)

    pth_path = args.weights
    net.load_state_dict(torch.load(pth_path), config.DEVICE)
    ##print(net)
Ejemplo n.º 2
0
def load_config(mode=None):
    r"""loads model config

    Args:
        mode (int): 1: train, 2: test, 3: eval, reads from config file if not specified
    """

    parser = argparse.ArgumentParser()
    parser.add_argument('--path',
                        '--checkpoints',
                        type=str,
                        default='./checkpoints',
                        help='model checkpoints path (default: ./checkpoints)')
    parser.add_argument('--output',
                        type=str,
                        default='./output',
                        help='path to the output directory')

    # test mode
    if mode >= 2:
        parser.add_argument(
            '--input',
            type=str,
            help='path to the input images directory or an input image')
        parser.add_argument('--mask',
                            type=str,
                            help='path to the masks directory or a mask file')

    args = parser.parse_args()
    config_path = os.path.join(args.path, 'config.yml')

    # create checkpoints path if does't exist
    create_dir(args.path)

    # copy config template if does't exist
    if not os.path.exists(config_path):
        copyfile('./config.yml.example', config_path)

    # load config file
    config = Config(config_path)
    config.print()
    # train mode
    if mode == 1:
        config.MODE = 1

    # test mode
    elif mode == 2:
        config.MODE = 2
        # config.INPUT_SIZE = 0         Set to 0 for one to one mapping

        if args.input is not None:
            config.TEST_FLIST = args.input

        if args.mask is not None:
            config.TEST_MASK_FLIST = args.mask

        if args.output is not None:
            config.RESULTS = args.output

    # eval mode
    elif mode == 3:
        config.MODE = 3
        # set cuda visble devices from config file

    # Initialization
    os.environ['CUDA_VISIBLE_DEVICES'] = ','.join(str(e) for e in config.GPU)
    # os.environ["CUDA_VISIBLE_DEVICES"] = "0"
    # init device
    if torch.cuda.is_available():
        config.DEVICE = torch.device("cuda")
        torch.backends.cudnn.benchmark = True  # cudnn auto-tuner
    else:
        config.DEVICE = torch.device("cpu")
    # set cv2 running threads to 1 (prevents deadlocks with pytorch dataloader)
    cv2.setNumThreads(0)
    # initialize random seed
    torch.manual_seed(config.SEED)
    torch.cuda.manual_seed_all(config.SEED)
    np.random.seed(config.SEED)
    random.seed(config.SEED)

    return config