Skip to content

Commit

Permalink
Added _get_plot_details function to MetricResult classes
Browse files Browse the repository at this point in the history
  • Loading branch information
David-rn committed Dec 11, 2024
1 parent 38886ef commit 5fa6fc0
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 56 deletions.
4 changes: 4 additions & 0 deletions supervision/metrics/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def to_pandas():
def plot():
raise NotImplementedError()

@abstractmethod
def _get_plot_details():
raise NotImplementedError()


class MetricTarget(Enum):
"""
Expand Down
36 changes: 24 additions & 12 deletions supervision/metrics/f1_score.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
oriented_box_iou_batch,
)
from supervision.draw.color import LEGACY_COLOR_PALETTE
from supervision.metrics.core import AveragingMethod, Metric, MetricTarget, MetricResult
from supervision.metrics.core import AveragingMethod, Metric, MetricResult, MetricTarget
from supervision.metrics.utils.object_size import (
ObjectSizeCategory,
get_detection_size_category,
Expand Down Expand Up @@ -583,15 +583,15 @@ def to_pandas(self) -> "pd.DataFrame":

return pd.DataFrame(pandas_data, index=[0])

def plot(self):
def _get_plot_details(self) -> Tuple[List[str], List[float], str, List[str]]:
"""
Plot the F1 results.
Obtain the metric details for plotting them.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/f1_plot_example.png\
){ align=center width="800" }
Returns:
Tuple[List[str], List[float], str, List[str]]: The details for plotting the
metric. It is a tuple of four elements: a list of labels, a list of
values, the title of the plot and the bar colors.
"""

labels = ["F1@50", "F1@75"]
values = [self.f1_50, self.f1_75]
colors = [LEGACY_COLOR_PALETTE[0]] * 2
Expand All @@ -614,16 +614,28 @@ def plot(self):
values += [large_objects.f1_50, large_objects.f1_75]
colors += [LEGACY_COLOR_PALETTE[4]] * 2

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
title = (
f"F1 Score, by Object Size"
f"\n(target: {self.metric_target.value},"
f" averaging: {self.averaging_method.value})"
)
return labels, values, title, colors

def plot(self):
"""
Plot the F1 results.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/f1_plot_example.png\
){ align=center width="800" }
"""
labels, values, title, colors = self._get_plot_details()

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
ax.set_title(title, fontweight="bold")

x_positions = range(len(labels))
Expand Down
30 changes: 20 additions & 10 deletions supervision/metrics/mean_average_precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -559,19 +559,19 @@ def to_pandas(self) -> "pd.DataFrame":
index=[0],
)

def plot(self, return_params=False):
def _get_plot_details(self) -> Tuple[List[str], List[float], str, List[str]]:
"""
Plot the mAP results.
Obtain the metric details for plotting them.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/mAP_plot_example.png\
){ align=center width="800" }
Returns:
Tuple[List[str], List[float], str, List[str]]: The details for plotting the
metric. It is a tuple of four elements: a list of labels, a list of
values, the title of the plot and the bar colors.
"""

labels = ["mAP@50:95", "mAP@50", "mAP@75"]
values = [self.map50_95, self.map50, self.map75]
colors = [LEGACY_COLOR_PALETTE[0]] * 3
plot_title = "Mean Average Precision"
title = "Mean Average Precision"

if self.small_objects is not None:
labels += ["Small: mAP@50:95", "Small: mAP@50", "Small: mAP@75"]
Expand Down Expand Up @@ -600,15 +600,25 @@ def plot(self, return_params=False):
]
colors += [LEGACY_COLOR_PALETTE[4]] * 3

if return_params:
return labels, values, plot_title
return labels, values, title, colors

def plot(self):
"""
Plot the mAP results.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/mAP_plot_example.png\
){ align=center width="800" }
"""

labels, values, title, colors = self._get_plot_details()

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
ax.set_title(plot_title, fontweight="bold")
ax.set_title(title, fontweight="bold")

x_positions = range(len(labels))
bars = ax.bar(x_positions, values, color=colors, align="center")
Expand Down
34 changes: 24 additions & 10 deletions supervision/metrics/mean_average_recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
oriented_box_iou_batch,
)
from supervision.draw.color import LEGACY_COLOR_PALETTE
from supervision.metrics.core import Metric, MetricTarget, MetricResult
from supervision.metrics.core import Metric, MetricResult, MetricTarget
from supervision.metrics.utils.object_size import (
ObjectSizeCategory,
get_detection_size_category,
Expand Down Expand Up @@ -622,13 +622,14 @@ def to_pandas(self) -> "pd.DataFrame":

return pd.DataFrame(pandas_data, index=[0])

def plot(self):
def _get_plot_details(self) -> Tuple[List[str], List[float], str, List[str]]:
"""
Plot the Mean Average Recall results.
Obtain the metric details for plotting them.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/mAR_plot_example.png\
){ align=center width="800" }
Returns:
Tuple[List[str], List[float], str, List[str]]: The details for plotting the
metric. It is a tuple of four elements: a list of labels, a list of
values, the title of the plot and the bar colors.
"""
labels = ["mAR @ 1", "mAR @ 10", "mAR @ 100"]
values = [self.mAR_at_1, self.mAR_at_10, self.mAR_at_100]
Expand Down Expand Up @@ -664,15 +665,28 @@ def plot(self):
]
colors += [LEGACY_COLOR_PALETTE[4]] * 3

title = (
f"Mean Average Recall, by Object Size"
f"\n(target: {self.metric_target.value})"
)
return labels, values, title, colors

def plot(self):
"""
Plot the Mean Average Recall results.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/mAR_plot_example.png\
){ align=center width="800" }
"""

labels, values, title, colors = self._get_plot_details()

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
title = (
f"Mean Average Recall, by Object Size"
f"\n(target: {self.metric_target.value})"
)
ax.set_title(title, fontweight="bold")

x_positions = range(len(labels))
Expand Down
37 changes: 25 additions & 12 deletions supervision/metrics/precision.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
oriented_box_iou_batch,
)
from supervision.draw.color import LEGACY_COLOR_PALETTE
from supervision.metrics.core import AveragingMethod, Metric, MetricTarget, MetricResult
from supervision.metrics.core import AveragingMethod, Metric, MetricResult, MetricTarget
from supervision.metrics.utils.object_size import (
ObjectSizeCategory,
get_detection_size_category,
Expand Down Expand Up @@ -588,15 +588,15 @@ def to_pandas(self) -> "pd.DataFrame":

return pd.DataFrame(pandas_data, index=[0])

def plot(self):
def _get_plot_details(self) -> Tuple[List[str], List[float], str, List[str]]:
"""
Plot the precision results.
Obtain the metric details for plotting them.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/precision_plot_example.png\
){ align=center width="800" }
Returns:
Tuple[List[str], List[float], str, List[str]]: The details for plotting the
metric. It is a tuple of four elements: a list of labels, a list of
values, the title of the plot and the bar colors.
"""

labels = ["Precision@50", "Precision@75"]
values = [self.precision_at_50, self.precision_at_75]
colors = [LEGACY_COLOR_PALETTE[0]] * 2
Expand All @@ -619,16 +619,29 @@ def plot(self):
values += [large_objects.precision_at_50, large_objects.precision_at_75]
colors += [LEGACY_COLOR_PALETTE[4]] * 2

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
title = (
f"Precision, by Object Size"
f"\n(target: {self.metric_target.value},"
f" averaging: {self.averaging_method.value})"
)
return labels, values, title, colors

def plot(self):
"""
Plot the precision results.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/precision_plot_example.png\
){ align=center width="800" }
"""

labels, values, title, colors = self._get_plot_details()

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
ax.set_title(title, fontweight="bold")

x_positions = range(len(labels))
Expand Down
38 changes: 26 additions & 12 deletions supervision/metrics/recall.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
oriented_box_iou_batch,
)
from supervision.draw.color import LEGACY_COLOR_PALETTE
from supervision.metrics.core import AveragingMethod, Metric, MetricTarget, MetricResult
from supervision.metrics.core import AveragingMethod, Metric, MetricResult, MetricTarget
from supervision.metrics.utils.object_size import (
ObjectSizeCategory,
get_detection_size_category,
Expand Down Expand Up @@ -587,15 +587,15 @@ def to_pandas(self) -> "pd.DataFrame":

return pd.DataFrame(pandas_data, index=[0])

def plot(self):
def _get_plot_details(self):
"""
Plot the recall results.
Obtain the metric details for plotting them.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/recall_plot_example.png\
){ align=center width="800" }
Returns:
Tuple[List[str], List[float], str, List[str]]: The details for plotting the
metric. It is a tuple of four elements: a list of labels, a list of
values, the title of the plot and the bar colors.
"""

labels = ["Recall@50", "Recall@75"]
values = [self.recall_at_50, self.recall_at_75]
colors = [LEGACY_COLOR_PALETTE[0]] * 2
Expand All @@ -618,16 +618,30 @@ def plot(self):
values += [large_objects.recall_at_50, large_objects.recall_at_75]
colors += [LEGACY_COLOR_PALETTE[4]] * 2

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
title = (
f"Recall, by Object Size"
f"\n(target: {self.metric_target.value},"
f" averaging: {self.averaging_method.value})"
)

return labels, values, title, colors

def plot(self):
"""
Plot the recall results.
![example_plot](\
https://media.roboflow.com/supervision-docs/metrics/recall_plot_example.png\
){ align=center width="800" }
"""

labels, values, title, colors = self._get_plot_details()

plt.rcParams["font.family"] = "monospace"

_, ax = plt.subplots(figsize=(10, 6))
ax.set_ylim(0, 1)
ax.set_ylabel("Value", fontweight="bold")
ax.set_title(title, fontweight="bold")

x_positions = range(len(labels))
Expand Down

0 comments on commit 5fa6fc0

Please sign in to comment.