Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update example scripts #2208

Merged
merged 9 commits into from
Jul 4, 2023
2 changes: 1 addition & 1 deletion examples/adversarial_training_FBF.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import torchvision.transforms as transforms
from torch.utils.data import Dataset, DataLoader

from art.classifiers import PyTorchClassifier
from art.estimators.classification import PyTorchClassifier
from art.data_generators import PyTorchDataGenerator
from art.defences.trainer import AdversarialTrainerFBFPyTorch
from art.utils import load_cifar10
Expand Down
4 changes: 4 additions & 0 deletions examples/adversarial_training_data_augmentation.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
"""
This is an example of how to use ART and Keras to perform adversarial training using data generators for CIFAR10
"""
import tensorflow as tf

tf.compat.v1.disable_eager_execution()

import keras
import numpy as np
from keras.layers import Conv2D, Dense, Flatten, MaxPooling2D, Input, BatchNormalization
Expand Down
2 changes: 1 addition & 1 deletion examples/get_started_lightgbm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Step 2: Create the model

params = {"objective": "multiclass", "metric": "multi_logloss", "num_class": 10}
params = {"objective": "multiclass", "metric": "multi_logloss", "num_class": 10, "force_col_wise": True}
train_set = lgb.Dataset(x_train, label=np.argmax(y_train, axis=1))
test_set = lgb.Dataset(x_test, label=np.argmax(y_test, axis=1))
model = lgb.train(params=params, train_set=train_set, num_boost_round=100, valid_sets=[test_set])
Expand Down
2 changes: 1 addition & 1 deletion examples/get_started_xgboost.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@

# Step 2: Create the model

params = {"objective": "multi:softprob", "metric": "accuracy", "num_class": 10}
params = {"objective": "multi:softprob", "eval_metric": ["mlogloss", "merror"], "num_class": 10}
dtrain = xgb.DMatrix(x_train, label=np.argmax(y_train, axis=1))
dtest = xgb.DMatrix(x_test, label=np.argmax(y_test, axis=1))
evals = [(dtest, "test"), (dtrain, "train")]
Expand Down
24 changes: 16 additions & 8 deletions examples/mnist_cnn_fgsm.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
"""Trains a convolutional neural network on the MNIST dataset, then attacks it with the FGSM attack."""
from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

tf.compat.v1.disable_eager_execution()

from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
import numpy as np
Expand Down Expand Up @@ -35,12 +39,16 @@
acc = np.sum(preds == np.argmax(y_test, axis=1)) / y_test.shape[0]
print("\nTest accuracy: %.2f%%" % (acc * 100))

# Craft adversarial samples with FGSM
epsilon = 0.1 # Maximum perturbation
adv_crafter = FastGradientMethod(classifier, eps=epsilon)
x_test_adv = adv_crafter.generate(x=x_test)
# Define epsilon values
epsilon_values = [0.01, 0.1, 0.15, 0.2, 0.25, 0.3]

# Evaluate the classifier on the adversarial examples
preds = np.argmax(classifier.predict(x_test_adv), axis=1)
acc = np.sum(preds == np.argmax(y_test, axis=1)) / y_test.shape[0]
print("\nTest accuracy on adversarial sample: %.2f%%" % (acc * 100))
# Iterate over epsilon values
for epsilon in epsilon_values:
# Craft adversarial samples with FGSM
adv_crafter = FastGradientMethod(classifier, eps=epsilon)
x_test_adv = adv_crafter.generate(x=x_test, y=y_test)

# Evaluate the classifier on the adversarial examples
preds = np.argmax(classifier.predict(x_test_adv), axis=1)
acc = np.sum(preds == np.argmax(y_test, axis=1)) / y_test.shape[0]
print("Test accuracy on adversarial sample (epsilon = %.2f): %.2f%%" % (epsilon, acc * 100))
2 changes: 2 additions & 0 deletions examples/mnist_poison_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@

import pprint
import json
import tensorflow as tf

tf.compat.v1.disable_eager_execution()
from keras.models import Sequential
from keras.layers import Dense, Flatten, Conv2D, MaxPooling2D, Dropout
import numpy as np
Expand Down
4 changes: 3 additions & 1 deletion examples/mnist_transferability.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
import numpy as np
import tensorflow as tf

tf.compat.v1.disable_eager_execution()

from art.attacks.evasion import DeepFool
from art.estimators.classification import KerasClassifier, TensorFlowClassifier
from art.utils import load_mnist
Expand Down Expand Up @@ -60,7 +62,7 @@ def cnn_mnist_k(input_shape):


# Get session
session = tf.Session()
session = tf.compat.v1.Session()
k.set_session(session)

# Read MNIST dataset
Expand Down