alseambusher

facenet3

Jan 11th, 2018
14,524
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.48 KB | None | 0 0
  1. import matplotlib.pyplot as plt
  2. import copy
  3.  
  4. # get session from keras to use it with tf
  5. sess = K.get_session()
  6.  
  7. # keep track of l2 distance between the generated image and target image.
  8. dist = []
  9.  
  10. # this will hold the generated image
  11. new_img = copy.deepcopy(attacker)
  12.  
  13. noise = None
  14. for i in range(140):
  15.     # we pass target and attacker images as mentioned earlier
  16.     gradout = sess.run(grad[0], feed_dict={"input_1:0":np.array([new_img, target, attacker]), K.learning_phase(): 0})
  17.  
  18.     # if the max gradient is too small, we set a small value
  19.     grad_absmax = max(np.abs(gradout[0]).max(), 1e-10)
  20.  
  21.     # by doing this way, we get a faster convergence while gradients don't vanish soon.
  22.     step_size = 0.0003 / grad_absmax
  23.    
  24.     # update noise
  25.     if noise is None:
  26.         noise = step_size * gradout[0]
  27.     else:
  28.         noise -= step_size * gradout[0]
  29.    
  30.     # also note that, one of the goals is to not visibly alter the image. So we limit the changes to noise.
  31.     noise = np.clip(a=noise, a_min=-0.012,
  32.                            a_max=0.012)
  33.  
  34.     # apply noise to the attacker image
  35.     new_img = copy.deepcopy(attacker) + noise
  36.     # if the values go beyond 1 or fall below 0, the image will be invalid. So clip it to keep it in range.
  37.     new_img = np.clip(a=new_img, a_min=0.0, a_max=1.0)
  38.    
  39.     # l2 distance between embeddings of target image and the generated image
  40.     dist.append(np.linalg.norm(database["kian"] - FRmodel.predict_on_batch(np.array([new_img]))))
  41.  
  42. plt.plot(dist)
Add Comment
Please, Sign In to add comment