Почему я получаю ошибку градиента None в функции пользовательских потерь keras? - PullRequest
0 голосов
/ 19 июня 2020
I am trying to train a convolutional auto encoder, along with the bounding box for penalizing the inside the boundign box region, for anomaly detection.

Моя настраиваемая функция потерь и вспомогательные функции приведены ниже.

def assymetric_loss(input_bboxes):   
      def custom_loss(input_images,recons_images):    
        losses=[]
        for i in range(20):
          input_image=input_images[i]
          recons_image=recons_images[i]
          bbox=input_bboxes[i]
          #check_null=tf.equal(tf.size(bbox) , 0) 
          x = tf.Variable(-1.0, dtype=tf.float32)
          check_equal=tf.math.equal(bbox[0],x)      
          loss=tf.cond(check_equal ,
                                lambda:  tf.reduce_sum(tf.square(tf.subtract(input_image,recons_image))),
                                lambda:  error1(input_image,recons_image, bbox))
          losses.append(loss)
        loss = tf.stack(losses)
        return loss
      return custom_loss

другие функции, которые я использовал, чтобы помочь мне получить желаемую настраиваемую потерю, приведены ниже. это включает создание маски и инверсную маску, чтобы по-разному наказывать внутреннюю и внешнюю область bbox.

def create_mask_from_bounding_boxes(image, bbox):
  bounding_box_masked_image = image
  x,y,_= image.get_shape().as_list()
  ymin, xmin, ymax, xmax = bbox[0],bbox[1],bbox[2],bbox[3]
  h = xmax - xmin

  z0 = tf.zeros([xmin, 512])
  z1 = tf.concat(
      [tf.zeros([h, ymin]),
       tf.ones([h, ymax - ymin]),
       tf.zeros([h, 512 - ymax])],
      axis=1)
  z2 = tf.zeros([512 - xmax, 512])

  tf_temp=tf.concat([z0, z1, z2], axis=0)
  mask_temp=tf.stack([tf_temp, tf_temp, tf_temp], axis=-1)
  bounding_box_masked_image =  bounding_box_masked_image*mask_temp 
  return bounding_box_masked_image


def create_inverse_mask_from_bounding_boxes(image, bbox):
  bounding_box_masked_image = image
  x,y,c= image.get_shape()
  ymin, xmin, ymax, xmax = bbox[0],bbox[1],bbox[2],bbox[3]
  h = xmax - xmin
  z0 = tf.ones([xmin, 512])
  z1 = tf.concat(
      [tf.ones([h, ymin]),
       tf.zeros([h, ymax - ymin]),
       tf.ones([h, 512 - ymax])],
      axis=1)
  z2 = tf.ones([512 - xmax, 512])

  tf_temp=tf.concat([z0, z1, z2], axis=0)
  mask_temp=tf.stack([tf_temp, tf_temp, tf_temp], axis=-1)
  bounding_box_masked_image =  bounding_box_masked_image*mask_temp 
  return bounding_box_masked_image

def error1(input_image,recons_image, bbox):
  bbarea_input_image=create_mask_from_bounding_boxes(input_image, bbox)
  bbarea_recons_image=create_mask_from_bounding_boxes(recons_image, bbox)
  square = tf.square(tf.subtract(bbarea_input_image, bbarea_recons_image))
  reconstruction_error_bbarea=tf.reduce_sum(square)

  nonbbx_area_input_image    = create_inverse_mask_from_bounding_boxes(input_image, bbox)
  nonbbx_area_recons_image  = create_inverse_mask_from_bounding_boxes(recons_image, bbox)    
  square2 = tf.square(tf.subtract(nonbbx_area_input_image, nonbbx_area_recons_image))
  reconstruction_error_nonbbx=tf.reduce_sum(square2)
  return reconstruction_error_nonbbx+reconstruction_error_bbarea

и, наконец, компиляцию модели с вводом пользовательской функции потерь. error:

raise ValueError ('Операция имеет None для градиента.' ValueError: Операция имеет None для градиента. Убедитесь, что все ваши операции имеют определенный градиент (т. е. дифференцируемые) . Обычные операции без градиента: K.argmax, K.round, K.eval.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...