예제 #1
0
def attack(model, dataset, attack_alg='fgsm', eps=0.1, device='cuda'):
    accuracy = Accuracy().to(device)
    model = model.to(device)
    model.eval()
    for x, y in dataset:
        x, y = x.to(device), y.to(device)
        # if model.binary:
        #     x[x >= 0.5] = 1
        #     x[x < 0.5] = 0
        x_fgsm = fast_gradient_method(model,
                                      x,
                                      eps,
                                      np.inf,
                                      clip_min=0,
                                      clip_max=1)
        _, y_pred_fgsm = model(x_fgsm).max(1)
        accuracy(y_pred_fgsm, y)
    acc = accuracy.compute()
    print(acc)
예제 #2
0
 def test_step(self, batch, batch_idx):
     x, y = batch
     x = x.view(x.size(0), -1)
     y_hat = self(x)
     acc = accuracy(y_hat, y)
     return {'test_loss': F.cross_entropy(y_hat, y), 'acc': acc}