-
Notifications
You must be signed in to change notification settings - Fork 258
added project argument to predict_tile for geo-coordinates
#1250
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
musaqlain
wants to merge
1
commit into
weecology:main
Choose a base branch
from
musaqlain:feature-predict-tile-projection
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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 | ||
|
|
@@ -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. | ||
| """ | ||
| self.model.eval() | ||
| self.model.nms_thresh = self.config.nms_thresh | ||
|
|
@@ -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
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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): | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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.