Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 26 additions & 1 deletion src/deepforest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import numpy as np
import pandas as pd
import pytorch_lightning as pl
import rasterio
import torch
import torchmetrics
from omegaconf import DictConfig, OmegaConf
Expand Down Expand Up @@ -577,6 +578,7 @@ def predict_tile(
iou_threshold=0.15,
dataloader_strategy="single",
crop_model=None,
project=False,
):
"""For images too large to input into the model, predict_tile cuts the
image into overlapping windows, predicts trees on each window and
Expand All @@ -593,9 +595,10 @@ def predict_tile(
- "batch" loads the entire image into GPU memory and creates views of an image as batch, requires in the entire tile to fit into GPU memory. CPU parallelization is possible for loading images.
- "window" loads only the desired window of the image from the raster dataset. Most memory efficient option, but cannot parallelize across windows.
crop_model: a deepforest.model.CropModel object to predict on crops
project (bool): If True, return a geopandas.GeoDataFrame with geometry column projected to the image CRS.

Returns:
pd.DataFrame or tuple: Predictions dataframe or (predictions, crops) tuple
pd.DataFrame, geopandas.GeoDataFrame, or tuple: Predictions dataframe, geopandas.GeoDataFrame, or (predictions, crops) tuple.
Comment on lines -598 to +601
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a little more cleanly: "pd.DataFrame, geopandas.GeoDataFrame, or tuple: Predictions dataframe with or without geometry or (predictions, crops) tuple.

"""
self.model.eval()
self.model.nms_thresh = self.config.nms_thresh
Expand Down Expand Up @@ -746,6 +749,28 @@ def predict_tile(
formatted_results = utilities.__pandas_to_geodataframe__(cropmodel_results)
formatted_results.root_dir = root_dir

if project:
if paths[0] is None:
raise ValueError(
"project=True requires a file path, not an in-memory image array."
)

if root_dir is None:
root_dir = os.path.dirname(paths[0])
Comment on lines +758 to +759
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does the code starting on Line 723 already cover this?


rgb_path = os.path.join(root_dir, os.path.basename(paths[0]))
with rasterio.open(rgb_path) as src:
if src.crs is None:
raise ValueError(
f"project=True requires a georeferenced image, "
f"but '{paths[0]}' has no CRS. Use a georeferenced "
f"raster (e.g., a GeoTIFF) or set project=False."
)

formatted_results = utilities.image_to_geo_coordinates(
formatted_results, root_dir=root_dir
)

return formatted_results

def training_step(self, batch, batch_idx):
Expand Down
Loading