Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import matplotlib.pyplot as plt
- import copy
- # get session from keras to use it with tf
- sess = K.get_session()
- # keep track of l2 distance between the generated image and target image.
- dist = []
- # this will hold the generated image
- new_img = copy.deepcopy(attacker)
- noise = None
- for i in range(140):
- # we pass target and attacker images as mentioned earlier
- gradout = sess.run(grad[0], feed_dict={"input_1:0":np.array([new_img, target, attacker]), K.learning_phase(): 0})
- # if the max gradient is too small, we set a small value
- grad_absmax = max(np.abs(gradout[0]).max(), 1e-10)
- # by doing this way, we get a faster convergence while gradients don't vanish soon.
- step_size = 0.0003 / grad_absmax
- # update noise
- if noise is None:
- noise = step_size * gradout[0]
- else:
- noise -= step_size * gradout[0]
- # also note that, one of the goals is to not visibly alter the image. So we limit the changes to noise.
- noise = np.clip(a=noise, a_min=-0.012,
- a_max=0.012)
- # apply noise to the attacker image
- new_img = copy.deepcopy(attacker) + noise
- # if the values go beyond 1 or fall below 0, the image will be invalid. So clip it to keep it in range.
- new_img = np.clip(a=new_img, a_min=0.0, a_max=1.0)
- # l2 distance between embeddings of target image and the generated image
- dist.append(np.linalg.norm(database["kian"] - FRmodel.predict_on_batch(np.array([new_img]))))
- plt.plot(dist)
Add Comment
Please, Sign In to add comment