diff --git a/supervision/metrics/core.py b/supervision/metrics/core.py index bae56785f..b1335ecd6 100644 --- a/supervision/metrics/core.py +++ b/supervision/metrics/core.py @@ -46,6 +46,10 @@ def to_pandas(): def plot(): raise NotImplementedError() + @abstractmethod + def _get_plot_details(): + raise NotImplementedError() + class MetricTarget(Enum): """ diff --git a/supervision/metrics/f1_score.py b/supervision/metrics/f1_score.py index 91d587fbf..5184d5121 100644 --- a/supervision/metrics/f1_score.py +++ b/supervision/metrics/f1_score.py @@ -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, @@ -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 @@ -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)) diff --git a/supervision/metrics/mean_average_precision.py b/supervision/metrics/mean_average_precision.py index 4854697fb..1e0637a33 100644 --- a/supervision/metrics/mean_average_precision.py +++ b/supervision/metrics/mean_average_precision.py @@ -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"] @@ -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") diff --git a/supervision/metrics/mean_average_recall.py b/supervision/metrics/mean_average_recall.py index 6f2aa28ae..6e4d9c0d6 100644 --- a/supervision/metrics/mean_average_recall.py +++ b/supervision/metrics/mean_average_recall.py @@ -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, @@ -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] @@ -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)) diff --git a/supervision/metrics/precision.py b/supervision/metrics/precision.py index c10c7aca4..49eae5adb 100644 --- a/supervision/metrics/precision.py +++ b/supervision/metrics/precision.py @@ -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, @@ -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 @@ -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)) diff --git a/supervision/metrics/recall.py b/supervision/metrics/recall.py index 3f8b9e808..b84658aec 100644 --- a/supervision/metrics/recall.py +++ b/supervision/metrics/recall.py @@ -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, @@ -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 @@ -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))