{"record":{"id":"a455101cf877f916","repo":"tensorflow/models","slug":"the-number-of-classes-from-groundtruth-labels-and","errorCode":null,"errorMessage":"The number of classes from groundtruth labels and `num_classes` should equal, but they are {0} and {1}.","messagePattern":"The number of classes from groundtruth labels and `num_classes` should equal, but they are (.+?) and (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/projects/volumetric_models/evaluation/segmentation_metrics.py","lineNumber":85,"sourceCode":"\n    self.name = name\n    self.dtype = dtype\n\n  def update_state(self, y_true: tf.Tensor, y_pred: tf.Tensor):\n    \"\"\"Updates metric state.\n\n    Args:\n      y_true: The true labels of size [batch, width, height, volume,\n        num_classes].\n      y_pred: The prediction of size [batch, width, height, volume,\n        num_classes].\n\n    Raises:\n      ValueError: If number of classes from groundtruth label does not equal to\n        `num_classes`.\n    \"\"\"\n    if self._num_classes != y_true.get_shape()[-1]:\n      raise ValueError(\n          'The number of classes from groundtruth labels and `num_classes` '\n          'should equal, but they are {0} and {1}.'.format(\n              self._num_classes,\n              y_true.get_shape()[-1]))\n\n    # If both y_pred and y_true are all 0s, we skip computing the metrics;\n    # otherwise the averaged metrics will be erroneously lower.\n    if tf.reduce_sum(y_true) != 0 or tf.reduce_sum(y_pred) != 0:\n      self._count.assign_add(1.)\n      self._dice_scores_overall.assign_add(\n          1 - self._dice_op_overall(y_pred, y_true))\n      if self._per_class_metric:\n        for class_id in range(self._num_classes):\n          if tf.reduce_sum(y_true[..., class_id]) != 0 or tf.reduce_sum(\n              y_pred[..., class_id]) != 0:\n            self._count_per_class[class_id].assign_add(1.)\n            self._dice_scores_per_class[class_id].assign_add(\n                1 - self._dice_op_per_class(y_pred[...,","sourceCodeStart":67,"sourceCodeEnd":103,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/projects/volumetric_models/evaluation/segmentation_metrics.py#L67-L103","documentation":"Error \"The number of classes from groundtruth labels and `num_classes` should equal, but they are {0} and {1}.\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/projects/volumetric_models/evaluation/segmentation_metrics.py:85 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":[],"exampleFix":null,"handlingStrategy":null,"validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"e006f5f0d534913e49c1f1dae87364039fa607e2","analyzedAt":"2026-08-24T14:09:15.576Z","schemaVersion":2},"datasetVersion":"2026-08-24T17:17:21.512Z"}