tensorflow/models · error · ValueError
The masks must have the same dimensions.
Error message
The masks must have the same dimensions.
What it means
Shape guard in calculate_iou: element-wise np.logical_and/or require mask1 and mask2 to have identical shapes. Mismatched shapes (masks from images resized differently, or a full-image mask compared against a cropped detection mask) make intersection/union undefined, so the function refuses before computing. Callers like find_similar_masks hit this when masks come from heterogeneous sources.
Solutions
- Normalize all masks to the same shape (e.g. same image resolution) before comparison
- In find_similar_masks, ensure candidate masks are generated from the same preprocessed image as the reference
- Resize or pad the smaller mask to match the larger one's dimensions when semantically valid
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at official/projects/waste_identification_ml/model_inference/postprocessing.py:295 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/e5a23b5ea5cd3bff.
Report an issue: GitHub.
Appendix: source
Thrown at official/projects/waste_identification_ml/model_inference/postprocessing.py:295
return score, box, combined_label
def calculate_iou(
mask1: np.ndarray, mask2: np.ndarray
) -> Tuple[float, np.ndarray]:
"""Calculates the intersection over union (IoU) score for two masks.
Args:
mask1: The first mask.
mask2: The second mask.
Returns:
The IoU scorea and union of two masks.
"""
# Check if the masks have the same dimensions.
if mask1.shape != mask2.shape:
raise ValueError('The masks must have the same dimensions.')
intersection = np.logical_and(mask1, mask2)
union = np.logical_or(mask1, mask2)
iou_score = np.sum(intersection) / np.sum(union)
return iou_score, union
def find_similar_masks(
results_1: DetectionResult,
results_2: DetectionResult,
num_detections: int,
min_score_thresh: float,
category_indices: List[List[Any]],
category_index_combined: Dict[int, ItemDict],
area_threshold: float,
iou_threshold: float = 0.8,
) -> Dict[str, np.ndarray]:
"""Aligns the masks of the detections in `results_1` and `results_2`.View on GitHub (pinned to e006f5f0d5)