Skip to content

Commit

Permalink
squashed into one geotype commit
Browse files Browse the repository at this point in the history
  • Loading branch information
bw4sz committed Jul 3, 2024
1 parent cce441c commit 2a2528e
Show file tree
Hide file tree
Showing 32 changed files with 1,629 additions and 595 deletions.
11 changes: 11 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"python.testing.unittestEnabled": false,
"python.testing.pytestEnabled": true,
"python.testing.unittestArgs": [
"-v",
"-s",
"./tests",
"-p",
"test_*.py"
]
}
2 changes: 1 addition & 1 deletion deepforest/IoU.py
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,6 @@ def compute_IoU(ground_truth, submission):
}))

iou_df = pd.concat(iou_df)
iou_df = iou_df.merge(ground_truth[["truth_id", "xmin", "xmax", "ymin", "ymax"]])
iou_df = iou_df.merge(ground_truth[["truth_id", "geometry"]])

return iou_df
1 change: 1 addition & 0 deletions deepforest/data/australia.cpg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ISO-8859-1
Binary file added deepforest/data/australia.dbf
Binary file not shown.
1 change: 1 addition & 0 deletions deepforest/data/australia.prj
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
PROJCS["WGS_1984_UTM_Zone_53S",GEOGCS["GCS_WGS_1984",DATUM["D_WGS_1984",SPHEROID["WGS_1984",6378137.0,298.257223563]],PRIMEM["Greenwich",0.0],UNIT["Degree",0.0174532925199433]],PROJECTION["Transverse_Mercator"],PARAMETER["False_Easting",500000.0],PARAMETER["False_Northing",10000000.0],PARAMETER["Central_Meridian",135.0],PARAMETER["Scale_Factor",0.9996],PARAMETER["Latitude_Of_Origin",0.0],UNIT["Meter",1.0]]
Binary file added deepforest/data/australia.shp
Binary file not shown.
Binary file added deepforest/data/australia.shx
Binary file not shown.
Binary file added deepforest/data/australia.tif
Binary file not shown.
44 changes: 17 additions & 27 deletions deepforest/evaluate.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,17 @@
from PIL import Image

from deepforest import IoU
from deepforest.utilities import check_file
from deepforest import visualize
from deepforest.utilities import determine_geometry_type

import warnings


