Skip to content
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

fix confusion matrix in model evaluation panel #5186

Merged
merged 6 commits into from
Nov 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1155,6 +1155,17 @@ export default function Evaluation(props: EvaluationProps) {
colorscale: confusionMatrixConfig.log
? confusionMatrix?.colorscale || "viridis"
: "viridis",
hovertemplate:
[
"<b>count: %{z:d}</b>",
`${
evaluation?.info?.config?.gt_field || "truth"
}: %{y}`,
`${
evaluation?.info?.config?.pred_field ||
"predicted"
}: %{x}`,
].join(" <br>") + "<extra></extra>",
},
]}
onClick={({ points }) => {
Expand Down Expand Up @@ -1183,6 +1194,17 @@ export default function Evaluation(props: EvaluationProps) {
colorscale: confusionMatrixConfig.log
? compareConfusionMatrix?.colorscale || "viridis"
: "viridis",
hovertemplate:
[
"<b>count: %{z:d}</b>",
`${
evaluation?.info?.config?.gt_field || "truth"
}: %{y}`,
`${
evaluation?.info?.config?.pred_field ||
"predicted"
}: %{x}`,
].join(" <br>") + "<extra></extra>",
},
]}
/>
Expand Down Expand Up @@ -1532,8 +1554,8 @@ function getMatrix(matrices, config) {
if (!matrices) return;
const { sortBy = "az", limit } = config;
const parsedLimit = typeof limit === "number" ? limit : undefined;
const classes = matrices[`${sortBy}_classes`].slice(parsedLimit);
const matrix = matrices[`${sortBy}_matrix`].slice(parsedLimit);
const classes = matrices[`${sortBy}_classes`].slice(0, parsedLimit);
const matrix = matrices[`${sortBy}_matrix`].slice(0, parsedLimit);
const colorscale = matrices[`${sortBy}_colorscale`];
return { labels: classes, matrix, colorscale };
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export default function EvaluationPlot(props: EvaluationPlotProps) {
color: theme.text.secondary,
gridcolor: theme.primary.softBorder,
automargin: true, // Enable automatic margin adjustment
scaleanchor: "x",
autorange: "reversed",
},
autosize: true,
margin: { t: 20, l: 50, b: 50, r: 20, pad: 0 },
Expand Down
34 changes: 27 additions & 7 deletions fiftyone/core/plots/plotly.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,8 @@ def plot_confusion_matrix(
labels,
colorscale=colorscale,
title=title,
gt_field=gt_field,
pred_field=pred_field,
**kwargs,
)

Expand All @@ -121,16 +123,24 @@ def plot_confusion_matrix(


def _plot_confusion_matrix_static(
confusion_matrix, labels, colorscale=None, title=None, **kwargs
confusion_matrix,
labels,
colorscale=None,
title=None,
gt_field=None,
pred_field=None,
**kwargs,
):
confusion_matrix = np.asarray(confusion_matrix)
num_rows, num_cols = confusion_matrix.shape
zlim = [0, confusion_matrix.max()]
truth = gt_field or "truth"
predicted = pred_field or "predicted"

hover_lines = [
"<b>count: %{z:d}</b>",
"truth: %{y}",
"predicted: %{x}",
f"{truth}: %{{y}}",
f"{predicted}: %{{x}}",
]
hovertemplate = "<br>".join(hover_lines) + "<extra></extra>"

Expand Down Expand Up @@ -164,8 +174,6 @@ def _plot_confusion_matrix_static(
scaleanchor="x",
scaleratio=1,
),
xaxis_title="Predicted label",
yaxis_title="True label",
title=title,
)

Expand Down Expand Up @@ -234,6 +242,8 @@ def _plot_confusion_matrix_interactive(
xlabels=xlabels,
ylabels=ylabels,
zlim=zlim,
gt_field=gt_field,
pred_field=pred_field,
colorscale=colorscale,
link_type="labels",
init_view=samples,
Expand Down Expand Up @@ -1959,6 +1969,10 @@ class InteractiveHeatmap(PlotlyInteractivePlot):
zlim (None): a ``[zmin, zmax]`` limit to use for the colorbar
values_title ("count"): the semantic meaning of the heatmap values.
Used for tooltips
gt_field (None): the name of the ground truth field, if known. Used for
tooltips
pred_field (None): the name of the predictions field, if known. Used
for tooltips
colorscale (None): a plotly colorscale to use
grid_opacity (0.1): an opacity value for the grid points
bg_opacity (0.25): an opacity value for background (unselected) cells
Expand All @@ -1974,6 +1988,8 @@ def __init__(
ylabels=None,
zlim=None,
values_title="count",
gt_field=None,
pred_field=None,
colorscale=None,
grid_opacity=0.1,
bg_opacity=0.25,
Expand All @@ -1991,6 +2007,8 @@ def __init__(
self.ylabels = ylabels
self.zlim = zlim
self.values_title = values_title
self.gt_field = gt_field
self.pred_field = pred_field
self.colorscale = colorscale
self.grid_opacity = grid_opacity
self.bg_opacity = bg_opacity
Expand Down Expand Up @@ -2185,11 +2203,13 @@ def _make_heatmap(self):
xticks = np.arange(num_cols)
yticks = np.arange(num_rows)
X, Y = np.meshgrid(xticks, yticks)
truth = self.gt_field or "truth"
predicted = self.pred_field or "predicted"

hover_lines = [
"<b>%s: %%{z}</b>" % self.values_title,
"truth: %{y}",
"predicted: %{x}",
f"{truth}: %{{y}}",
f"{predicted}: %{{x}}",
]
hovertemplate = "<br>".join(hover_lines) + "<extra></extra>"

Expand Down
13 changes: 9 additions & 4 deletions fiftyone/utils/eval/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,15 +333,17 @@ def _confusion_matrix(
# Omit `(other, other)`
i = labels.index(other_label)
cmat[i, i] = 0
ids[i, i] = []
if tabulate_ids:
ids[i, i] = []

if added_missing:
# Omit `(other, missing)` and `(missing, other)`
j = labels.index(self.missing)
cmat[i, j] = 0
cmat[j, i] = 0
ids[i, j] = []
ids[j, i] = []
if tabulate_ids:
ids[i, j] = []
ids[j, i] = []

rm_inds = []

Expand All @@ -357,7 +359,10 @@ def _confusion_matrix(

if rm_inds:
cmat = np.delete(np.delete(cmat, rm_inds, axis=0), rm_inds, axis=1)
ids = np.delete(np.delete(ids, rm_inds, axis=0), rm_inds, axis=1)
if tabulate_ids:
ids = np.delete(
np.delete(ids, rm_inds, axis=0), rm_inds, axis=1
)
labels = [l for i, l in enumerate(labels) if i not in rm_inds]

return cmat, labels, ids
Expand Down
Loading