Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
nb_epochs=FLAGS.nb_epochs,
batch_size=FLAGS.batch_size,
source_samples=FLAGS.source_samples,
learning_rate=FLAGS.learning_rate,
attack_iterations=FLAGS.attack_iterations,
model_path=FLAGS.model_path,
targeted=FLAGS.targeted)
if __name__ == '__main__':
flags.DEFINE_boolean('viz_enabled', VIZ_ENABLED,
'Visualize adversarial ex.')
flags.DEFINE_integer('nb_epochs', NB_EPOCHS,
'Number of epochs to train model')
flags.DEFINE_integer('batch_size', BATCH_SIZE, 'Size of training batches')
flags.DEFINE_integer('source_samples', SOURCE_SAMPLES,
'Number of test inputs to attack')
flags.DEFINE_float('learning_rate', LEARNING_RATE,
'Learning rate for training')
flags.DEFINE_string('model_path', MODEL_PATH,
'Path to save or load the model file')
flags.DEFINE_integer('attack_iterations', ATTACK_ITERATIONS,
'Number of iterations to run attack; 1000 is good')
flags.DEFINE_boolean('targeted', TARGETED,
'Run the tutorial in targeted mode?')
tf.app.run()
report_path = FLAGS.report_path
if report_path is None:
suffix = "_bundled_examples_report.joblib"
assert model_filepath.endswith('.joblib')
report_path = model_filepath[:-len('.joblib')] + suffix
goal = MaxConfidence()
bundle_examples_with_goal(sess, model, adv_x_list, y, goal,
report_path, batch_size=FLAGS.batch_size)
if __name__ == '__main__':
flags.DEFINE_string('report_path', None, 'Report path')
flags.DEFINE_integer('train_start', TRAIN_START, 'Starting point (inclusive)'
'of range of train examples to use')
flags.DEFINE_integer('train_end', TRAIN_END, 'Ending point (non-inclusive) '
'of range of train examples to use')
flags.DEFINE_integer('test_start', TEST_START, 'Starting point '
'(inclusive) of range of test examples to use')
flags.DEFINE_integer('test_end', TEST_END, 'End point (non-inclusive) of '
'range of test examples to use')
flags.DEFINE_string('which_set', WHICH_SET, '"train" or "test"')
flags.DEFINE_integer('batch_size', BATCH_SIZE, 'batch size')
tf.app.run()
from cleverhans.utils_tf import silence
# The silence() call must precede other imports in order to silence them.
# pylint does not like it but that's how it has to be.
# pylint: disable=C0413
silence()
from cleverhans.compat import flags
from cleverhans.confidence_report import make_confidence_report_bundled
from cleverhans.confidence_report import BATCH_SIZE
from cleverhans.confidence_report import TRAIN_START, TRAIN_END
from cleverhans.confidence_report import TEST_START, TEST_END
from cleverhans.confidence_report import WHICH_SET
from cleverhans.confidence_report import RECIPE
from cleverhans.confidence_report import REPORT_PATH
FLAGS = flags.FLAGS
def main(argv=None):
"""
Make a confidence report and save it to disk.
"""
try:
_name_of_script, filepath = argv
except ValueError:
raise ValueError(argv)
print(filepath)
make_confidence_report_bundled(filepath=filepath,
test_start=FLAGS.test_start,
test_end=FLAGS.test_end,
which_set=FLAGS.which_set,
recipe=FLAGS.recipe,
flags.DEFINE_integer('train_start', TRAIN_START, 'Starting point (inclusive)'
'of range of train examples to use')
flags.DEFINE_integer('train_end', TRAIN_END, 'Ending point (non-inclusive) '
'of range of train examples to use')
flags.DEFINE_integer('test_start', TEST_START, 'Starting point (inclusive) '
'of range of test examples to use')
flags.DEFINE_integer('test_end', TEST_END, 'End point (non-inclusive) of '
'range of test examples to use')
flags.DEFINE_integer('nb_iter', NB_ITER, 'Number of iterations of PGD')
flags.DEFINE_string('which_set', WHICH_SET, '"train" or "test"')
flags.DEFINE_string('report_path', REPORT_PATH, 'Path to save to')
flags.DEFINE_integer('mc_batch_size', MC_BATCH_SIZE,
'Batch size for MaxConfidence')
flags.DEFINE_integer('batch_size', BATCH_SIZE,
'Batch size for most jobs')
flags.DEFINE_float('base_eps_iter', BASE_EPS_ITER,
'epsilon per iteration, if data were in [0, 1]')
flags.DEFINE_integer('save_advx', SAVE_ADVX,
'If True, saves the adversarial examples to the '
'filesystem.')
tf.app.run()
_name_of_script, filepath = argv
except ValueError:
raise ValueError(argv)
print(filepath)
make_confidence_report_bundled(filepath=filepath,
test_start=FLAGS.test_start,
test_end=FLAGS.test_end,
which_set=FLAGS.which_set,
recipe=FLAGS.recipe,
report_path=FLAGS.report_path, batch_size=FLAGS.batch_size)
if __name__ == '__main__':
flags.DEFINE_integer('train_start', TRAIN_START, 'Starting point (inclusive)'
'of range of train examples to use')
flags.DEFINE_integer('train_end', TRAIN_END, 'Ending point (non-inclusive) '
'of range of train examples to use')
flags.DEFINE_integer('test_start', TEST_START, 'Starting point (inclusive) '
'of range of test examples to use')
flags.DEFINE_integer('test_end', TEST_END, 'End point (non-inclusive) of '
'range of test examples to use')
flags.DEFINE_string('recipe', RECIPE, 'Name of function from attack_bundling'
' to run')
flags.DEFINE_string('which_set', WHICH_SET, '"train" or "test"')
flags.DEFINE_string('report_path', REPORT_PATH, 'Report path')
flags.DEFINE_integer('batch_size', BATCH_SIZE, 'Batch size')
tf.app.run()
def main(argv=None):
f = {x: flags.FLAGS[x].value for x in dir(flags.FLAGS)}
HParams = namedtuple('HParams', f.keys())
hparams = HParams(**f)
run_trainer(hparams)
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
from __future__ import unicode_literals
import tensorflow as tf
from tensorflow import keras
from cleverhans.attacks import FastGradientMethod
from cleverhans.compat import flags
from cleverhans.dataset import MNIST
from cleverhans.utils import AccuracyReport
from cleverhans.utils_keras import cnn_model
from cleverhans.utils_keras import KerasModelWrapper
FLAGS = flags.FLAGS
NB_EPOCHS = 6
BATCH_SIZE = 128
LEARNING_RATE = .001
def mnist_tutorial(train_start=0, train_end=60000, test_start=0,
test_end=10000, nb_epochs=NB_EPOCHS, batch_size=BATCH_SIZE,
learning_rate=LEARNING_RATE, testing=False,
label_smoothing=0.1):
"""
MNIST CleverHans tutorial
:param train_start: index of first training set example
:param train_end: index of last training set example
:param test_start: index of first test set example
:param test_end: index of last test set example
Run the tutorial using command line flags.
"""
from cleverhans_tutorials import check_installation
check_installation(__file__)
mnist_tutorial(nb_epochs=FLAGS.nb_epochs, batch_size=FLAGS.batch_size,
learning_rate=FLAGS.learning_rate,
clean_train=FLAGS.clean_train,
backprop_through_attack=FLAGS.backprop_through_attack,
nb_filters=FLAGS.nb_filters)
if __name__ == '__main__':
flags.DEFINE_integer('nb_filters', NB_FILTERS,
'Model size multiplier')
flags.DEFINE_integer('nb_epochs', NB_EPOCHS,
'Number of epochs to train model')
flags.DEFINE_integer('batch_size', BATCH_SIZE,
'Size of training batches')
flags.DEFINE_float('learning_rate', LEARNING_RATE,
'Learning rate for training')
flags.DEFINE_bool('clean_train', CLEAN_TRAIN, 'Train on clean examples')
flags.DEFINE_bool('backprop_through_attack', BACKPROP_THROUGH_ATTACK,
('If True, backprop through adversarial example '
'construction process during adversarial training'))
tf.app.run()
import numpy as np
import tensorflow as tf
import torch
from torch import nn
import torch.nn.functional as F
from torch import optim
from torch.autograd import Variable
from torchvision import datasets, transforms
from cleverhans.attacks import FastGradientMethod
from cleverhans.compat import flags
from cleverhans.model import CallableModelWrapper
from cleverhans.utils import AccuracyReport
from cleverhans.utils_pytorch import convert_pytorch_model_to_tf
FLAGS = flags.FLAGS
NB_EPOCHS = 6
BATCH_SIZE = 128
LEARNING_RATE = .001
class PytorchMnistModel(nn.Module):
""" Basic MNIST model from github
https://github.com/rickiepark/pytorch-examples/blob/master/mnist.ipynb
"""
def __init__(self):
super(PytorchMnistModel, self).__init__()
# input is 28x28
# padding=2 for same padding
self.conv1 = nn.Conv2d(1, 32, 5, padding=2)