コード例 #1
0
    criterion = nn.CrossEntropyLoss()
    model = model.cuda(device) if use_gpu else model
    criterion = criterion.cuda(device) if use_gpu else criterion

    # load pretrained model
    if args.model2load != None:
        if args.model2load.endswith('.ckpt'):
            model = load_ckpt_model(model=model,
                                    name='fc1',
                                    ckpt_file=args.model2load,
                                    device=device,
                                    param2load=None)
        elif args.model2load.endswith('.mat'):
            model = load_mat_model(model=model,
                                   name='fc1',
                                   mat_file=args.model2load,
                                   device=device,
                                   param2load=None)
        elif args.model2load.endswith('.pth'):
            model = load_pth_model(model=model,
                                   name='fc1',
                                   pth_file=args.model2load,
                                   device=device,
                                   param2load=None)
        else:
            raise ValueError('The format of %s is not supported' %
                             args.model2load)

    # Parse the optimizer
    optimizer = parse_optim(policy=args.optim, params=model.parameters())
    # Parse norms
コード例 #2
0
ファイル: certify_mlp.py プロジェクト: liuchen11/PolyEnvelope
    out_dir = os.path.dirname(args.out_file)
    if out_dir != '' and not os.path.exists(out_dir):
        os.makedirs(out_dir)

    # Parse model
    model = MLP(in_dim=args.in_dim,
                hidden_dims=args.hidden_dims,
                out_dim=args.out_dim,
                nonlinearity=args.nonlinearity)
    model = model.cuda(device) if use_gpu else model
    if args.model2load.endswith('.ckpt'):
        ckpt = torch.load(args.model2load)
        model.load_state_dict(ckpt)
    elif args.model2load.endswith('.mat'):
        model = load_mat_model(model=model,
                               name='fc1',
                               mat_file=args.model2load,
                               device=device)
    elif args.model2load.endswith('.pth'):
        model = load_pth_model(model=model,
                               name='fc1',
                               pth_file=args.model2load,
                               device=device)
    else:
        raise ValueError('The format of %s is not supported' % args.model2load)

    # Parse norm
    norm = args.norm if args.norm > 0 else np.inf

    # Prepare the item to save
    configs = {kwarg: value for kwarg, value in args._get_kwargs()}
    tosave = {
コード例 #3
0
    criterion = nn.CrossEntropyLoss()
    model = model.cuda(device) if use_gpu else model
    criterion = criterion.cuda(device) if use_gpu else criterion

    # load pretrained model
    if args.model2load != None:
        if args.model2load.endswith('.ckpt'):
            model = load_ckpt_model(model=model,
                                    name='lenet',
                                    ckpt_file=args.model2load,
                                    device=device,
                                    param2load=args.load_mode)
        elif args.model2load.endswith('.mat'):
            model = load_mat_model(model=model,
                                   name='lenet',
                                   mat_file=args.model2load,
                                   device=device,
                                   param2load=args.load_mode)
        elif args.model2load.endswith('.pth'):
            model = load_pth_model(model=model,
                                   name='lenet',
                                   pth_file=args.model2load,
                                   device=device,
                                   param2load=args.load_mode)
        else:
            raise ValueError('The format of %s is not supported' %
                             args.model2load)

    # Parse the optimizer
    if args.frozen_mode == None:
        optimizer = parse_optim(policy=args.optim, params=model.parameters())