Skip to content

Instantly share code, notes, and snippets.

@miki998
Created April 23, 2020 12:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save miki998/74cc8ede47a047550b4886543f27b488 to your computer and use it in GitHub Desktop.
Save miki998/74cc8ede47a047550b4886543f27b488 to your computer and use it in GitHub Desktop.
#We load the image we want to create adversarial
nb_image = 0
img = X_test[nb_image]
#Get the correct label
label = np.zeros(len(AGE_CLASS)) ; label[int(y_test[nb_image])] = 1.
img = img.reshape(1,img.shape[0],img.shape[1],img.shape[2])
img = img.astype(np.float32)
#Convert it into tensor
tens = tf.convert_to_tensor(img)
#Do a feedforward and compute the loss compared to the ground truth
prediction = model(tens)
loss = loss_object(label,prediction)
#Computing the loss gradient as a function of the input
gradient = tf.gradients(loss,tens)
#Getting the sign of the gradient to know where each pixels should move toward to (or not move at all)
signed_grad = tf.sign(gradient)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment