예제 #1
0
xx = xx.unsqueeze_(0).float()

## Set Targetå
yy = datasets.MNIST('../data').targets[8888]
yy = yy.float()

lbfgs_params = {
    'epsilon': 1e-4,
    'maxiter': 20,
    'clip_max': 1,
    'clip_min': 0,
    'class_num': 10
}

attack = LBFGS(model, 8, device='cpu')
AdvExArray, dis, loss, valueofc = attack.generate(xx, yy, **lbfgs_params)
AdvExArray = AdvExArray.unsqueeze_(0).float()

#test the result
predict0 = model(xx)
predict0 = predict0.argmax(dim=1, keepdim=True)

#AdvExArray = torch.from_numpy(AdvExArray)
predict1 = model(AdvExArray)
predict1 = predict1.argmax(dim=1, keepdim=True)

print(predict0)
print(predict1, 'distance:', dis, 'target func:', loss, 'c:', valueofc)

import matplotlib.pyplot as plt
예제 #2
0
## Set Targetå
yy = datasets.MNIST('deeprobust/image/data', download=False).targets[8888]
yy = yy.float()

predict0 = model(xx)
predict0 = predict0.argmax(dim=1, keepdim=True)

attack_param = {
    'epsilon': 2e-1,
    'maxiter': 20,
    'clip_max': 1,
    'clip_min': 0,
    'class_num': 10
}

attack = LBFGS(model, device='cpu')
AdvExArray = attack.generate(xx, yy, target_label=2, **attack_param)

#AdvExArray = torch.from_numpy(AdvExArray)
predict1 = model(AdvExArray)
predict1 = predict1.argmax(dim=1, keepdim=True)

print(predict0)
print(predict1)

import matplotlib.pyplot as plt

plt.imshow(AdvExArray[0, 0] * 255, cmap='gray', vmin=0, vmax=255)
plt.savefig('advexample.png')