class_docstr: Test various loss functions in model_utils.
class_docstr_tok: ["Test","various","loss","functions","in","model_utils","."]
docstr: Tests the correctness of the segmentation dice loss.
docstr_tok: ["Tests","the","correctness","of","the","segmentation","dice","loss","."]
code: def test_dice_loss(self):
      """Tests the correctness of the segmentation dice loss."""
      # Create test targets:
      batch, num_objects, h, w = 1, 2, 128, 128
      stride = 2
      targets = np.zeros((batch, num_objects, h, w), dtype=np.float32)
      targets[0, 0, :64, :64] = 1.0  # Add object in top left of image.
      targets[
          0, 1, 64:, 64:
      ] = 1.0  # Add object in bottom right of image.
      input_shape = batch, num_objects, h // stride, w // stride
  
      # Test perfect predictions:
      inputs = np.zeros(input_shape, dtype=np.float32)
      inputs[0, 0, : 64 // stride, : 64 // stride] = 1.0
      inputs[0, 1, 64 // stride :, 64 // stride :] = 1.0
      inputs = (
          inputs - 0.5
      ) * 1e6  # Inputs will be passed through sigmoid.
      loss = model_utils.dice_loss(
          jnp.array(inputs), jnp.array(targets), interpolation="nearest"
      )
      np.testing.assert_array_almost_equal(
          loss, [[0.0, 0.0]], decimal=3
      )
  
      # Test one half-overlapping prediction:
      inputs = np.zeros(input_shape, dtype=np.float32)
      inputs[
          0, 0, 32 // stride : (32 + 64) // stride, : 64 // stride
      ] = 1.0
      inputs[0, 1, 64 // stride :, 64 // stride :] = 1.0
      inputs = (
          inputs - 0.5
      ) * 1e6  # Inputs will be passed through sigmoid.
      loss = model_utils.dice_loss(
          jnp.array(inputs), jnp.array(targets), interpolation="nearest"
      )
      np.testing.assert_array_almost_equal(
          loss, [[0.5, 0.0]], decimal=3
      )
  
      # Test one non-overlapping prediction:
      inputs = np.zeros(input_shape, dtype=np.float32)
      inputs[0, 0, 64 // stride :, 64 // stride :] = 1.0
      inputs[0, 1, 64 // stride :, 64 // stride :] = 1.0
      inputs = (
          inputs - 0.5
      ) * 1e6  # Inputs will be passed through sigmoid.
      loss = model_utils.dice_loss(
          jnp.array(inputs), jnp.array(targets), interpolation="nearest"
      )
      np.testing.assert_array_almost_equal(
          loss, [[1.0, 0.0]], decimal=3
      )
  
      # Test all-pairs with different instance numbers:
      inputs = np.zeros(
          (batch, 3, h // stride, w // stride), dtype=np.float32
      )
      inputs[0, 0, : 64 // stride, : 64 // stride] = 1.0
      inputs[
          0, 1, 32 // stride : (32 + 64) // stride, : 64 // stride
      ] = 1.0
      inputs[0, 2, 64 // stride :, 64 // stride :] = 1.0
      inputs = (
          inputs - 0.5
      ) * 1e6  # Inputs will be passed through sigmoid.
      loss = model_utils.dice_loss(
          jnp.array(inputs),
          jnp.array(targets),
          interpolation="nearest",
          all_pairs=True,
      )
      self.assertTupleEqual(
          loss.shape, (1, 3, 2)
      )  # [b, n_pred, n_true]
      np.testing.assert_array_almost_equal(
          loss, [[[0.0, 1.0], [0.5, 1.0], [1.0, 0.0]]], decimal=3
      )
  
imports: import itertools
  from absl.testing import absltest
  from absl.testing import parameterized
  from flax.training import common_utils
  import jax
  import jax.numpy
  import numpy
  from scenic.model_lib.base_models import model_utils
label_desc: Use image node_id 5 for calling the LossTest obj's underlying member method code with example usage: obj.test_dice_loss() without return types