Skip to content
434 changes: 37 additions & 397 deletions app.py

Large diffs are not rendered by default.

16 changes: 12 additions & 4 deletions perceptionmetrics/models/torch_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,10 @@
import torch
from torch.utils.data import DataLoader, Dataset
from torchvision.transforms import v2 as transforms
from torchvision import tv_tensors
try:
from torchvision import tv_tensors
except ImportError:
from torchvision import datapoints as tv_tensors
from tqdm.auto import tqdm

from perceptionmetrics.datasets import detection as detection_dataset
Expand Down Expand Up @@ -188,9 +191,14 @@ def __getitem__(
# Convert boxes/labels to tensors
if len(boxes) == 0:
boxes = torch.zeros((0, 4), dtype=torch.float32)
boxes = tv_tensors.BoundingBoxes(
boxes, format="XYXY", canvas_size=(image.height, image.width)
)
if hasattr(tv_tensors, "BoundingBoxes"):
boxes = tv_tensors.BoundingBoxes(
boxes, format="XYXY", canvas_size=(image.height, image.width)
)
else:
boxes = tv_tensors.BoundingBox(
boxes, format="XYXY", spatial_size=(image.height, image.width)
)
category_indices = torch.as_tensor(category_indices, dtype=torch.int64)

target = {
Expand Down
31 changes: 30 additions & 1 deletion perceptionmetrics/models/torch_segmentation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import os
import time
import tempfile
from typing import Any, List, Optional, Tuple, Union
from typing import Any, Callable, List, Optional, Tuple, Union

import numpy as np
import pandas as pd
Expand Down Expand Up @@ -374,6 +374,8 @@ def eval(
translation_direction: str = "dataset_to_model",
predictions_outdir: Optional[str] = None,
results_per_sample: bool = False,
progress_callback: Optional[Callable] = None,
metrics_callback: Optional[Callable] = None,
) -> pd.DataFrame:
"""Perform evaluation for an image segmentation dataset

Expand All @@ -389,6 +391,10 @@ def eval(
:type predictions_outdir: Optional[str], optional
:param results_per_sample: Whether to store results per sample or not, defaults to False. If True, predictions_outdir must be provided.
:type results_per_sample: bool, optional
:param progress_callback: Optional callback called as progress_callback(processed, total), defaults to None
:type progress_callback: Optional[Callable], optional
:param metrics_callback: Optional callback called as metrics_callback(metrics_df, processed, total), defaults to None
:type metrics_callback: Optional[Callable], optional
:return: DataFrame containing evaluation results
:rtype: pd.DataFrame
"""
Expand Down Expand Up @@ -444,6 +450,9 @@ def eval(

# Init metrics
metrics_factory = um.SegmentationMetricsFactory(n_classes)
total_samples = len(dataset)
processed_samples = 0
evaluation_step = self.model_cfg.get("evaluation_step", 1)

# Evaluation loop
with torch.no_grad():
Expand Down Expand Up @@ -504,6 +513,26 @@ def eval(
os.path.join(predictions_outdir, f"{sample_idx}.png")
)

processed_samples += len(idx)

if progress_callback is not None:
progress_callback(processed_samples, total_samples)

if (
metrics_callback is not None
and evaluation_step is not None
and evaluation_step > 0
and (
processed_samples % evaluation_step == 0
or processed_samples == total_samples
)
):
metrics_callback(
um.get_metrics_dataframe(metrics_factory, eval_ontology),
processed_samples,
total_samples,
)

return um.get_metrics_dataframe(metrics_factory, eval_ontology)

def get_computational_cost(
Expand Down
83 changes: 0 additions & 83 deletions perceptionmetrics/utils/gui.py

This file was deleted.

Loading
Loading