Confusion Matrix
See exactly where your classifier gets confused
- matrix
- image
CdlConfusionMatrix turns a confusion matrix tensor into a heatmap image you can actually read. Accuracy tells you how often the model is right; a confusion matrix tells you where it's wrong - which pairs of classes it keeps mixing up, which class is stealing all the errors, whether the model is systematically over-predicting one category. It's the diagnostic you reach for after the accuracy number looks fine but the results feel off. Like every ComfyDL Visualization node, the output is a real ComfyUI IMAGE.
How it works
It's a matplotlib imshow heatmap with each cell's value printed on top of it via ax.text. Your matrix tensor goes to numpy, the color grid is drawn, numbers are stamped in, and the figure is converted to an IMAGE tensor. Two behaviors worth knowing:
- Flattened input is accepted. If you feed a 1-D tensor, the node reshapes it to
n × nusing the square root of its length. Handy if upstream gave you the matrix flattened, but it means your data must be a perfect square - reshape happens silently. - Normalization is row-wise. With
normalizeon, each row is divided by its sum, so every cell becomes "of the times this true class appeared, how often did the model call it each class." That's the recall-oriented view, and it's usually the most useful one - bright diagonals are good, off-diagonal bright spots are systematic confusions.
Inputs and output
matrix- thecdlTensorconfusion matrix.class_labels- comma-separated class names for the axes (empty = no labels).cmap- matplotlib colormap, defaultBlues.normalize- boolean, default off; row-normalizes.fmt- number format for cell text:.0f,.1f,.2f, or.3f(default.1f).figsize_w,figsize_h- figure size, defaults 6×5.
The output is image, type IMAGE - wire it into PreviewImage or SaveImage.
Where you'd use it
Any multi-class evaluation. After you've accumulated predictions and true labels, build the matrix (ComfyDL's CdlAccuracy gives you the counts; the matrix itself you'll typically construct from a tally tensor), then drop it here and eyeball it. It's also a great teaching node - take a tiny 2×2 matrix and watch exactly how false positives and false negatives sit off the diagonal.
Installing it
It's part of ComfyDL, one install for all 106 nodes:
cd ComfyUI/custom_nodes
git clone https://github.com/Cynthia-lxx/ComfyDL
pip install -r ./ComfyDL/requirements.txt
Restart ComfyUI, or search "ComfyDL" in ComfyUI Manager. matplotlib is the only dependency this pack adds, and the chart nodes all need it.
Gotchas
- A flattened 1-D matrix reshapes via
sqrt(n). If your count isn't a perfect square the reshape silently drops the remainder - you'll get a slightly truncated matrix with no error. class_labelsis comma-separated and their count isn't validated against the matrix size. Mismatches can leave the axis labels misaligned with the actual classes.- A 2-D matrix isn't validated to be square either; feeding a non-square matrix is on you.
- Blank
class_labelsgives an unlabeled heatmap - fine for a quick look, unreadable for anything you'd show someone.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| matrix | TENSOR | — | |
| class_labels | STRING | — | |
| cmap | STRING | Blues | — |
| normalize | BOOLEAN | false | — |
| fmt | COMBO | .1f | 4 options: .0f, .1f, .2f, .3f |
| figsize_w | FLOAT | 6.03–20 | — |
| figsize_h | FLOAT | 5.03–20 | — |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |