code: def smooth_l1_loss(
      prediction, ground_truth, weight_map=None, value_thresh=0.5
  ):
      """
      Similarly to the Huber loss, the residuals are squared below a threshold
      value. In addition they are square above the inverse of this threshold
      :param prediction: the current prediction of the ground truth.
      :param ground_truth: the measurement you are approximating with regression.
      :param weight_map:
      :return: mean of the l1 loss across all voxels.
      """
      # Definition of thresholds
      if value_thresh > 1:
          value_thresh_max = value_thresh
          value_thresh = 1.0 / value_thresh
      else:
          value_thresh_max = 1.0 / value_thresh
  
      value_correction = value_thresh**3 - value_thresh
  
      value_correction_max = value_thresh_max - value_thresh_max**2
  
      prediction = tf.cast(prediction, dtype=tf.float32)
  
      ground_truth = tf.cast(ground_truth, dtype=tf.float32)
  
      absolute_residuals = tf.cast(
          tf.abs(tf.subtract(prediction, ground_truth)),
          dtype=tf.float32,
      )
  
      absolute_residuals = tf.where(
          absolute_residuals < value_thresh,
          value_thresh * tf.square(absolute_residuals),
          absolute_residuals + value_correction,
      )
  
      absolute_residuals = tf.where(
          tf.greater(absolute_residuals, value_thresh_max),
          tf.square(absolute_residuals) + value_correction_max,
          absolute_residuals,
      )
      if weight_map is not None:
          absolute_residuals = tf.multiply(
              absolute_residuals, weight_map
          )
          sum_residuals = tf.reduce_sum(absolute_residuals)
  
          sum_weights = tf.reduce_sum(weight_map)
  
      else:
          sum_residuals = tf.reduce_sum(absolute_residuals)
          sum_weights = tf.size(absolute_residuals)
      return tf.truediv(
          tf.cast(sum_residuals, dtype=tf.float32),
          tf.cast(sum_weights, dtype=tf.float32),
      )
  
file_path: NiftyNet/niftynet/layer/loss_regression.py
imports: from __future__ import absolute_import, print_function, division
  import tensorflow
  from niftynet.engine.application_factory import LossRegressionFactory
  from niftynet.layer.base_layer import Layer
label_desc: Use image node_id 6 for calling a global function with example usage: smooth_l1_loss(prediction, ground_truth, weight_map, value_thresh) and returns: tf