code: import os
  # supress tensorflow logging other than errors
  os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
  
  import numpy as np
  import tensorflow as tf
  
  from keras import backend as K
  from keras.datasets import mnist
  from keras.models import Sequential, load_model
  from keras.layers import Dense, Dropout, Activation, Flatten
  from keras.layers import Convolution2D, MaxPooling2D
  from keras.utils import np_utils
  
  import matplotlib
  matplotlib.use('Agg')
  import matplotlib.pyplot as plt
  import matplotlib.gridspec as gridspec
  
  from attacks.fgsm import fgsm
  
  
  img_rows = 28
  img_cols = 28
  img_chas = 1
  input_shape = (img_rows, img_cols, img_chas)
  nb_classes = 10
  
  
  print('\nLoading mnist')
  (X_train, y_train), (X_test, y_test) = mnist.load_data()
  
  X_train = X_train.astype('float32') / 255.
  X_test = X_test.astype('float32') / 255.
  
  X_train = X_train.reshape(-1, img_rows, img_cols, img_chas)
  X_test = X_test.reshape(-1, img_rows, img_cols, img_chas)
  
  # one hot encoding
  y_train = np_utils.to_categorical(y_train, nb_classes)
  z0 = y_test.copy()
  y_test = np_utils.to_categorical(y_test, nb_classes)
  
  
  sess = tf.InteractiveSession()
  K.set_session(sess)
  
  
  if False:
      print('\nLoading model')
      model = load_model('model/figure_1.h5')
  else:
      print('\nBuilding model')
      model = Sequential([
          Convolution2D(32, 3, 3, input_shape=input_shape),
          Activation('relu'),
          Convolution2D(32, 3, 3),
          Activation('relu'),
          MaxPooling2D(pool_size=(2, 2)),
          Dropout(0.25),
          Flatten(),
          Dense(128),
          Activation('relu'),
          Dropout(0.5),
          Dense(10),
          Activation('softmax')])
  
      model.compile(optimizer='adam', loss='categorical_crossentropy',
                    metrics=['accuracy'])
  
      print('\nTraining model')
      model.fit(X_train, y_train, nb_epoch=10)
  
      print('\nSaving model')
      os.makedirs('model', exist_ok=True)
      model.save('model/figure_1.h5')
  
  
  x = tf.placeholder(tf.float32, (None, img_rows, img_cols, img_chas))
  x_adv = fgsm(model, x, nb_epoch=9, eps=0.02)
  
  
  print('\nTest against clean data')
  score = model.evaluate(X_test, y_test)
  print('\nloss: {0:.4f} acc: {1:.4f}'.format(score[0], score[1]))
  
  
  if False:
      print('\nLoading adversarial data')
      X_adv = np.load('data/figure_1.npy')
  else:
      print('\nGenerating adversarial data')
      nb_sample = X_test.shape[0]
      batch_size = 128
      nb_batch = int(np.ceil(nb_sample/batch_size))
      X_adv = np.empty(X_test.shape)
      for batch in range(nb_batch):
          print('batch {0}/{1}'.format(batch+1, nb_batch), end='\r')
          start = batch * batch_size
          end = min(nb_sample, start+batch_size)
          tmp = sess.run(x_adv, feed_dict={x: X_test[start:end],
                                           K.learning_phase(): 0})
          X_adv[start:end] = tmp
  
      os.makedirs('data', exist_ok=True)
      np.save('data/figure_1.npy', X_adv)
  
  
  print('\nTest against adversarial data')
  score = model.evaluate(X_adv, y_test)
  print('\nloss: {0:.4f} acc: {1:.4f}'.format(score[0], score[1]))
  
  
  print('\nMake predictions')
  y1 = model.predict(X_test)
  z1 = np.argmax(y1, axis=1)
  y2 = model.predict(X_adv)
  z2 = np.argmax(y2, axis=1)
  
  print('\nSelecting figures')
  X_tmp = np.empty((2, 10, 28, 28))
  y_proba = np.empty((2, 10, 10))
  for i in range(10):
      print('Target {0}'.format(i))
      ind, = np.where(np.all([z0==i, z1==i, z2!=i], axis=0))
      cur = np.random.choice(ind)
      X_tmp[0][i] = np.squeeze(X_test[cur])
      X_tmp[1][i] = np.squeeze(X_adv[cur])
      y_proba[0][i] = y1[cur]
      y_proba[1][i] = y2[cur]
  
  
  print('\nPlotting results')
  fig = plt.figure(figsize=(10, 3))
  gs = gridspec.GridSpec(2, 10, wspace=0.1, hspace=0.1)
  
  label = np.argmax(y_proba, axis=2)
  proba = np.max(y_proba, axis=2)
  for i in range(10):
      for j in range(2):
          ax = fig.add_subplot(gs[j, i])
          ax.imshow(X_tmp[j][i], cmap='gray', interpolation='none')
          ax.set_xticks([])
          ax.set_yticks([])
          ax.set_xlabel('{0} ({1:.2f})'.format(label[j][i],
                                               proba[j][i]),
                        fontsize=12)
  
  print('\nSaving figure')
  gs.tight_layout(fig)
  os.makedirs('img', exist_ok=True)
  plt.savefig('img/figure_1.pdf')
  
