コード例 #1
0
    args = parser.parse_args()
    args.prefix = args.prefix or 'svhn_conv_{:.4f}_{:.4f}_0'.format(
        args.epsilon, args.lr).replace(".", "_")
    setproctitle.setproctitle(args.prefix)

    train_log = open(args.prefix + "_train.log", "w")
    test_log = open(args.prefix + "_test.log", "w")

    train_loader, test_loader = pblm.svhn_loaders(args.batch_size)

    torch.manual_seed(args.seed)
    torch.cuda.manual_seed(args.seed)

    # new svhn
    model = pblm.svhn_model().cuda()

    if args.attack:
        model.load_state_dict(torch.load('../models/svhn.pth'))
        with open("attack_svhn.txt", "w") as f:
            f.write("eps acc\n")
            #  for eps in range(1, 15):
            for eps in range(1):
                eps /= 10
                #  for eps in range(1, 17):
                #  eps *= 0.005
                niters = 100
                #  alpha  = 2.5 * eps / niters
                alpha = 1e-2
                #  total_err, total_err_attack, _ = pgd_l2(test_loader, model,
                total_err, total_err_attack, _ = carlini_l2(test_loader,
コード例 #2
0
    parser.add_argument('--mnist', action='store_true')
    parser.add_argument('--svhn', action='store_true')
    parser.add_argument('--har', action='store_true')
    parser.add_argument('--fashion', action='store_true')
    parser.add_argument('--model')

    args = parser.parse_args()

    if args.mnist: 
        train_loader, test_loader = pblm.mnist_loaders(args.batch_size)
        model = pblm.mnist_model().to(device)
        model.load_state_dict(torch.load('icml/mnist_epochs_100_baseline_model.pth'))
    elif args.svhn: 
        train_loader, test_loader = pblm.svhn_loaders(args.batch_size)
        model = pblm.svhn_model().to(device)
        model.load_state_dict(torch.load('pixel2/svhn_small_batch_size_50_epochs_100_epsilon_0.0078_l1_proj_50_l1_test_median_l1_train_median_lr_0.001_opt_adam_schedule_length_20_seed_0_starting_epsilon_0.001_checkpoint.pth')['state_dict'])
    elif args.model == 'cifar': 
        train_loader, test_loader = pblm.cifar_loaders(args.batch_size)
        model = pblm.cifar_model().to(device)
        model.load_state_dict(torch.load('pixel2/cifar_small_batch_size_50_epochs_100_epsilon_0.0347_l1_proj_50_l1_test_median_l1_train_median_lr_0.05_momentum_0.9_opt_sgd_schedule_length_20_seed_0_starting_epsilon_0.001_weight_decay_0.0005_checkpoint.pth')['state_dict'])
    elif args.har:
        pass
    elif args.fashion: 
        pass
    else:
        raise ValueError("Need to specify which problem.")
    for p in model.parameters(): 
        p.requires_grad = False

    num_classes = model[-1].out_features