{"action":"opened","issue":{"url":"https://api.github.com/repos/bethgelab/foolbox/issues/178","repository_url":"https://api.github.com/repos/bethgelab/foolbox","labels_url":"https://api.github.com/repos/bethgelab/foolbox/issues/178/labels{/name}","comments_url":"https://api.github.com/repos/bethgelab/foolbox/issues/178/comments","events_url":"https://api.github.com/repos/bethgelab/foolbox/issues/178/events","html_url":"https://github.com/bethgelab/foolbox/issues/178","id":336096648,"node_id":"MDU6SXNzdWUzMzYwOTY2NDg=","number":178,"title":"Foolbox predictions doesnot match tensorflow slim predictions ","user":{"login":"Arkadeep-sophoIITG","id":19313432,"node_id":"MDQ6VXNlcjE5MzEzNDMy","avatar_url":"https://avatars0.githubusercontent.com/u/19313432?v=4","gravatar_id":"","url":"https://api.github.com/users/Arkadeep-sophoIITG","html_url":"https://github.com/Arkadeep-sophoIITG","followers_url":"https://api.github.com/users/Arkadeep-sophoIITG/followers","following_url":"https://api.github.com/users/Arkadeep-sophoIITG/following{/other_user}","gists_url":"https://api.github.com/users/Arkadeep-sophoIITG/gists{/gist_id}","starred_url":"https://api.github.com/users/Arkadeep-sophoIITG/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/Arkadeep-sophoIITG/subscriptions","organizations_url":"https://api.github.com/users/Arkadeep-sophoIITG/orgs","repos_url":"https://api.github.com/users/Arkadeep-sophoIITG/repos","events_url":"https://api.github.com/users/Arkadeep-sophoIITG/events{/privacy}","received_events_url":"https://api.github.com/users/Arkadeep-sophoIITG/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"assignees":[],"milestone":null,"comments":0,"created_at":"2018-06-27T06:44:46Z","updated_at":"2018-06-27T06:44:46Z","closed_at":null,"author_association":"NONE","body":"Foolbox tensorflow model predictions donot match tensorflow slim model predictions on clean images .\r\nIs there some issue while restoring the model ?\r\nHere's the code: \r\n`\r\nfrom __future__ import absolute_import\r\nfrom __future__ import division\r\nfrom __future__ import print_function\r\n\r\nimport argparse\r\nimport os\r\nimport time\r\n\r\nimport numpy as np\r\nimport tensorflow as tf\r\nimport tensorflow.contrib.slim as slim\r\n\r\nfrom config.parse_config import parse_config_file\r\nfrom nets import nets_factory\r\nfrom preprocessing import inputs\r\nimport foolbox\r\nfrom scipy.misc import imsave\r\nfrom tensorflow.python.training import saver as tf_saver\r\n\r\nfrom PIL import Image\r\nimport cv2\r\n\r\n# !/usr/bin/python\r\n# -*- coding: utf-8 -*-\r\nfrom tensorflow.python.framework import dtypes\r\nfrom tensorflow.python.framework import ops\r\nfrom tensorflow.python.ops import array_ops\r\nfrom tensorflow.python.ops import init_ops\r\nfrom tensorflow.python.ops import state_ops\r\nfrom tensorflow.python.ops import variable_scope\r\nfrom tensorflow.python.platform import tf_logging as logging\r\nfrom tensorflow.python.training import basic_session_run_hooks\r\nfrom tensorflow.python.training import monitored_session\r\nfrom tensorflow.python.training import session_run_hook\r\nfrom foolbox.criteria import TopKMisclassification\r\nimport csv\r\n\r\n\r\ndef test(tfrecords, checkpoint_path, save_dir, max_iterations, eval_interval_secs, cfg, read_images=False):\r\n    \"\"\"\r\n    Args:\r\n        tfrecords (list)\r\n        checkpoint_path (str)\r\n        savedir (str)\r\n        max_iterations (int)\r\n        cfg (EasyDict)\r\n    \"\"\"\r\n    tf.logging.set_verbosity(tf.logging.DEBUG)\r\n\r\n    graph = tf.Graph()\r\n    with graph.as_default():\r\n\r\n        global_step = slim.get_or_create_global_step()\r\n\r\n        with tf.device('/cpu:0'):\r\n            batch_dict = inputs.input_nodes(\r\n                tfrecords=tfrecords,\r\n                cfg=cfg.IMAGE_PROCESSING,\r\n                num_epochs=1,\r\n                batch_size=cfg.BATCH_SIZE,\r\n                num_threads=cfg.NUM_INPUT_THREADS,\r\n                shuffle_batch=cfg.SHUFFLE_QUEUE,\r\n                random_seed=cfg.RANDOM_SEED,\r\n                capacity=cfg.QUEUE_CAPACITY,\r\n                min_after_dequeue=cfg.QUEUE_MIN,\r\n                add_summaries=False,\r\n                input_type='test',\r\n                read_filenames=read_images\r\n            )\r\n\r\n            batched_one_hot_labels = slim.one_hot_encoding(batch_dict['labels'],\r\n                                                           num_classes=cfg.NUM_CLASSES)\r\n\r\n        arg_scope = nets_factory.arg_scopes_map[cfg.MODEL_NAME]()\r\n\r\n        with slim.arg_scope(arg_scope):\r\n            logits, end_points = nets_factory.networks_map[cfg.MODEL_NAME](\r\n                images=batch_dict['inputs'],\r\n                num_classes=cfg.NUM_CLASSES,\r\n                is_training=False\r\n            )\r\n\r\n            predictions = end_points['Predictions']\r\n            # labels = tf.squeeze(batch_dict['labels'])\r\n            labels = batch_dict['labels']\r\n            # Add the loss summary\r\n            loss = tf.losses.softmax_cross_entropy(\r\n                logits=logits, onehot_labels=batched_one_hot_labels, label_smoothing=0., weights=1.0)\r\n\r\n        if 'MOVING_AVERAGE_DECAY' in cfg and cfg.MOVING_AVERAGE_DECAY > 0:\r\n            variable_averages = tf.train.ExponentialMovingAverage(\r\n                cfg.MOVING_AVERAGE_DECAY, global_step)\r\n            variables_to_restore = variable_averages.variables_to_restore(\r\n                slim.get_model_variables())\r\n            variables_to_restore[global_step.op.name] = global_step\r\n        else:\r\n            variables_to_restore = slim.get_variables_to_restore()\r\n            variables_to_restore.append(global_step)\r\n\r\n        if variables_to_restore is not None:\r\n            saver = tf_saver.Saver(variables_to_restore)\r\n        # Define the metrics:\r\n        metric_map = {\r\n            'Accuracy': tf.metrics.accuracy(labels=labels, predictions=tf.argmax(predictions, 1)),\r\n            # slim.metrics.streaming_accuracy(labels=labels, predictions=tf.argmax(predictions, 1)),\r\n            loss.op.name: slim.metrics.streaming_mean(loss)\r\n        }\r\n\r\n        if len(cfg.ACCURACY_AT_K_METRIC) > 0:\r\n            bool_labels = tf.ones([cfg.BATCH_SIZE], dtype=tf.bool)\r\n            for k in cfg.ACCURACY_AT_K_METRIC:\r\n                if k <= 1 or k > cfg.NUM_CLASSES:\r\n                    continue\r\n                in_top_k = tf.nn.in_top_k(predictions=predictions, targets=labels, k=k)\r\n                metric_map['Accuracy_at_%s' % k] = tf.metrics.accuracy(labels=bool_labels,\r\n                                                                       predictions=in_top_k)  # slim.metrics.streaming_accuracy(labels=bool_labels, predictions=in_top_k)\r\n\r\n        names_to_values, names_to_updates = slim.metrics.aggregate_metric_map(metric_map)\r\n\r\n        # Print the summaries to screen.\r\n        print_global_step = True\r\n        for name, value in names_to_values.iteritems():\r\n            summary_name = 'eval/%s' % name\r\n            op = tf.summary.scalar(summary_name, value, collections=[])\r\n            if print_global_step:\r\n                op = tf.Print(op, [global_step], \"Model Step \")\r\n                print_global_step = False\r\n            op = tf.Print(op, [value], summary_name)\r\n            tf.add_to_collection(tf.GraphKeys.SUMMARIES, op)\r\n\r\n        if max_iterations > 0:\r\n            num_batches = max_iterations\r\n        else:\r\n            # This ensures that we make a single pass over all of the data.\r\n            # We could use ceil if the batch queue is allowed to pad the last batch\r\n            num_batches = np.floor(cfg.NUM_TEST_EXAMPLES / float(cfg.BATCH_SIZE))\r\n\r\n        sess_config = tf.ConfigProto(\r\n            log_device_placement=cfg.SESSION_CONFIG.LOG_DEVICE_PLACEMENT,\r\n            allow_soft_placement=True,\r\n            gpu_options=tf.GPUOptions(\r\n                per_process_gpu_memory_fraction=cfg.SESSION_CONFIG.PER_PROCESS_GPU_MEMORY_FRACTION\r\n            ),\r\n            intra_op_parallelism_threads=cfg.SESSION_CONFIG.INTRA_OP_PARALLELISM_THREADS if 'INTRA_OP_PARALLELISM_THREADS' in cfg.SESSION_CONFIG else None,\r\n            inter_op_parallelism_threads=cfg.SESSION_CONFIG.INTER_OP_PARALLELISM_THREADS if 'INTER_OP_PARALLELISM_THREADS' in cfg.SESSION_CONFIG else None\r\n        )\r\n\r\n        if eval_interval_secs > 0:\r\n\r\n            if not os.path.isdir(checkpoint_path):\r\n                raise ValueError(\"checkpoint_path should be a path to a directory when \" \\\r\n                                 \"evaluating in a loop.\")\r\n\r\n            slim.evaluation.evaluation_loop(\r\n                master='',\r\n                checkpoint_dir=checkpoint_path,\r\n                logdir=save_dir,\r\n                num_evals=num_batches,\r\n                initial_op=None,\r\n                initial_op_feed_dict=None,\r\n                eval_op=names_to_updates.values(),\r\n                eval_op_feed_dict=None,\r\n                final_op=None,\r\n                final_op_feed_dict=None,\r\n                summary_op=tf.summary.merge_all(),\r\n                summary_op_feed_dict=None,\r\n                variables_to_restore=variables_to_restore,\r\n                eval_interval_secs=eval_interval_secs,\r\n                max_number_of_evaluations=None,\r\n                session_config=sess_config,\r\n                timeout=None\r\n            )\r\n\r\n        else:\r\n            if os.path.isdir(checkpoint_path):\r\n                checkpoint_dir = checkpoint_path\r\n                checkpoint_path = tf.train.latest_checkpoint(checkpoint_dir)\r\n\r\n                if checkpoint_path is None:\r\n                    raise ValueError(\"Unable to find a model checkpoint in the \" \\\r\n                                     \"directory %s\" % (checkpoint_dir,))\r\n\r\n            tf.logging.info('Evaluating %s' % checkpoint_path)\r\n\r\n            slim.evaluation.evaluate_once(\r\n                master='',\r\n                checkpoint_path=checkpoint_path,\r\n                logdir=save_dir,\r\n                num_evals=num_batches,\r\n                eval_op=tf.Print(list(names_to_updates.values()), [labels, predictions, tf.argmax(predictions,1)],\r\n                                 message=\"original label   predictions       predicted label \", summarize=100),\r\n                variables_to_restore=variables_to_restore,\r\n                session_config=sess_config\r\n            )\r\n\r\n        with tf.Session() as sess:\r\n            # init_fn(sess)\r\n            saver.restore(sess, checkpoint_path)\r\n            model = foolbox.models.TensorFlowModel(batch_dict['inputs'], logits, (0, 255))\r\n            criteria = TopKMisclassification(k=1)\r\n            attacks = ['deepfool', 'igrad', 'igradsign']\r\n            criter1 = foolbox.criteria.TargetClassProbability(0, 0.6)\r\n            criter2 = foolbox.criteria.TargetClassProbability(1, 0.6)\r\n            attack_1 = foolbox.attacks.DeepFoolAttack(model)\r\n            attack_2 = foolbox.attacks.IterativeGradientAttack(model)\r\n            attack_3 = foolbox.attacks.IterativeGradientSignAttack(model)\r\n            attack_4 = foolbox.attacks.ApproximateLBFGSAttack(model, criter1)\r\n            attack_5 = foolbox.attacks.SinglePixelAttack(model)\r\n            attack_6 = foolbox.attacks.AdditiveUniformNoiseAttack(model)\r\n            path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/chest_disease/'\r\n            dirs = os.listdir(path)\r\n            counter = 0\r\n            header = ['image', 'clean predicted label', 'clean predicted label confidence', 'other label conf',\r\n                      'adversarial label', 'adversarial predicted label confidence', 'other adv label confidence ']\r\n            data = []\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_1(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/deepfool/chest_disease/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n            with open('deepfool.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path2 = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/No_finding/'\r\n            dirs = os.listdir(path2)\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path2 + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_1(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite(\r\n                        '/local-scratch/ada77/cedar-rm/scratch/adversary/deepfool/No_finding/' + str(counter) + '.png',\r\n                        adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n            with open('deepfool.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                # writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/chest_disease/'\r\n            dirs = os.listdir(path)\r\n            counter = 0\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_3(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/igradsignattack/chest_disease/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('igradsignattack.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path2 = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/No_finding/'\r\n            dirs = os.listdir(path2)\r\n            attacks = ['deepfool', 'igrad', 'igradsign']\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path2 + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_3(image, 1, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/igradsignattack/No_finding/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('igradsignattack.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                # writer.writerow( header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/chest_disease/'\r\n            dirs = os.listdir(path)\r\n            counter = 0\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_3(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/igradattack/chest_disease/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('igradattack.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path2 = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/No_finding/'\r\n            dirs = os.listdir(path2)\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path2 + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_3(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/igradattack/No_finding/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('igradattack.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                # writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/chest_disease/'\r\n            dirs = os.listdir(path)\r\n            counter = 0\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_6(image, 0, unpack=False)\r\n\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite(\r\n                        '/local-scratch/ada77/cedar-rm/scratch/adversary/add_uniform_noise/chest_disease/' + str(\r\n                            counter) + '.png',\r\n                        adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('add_unif_noise.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path2 = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/No_finding/'\r\n            dirs = os.listdir(path2)\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path2 + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_6(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/add_uniform_noise/No_finding/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('add_unif_noise.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                # writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/chest_disease/'\r\n            dirs = os.listdir(path)\r\n            counter = 0\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_4(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/approxlbfgs/chest_disease/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('approxlbfgs.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n            adversarial_1 = None\r\n            path2 = '/local-scratch/ada77/cedar-rm/scratch/afresh/organised_test/No_finding/'\r\n            dirs = os.listdir(path2)\r\n            for item in dirs:\r\n                counter = counter + 1\r\n                image = cv2.imread(path2 + item)\r\n                print(item)\r\n                example_label = np.argmax(model.predictions(image))\r\n                print(example_label)\r\n                try:\r\n                    adversarial_1 = attack_4(image, 0, unpack=False)\r\n                except:\r\n                    print('Not found adversarials dammit.. or some other weird error')\r\n                pred = foolbox.utils.softmax(model.predictions(image))[example_label]\r\n                if adversarial_1.image is not None:\r\n                    cv2.imwrite('/local-scratch/ada77/cedar-rm/scratch/adversary/approxlbfgs/No_finding/' + str(\r\n                        counter) + '.png', adversarial_1.image)\r\n                    label_adv = np.argmax(model.predictions(adversarial_1.image))\r\n                    print(label_adv)\r\n                    pred_adv = foolbox.utils.softmax(model.predictions(adversarial_1.image))[label_adv]\r\n                    data.append([item, example_label, pred, 1 - pred, label_adv, pred_adv, 1 - pred_adv])\r\n                else:\r\n                    data.append([item, example_label, pred, 1 - pred, example_label, pred, 1 - pred])\r\n            with open('approxlbfgs.csv', 'a') as file:\r\n                writer = csv.writer(file, delimiter=',')\r\n                # writer.writerow(header)\r\n                for j in data:\r\n                    writer.writerow(j)\r\n            del data[:]\r\n\r\n\r\ndef parse_args():\r\n    parser = argparse.ArgumentParser(description='Test the person classifier')\r\n\r\n    parser.add_argument('--tfrecords', dest='tfrecords',\r\n                        help='Paths to tfrecords.', type=str,\r\n                        nargs='+', required=True)\r\n\r\n    parser.add_argument('--checkpoint_path', dest='checkpoint_path',\r\n                        help='Path to a specific model to test against. If a directory, then the newest checkpoint file will be used.',\r\n                        type=str,\r\n                        required=True, default=None)\r\n\r\n    parser.add_argument('--save_dir', dest='savedir',\r\n                        help='Path to directory to store summary files.', type=str,\r\n                        required=True)\r\n\r\n    parser.add_argument('--config', dest='config_file',\r\n                        help='Path to the configuration file.',\r\n                        required=True, type=str)\r\n\r\n    parser.add_argument('--eval_interval_secs', dest='eval_interval_secs',\r\n                        help='Go into an evaluation loop, waiting this many seconds between evaluations. Default is to evaluate once.',\r\n                        required=False, type=int, default=0)\r\n\r\n    parser.add_argument('--batch_size', dest='batch_size',\r\n                        help='The number of images in a batch.',\r\n                        required=False, type=int, default=None)\r\n\r\n    parser.add_argument('--batches', dest='batches',\r\n                        help='Maximum number of iterations to run. Default is all records (modulo the batch size).',\r\n                        required=False, type=int, default=0)\r\n\r\n    parser.add_argument('--model_name', dest='model_name',\r\n                        help='The name of the architecture to use.',\r\n                        required=False, type=str, default=None)\r\n\r\n    parser.add_argument('--read_images', dest='read_images',\r\n                        help='Read the images from the file system using the `filename` field rather than using the `encoded` field of the tfrecord.',\r\n                        action='store_true', default=False)\r\n\r\n    args = parser.parse_args()\r\n    return args\r\n\r\n\r\ndef main():\r\n    args = parse_args()\r\n\r\n    cfg = parse_config_file(args.config_file)\r\n\r\n    if args.batch_size != None:\r\n        cfg.BATCH_SIZE = args.batch_size\r\n\r\n    if args.model_name != None:\r\n        cfg.MODEL_NAME = args.model_name\r\n\r\n    test(\r\n        tfrecords=args.tfrecords,\r\n        checkpoint_path=args.checkpoint_path,\r\n        save_dir=args.savedir,\r\n        max_iterations=args.batches,\r\n        eval_interval_secs=args.eval_interval_secs,\r\n        cfg=cfg,\r\n        read_images=args.read_images\r\n    )\r\n\r\n\r\nif __name__ == '__main__':\r\n    main()\r\n\r\n`"}}
{"actor":{"display_login":"Arkadeep-sophoIITG"}}