def test(model, tokenizer, test_data, args): logger.info("Test starts!") model_load(args.model_dir, model) model = model.to(device) test_dataset = QueryDataset(test_data) test_data_loader = DataLoader( test_dataset, sampler=SequentialSampler(test_dataset), batch_size=args.bsz, num_workers=args.num_workers, collate_fn=lambda x: collate_fn(x, tokenizer, args.sample, args. max_seq_len)) test_loss, test_str = evaluate(model, test_data_loader) logger.info(f"| test | {test_str}")
def main(args): train_iter, valid_iter, test_iter = get_data_loader('data', args.batch_size, model=args.model) # ---------- Try to load model first ---------- model = None if args.load_model: try: model = utils.model_load('P3_{}.pt'.format(args.model)) except: logger.info('Loading model failed, will train the model first.') args.load_model = False # ---------- GAN related ---------- if args.model == 'GAN': if not args.load_model or args.mode == 'train': if not model: model = GAN(N_LATENT) model.to(dev) train_gan(model, train_iter, test_iter, args.num_epochs, G_update_iterval=args.G_update_interval, test_interval=args.test_interval, save_model=args.save_model) model.eval() if args.mode == 'test': test_all(model, test_iter, args.batch_size, N_LATENT, model_name='GAN') if args.mode == 'gen': latent = torch.randn(size=(1000, N_LATENT), device=dev) generate_images(model.generator, latent, save_path='sample/GAN/samples') # ---------- VAE related ---------- else: if args.mode == 'train' or not args.load_model: model = VAE(N_LATENT) model.to(dev) train_vae(model, train_iter, test_iter, args.num_epochs, test_interval=args.test_interval, save_model=args.save_model) model.eval() if args.mode == 'test': test_all(model, test_iter, args.batch_size, N_LATENT, model_name='VAE') if args.mode == 'gen': # n_imgs = 0 # latent = [] # with torch.no_grad(): # for batch in test_iter: # X = batch[0].to(dev) # n_imgs += X.shape[0] # latent.append(model.reparam(*model.encode(X))) # # if n_imgs > 1000: # break # latent = torch.cat(latent, dim=0)[:1000] latent = torch.randn(size=(1000, N_LATENT), device=dev) generate_images(model.decoder, latent, save_path='sample/VAE/samples')
def main(args): logger.info(f"Args: {json.dumps(args.__dict__, indent=2, sort_keys=True)}") spm_path = os.path.join('spm', args.spm, "spm.model") args.sample = parse_sample_options(args.sample) logger.info(f"Loading tokenizer from {spm_path}") tokenizer = Tokenizer(spm_path) args.ntoken = ntoken = len(tokenizer) logger.info(f" Vocabulary size: {ntoken}") logger.info("Reading dataset") data = {} for x in ['train', 'valid', 'test']: data[x] = read_data(os.path.join(args.data_dir, f"{x}.query.txt"), min_len=args.min_len) logger.info(f" Number of {x:>5s} data: {len(data[x]):8d}") logger.info("Preparing model and optimizer") config = LMConfig(ntoken, args.ninp, args.nhid, args.nlayers, args.dropouti, args.dropoutr, args.dropouth, args.dropouto) model = LanguageModel(config).to(device) params = get_params(model) logger.info( f" Number of model parameters: {sum(p.numel() for p in params)}") optimizer = torch.optim.Adam(params) if args.resume: logger.info(f"Loading model from {args.resume}") model_load(args.resume, model, optimizer) model = model.to(device) if n_gpu > 1: logger.info(f"Making model as data parallel") model = torch.nn.DataParallel(model, dim=1) train(model, optimizer, tokenizer, data['train'], data['valid'], args) test(model, tokenizer, data['test'], args)
def gen_gradcam_image(model_parameter, data_dir, result_dir): cnn = utils.model_load(model_parameter) input_size = vis_utils.get_input_size(cnn.meta['base_model']) target_layer = vis_utils.get_conv_layer(cnn.meta['base_model']) preprocess = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) visualizer = Visualize(cnn, preprocess, target_layer, num_classes=cnn.meta['num_classes'], retainModel=False) result_dir = os.sep.join(["results", result_dir]) if os.path.isdir(result_dir): if len(os.listdir(result_dir)) != 0: print("Result path exist and not empty, not generating") return else: os.mkdir(result_dir) from torchvision.datasets.folder import find_classes, make_dataset classes, class_to_idx = find_classes(data_dir) dataset = make_dataset(data_dir, class_to_idx) for class_name in os.listdir(data_dir): os.mkdir(os.sep.join([result_dir, class_name])) for img_path, idx in dataset: target_img_path = img_path.replace(data_dir + os.sep, "") print("Processing: ", target_img_path) img_pil = Image.open(img_path) img_pil = img_pil.resize((input_size, input_size)) visualizer.input_image(img_pil) x = visualizer.get_prediction_output() x = F.softmax(Variable(x)).data for pred_c, idx in class_to_idx.items(): gradcam = visualizer.get_gradcam_heatmap(idx)[0] target_img = target_img_path.replace( '.jpg', '_{}_{:.4f}.jpg'.format(pred_c, x[0][idx])) gradcam.save(os.sep.join([result_dir, target_img]))
def test(args): fn = 'corpus.{}.data'.format(hashlib.md5(args.data.encode()).hexdigest()) if os.path.exists(fn): print('Loading cached dataset...') corpus = torch.load(fn) print('Done') else: print('Producing dataset...') corpus = data.Corpus(args.data) torch.save(corpus, fn) print('Done') ntokens = len(corpus.dictionary) batch_size = args.batchSize val_data = batchify(corpus.valid, batch_size, args) test_data = batchify(corpus.test, batch_size, args) if not os.path.isfile(args.weightFile): print('Pre-trained weight file does not exist. Please check the location: {}'.format(args.weightFile)) exit() model, criterion, _, _ = model_load(args.weightFile) if torch.cuda.is_available(): model = model.cuda() criterion = criterion.cuda() # Run on validation data. val_loss = evaluate(args, model, criterion, val_data, ntokens, batch_size) print('=' * 89) print('| End of Validation | val loss {:5.2f} | val ppl {:8.2f}'.format( val_loss, math.exp(val_loss))) print('=' * 89) # Run on test data. test_loss = evaluate(args, model, criterion, test_data, ntokens, batch_size) print('=' * 89) print('| End of Testing | test loss {:5.2f} | test ppl {:8.2f}'.format( test_loss, math.exp(test_loss))) print('=' * 89)
def main(args): logger.info(f"Args: {json.dumps(args.__dict__, indent=2, sort_keys=True)}") spm_path = os.path.join('spm', args.spm, "spm.model") logger.info(f"Loading tokenizer from {spm_path}") tokenizer = Tokenizer(spm_path) args.ntoken = ntoken = len(tokenizer) args.branching_factor = min([args.branching_factor, args.ntoken]) logger.info(f" Vocab size: {ntoken}") n_queries_str = f"{f'only {args.n_queries} samples' if args.n_queries else 'all'} quries from" logger.info(f"Reading a dataset ({n_queries_str} test.query.txt)") seen_set = set( read_data(os.path.join(args.data_dir, "train.query.txt"), min_len=args.min_len)) test_data = read_data(os.path.join(args.data_dir, "test.query.txt"), min_len=args.min_len) if args.n_queries: random.seed(args.seed) test_data = random.sample(test_data, args.n_queries) n_seen_test_data = len([x for x in test_data if x in seen_set]) n_unseen_test_data = len(test_data) - n_seen_test_data logger.info( f" Number of test data: {len(test_data):8d} (seen {n_seen_test_data}, unseen {n_unseen_test_data})" ) logger.info(f"Loading model from {args.model_dir}") model = model_load(args.model_dir) model = model.to(device) logger.info('Generation starts!') with torch.no_grad(): generate(model, tokenizer, test_data, args, seen_set=seen_set, calc_mrl=args.calc_mrl)
def gen_gradcam_numpy(model_parameter, data_dir): cnn = utils.model_load(model_parameter) input_size = vis_utils.get_input_size(cnn.meta['base_model']) target_layer = vis_utils.get_conv_layer(cnn.meta['base_model']) preprocess = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) visualizer = Visualize(cnn, preprocess, target_layer, num_classes=cnn.meta['num_classes'], retainModel=False) result = np.empty(0) from torchvision.datasets.folder import find_classes, make_dataset classes, class_to_idx = find_classes(data_dir) dataset = make_dataset(data_dir, class_to_idx) for img_path, idx in dataset: print("Processing: ", img_path.replace(data_dir + os.sep, "")) img_pil = Image.open(img_path) img_pil = img_pil.resize((input_size, input_size)) visualizer.input_image(img_pil) gradcam = visualizer.get_gradcam_intensity(idx) gradcam = vis_utils.normalize(gradcam) result = np.append(result, gradcam) print(result.shape) np.save(os.sep.join(['results', model_parameter, 'gradcam.npy']), result)
def trainEvalLM(args): fn = 'corpus.{}.data'.format(hashlib.md5(args.data.encode()).hexdigest()) if os.path.exists(fn): print('Loading cached dataset...') corpus = torch.load(fn) else: print('Producing dataset...') corpus = data.Corpus(args.data) torch.save(corpus, fn) if torch.cuda.is_available(): args.cuda = True ntokens = len(corpus.dictionary) eval_batch_size = 10 train_data = batchify(corpus.train, args.batch_size, args) val_data = batchify(corpus.valid, eval_batch_size, args) # Build the model and loss function model = lmModel.RNNModel(args.model, ntokens, args.emsize, args.nhid, args.nlayers, args.dropout, args.tied, g=args.g, k=args.k) criterion = nn.CrossEntropyLoss() if torch.cuda.is_available(): model = model.cuda() criterion = criterion.cuda() #compute network parameters params = list(model.parameters()) total_params = np.sum([np.prod(p.size()) for p in params]) print( '\033[1;32;40mTotal parameters (in million):\033[0m\033[1;31;40m {:0.2f} \033[0m\n' .format(total_params / 1e6, 2)) optimizer = torch.optim.SGD(params, lr=args.lr, weight_decay=args.wdecay) start_epoch = 1 if args.resume: print('Resuming model ...') model, criterion, optimizer, start_epoch = model_load(args.resume) optimizer.param_groups[0]['lr'] = args.lr model.dropout = args.dropout # At any point you can hit Ctrl + C to break out of training early. try: #Create folder for saving model and log files args.saveDir += '_' + args.model # ===================== if not os.path.isdir(args.saveDir): os.mkdir(args.saveDir) save_str = 'nl_' + str(args.nlayers) + '_nh_' + str( args.nhid) + '_g_' + str(args.g) + '_k_' + str(args.k) args.save = args.saveDir + '/model_' + save_str + '.pt' logFileLoc = args.saveDir + '/logs_' + save_str + '.txt' logger = open(logFileLoc, 'w') logger.write(str(args)) logger.write('\n Total parameters (in million): {:0.2f}'.format( total_params / 1e6, 2)) logger.write('\n\n') logger.write( "\n%s\t%s\t%s\t%s\t%s" % ('Epoch', 'Loss(Tr)', 'Loss(val)', 'ppl (tr)', 'ppl (val)')) logger.flush() best_val_loss = [] stored_loss = 100000000 # Loop over epochs. for epoch in range(start_epoch, args.epochs + 1): epoch_start_time = time.time() train_loss = train(args, model, criterion, optimizer, epoch, train_data, ntokens) ### TRAIN WITH ASGD if 't0' in optimizer.param_groups[0]: tmp = {} for prm in model.parameters(): tmp[prm] = prm.data.clone() prm.data = optimizer.state[prm]['ax'].clone() val_loss = evaluate(args, model, criterion, val_data, ntokens, eval_batch_size) print('-' * 89) print( '| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | ' 'valid ppl {:8.2f}'.format( epoch, (time.time() - epoch_start_time), val_loss, math.exp(val_loss))) print('-' * 89) logger.write("\n%d\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.4f" % (epoch, train_loss, val_loss, math.exp(train_loss), math.exp(val_loss))) logger.flush() if val_loss < stored_loss: model_save(args.save, model, criterion, optimizer, epoch) print('Saving Averaged (new best validation)') stored_loss = val_loss for prm in model.parameters(): prm.data = tmp[prm].clone() else: val_loss = evaluate(args, model, criterion, val_data, ntokens, eval_batch_size) print('-' * 89) print( '| end of epoch {:3d} | time: {:5.2f}s | valid loss {:5.2f} | ' 'valid ppl {:8.2f}'.format( epoch, (time.time() - epoch_start_time), val_loss, math.exp(val_loss))) print('-' * 89) logger.write("\n%d\t\t%.4f\t\t%.4f\t\t%.4f\t\t%.4f" % (epoch, train_loss, val_loss, math.exp(train_loss), math.exp(val_loss))) logger.flush() if val_loss < stored_loss: model_save(args.save, model, criterion, optimizer, epoch) print('Saving model (new best validation)') stored_loss = val_loss if 't0' not in optimizer.param_groups[0] and ( len(best_val_loss) > args.nonmono and val_loss > min(best_val_loss[:-args.nonmono])): print('Switching to ASGD') optimizer = torch.optim.ASGD(model.parameters(), lr=args.lr, t0=0, lambd=0., weight_decay=args.wdecay) best_val_loss.append(val_loss) except KeyboardInterrupt: print('-' * 89) print('Exiting from training early')
stored_loss = val_loss if 't0' not in optimizer.param_groups[0] and ( len(best_val_loss) > args.nonmono and val_loss > min(best_val_loss[:-args.nonmono])): logging('Switching!') optimizer = torch.optim.ASGD(model.parameters(), lr=args.lr, t0=0, lambd=0., weight_decay=args.wdecay) optimizer.param_groups[0]['lr'] /= 2. best_val_loss.append(val_loss) torch.cuda.empty_cache() except KeyboardInterrupt: logging('-' * 89) logging('Exiting from training early') # Load the best saved model. model = model_load(os.path.join(args.save, 'model.pt')) parallel_model = nn.DataParallel(model, dim=1).cuda() # Run on test data. test_loss = evaluate(test_data, test_batch_size) logging('=' * 89) logging('| End of training | test loss {:5.2f} | test ppl {:8.2f}'.format( test_loss, math.exp(test_loss))) logging('=' * 89)
def main(argv=None): """ CIFAR10 CleverHans tutorial :return: """ # Object used to keep track of (and return) key accuracies report = AccuracyReport() # CIFAR10-specific dimensions img_rows = 32 img_cols = 32 channels = 3 nb_classes = 10 # Set TF random seed to improve reproducibility tf.set_random_seed(1234) sess = tf.Session() set_log_level(logging.DEBUG) # Get CIFAR10 test data X_train, Y_train, X_test, Y_test = data_cifar10() # Label smoothing assert Y_train.shape[1] == 10. # Define input TF placeholder x = tf.placeholder(tf.float32, shape=(None, img_rows, img_cols, channels)) y = tf.placeholder(tf.float32, shape=(None, 10)) model_path = FLAGS.model_path nb_samples = FLAGS.nb_samples from cnn_models import make_basic_cnn model = make_basic_cnn('fp_', input_shape=(None, img_rows, img_cols, channels), nb_filters=FLAGS.nb_filters) preds = model(x) print("Defined TensorFlow model graph with %d parameters" % model.n_params) rng = np.random.RandomState([2017, 8, 30]) def evaluate(eval_params): # Evaluate the model on legitimate test examples acc = model_eval(sess, x, y, preds, X_test, Y_test, args=eval_params) return acc model_load(sess, model_path) print('Restored model from %s' % model_path) eval_params = {'batch_size': FLAGS.batch_size} accuracy = evaluate(eval_params) print('Test accuracy on legitimate test examples: {0}'.format(accuracy)) ########################################################################### # Craft adversarial examples using the Jacobian-based saliency map approach ########################################################################### print('Crafting ' + str(nb_samples) + ' * ' + str(nb_classes - 1) + ' adversarial examples') # Keep track of success (adversarial example classified in target) results = np.zeros((nb_classes, nb_samples), dtype='i') # Rate of perturbed features for each test set example and target class perturbations = np.zeros((nb_classes, nb_samples), dtype='f') # Initialize our array for grid visualization grid_shape = (nb_classes, nb_classes, img_rows, img_cols, channels) grid_viz_data = np.zeros(grid_shape, dtype='f') from cleverhans.attacks import SaliencyMapMethod jsma = SaliencyMapMethod(model, sess=sess) jsma_params = { 'gamma': FLAGS.gamma, 'theta': 1., 'symbolic_impl': True, 'clip_min': 0., 'clip_max': 1., 'y_target': None } figure = None # Loop over the samples we want to perturb into adversarial examples for sample_ind in range(0, nb_samples): print('--------------------------------------') print('Attacking input %i/%i' % (sample_ind + 1, nb_samples)) sample = X_test[sample_ind:(sample_ind + 1)] # We want to find an adversarial example for each possible target class # (i.e. all classes that differ from the label given in the dataset) current_class = int(np.argmax(Y_test[sample_ind])) target_classes = other_classes(nb_classes, current_class) # For the grid visualization, keep original images along the diagonal grid_viz_data[current_class, current_class, :, :, :] = np.reshape( sample, (img_rows, img_cols, channels)) # Loop over all target classes for target in target_classes: print('Generating adv. example for target class %i' % target) # This call runs the Jacobian-based saliency map approach one_hot_target = np.zeros((1, nb_classes), dtype=np.float32) one_hot_target[0, target] = 1 jsma_params['y_target'] = one_hot_target adv_x = jsma.generate_np(sample, **jsma_params) # Check if success was achieved res = int(model_argmax(sess, x, preds, adv_x) == target) # Computer number of modified features adv_x_reshape = adv_x.reshape(-1) test_in_reshape = X_test[sample_ind].reshape(-1) nb_changed = np.where(adv_x_reshape != test_in_reshape)[0].shape[0] percent_perturb = float(nb_changed) / adv_x.reshape(-1).shape[0] # Display the original and adversarial images side-by-side if FLAGS.viz_enabled: figure = pair_visual( np.reshape(sample, (img_rows, img_cols, channels)), np.reshape(adv_x, (img_rows, img_cols, channels)), figure) # Add our adversarial example to our grid data grid_viz_data[target, current_class, :, :, :] = np.reshape( adv_x, (img_rows, img_cols, channels)) # Update the arrays for later analysis results[target, sample_ind] = res perturbations[target, sample_ind] = percent_perturb print('--------------------------------------') # Compute the number of adversarial examples that were successfully found nb_targets_tried = ((nb_classes - 1) * nb_samples) succ_rate = float(np.sum(results)) / nb_targets_tried print('Avg. rate of successful adv. examples {0:.4f}'.format(succ_rate)) report.clean_train_adv_eval = 1. - succ_rate # Compute the average distortion introduced by the algorithm percent_perturbed = np.mean(perturbations) print('Avg. rate of perturbed features {0:.4f}'.format(percent_perturbed)) # Compute the average distortion introduced for successful samples only percent_perturb_succ = np.mean(perturbations * (results == 1)) print('Avg. rate of perturbed features for successful ' 'adversarial examples {0:.4f}'.format(percent_perturb_succ)) # Close TF session sess.close() # Finally, block & display a grid of all the adversarial examples if FLAGS.viz_enabled: import matplotlib.pyplot as plt plt.close(figure) _ = grid_visual(grid_viz_data)
import models, utils import os, argparse # Visualizing some training result with saliency map and gradcam # Command line argument parser = argparse.ArgumentParser(description='Visualize prediction') parser.add_argument('model', help='directory of the trained model saves') parser.add_argument('image', help='image for generating the visualization') args = parser.parse_args() # parameter model_parameter = args.model cnn = utils.model_load(model_parameter) input_size = vis_utils.get_input_size(cnn.meta['base_model']) target_layer = vis_utils.get_conv_layer(cnn.meta['base_model']) preprocess = transforms.Compose([ transforms.ToTensor(), transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]) ]) visualizer = Visualize(cnn, preprocess, target_layer, num_classes=cnn.meta['num_classes'], retainModel=False) img_pil = Image.open(args.image) img_pil = img_pil.resize((input_size, input_size))
import os import numpy as np import pickle from flask import Flask, flash, request, redirect, url_for, render_template, send_from_directory from werkzeug.utils import secure_filename from config import Config from utils import model_load, get_class, from_pdf_to_vector, find_similar import matplotlib.pyplot as plt ALLOWED_EXTENSIONS = {'pdf'} tfidf, logr = model_load('data/tfidf.p', 'data/logr.p') with open('data/vectorized_articles.p', 'rb') as file: database = pickle.load(file) app = Flask(__name__) app.config.from_object(Config) def allowed_file(filename): return '.' in filename and \ filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS @app.route('/', methods=['GET', 'POST']) def upload_file(): if request.method == 'POST': # check if the post request has the file part if 'file' not in request.files:
def main(argv=None): """ CIFAR10 CleverHans tutorial :return: """ # Object used to keep track of (and return) key accuracies report = AccuracyReport() # CIFAR10-specific dimensions img_rows = 32 img_cols = 32 channels = 3 nb_classes = 10 # Set TF random seed to improve reproducibility tf.set_random_seed(1234) sess = tf.Session() set_log_level(logging.DEBUG) # Get CIFAR10 test data X_train, Y_train, X_test, Y_test = data_cifar10() # Label smoothing assert Y_train.shape[1] == 10. # Define input TF placeholder x = tf.placeholder(tf.float32, shape=(None, img_rows, img_cols, channels)) y = tf.placeholder(tf.float32, shape=(None, 10)) model_path = FLAGS.model_path from cnn_models import make_basic_cnn model = make_basic_cnn('fp_', input_shape=(None, img_rows, img_cols, channels), nb_filters=FLAGS.nb_filters) preds = model(x) print("Defined TensorFlow model graph with %d parameters" % model.n_params) rng = np.random.RandomState([2017, 8, 30]) def evaluate(eval_params): # Evaluate the model on legitimate test examples acc = model_eval(sess, x, y, preds, X_test, Y_test, args=eval_params) return acc model_load(sess, model_path) print('Restored model from %s' % model_path) eval_params = {'batch_size': FLAGS.batch_size} accuracy = evaluate(eval_params) print('Test accuracy on legitimate test examples: {0}'.format(accuracy)) class_names = [ 'airplane', 'auto', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck' ] np.random.seed(0) labels = np.zeros((1, nb_classes)) conf_ar = np.zeros(nb_classes) path = './cifar10_cleverhans_gen' attack_params = { 'eps': 1, 'eps_iter': FLAGS.eps_iter, 'nb_iter': FLAGS.nb_iter, 'clip_min': 0., 'clip_max': 1. } from cleverhans.attacks import BasicIterativeMethod attacker = BasicIterativeMethod(model, back='tf', sess=sess) # build a rectangle in axes coords left, width = .25, .5 bottom, height = .25, .5 right = left + width top = bottom + height fig, axes = plt.subplots(1, 10, squeeze=True, figsize=(8, 1.25)) # generate unrecognizable adversarial examples for i in range(nb_classes): print("Generating %s" % class_names[i]) ''' Draw some noise from a uniform or Gaussian distribution. these settings are fairly arbitrary, feel free to tune the knobs You may also want to try: adv_img = np.clip(np.random.normal( loc=0.5, scale=0.25, size=(1, img_rows, img_cols, channels)), 0, 1) ''' adv_img = 0.5 + \ np.random.rand(1, img_rows, img_cols, channels) / 10 labels[0, :] = 0 labels[0, i] = 1 attack_params.update({'y_target': labels}) adv_img = attacker.generate_np(adv_img, **attack_params) axes[i].imshow(adv_img.reshape(img_rows, img_cols, channels)) axes[i].get_xaxis().set_visible(False) axes[i].get_yaxis().set_visible(False) if FLAGS.annot: ax = axes[i] ax.text(0.5 * (left + right), 1.0, class_names[i], horizontalalignment='center', verticalalignment='bottom', rotation=30, transform=ax.transAxes, size='larger') top = 0.6 else: top = 1.0 plt.tight_layout(pad=0) plt.subplots_adjust(left=0, bottom=0, right=1.0, top=top, wspace=0.2, hspace=0.2) plt.show() sess.close()
def main(argv=None): """ CIFAR10 CleverHans tutorial :return: """ # Object used to keep track of (and return) key accuracies report = AccuracyReport() # CIFAR10-specific dimensions img_rows = 32 img_cols = 32 channels = 3 nb_classes = 10 # Set TF random seed to improve reproducibility tf.set_random_seed(1234) sess = tf.Session() set_log_level(logging.DEBUG) # Get CIFAR10 test data X_train, Y_train, X_test, Y_test = data_cifar10() # Label smoothing assert Y_train.shape[1] == 10. # Define input TF placeholder x = tf.placeholder(tf.float32, shape=(None, img_rows, img_cols, channels)) y = tf.placeholder(tf.float32, shape=(None, 10)) model_path = FLAGS.model_path nb_samples = FLAGS.nb_samples from cnn_models import make_basic_cnn model = make_basic_cnn('fp_', input_shape=(None, img_rows, img_cols, channels), nb_filters=FLAGS.nb_filters) preds = model(x) print("Defined TensorFlow model graph with %d parameters" % model.n_params) rng = np.random.RandomState([2017, 8, 30]) def evaluate(eval_params): # Evaluate the model on legitimate test examples acc = model_eval(sess, x, y, preds, X_test, Y_test, args=eval_params) return acc model_load(sess, model_path) print('Restored model from %s' % model_path) eval_params = {'batch_size': FLAGS.batch_size} accuracy = evaluate(eval_params) print('Test accuracy on legitimate test examples: {0}'.format(accuracy)) ########################################################################### # Build dataset to perturb ########################################################################### if FLAGS.targeted: from utils import build_targeted_dataset adv_inputs, true_labels, adv_ys = build_targeted_dataset( X_test, Y_test, np.arange(nb_samples), nb_classes, img_rows, img_cols, channels) att_batch_size = np.clip(nb_samples * (nb_classes - 1), a_max=MAX_BATCH_SIZE, a_min=1) nb_adv_per_sample = nb_classes - 1 yname = "y_target" else: adv_inputs = X_test[:nb_samples] true_labels = Y_test[:nb_samples] att_batch_size = np.minimum(nb_samples, MAX_BATCH_SIZE) nb_adv_per_sample = 1 adv_ys = None yname = "y" print('Crafting ' + str(nb_samples) + ' * ' + str(nb_adv_per_sample) + ' adversarial examples') print("This could take some time ...") if FLAGS.attack == 'pgd': from cleverhans.attacks import MadryEtAl attacker = MadryEtAl(model, sess=sess) attack_params = { 'eps': FLAGS.eps / 255., 'eps_iter': EPS_ITER / 255., 'nb_iter': FLAGS.nb_iter, 'ord': np.inf, 'rand_init': True, 'batch_size': att_batch_size } elif FLAGS.attack == 'cwl2': from cleverhans.attacks import CarliniWagnerL2 attacker = CarliniWagnerL2(model, sess=sess) learning_rate = 0.1 attack_params = { 'binary_search_steps': 1, 'max_iterations': FLAGS.nb_iter, 'learning_rate': learning_rate, 'initial_const': 10, 'batch_size': att_batch_size } attack_params.update({ 'clip_min': 0., 'clip_max': 1., }) # yname: adv_ys}) X_test_adv = attacker.generate_np(adv_inputs, **attack_params) if FLAGS.targeted: assert X_test_adv.shape[0] == nb_samples * \ (nb_classes - 1), X_test_adv.shape # Evaluate the accuracy of the CIFAR10 model on adversarial # examples print("Evaluating targeted results") # adv_accuracy = model_eval(sess, x, y, preds, X_test_adv, true_labels, adv_accuracy = model_eval(sess, x, y, preds_adv, adv_inputs, true_labels, args=eval_params) else: # Evaluate the accuracy of the CIFAR10 model on adversarial # examples print("Evaluating un-targeted results") adv_accuracy = model_eval(sess, x, y, preds, X_test_adv, Y_test, args=eval_params) print('Test accuracy on adversarial examples %.4f' % adv_accuracy) # Compute the avg. distortion introduced by the attack diff = np.abs(X_test_adv - adv_inputs) percent_perturbed = np.mean(np.sum(diff, axis=(1, 2, 3))) print('Avg. L_1 norm of perturbations {0:.4f}'.format(percent_perturbed)) norm = np.mean(np.sqrt(np.sum(np.square(diff), axis=(1, 2, 3)))) print('Avg. L_2 norm of perturbations {0:.4f}'.format(norm)) sess.close()