0

I am having a problem calculating the gradient in TensorFlow 1.15. I think it's something related context manager or keras session, but I am not sure about it. Following is the code I have written:

def create_adversarial_pattern_CW(input_patch, input_label, target_label):
 input_patch_T = tf.cast(input_patch,tf.float32)
 with tf.GradientTape() as tape:
  tape.watch(input_patch_T)
  patch_pred = model(input_patch_T)
  loss_input_label = soft_dice_loss(input_label, patch_pred[0])
  loss_target_label = soft_dice_loss(target_label, patch_pred[0])
  f = loss_input_label - loss_target_label
 f_grad = tape.gradient(f, input_patch_T)

 #-------------------------#
 print(type(f_grad)) 
 #-------------------------#

 f_grad_sign = tf.sign(f_grad)
 return f_grad_sign


def DAG():
 sess = K.get_session()
 with sess.as_default() as sess:
  adv_x_old = tf.cast(X,dtype=tf.float32)
  for i in range(iters):
   #-------------------------#
   #y_pred = model(adv_x_old) -> If I uncomment this line the value of f_grad returned is None, otherwise it works fine, but I need this line
   #-------------------------#
   perturbations = create_adversarial_pattern_CW(adv_x_old, y, y_target)
   adv_x_new = adv_x_old - alpha*perturbations
   adv_x_old = adv_x_new
  adv_patch_pred = model(adv_x_old)

To fix it, I tried to wrap the commented line as :

with tf.GradientTape() as tape:
  with tape.stop_recording():
    y_pred = model(adv_x_old)

but I still get the value of f_grad as None.

  • 1. What is the shape of `patch_pred`? 2. What are `input_label` and `target_label`? – Susmit Agrawal Apr 27 '20 at 12:12
  • patch_pred is a patch of 3-D image, input_label is the label of each individual pixel and target_label is the required labels of every pixel. This code is for carrying out an adversarial attack on image segmentation model. But my main query was how y_pred = model(adv_x_old) which is not even in the same function causing so much trouble? – Tarun Gupta Apr 27 '20 at 12:17
  • I think the problem is something related to calculating gradients with GradientTape or something similar. – Tarun Gupta Apr 27 '20 at 12:19
  • The shape of patch_pred is (4,160,160,16), 4 channels for 3d image. – Tarun Gupta Apr 27 '20 at 12:28
  • What do you mean by "input of each individual pixel"? If it is an input, why are you using it for computing the loss? – Susmit Agrawal Apr 27 '20 at 13:00
  • Here input_label refers to the labels of adversarial image of a particular iteration, and that is needed for the Carlini and Wagner (CW) attack. I have deliberately removed the code which was not relevant for my query. My main question is why that commented line creates a problem when I am trying to calculate the gradient. The rest of code is moslty irrelevant. I have simplified and removed codes unnecessary for my question, so the code is not complete and will not make full sense. But my question remains about that commented line. – Tarun Gupta Apr 27 '20 at 13:14

0 Answers0