DeepForest icon indicating copy to clipboard operation
DeepForest copied to clipboard

Add format_prediction function to handle empty predictions gracefully

Open Copilot opened this issue 4 months ago • 3 comments

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:

  1. Automatically detects geometry type using determine_geometry_type
  2. Handles empty predictions gracefully by returning empty DataFrames with correct structure and dtypes
  3. Maintains consistency between empty and non-empty results
  4. 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 of None for empty predictions
  • Updated format_geometry: Removed None handling since format_boxes no longer returns None
  • 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_geometry function 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.

Copilot avatar Aug 18 '25 14:08 Copilot

@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.

Copilot avatar Aug 18 '25 14:08 Copilot

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.

image

bw4sz avatar Aug 22 '25 19:08 bw4sz

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.

ethanwhite avatar Sep 04 '25 16:09 ethanwhite