微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

Tensorflow 对象检测:零损失

如何解决Tensorflow 对象检测:零损失

来自 deeplearning.ai 使用 TF2 的高级深度学习技术

我正在调试这个函数以进行训练,无论我做什么,损失都保持为零值

 losses_dict = model.loss(prediction_dict,true_shape_tensor)
  print(losses_dict)

打印零。

你在下面的代码中看到任何明显的错误吗?

我怀疑 providegroundtruth 被滥用,但不知道是什么。

@tf.function
def train_step_fn(image_list,groundtruth_Boxes_list,groundtruth_classes_list,model,optimizer,vars_to_fine_tune):
"""A single training iteration.

Args:
  image_list: A list of [1,height,width,3] Tensor of type tf.float32.
    Note that the height and width can vary across images,as they are
    reshaped within this function to be 640x640.
  groundtruth_Boxes_list: A list of Tensors of shape [N_i,4] with type
    tf.float32 representing groundtruth Boxes for each image in the batch.
  groundtruth_classes_list: A list of Tensors of shape [N_i,num_classes]
    with type tf.float32 representing groundtruth Boxes for each image in
    the batch.

Returns:
  A scalar tensor representing the total loss for the input batch.
"""

with tf.GradientTape() as tape:
    
  preprocessed_image_list = []
  true_shape_list = []

  # Preprocess the images
  for img in image_list: 
    processed_img,true_shape = model.preprocess(img)
    preprocessed_image_list.append(processed_img)
    true_shape_list.append(true_shape) 
        
  preprocessed_image_tensor = tf.concat(preprocessed_image_list,axis=0)
  true_shape_tensor = tf.concat(true_shape_list,axis=0)

  print(f"preprocessed_image_tensor shape: {preprocessed_image_tensor.shape}")
  print(f"true_shape_tensor shape: {true_shape_tensor.shape}")

  # Make a prediction
  prediction_dict = model.predict(preprocessed_image_tensor,true_shape_tensor)

  print("keys in prediction_dict:")

  for key in prediction_dict.keys():
    print(key)

  print(groundtruth_Boxes_list)
  # Provide the ground truth to the model
  model.provide_groundtruth(
        groundtruth_Boxes_list=groundtruth_Boxes_list,groundtruth_classes_list=groundtruth_classes_list
        )

  # Calculate the total loss (sum of both losses)
  losses_dict = model.loss(prediction_dict,true_shape_tensor)
  print(losses_dict)

  # Calculate the total loss (sum of both losses)        
  total_loss = losses_dict['Loss/localization_loss'] + losses_dict['Loss/classification_loss']

  # Calculate the gradients
  gradients = tape.gradient(total_loss,vars_to_fine_tune)

  # Optimize the model's selected variables
  optimizer.apply_gradients(zip(gradients,vars_to_fine_tune))

return total_loss   

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。