tensorflow/models · error · ValueError
Length of class_weights should be {}
Error message
Length of class_weights should be {} What it means
Error "Length of class_weights should be {}" thrown in tensorflow/models.
Source
Thrown at official/vision/losses/maskrcnn_losses.py:197
Args:
class_outputs: a float tensor representing the class prediction for each
box with a shape of [batch_size, num_boxes, num_classes].
class_targets: a float tensor representing the class label for each box
with a shape of [batch_size, num_boxes].
class_weights: A float list containing the weight of each class.
Returns:
a scalar tensor representing total class loss.
"""
with tf.name_scope('fast_rcnn_loss'):
output_dtype = class_outputs.dtype
num_classes = class_outputs.get_shape().as_list()[-1]
class_weights = (
class_weights if class_weights is not None else [1.0] * num_classes
)
if num_classes != len(class_weights):
raise ValueError(
'Length of class_weights should be {}'.format(num_classes)
)
class_weights = tf.constant(class_weights, dtype=output_dtype)
class_targets_one_hot = tf.one_hot(
tf.cast(class_targets, dtype=tf.int32),
num_classes,
dtype=class_outputs.dtype)
if self._use_binary_cross_entropy:
# (batch_size, num_boxes, num_classes)
cross_entropy_loss = tf.nn.sigmoid_cross_entropy_with_logits(
labels=class_targets_one_hot, logits=class_outputs)
cross_entropy_loss *= class_weights
else:
# (batch_size, num_boxes)
cross_entropy_loss = tf.nn.softmax_cross_entropy_with_logits(
labels=class_targets_one_hot, logits=class_outputs)View on GitHub (pinned to e006f5f0d5)
When it happens
Trigger: Thrown at official/vision/losses/maskrcnn_losses.py:197 when the library encounters an invalid state.
Common situations: See trigger scenarios.
AI-assisted analysis of tensorflow/models@e006f5f0d5 (2026-08-24).
Data as JSON: /api/errors/e9b738643634eb15.
Report an issue: GitHub.