Created
September 5, 2018 19:11
-
-
Save shikhar-goknit/fe66e660fa04c4954b50ac724adc4fd9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "cells": [ | |
| { | |
| "cell_type": "code", | |
| "execution_count": 2, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "import tensorflow as tf\n", | |
| "import keras\n", | |
| "from keras.backend.tensorflow_backend import set_session\n", | |
| "import os\n", | |
| "os.environ[\"CUDA_DEVICE_ORDER\"]=\"PCI_BUS_ID\" \n", | |
| "os.environ[\"CUDA_VISIBLE_DEVICES\"]=\"1\"\n", | |
| "config = tf.ConfigProto()\n", | |
| "config.gpu_options.per_process_gpu_memory_fraction = 0.7\n", | |
| "\n", | |
| "keras.backend.set_learning_phase(0)\n", | |
| "set_session(tf.Session(config=config))\n", | |
| "import numpy as np\n", | |
| "import matplotlib.pyplot as plt\n", | |
| "%matplotlib inline\n", | |
| "from keras.applications.inception_v3 import InceptionV3,preprocess_input\n", | |
| "import foolbox\n", | |
| "from keras.models import load_model" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 3, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "WARNING:tensorflow:From /usr/local/lib/python3.4/dist-packages/keras/backend/tensorflow_backend.py:1349: calling reduce_mean (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n", | |
| "Instructions for updating:\n", | |
| "keep_dims is deprecated, use keepdims instead\n", | |
| "WARNING:tensorflow:From /usr/local/lib/python3.4/dist-packages/keras/backend/tensorflow_backend.py:2885: calling reduce_sum (from tensorflow.python.ops.math_ops) with keep_dims is deprecated and will be removed in a future version.\n", | |
| "Instructions for updating:\n", | |
| "keep_dims is deprecated, use keepdims instead\n", | |
| "WARNING:tensorflow:Variable *= will be deprecated. Use variable.assign_mul if you want assignment to the variable value or 'x = x * y' if you want a new python Tensor object.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "from keras.preprocessing import image\n", | |
| "from foolbox.criteria import TargetClass\n", | |
| "\n", | |
| "model=load_model('/home/shikhar/notebooks/standard_imagenet_100_inceptiov3.h5') ## Inception v3\n", | |
| "fmodel=foolbox.models.KerasModel(model,bounds=(-1,1))\n", | |
| "\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 4, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "-1.0 1.0\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "img_path='/home/shikhar/Downloads/Imagenet_100/TEST/acoustic_guitar/guitar.jpg'\n", | |
| "img=image.load_img(img_path)\n", | |
| "img=img.resize((299,299))\n", | |
| "img=image.img_to_array(img)\n", | |
| "pre_img=preprocess_input (img)\n", | |
| "print (pre_img.min(),pre_img.max())\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 5, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "34\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "WARNING:root:Running an attack that tries to minimize the Linfinity norm of the perturbation without specifying foolbox.distances.Linfinity as the distance metric might lead to suboptimal results.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "label=np.argmax(model.predict(np.expand_dims(pre_img,axis=0)))\n", | |
| "print (label)\n", | |
| "attack=foolbox.attacks.RandomPGD(fmodel,TargetClass(11))\n", | |
| "adversarial=attack(pre_img,label,unpack=False) " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 6, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [ | |
| "model1=load_model('/home/shikhar/notebooks/semantic_imagenet_100_inceptionV3.h5')\n", | |
| "fmodel1=foolbox.models.KerasModel(model1,bounds=(-1,1))" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 7, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "34\n" | |
| ] | |
| }, | |
| { | |
| "name": "stderr", | |
| "output_type": "stream", | |
| "text": [ | |
| "WARNING:root:Running an attack that tries to minimize the Linfinity norm of the perturbation without specifying foolbox.distances.Linfinity as the distance metric might lead to suboptimal results.\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "label1=np.argmax(model1.predict(np.expand_dims(pre_img,axis=0)))\n", | |
| "print (label1)\n", | |
| "attack1=foolbox.attacks.RandomPGD(fmodel1,TargetClass(11))\n", | |
| "adversarial1=attack1(pre_img,label1,unpack=False)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 8, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "standard 1.78231362111043e-05\n", | |
| "semantic 6.142532629056862e-05\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "print ('standard ',adversarial.distance.value)\n", | |
| "print ('semantic ',adversarial1.distance.value)\n" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 22, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "semantic- wallet | standard- wallet || True || wallet\n", | |
| "semantic- wallet | standard- wallet || True || wallet\n", | |
| "semantic- volcano | standard- volcano || True || volcano\n", | |
| "semantic- bookshop | standard- library || False || bookshop\n", | |
| "semantic- bookshop | standard- library || False || bookshop\n", | |
| "semantic- crossword | standard- crossword || True || crossword\n", | |
| "semantic- crossword | standard- crossword || True || crossword\n", | |
| "semantic- sea_urchin | standard- sea_urchin || True || sea_urchin\n", | |
| "semantic- restaurant | standard- restaurant || True || restaurant\n", | |
| "semantic- assault_rifle | standard- restaurant || True || restaurant\n", | |
| "semantic- pretzel | standard- pretzel || True || pretzel\n", | |
| "semantic- pretzel | standard- pretzel || True || pretzel\n", | |
| "semantic- hamster | standard- hamster || True || hamster\n", | |
| "semantic- hamster | standard- polar_bear || False || hamster\n", | |
| "semantic- television | standard- television || True || television\n", | |
| "semantic- television | standard- television || True || television\n", | |
| "semantic- castle | standard- castle || True || castle\n", | |
| "semantic- candle | standard- candle || True || candle\n", | |
| "semantic- candle | standard- candle || True || candle\n", | |
| "semantic- staffordshire_bull_terrier | standard- staffordshire_bull_terrier || True || staffordshire_bull_terrier\n", | |
| "semantic- staffordshire_bull_terrier | standard- staffordshire_bull_terrier || True || staffordshire_bull_terrier\n", | |
| "semantic- polar_bear | standard- polar_bear || True || polar_bear\n", | |
| "semantic- polar_bear | standard- polar_bear || True || polar_bear\n", | |
| "semantic- ipod | standard- laptop || True || laptop\n", | |
| "semantic- laptop | standard- laptop || True || laptop\n", | |
| "semantic- speaker | standard- speaker || False || radio\n", | |
| "semantic- radio | standard- radio || True || radio\n", | |
| "semantic- peacock | standard- peacock || True || peacock\n", | |
| "semantic- peacock | standard- peacock || True || peacock\n", | |
| "semantic- ambulance | standard- ambulance || True || ambulance\n", | |
| "semantic- firetruck | standard- ambulance || True || ambulance\n", | |
| "semantic- billiard_table | standard- billiard_table || True || billiard_table\n", | |
| "semantic- billiard_table | standard- printer || False || billiard_table\n", | |
| "semantic- vase | standard- vase || True || vase\n", | |
| "semantic- vase | standard- vase || True || vase\n", | |
| "semantic- bathtub | standard- coffee_mug || False || bathtub\n", | |
| "semantic- zebra | standard- zebra || True || zebra\n", | |
| "semantic- zebra | standard- zebra || True || zebra\n", | |
| "semantic- suit | standard- suit || True || suit\n", | |
| "semantic- suit | standard- suit || True || suit\n", | |
| "semantic- sunglass | standard- sunglass || True || sunglass\n", | |
| "semantic- cellphone | standard- vase || False || screw_driver\n", | |
| "semantic- golden_retriever | standard- golden_retriever || True || golden_retriever\n", | |
| "semantic- golden_retriever | standard- golden_retriever || True || golden_retriever\n", | |
| "semantic- cinema | standard- television || False || cinema\n", | |
| "semantic- cinema | standard- cinema || True || cinema\n", | |
| "semantic- dumbbell | standard- dumbbell || True || dumbbell\n", | |
| "semantic- dumbbell | standard- speaker || False || dumbbell\n", | |
| "semantic- cellphone | standard- cellphone || True || cellphone\n", | |
| "semantic- ipod | standard- cellphone || True || cellphone\n", | |
| "semantic- speaker | standard- speaker || True || speaker\n", | |
| "semantic- speaker | standard- speaker || True || speaker\n", | |
| "semantic- lion | standard- lion || True || lion\n", | |
| "semantic- lion | standard- lion || True || lion\n", | |
| "semantic- bakery | standard- bakery || True || bakery\n", | |
| "semantic- dalmatian | standard- dalmatian || True || dalmatian\n", | |
| "semantic- dalmatian | standard- dalmatian || True || dalmatian\n", | |
| "semantic- aircraft_carrier | standard- aircraft_carrier || True || aircraft_carrier\n", | |
| "semantic- panda | standard- panda || True || panda\n", | |
| "semantic- panda | standard- panda || True || panda\n", | |
| "semantic- paintbrush | standard- broom || False || paintbrush\n", | |
| "semantic- radio | standard- lighter || False || paintbrush\n", | |
| "semantic- piggybank | standard- piggybank || True || piggybank\n", | |
| "semantic- umbrella | standard- piggybank || True || piggybank\n", | |
| "semantic- printer | standard- printer || False || umbrella\n", | |
| "semantic- langur | standard- langur || True || langur\n", | |
| "semantic- tarantula | standard- tarantula || True || tarantula\n", | |
| "semantic- tarantula | standard- tarantula || True || tarantula\n", | |
| "semantic- dustbin | standard- bucket || False || dustbin\n", | |
| "semantic- bucket | standard- bucket || False || dustbin\n", | |
| "semantic- assault_rifle | standard- assault_rifle || True || assault_rifle\n", | |
| "semantic- assault_rifle | standard- assault_rifle || True || assault_rifle\n", | |
| "semantic- lighter | standard- perfume || True || perfume\n", | |
| "semantic- perfume | standard- perfume || True || perfume\n", | |
| "semantic- snail | standard- snail || True || snail\n", | |
| "semantic- snail | standard- snail || True || snail\n", | |
| "semantic- acoustic_guitar | standard- acoustic_guitar || True || acoustic_guitar\n", | |
| "semantic- acoustic_guitar | standard- acoustic_guitar || True || acoustic_guitar\n", | |
| "semantic- laptop | standard- laptop || False || cab\n", | |
| "semantic- cab | standard- cab || True || cab\n", | |
| "semantic- corn | standard- corn || True || corn\n", | |
| "semantic- corn | standard- corn || True || corn\n", | |
| "semantic- pomeranian | standard- pomeranian || True || pomeranian\n" | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "labels={'scuba_diver': 98, 'sea_urchin': 28, 'crossword': 93, 'chihuahua': 15, 'dumbbell': 57, 'printer': 76, 'mailbox': 65, 'cellphone': 53, 'balloon': 42, 'doberman': 18, 'mask': 66, 'bathtub': 44, 'gray_wolf': 22, 'cinema': 54, 'kite': 4, 'lion': 23, 'lemon': 95, 'bakery': 41, 'bookshop': 47, 'dustbin': 38, 'snail': 13, 'assault_rifle': 39, 'tank': 83, 'speaker': 64, 'iguana': 6, 'dragonfly': 27, 'hamster': 29, 'knapsack': 40, 'umbrella': 86, 'hen': 1, 'wallet': 88, 'pirate': 74, 'dishwasher': 56, 'ambulance': 37, 'toyshop': 85, 'broom': 48, 'aircraft_carrier': 35, 'necklace': 68, 'restaurant': 78, 'corn': 99, 'panda': 33, 'jellyfish': 12, 'goose': 11, 'cock': 0, 'billiard_table': 75, 'pretzel': 94, 'tarantula': 9, 'minivan': 67, 'piggybank': 72, 'staffordshire_bull_terrier': 16, 'pillow': 73, 'polar_bear': 24, 'snowmobile': 80, 'grasshopper': 26, 'suit': 81, 'vase': 87, 'volcano': 97, 'castle': 52, 'airliner': 36, 'sock': 91, 'ipod': 60, 'ox': 31, 'lighthouse': 45, 'laptop': 61, 'pomeranian': 21, 'ostrich': 2, 'dalmatian': 19, 'paintbrush': 69, 'zebra': 30, 'radio': 77, 'candle': 51, 'cab': 50, 'bucket': 49, 'golfball': 59, 'peacock': 10, 'sunglass': 82, 'perfume': 71, 'screwdriver': 79, 'pug': 20, 'barrel': 43, 'golden_retriever': 17, 'terrapin': 5, 'bulbul': 3, 'lighter': 63, 'shield': 90, 'flamingo': 14, 'banana': 96, 'library': 62, 'whistle': 89, 'mongoose': 25, 'langur': 32, 'bikini': 46, 'scorpion': 8, 'television': 92, 'acoustic_guitar': 34, 'coffee_mug': 55, 'teapot': 84, 'firetruck': 58, 'kingsnake': 7, 'pajama': 70}\n", | |
| "import collections\n", | |
| "inv_map={v:k for k,v in labels.items()}\n", | |
| "od=collections.OrderedDict(sorted(inv_map.items()))\n", | |
| "dir_path='/home/shikhar/Downloads/Imagenet_100/TEST/'\n", | |
| "for i in os.listdir(dir_path):\n", | |
| " for j in os.listdir(dir_path+i+'/'):\n", | |
| " im=image.load_img(dir_path+'/'+i+'/'+j)\n", | |
| " im=im.resize((299,299))\n", | |
| " im=image.img_to_array(im)\n", | |
| " im=np.expand_dims(im,axis=0)\n", | |
| " im=preprocess_input(im)\n", | |
| " pred=np.argmax(model.predict(im))\n", | |
| " pred1=np.argmax(model1.predict(im))\n", | |
| " flag=False\n", | |
| " if (od[pred1]==od[pred1]==i):\n", | |
| " flag=True\n", | |
| " print ('semantic-',od[pred],'|','standard-',od[pred1],' ||',flag,'|| ', i)" | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": 39, | |
| "metadata": {}, | |
| "outputs": [ | |
| { | |
| "name": "stdout", | |
| "output_type": "stream", | |
| "text": [ | |
| "wallet wallet\n", | |
| "16\n" | |
| ] | |
| }, | |
| { | |
| "ename": "AssertionError", | |
| "evalue": "", | |
| "output_type": "error", | |
| "traceback": [ | |
| "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", | |
| "\u001b[0;31mAssertionError\u001b[0m Traceback (most recent call last)", | |
| "\u001b[0;32m<ipython-input-39-4dcf542c6b55>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 25\u001b[0m \u001b[0mprint\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 26\u001b[0m \u001b[0mattack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfoolbox\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mattacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mLBFGSAttack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfmodel\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mTargetClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m3\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 27\u001b[0;31m \u001b[0madversarial\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mattack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpreprocess_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mpred\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0munpack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 28\u001b[0m \u001b[0mattack1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mfoolbox\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mattacks\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mL2BasicIterativeAttack\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfmodel1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mTargetClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 29\u001b[0m \u001b[0maversarial1\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mattack1\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpreprocess_input\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mim\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mpred1\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0munpack\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;32m/usr/local/lib/python3.4/dist-packages/foolbox/attacks/base.py\u001b[0m in \u001b[0;36mwrapper\u001b[0;34m(self, input_or_adv, label, unpack, **kwargs)\u001b[0m\n\u001b[1;32m 86\u001b[0m \u001b[0;34m' needs to be called with an Adversarial'\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 87\u001b[0m ' instance.')\n\u001b[0;32m---> 88\u001b[0;31m \u001b[0ma\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mAdversarial\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mmodel\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minput_or_adv\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 89\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 90\u001b[0m \u001b[0;32massert\u001b[0m \u001b[0ma\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;32m/usr/local/lib/python3.4/dist-packages/foolbox/adversarial.py\u001b[0m in \u001b[0;36m__init__\u001b[0;34m(self, model, criterion, original_image, original_class, distance, verbose)\u001b[0m\n\u001b[1;32m 59\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 60\u001b[0m \u001b[0;31m# check if the original image is already adversarial\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 61\u001b[0;31m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpredictions\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moriginal_image\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 62\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 63\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0m_reset\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;32m/usr/local/lib/python3.4/dist-packages/foolbox/adversarial.py\u001b[0m in \u001b[0;36mpredictions\u001b[0;34m(self, image, strict, return_details)\u001b[0m\n\u001b[1;32m 235\u001b[0m \"\"\"\n\u001b[1;32m 236\u001b[0m \u001b[0min_bounds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0min_bounds\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimage\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 237\u001b[0;31m \u001b[0;32massert\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mstrict\u001b[0m \u001b[0;32mor\u001b[0m \u001b[0min_bounds\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 238\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 239\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_total_prediction_calls\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0;36m1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", | |
| "\u001b[0;31mAssertionError\u001b[0m: " | |
| ] | |
| } | |
| ], | |
| "source": [ | |
| "sem_dist=[]\n", | |
| "stand_dist=[]\n", | |
| "sem_steps=[]\n", | |
| "stand_steps=[]\n", | |
| "for i in os.listdir(dir_path):\n", | |
| " for j in os.listdir(dir_path+i+'/'):\n", | |
| " im=image.load_img(dir_path+'/'+i+'/'+j)\n", | |
| " im=im.resize((299,299))\n", | |
| " im=image.img_to_array(im)\n", | |
| " #pre_im=preprocess_input(im)\n", | |
| " #print (pre_im.shape)\n", | |
| " im1=np.expand_dims(im,axis=0)\n", | |
| " im1=preprocess_input(im1)\n", | |
| " pred=np.argmax(model.predict(im1))\n", | |
| " pred1=np.argmax(model1.predict(im1))\n", | |
| " \n", | |
| " \n", | |
| " \n", | |
| " \n", | |
| " \n", | |
| " \n", | |
| " \n", | |
| " print (od[pred],od[pred1])\n", | |
| " a=np.random.randint(1,100)\n", | |
| " print (a)\n", | |
| " attack=foolbox.attacks.LBFGSAttack(fmodel,TargetClass(a))\n", | |
| " adversarial=attack(preprocess_input(im),pred,unpack=False)\n", | |
| " attack1=foolbox.attacks.L2BasicIterativeAttack(fmodel1,TargetClass(a))\n", | |
| " aversarial1=attack1(preprocess_input(im),pred1,unpack=False)\n", | |
| " sem_dist.append(adversarial.distance)\n", | |
| " stand_dist.append(adversarial1.distance)\n", | |
| " sem_steps.append(adversarial._total_prediction_calls)\n", | |
| " stand_steps.append(adversarial1._total_prediction_calls)\n", | |
| " print ('semantic ',adversarial.distance,' ',adversarial._total_prediction_calls,'||','standard',adversarial1.distance,' ',adversarial1._total_prediction_calls) \n", | |
| " " | |
| ] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| }, | |
| { | |
| "cell_type": "code", | |
| "execution_count": null, | |
| "metadata": {}, | |
| "outputs": [], | |
| "source": [] | |
| } | |
| ], | |
| "metadata": { | |
| "kernelspec": { | |
| "display_name": "Python 3", | |
| "language": "python", | |
| "name": "python3" | |
| }, | |
| "language_info": { | |
| "codemirror_mode": { | |
| "name": "ipython", | |
| "version": 3 | |
| }, | |
| "file_extension": ".py", | |
| "mimetype": "text/x-python", | |
| "name": "python", | |
| "nbconvert_exporter": "python", | |
| "pygments_lexer": "ipython3", | |
| "version": "3.5.2" | |
| }, | |
| "varInspector": { | |
| "cols": { | |
| "lenName": 16, | |
| "lenType": 16, | |
| "lenVar": 40 | |
| }, | |
| "kernels_config": { | |
| "python": { | |
| "delete_cmd_postfix": "", | |
| "delete_cmd_prefix": "del ", | |
| "library": "var_list.py", | |
| "varRefreshCmd": "print(var_dic_list())" | |
| }, | |
| "r": { | |
| "delete_cmd_postfix": ") ", | |
| "delete_cmd_prefix": "rm(", | |
| "library": "var_list.r", | |
| "varRefreshCmd": "cat(var_dic_list()) " | |
| } | |
| }, | |
| "oldHeight": 123, | |
| "position": { | |
| "height": "40px", | |
| "left": "1680.5px", | |
| "right": "20px", | |
| "top": "120px", | |
| "width": "251px" | |
| }, | |
| "types_to_exclude": [ | |
| "module", | |
| "function", | |
| "builtin_function_or_method", | |
| "instance", | |
| "_Feature" | |
| ], | |
| "varInspector_section_display": "none", | |
| "window_display": true | |
| } | |
| }, | |
| "nbformat": 4, | |
| "nbformat_minor": 2 | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment