コード例 #1
0
    scheduler = lr_scheduler.MultiStepLR(optimizer,
                                         milestones=[60, 120, 160],
                                         gamma=0.2)

attacker = DDN(steps=args.steps, device=DEVICE)

max_loss = torch.log(torch.tensor(10.)).item()  # for callback
best_acc = 0
best_epoch = 0

for epoch in range(args.epochs):

    scheduler.step()
    cudnn.benchmark = True
    model.train()
    requires_grad_(m, True)
    accs = AverageMeter()
    losses = AverageMeter()
    attack_norms = AverageMeter()

    length = len(train_loader)
    for i, (images, labels) in enumerate(tqdm.tqdm(train_loader, ncols=80)):

        images, labels = images.to(DEVICE), labels.to(DEVICE)
        #原图loss
        #logits_clean = model.forward(images)
        logits_clean, feature_clean = model.forward(images)
        #loss = F.cross_entropy(logits_clean, labels)

        if args.adv is not None and epoch >= args.adv:
            model.eval()
コード例 #2
0
    print('Loading data')
    dataset = datasets.MNIST(args.data_path, train=False,
                             transform=transforms.ToTensor(),
                             download=True)
    loader = data.DataLoader(dataset, shuffle=False, batch_size=16)

    x, y = next(iter(loader))
    x = x.to(device)
    y = y.to(device)

    print('Loading model')
    model = SmallCNN()
    model.load_state_dict(torch.load(args.model_path))
    model.eval().to(device)
    requires_grad_(model, False)

    print('Running DDN attack')
    attacker = DDN(steps=100, device=device)
    start = time.time()
    ddn_atk = attacker.attack(model, x, labels=y, targeted=False)
    ddn_time = time.time() - start

    print('Running C&W attack')
    cwattacker = CarliniWagnerL2(device=device,
                                 image_constraints=(0, 1),
                                 num_classes=10)

    start = time.time()
    cw_atk = cwattacker.attack(model, x, labels=y, targeted=False)
    cw_time = time.time() - start