Add format_prediction function to handle empty predictions gracefully
This PR adds a new format_prediction function to utilities.py that provides a clean, single-function interface for formatting model predictions while handling empty predictions gracefully.
Problem
Previously, when processing predictions that contained no detections, developers had to write manual boilerplate code to handle the empty case:
for pred in predictions:
if len(pred["boxes"]) == 0:
# Manual handling required
y_pred = {}
y_pred["y"] = torch.zeros(4)
y_pred["labels"] = torch.zeros(1)
y_pred["scores"] = torch.zeros(1)
else:
geom_type = utilities.determine_geometry_type(pred)
result = utilities.format_geometry(pred, geom_type=geom_type)
This approach was error-prone, inconsistent, and required developers to understand the internal structure of predictions.
Solution
The new format_prediction function provides a unified interface that:
-
Automatically detects geometry type using
determine_geometry_type - Handles empty predictions gracefully by returning empty DataFrames with correct structure and dtypes
- Maintains consistency between empty and non-empty results
- Simplifies the API to a single function call
# New simplified approach
for pred in predictions:
result = utilities.format_prediction(pred)
# Empty predictions handled automatically!
Key Changes
-
Modified
format_boxes: Now returns empty DataFrame with correct structure instead ofNonefor empty predictions -
Updated
format_geometry: RemovedNonehandling sinceformat_boxesno longer returnsNone -
Added
format_prediction: New function that combines geometry type detection and formatting in one call - Updated tests: Modified existing tests and added comprehensive test coverage for the new function
-
Maintained backwards compatibility: Existing
format_geometryfunction still works as before
Benefits
- Eliminates boilerplate code for empty prediction handling
- Provides consistent DataFrame structure for both empty and non-empty predictions
- Reduces complexity in prediction processing pipelines
- Improves code maintainability by centralizing prediction formatting logic
- Handles edge cases automatically without manual intervention
The function supports box geometry predictions and provides appropriate error handling for unsupported geometry types (points and polygons).
Fixes #1101.
✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.
@bw4sz 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.
I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.
This one is a mystery right now, passes locally. Let's let it run and I'll need someone else to try and confirm if it still fails here.
This one is a mystery right now, passes locally. Let's let it run and I'll need someone else to try and confirm if it still fails here.
I can confirm that both of these tests are passing locally on Ubuntu 22.04, Python 3.10.12, torch 2.7.1+cu126, and torchvision 0.22.1+cu126. UPDATE: same behavior when pinning the torch and torchvision versions to those use on GH Actions (2.8.0 and 0.23.0).
The first error is interesting in that it shows that there are a bunch of predictions, that some of them are reasonably close to the far edge (can't tell if the fail is on the x or y dimension) but that it's not far enough toward the edge to meet our threshold. So predict_tile() ran, produced predictions, but missed some trees? Weird. We should probably try to store the resulting predictions as part of an action run to see what it shows.