Comments (3)
Hi @realbadbytes! The way you apply the attack is correct. Just the interpretation of the results is slightly wrong. The returned adversarial is actually classified as 781 (scoreboard) with a probability of 84%.
After running the attack using adversarial = attack(image[:, :, ::-1], label)
, you can check it like this:
np.argmax(fmodel.predictions(adversarial))
This outputs 781.
Note that the returned adversarial has BGR color channel ordering (so for plotting you would need to reverse it, but not for passing it to the Keras model kmodel
or its Foolbox-compatible wrapper fmodel
). Also not that I am passing it to the Foolbox model that handles the preprocessing for us.
To get the the probability, you can apply the softmax to the logits returned by the Foolbox-compatible model wrapper like this: foolbox.utils.softmax(fmodel.predictions(adversarial))[781]
This outputs 0.830603.
Now you wanted to pass it directly to the original Keras model. To do that, you need to apply the preprocessing expected by Keras yourself, i.e. transform from RGB to BGR and subtracting the mean. You can either do it manually, or you use Keras preprocess_input
function.
Manually:
The adversarial we have is already BGR, because the Keras Resnet model expected it like this. So all we need to do is add the 4th dimension and subtract the mean:
preprocessed_adv = adversarial[np.newaxis] - preprocessing[0].reshape(1, 1, 1, 3)
And then kmodel.predict(preprocessed_adv)[0, 781] outputs 0.83060318.
Using preprocess_input
:
from keras.applications.resnet50 import preprocess_input
We need to transform the BGR adversarial back to RGB because Keras preprocess_input will transform from RGB to BGR. And we need to add the 4th dimension.
adversarial_rgb = adversarial[np.newaxis, :, :, ::-1]
And then kmodel.predict(preprocess_input(adversarial_rgb.copy()))[0, 781]
outputs 0.82709205
.
Note, we copy the array, because preprocess_input
works inplace, for whatever reason and so without copying, we would run into problems when calling it multiply times.
Finally, we can also look at the output of decode_predictions
:
from keras.applications.resnet50 import decode_predictions
preds = kmodel.predict(preprocess_input(adversarial_rgb.copy()))
print("Top 5 predictions (adversarial: ", decode_predictions(preds, top=5))
This outputs Top 5 predictions (adversarial: [[('n04149813', 'scoreboard', 0.82709193), ('n03196217', 'digital_clock', 0.03087672), ('n04152593', 'screen', 0.014944675), ('n04141975', 'scale', 0.01231501), ('n04074963', 'remote_control', 0.0091410046)]]
.
Summary:
import foolbox
from foolbox.models import KerasModel
from foolbox.attacks import LBFGSAttack
from foolbox.criteria import TargetClassProbability
import numpy as np
import keras
from keras.applications.resnet50 import ResNet50
from keras.applications.resnet50 import preprocess_input
from keras.applications.resnet50 import decode_predictions
keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')
preprocessing = (np.array([104, 116, 123]), 1)
fmodel = KerasModel(kmodel, bounds=(0, 255), preprocessing=preprocessing)
image, label = foolbox.utils.imagenet_example()
# run the attack
attack = LBFGSAttack(model=fmodel, criterion=TargetClassProbability(781, p=.5))
adversarial = attack(image[:, :, ::-1], label)
# show results
print(np.argmax(fmodel.predictions(adversarial)))
print(foolbox.utils.softmax(fmodel.predictions(adversarial))[781])
adversarial_rgb = adversarial[np.newaxis, :, :, ::-1]
preds = kmodel.predict(preprocess_input(adversarial_rgb.copy()))
print("Top 5 predictions (adversarial: ", decode_predictions(preds, top=5))
outputs
781
0.832095
Top 5 predictions (adversarial: [[('n04149813', 'scoreboard', 0.83013469), ('n03196217', 'digital_clock', 0.030192226), ('n04152593', 'screen', 0.016133979), ('n04141975', 'scale', 0.011708578), ('n03782006', 'monitor', 0.0091574294)]]
from foolbox.
@realbadbytes Please close this issue once you confirmed that it works.
from foolbox.
Thank you very much for the detailed explanation. Working as intended
from foolbox.
Related Issues (20)
- Changing CUDA device at runtime HOT 1
- Logit optimization
- about PGD attack HOT 2
- specifying criterion fails with TypeError HOT 2
- "nll_loss_forward_no_reduce_cuda_kernel_index" not implemented for 'Float' HOT 3
- Deprecation warning using old scipy namespace for gaussian_filter
- how to define the bounds HOT 2
- About the pgd attacks HOT 1
- how to use GaussianBlurAttack HOT 1
- FGSM TargetedMisclassfication HOT 1
- Use foolbox for multi-label classification HOT 1
- Local datasets supported?
- Is there a criterion for query budget? HOT 1
- It seems like the 'success' value in the return of the 'attack' function is overconfident. HOT 2
- About Carlini-Wagner Attack
- Are the wrong classified images sorted out? HOT 1
- It seems your CI/CD has a bug. HOT 1
- How to load datasets that are not among the sample?
- criteria: Misclassification
- Using Foolbox with custom dataset
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from foolbox.