code: def dice_loss(
      logits,
      labels,
      num_classes,
      smooth=1e-5,
      include_background=True,
      only_present=False,
  ):
      """Calculates a smooth Dice coefficient loss from sparse labels.
  
      Args:
          logits (tf.Tensor): logits prediction for which to calculate
              crossentropy error
          labels (tf.Tensor): sparse labels used for crossentropy error
              calculation
          num_classes (int): number of class labels to evaluate on
          smooth (float): smoothing coefficient for the loss computation
          include_background (bool): flag to include a loss on the background
              label or not
          only_present (bool): flag to include only labels present in the
              inputs or not
  
      Returns:
          tf.Tensor: Tensor scalar representing the loss
      """
  
      # Get a softmax probability of the logits predictions and a one hot
      # encoding of the labels tensor
      probs = tf.nn.softmax(logits)
      onehot_labels = tf.one_hot(
          indices=labels,
          depth=num_classes,
          dtype=tf.float32,
          name="onehot_labels",
      )
  
      # Compute the Dice similarity coefficient
      label_sum = tf.reduce_sum(
          onehot_labels, axis=[1, 2, 3], name="label_sum"
      )
      pred_sum = tf.reduce_sum(probs, axis=[1, 2, 3], name="pred_sum")
      intersection = tf.reduce_sum(
          onehot_labels * probs, axis=[1, 2, 3], name="intersection"
      )
  
      per_sample_per_class_dice = 2.0 * intersection + smooth
      per_sample_per_class_dice /= label_sum + pred_sum + smooth
  
      # Include or exclude the background label for the computation
      if include_background:
          flat_per_sample_per_class_dice = tf.reshape(
              per_sample_per_class_dice, (-1,)
          )
          flat_label = tf.reshape(label_sum, (-1,))
      else:
          flat_per_sample_per_class_dice = tf.reshape(
              per_sample_per_class_dice[:, 1:], (-1,)
          )
          flat_label = tf.reshape(label_sum[:, 1:], (-1,))
  
      # Include or exclude non-present labels for the computation
      if only_present:
          masked_dice = tf.boolean_mask(
              flat_per_sample_per_class_dice,
              tf.logical_not(tf.equal(flat_label, 0)),
          )
      else:
          masked_dice = tf.boolean_mask(
              flat_per_sample_per_class_dice,
              tf.logical_not(tf.is_nan(flat_per_sample_per_class_dice)),
          )
  
      dice = tf.reduce_mean(masked_dice)
      loss = 1.0 - dice
  
      return loss
  
imports: from __future__ import unicode_literals
  from __future__ import print_function
  from __future__ import division
  from __future__ import absolute_import
  import tensorflow
  import numpy
label_desc: Use image node_id 2 for calling a global function with example usage: dice_loss(logits, labels, num_classes, smooth, include_background, only_present) and returns: loss