{"record":{"id":"14154dfd272ceba6","repo":"tensorflow/models","slug":"length-of-class-weights-should-be-14154d","errorCode":null,"errorMessage":"Length of class_weights should be {}","messagePattern":"Length of class_weights should be (.+?)","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"official/vision/losses/segmentation_losses.py","lineNumber":96,"sourceCode":"    if not self._use_binary_cross_entropy:\n      if num_layers > 1:\n        raise ValueError(\n            'Groundtruth mask must have only 1 layer if using categorical'\n            'cross entropy, but got {} layers.'.format(num_layers))\n    if self._gt_is_matting_map:\n      if num_classes != 2:\n        raise ValueError(\n            'Groundtruth matting map only supports 2 classes, but got {} '\n            'classes.'.format(num_classes))\n      if num_layers > 1:\n        raise ValueError(\n            'Groundtruth matting map must have only 1 layer, but got {} '\n            'layers.'.format(num_layers))\n\n    class_weights = (\n        self._class_weights if self._class_weights else [1] * num_classes)\n    if num_classes != len(class_weights):\n      raise ValueError(\n          'Length of class_weights should be {}'.format(num_classes))\n    class_weights = tf.constant(class_weights, dtype=output_dtype)\n\n    if not self._gt_is_matting_map:\n      labels = tf.cast(labels, tf.int32)\n    if self._use_groundtruth_dimension:\n      # TODO(arashwan): Test using align corners to match deeplab alignment.\n      logits = tf.image.resize(\n          logits, tf.shape(labels)[1:3], method=tf.image.ResizeMethod.BILINEAR)\n    else:\n      labels = tf.image.resize(\n          labels, (height, width),\n          method=tf.image.ResizeMethod.NEAREST_NEIGHBOR)\n\n    valid_mask = tf.not_equal(tf.cast(labels, tf.int32), self._ignore_label)\n\n    # (batch_size, height, width, num_classes)\n    labels_with_prob = self.get_labels_with_prob(logits, labels, valid_mask,","sourceCodeStart":78,"sourceCodeEnd":114,"githubUrl":"https://github.com/tensorflow/models/blob/e006f5f0d534913e49c1f1dae87364039fa607e2/official/vision/losses/segmentation_losses.py#L78-L114","documentation":"Error \"Length of class_weights should be {}\" thrown in tensorflow/models.","triggerScenarios":"Thrown at official/vision/losses/segmentation_losses.py:96 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Pass class_weights with the same length as the number of segmentation classes indicated in the error.","Remove class_weights (use uniform weighting) if per-class weighting is not needed."],"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"}