Coder Social home page Coder Social logo

Comments (5)

wielandbrendel avatar wielandbrendel commented on May 24, 2024

Your evaluation is wrong: you have to apply the same image preprocessing that you passed to the Foolbox model. In your example above you are not applying any preprocessing when you evaluate.

from foolbox.

shenqixiaojiang avatar shenqixiaojiang commented on May 24, 2024

@wielandbrendel Now, I use the following code to get the attack examples:

adv = np.zeros((len(imList),width,width,3))  #for saving the adv images
src = np.zeros((len(imList),width,width,3))   #for saving the original images
srcLabel = np.zeros(len(imList))    #for saving the label 
for j in range(len(imList)):
      image = Image.open(valpath + imList[j])
      image = image.resize((width,width))
      image = np.asarray(image, dtype="float32")
      label = mm[imList[j]]  #get the label
      srcLabel[j] = label
      src[j] = image
      ans = attack(image[:,:,::-1], label)
      if ans == None:
          adv[j] = image
      else:
          adv[j] = ans[:,:,::-1]

np.save("srcFoolbox",src)
np.save("advFoolbox",adv)
np.save("srcLabel",srcLabel)

and use the following code to test the accuracy:

keras.backend.set_learning_phase(0)
kmodel = ResNet50(weights='imagenet')

srcLabel = np.load('srcLabel.npy')
srcdata = np.load('srcFoolbox.npy')
advdata = np.load('advFoolbox.npy')

for i in range(2):
    right = 0
    for j in range(len(srcdata)):
        if i == 0:
            image = srcdata[j]
        else:
            image = advdata[j]
        label = srcLabel[j]
        x = np.expand_dims(image, axis=0)
        x = preprocess_input(x)
        preds = kmodel.predict(x)  #the code of predicting is got from the example code of [Keras](https://keras.io/applications/#mobilenet)
        print('pre-label,',preds.argmax(),label)
        if preds.argmax() == label:
            right += 1
    print right * 1.0 / len(srcdata)

And we get the accuracy of original images is 0.675 and the adv images is 0.575.

from foolbox.

wielandbrendel avatar wielandbrendel commented on May 24, 2024

Could you count how many images DeepFool thinks it found an adversarial for? I.e. please add something like

      if ans == None:
          failures += 1
          adv[j] = image
      else:
          successes += 1
          adv[j] = ans[:,:,::-1]

and report the result (failures, successes).

from foolbox.

shenqixiaojiang avatar shenqixiaojiang commented on May 24, 2024

@wielandbrendel Here, 40 test images of ImageNet are used to get the attack examples, and the result of (failures, successes) is (5,35).
The images of Cifar10 and Mnist were tested and it was normal.

from foolbox.

wielandbrendel avatar wielandbrendel commented on May 24, 2024

Please check that your preprocessing is the same, i.e. that preprocess_input(x) yields the same as fmodel._process_input(x).

from foolbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.