def evaluate_image(predictions, ground_df, root_dir, savedir=None):
def evaluate_image_boxes(predictions, ground_df, root_dir, savedir=None):
"""
Compute intersection-over-union matching among prediction and ground truth boxes for one image
Args:
df: a pandas dataframe with columns name, xmin, xmax, ymin, ymax, label. The 'name' column should be the path relative to the location of the file.
df: a geopandas dataframe with geometry columns
summarize: Whether to group statistics by plot and overall score
image_coordinates: Whether the current boxes are in coordinate system of the image, e.g. origin (0,0) upper left.
root_dir: Where to search for image names in df
Expand All @@ -32,14 +33,6 @@ def evaluate_image(predictions, ground_df, root_dir, savedir=None):
else:
plot_name = plot_names[0]

predictions['geometry'] = predictions.apply(
lambda x: shapely.geometry.box(x.xmin, x.ymin, x.xmax, x.ymax), axis=1)
predictions = gpd.GeoDataFrame(predictions, geometry='geometry')

ground_df['geometry'] = ground_df.apply(
lambda x: shapely.geometry.box(x.xmin, x.ymin, x.xmax, x.ymax), axis=1)
ground_df = gpd.GeoDataFrame(ground_df, geometry='geometry')

# match
result = IoU.compute_IoU(ground_df, predictions)

Expand Down Expand Up @@ -100,21 +93,23 @@ def __evaluate_wrapper__(predictions,
"""Evaluate a set of predictions against a ground truth csv file
Args:
predictions: a pandas dataframe, if supplied a root dir is needed to give the relative path of files in df.name. The labels in ground truth and predictions must match. If one is numeric, the other must be numeric.
csv_file: a csv file with columns xmin, ymin, xmax, ymax, label, image_path
root_dir: location of files in the dataframe 'name' column.
iou_threshold: intersection-over-union threshold, see deepforest.evaluate
savedir: optional directory to save image with overlaid predictions and annotations
Returns:
results: a dictionary of results with keys, results, box_recall, box_precision, class_recall
"""
# remove empty samples from ground truth
ground_df = ground_df[~((ground_df.xmin == 0) & (ground_df.xmax == 0))]

results = evaluate(predictions=predictions,
ground_df=ground_df,
root_dir=root_dir,
iou_threshold=iou_threshold,
savedir=savedir)
prediction_geometry = determine_geometry_type(predictions)
if prediction_geometry == "point":
raise NotImplementedError("Point evaluation is not yet implemented")
elif prediction_geometry == "box":
results = evaluate_boxes(predictions=predictions,
ground_df=ground_df,
root_dir=root_dir,
iou_threshold=iou_threshold,
savedir=savedir)
else:
raise NotImplementedError("Geometry type {} not implemented".format(prediction_geometry))

# replace classes if not NUll
if not results is None:
Expand All @@ -130,7 +125,7 @@ def __evaluate_wrapper__(predictions,
return results


def evaluate(predictions, ground_df, root_dir, iou_threshold=0.4, savedir=None):
def evaluate_boxes(predictions, ground_df, root_dir, iou_threshold=0.4, savedir=None):
"""Image annotated crown evaluation routine
submission can be submitted as a .shp, existing pandas dataframe or .csv path
Expand All @@ -145,10 +140,6 @@ def evaluate(predictions, ground_df, root_dir, iou_threshold=0.4, savedir=None):
box_precision: proportion of predictions that are true positive, regardless of class
class_recall: a pandas dataframe of class level recall and precision with class sizes
"""

check_file(ground_df)
check_file(predictions)

# Run evaluation on all plots
results = []
box_recalls = []
Expand All @@ -175,7 +166,7 @@ def evaluate(predictions, ground_df, root_dir, iou_threshold=0.4, savedir=None):
continue
else:
group = group.reset_index(drop=True)
result = evaluate_image(predictions=image_predictions,
result = evaluate_image_boxes(predictions=image_predictions,
ground_df=group,
root_dir=root_dir,
savedir=savedir)
Expand Down Expand Up @@ -266,7 +257,6 @@ def point_recall(predictions, ground_df, root_dir=None, savedir=None):
box_recall: proportion of true positives between predicted boxes and ground truth points, regardless of class
class_recall: a pandas dataframe of class level recall and precision with class sizes
"""
check_file(predictions)
if savedir:
if root_dir is None:
raise AttributeError("savedir is {}, but root dir is None".format(savedir))
Expand Down
55 changes: 35 additions & 20 deletions deepforest/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -399,7 +399,7 @@ def predict_file(self, csv_file, root_dir, savedir=None, color=None, thickness=1
Returns:
df: pandas dataframe with bounding boxes, label and scores for each image in the csv file
"""
df = pd.read_csv(csv_file)
df = utilities.read_file(csv_file)
ds = dataset.TreeDataset(csv_file=csv_file,
root_dir=root_dir,
transforms=None,
Expand All @@ -415,6 +415,8 @@ def predict_file(self, csv_file, root_dir, savedir=None, color=None, thickness=1
savedir=savedir,
color=color,
thickness=thickness)



return results

Expand Down Expand Up @@ -613,28 +615,41 @@ def on_validation_epoch_end(self):

# Evaluate on validation data predictions
self.predictions_df = pd.concat(self.predictions)
ground_df = pd.read_csv(self.config["validation"]["csv_file"])

#Create a geospatial column
self.predictions_df = utilities.read_file(self.predictions_df)

ground_df = utilities.read_file(self.config["validation"]["csv_file"])
ground_df["label"] = ground_df.label.apply(lambda x: self.label_dict[x])

#Evaluate every n epochs
if self.current_epoch % self.config["validation"]["val_accuracy_interval"] == 0:
results = evaluate_iou.__evaluate_wrapper__(
predictions=self.predictions_df,
ground_df=ground_df,
root_dir=self.config["validation"]["root_dir"],
iou_threshold=self.config["validation"]["iou_threshold"],
savedir=None,
numeric_to_label_dict=self.numeric_to_label_dict)

self.log("box_recall", results["box_recall"])
self.log("box_precision", results["box_precision"])
if isinstance(results, pd.DataFrame):
for index, row in results["class_recall"].iterrows():
self.log("{}_Recall".format(self.numeric_to_label_dict[row["label"]]),
row["recall"])
self.log(
"{}_Precision".format(self.numeric_to_label_dict[row["label"]]),
row["precision"])
if self.predictions_df.empty:
warnings.warn("No predictions made, skipping evaluation")
geom_type = utilities.determine_geometry_type(ground_df)
if geom_type == "box":
result = {"box_recall": 0, "box_precision": 0, "class_recall": pd.DataFrame()}
else:
results = evaluate_iou.__evaluate_wrapper__(
predictions=self.predictions_df,
ground_df=ground_df,
root_dir=self.config["validation"]["root_dir"],
iou_threshold=self.config["validation"]["iou_threshold"],
savedir=None,
numeric_to_label_dict=self.numeric_to_label_dict)

# Log each key value pair of the results dict
for key, value in results.items():
if isinstance(results, pd.DataFrame):
# Log each row in the dataframe
for index, row in value.iterrows():
self.log("{}_Recall".format(self.numeric_to_label_dict[row["label"]]),
row["recall"])
self.log(
"{}_Precision".format(self.numeric_to_label_dict[row["label"]]),
row["precision"])
else:
self.log(key, value)

def predict_step(self, batch, batch_idx):
batch_results = self.model(batch)
Expand Down Expand Up @@ -706,7 +721,7 @@ def evaluate(self, csv_file, root_dir, iou_threshold=None, savedir=None):
Returns:
results: dict of ("results", "precision", "recall") for a given threshold
"""
ground_df = pd.read_csv(csv_file)
ground_df = utilities.read_file(csv_file)
ground_df["label"] = ground_df.label.apply(lambda x: self.label_dict[x])
predictions = self.predict_file(csv_file=csv_file,
root_dir=root_dir,
Expand Down
6 changes: 2 additions & 4 deletions deepforest/predict.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
import pandas as pd
import numpy as np
import os
from PIL import Image
import warnings

import torch
from torchvision.ops import nms
import typing

from deepforest import visualize, dataset
import rasterio

from deepforest.utilities import read_file

def _predict_image_(model,
image: typing.Optional[np.ndarray] = None,
Expand Down Expand Up @@ -180,6 +177,7 @@ def _dataloader_wrapper_(model,
results.append(prediction)

results = pd.concat(results, ignore_index=True)
results = read_file(results, root_dir)

if savedir:
visualize.plot_prediction_dataframe(results,
Expand Down
Loading

0 comments on commit 2a2528e

Please sign in to comment.