cg563 / simple-blackbox-attack

Code for ICML 2019 paper "Simple Black-box Adversarial Attacks"
MIT License
191 stars 56 forks source link

how to use simba_single.py to generating adv example for a specific image? #20

Closed cyber-ranger closed 2 years ago

cyber-ranger commented 3 years ago

Hello, Can I use simba_single.py to generating adv example based on a given image? How do I set the parameter y of method simba_single()? Could you share some exeutable example code about simba_single()?

THX

cg563 commented 3 years ago

You can, but it is not very efficient since the model is just forwarding a single image at a time, rather than say 50 in a batch. When run on a GPU, it takes much lower than 50 times the computation cost to forward a batch of 50 images. I would recommend against using simba_single() unless you really just want to generate one adversarial image.

The y parameter is the correct label for the image x. It should work for any PyTorch model and image tensor x that is normalized in the same way as ImageNet samples.

I updated the repo to combine simba_single() and simba_batch() into a single class. You can replace https://github.com/cg563/simple-blackbox-attack/blob/master/run_simba.py#L84 with the following to run the attack using simba_single():

attacker.simba_single(images_batch[0], labels_batch[0], max_iters, args.epsilon)

Hope this helps.

cyber-ranger commented 3 years ago

Hello, Thanks for supporting. After switching code from simba_batch() to simba_single() as you recommended, I got the following error. Could you help me on solving this problem. Thanks very much.

tensor([0.9538]) tensor([0.9531]) tensor([0.9349]) tensor([0.9276]) tensor([0.9260]) tensor([0.9238]) tensor([0.9209]) tensor([0.8936]) tensor([0.8828]) tensor([0.8616]) tensor([0.8582]) tensor([0.8459]) tensor([0.8426]) tensor([0.8251]) tensor([0.8094]) tensor([0.7666]) tensor([0.7541]) tensor([0.7310]) tensor([0.7207]) tensor([0.7112]) tensor([0.6874]) tensor([0.6684]) tensor([0.6550]) tensor([0.6413]) tensor([0.6231]) tensor([0.6180]) tensor([0.5906]) tensor([0.5682]) tensor([0.5571]) tensor([0.5423]) tensor([0.5317]) tensor([0.5163]) tensor([0.4677]) tensor([0.4594]) tensor([0.4359]) tensor([0.4280]) tensor([0.4189]) tensor([0.4124]) tensor([0.4011]) tensor([0.3839]) tensor([0.3372]) tensor([0.3125]) tensor([0.3081]) tensor([0.2765]) tensor([0.2652]) tensor([0.2532]) tensor([0.2390]) tensor([0.2321]) tensor([0.2021]) tensor([0.1971]) tensor([0.1902]) tensor([0.1845]) tensor([0.1749]) tensor([0.1718]) tensor([0.1644]) tensor([0.1547]) tensor([0.1313]) tensor([0.1275]) tensor([0.1211]) Traceback (most recent call last): File "/home/ranger/.pycharm_helpers/pydev/pydevd.py", line 1438, in _exec pydev_imports.execfile(file, globals, locals) # execute the script File "/home/ranger/.pycharm_helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile exec(compile(contents+"\n", file, 'exec'), glob, loc) File "/home/ranger/lizhu/home/ranger/lizhu/src/experiments/simple-blackbox-attack/run_simba.py", line 92, in adv, probs, succs, queries, l2_norms, linf_norms = attacker.simba_single(images_batch[0], labels_batch[0], max_iters, args.epsilon) ValueError: not enough values to unpack (expected 6, got 3)