apis: tensorflow.InteractiveSession numpy.random.choice matplotlib.use numpy.squeeze tensorflow.placeholder matplotlib.pyplot.savefig numpy.save numpy.all numpy.max numpy.ceil numpy.argmax matplotlib.gridspec.GridSpec numpy.load numpy.empty matplotlib.pyplot.figure
api_extract: [(16, 'matplotlib.use', 'matplotlib.use', (['"""Agg"""'], {}), False, 'import matplotlib\n'), (31, 'keras.datasets.mnist.load_data', 'mnist.load_data', ([], {}), False, 'from keras.datasets import mnist\n'), (40, 'keras.utils.np_utils.to_categorical', 'np_utils.to_categorical', (['y_train', 'nb_classes'], {}), False, 'from keras.utils import np_utils\n'), (42, 'keras.utils.np_utils.to_categorical', 'np_utils.to_categorical', (['y_test', 'nb_classes'], {}), False, 'from keras.utils import np_utils\n'), (45, 'tensorflow.InteractiveSession', 'tf.InteractiveSession', ([], {}), True, 'import tensorflow as tf\n'), (46, 'keras.backend.set_session', 'K.set_session', (['sess'], {}), True, 'from keras import backend as K\n'), (79, 'tensorflow.placeholder', 'tf.placeholder', (['tf.float32', '(None, img_rows, img_cols, img_chas)'], {}), True, 'import tensorflow as tf\n'), (80, 'attacks.fgsm.fgsm', 'fgsm', (['model', 'x'], {'nb_epoch': '(9)', 'eps': '(0.02)'}), False, 'from attacks.fgsm import fgsm\n'), (116, 'numpy.argmax', 'np.argmax', (['y1'], {'axis': '(1)'}), True, 'import numpy as np\n'), (118, 'numpy.argmax', 'np.argmax', (['y2'], {'axis': '(1)'}), True, 'import numpy as np\n'), (121, 'numpy.empty', 'np.empty', (['(2, 10, 28, 28)'], {}), True, 'import numpy as np\n'), (122, 'numpy.empty', 'np.empty', (['(2, 10, 10)'], {}), True, 'import numpy as np\n'), (134, 'matplotlib.pyplot.figure', 'plt.figure', ([], {'figsize': '(10, 3)'}), True, 'import matplotlib.pyplot as plt\n'), (135, 'matplotlib.gridspec.GridSpec', 'gridspec.GridSpec', (['(2)', '(10)'], {'wspace': '(0.1)', 'hspace': '(0.1)'}), True, 'import matplotlib.gridspec as gridspec\n'), (137, 'numpy.argmax', 'np.argmax', (['y_proba'], {'axis': '(2)'}), True, 'import numpy as np\n'), (138, 'numpy.max', 'np.max', (['y_proba'], {'axis': '(2)'}), True, 'import numpy as np\n'), (151, 'os.makedirs', 'os.makedirs', (['"""img"""'], {'exist_ok': '(True)'}), False, 'import os\n'), (152, 'matplotlib.pyplot.savefig', 'plt.savefig', (['"""img/figure_1.pdf"""'], {}), True, 'import matplotlib.pyplot as plt\n'), (51, 'keras.models.load_model', 'load_model', (['"""model/figure_1.h5"""'], {}), False, 'from keras.models import Sequential, load_model\n'), (75, 'os.makedirs', 'os.makedirs', (['"""model"""'], {'exist_ok': '(True)'}), False, 'import os\n'), (90, 'numpy.load', 'np.load', (['"""data/figure_1.npy"""'], {}), True, 'import numpy as np\n'), (96, 'numpy.empty', 'np.empty', (['X_test.shape'], {}), True, 'import numpy as np\n'), (105, 'os.makedirs', 'os.makedirs', (['"""data"""'], {'exist_ok': '(True)'}), False, 'import os\n'), (106, 'numpy.save', 'np.save', (['"""data/figure_1.npy"""', 'X_adv'], {}), True, 'import numpy as np\n'), (126, 'numpy.random.choice', 'np.random.choice', (['ind'], {}), True, 'import numpy as np\n'), (127, 'numpy.squeeze', 'np.squeeze', (['X_test[cur]'], {}), True, 'import numpy as np\n'), (128, 'numpy.squeeze', 'np.squeeze', (['X_adv[cur]'], {}), True, 'import numpy as np\n'), (95, 'numpy.ceil', 'np.ceil', (['(nb_sample / batch_size)'], {}), True, 'import numpy as np\n'), (125, 'numpy.all', 'np.all', (['[z0 == i, z1 == i, z2 != i]'], {'axis': '(0)'}), True, 'import numpy as np\n'), (55, 'keras.layers.Convolution2D', 'Convolution2D', (['(32)', '(3)', '(3)'], {'input_shape': 'input_shape'}), False, 'from keras.layers import Convolution2D, MaxPooling2D\n'), (56, 'keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (57, 'keras.layers.Convolution2D', 'Convolution2D', (['(32)', '(3)', '(3)'], {}), False, 'from keras.layers import Convolution2D, MaxPooling2D\n'), (58, 'keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (59, 'keras.layers.MaxPooling2D', 'MaxPooling2D', ([], {'pool_size': '(2, 2)'}), False, 'from keras.layers import Convolution2D, MaxPooling2D\n'), (60, 'keras.layers.Dropout', 'Dropout', (['(0.25)'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (61, 'keras.layers.Flatten', 'Flatten', ([], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (62, 'keras.layers.Dense', 'Dense', (['(128)'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (63, 'keras.layers.Activation', 'Activation', (['"""relu"""'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (64, 'keras.layers.Dropout', 'Dropout', (['(0.5)'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (65, 'keras.layers.Dense', 'Dense', (['(10)'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (66, 'keras.layers.Activation', 'Activation', (['"""softmax"""'], {}), False, 'from keras.layers import Dense, Dropout, Activation, Flatten\n'), (102, 'keras.backend.learning_phase', 'K.learning_phase', ([], {}), True, 'from keras import backend as K\n')]