CV Train Classifier
Trains a cv2.ml classifier and evaluates it, all inside one node: the features are split into a train and a held-out test set (stratified per class, deterministic per seed), the picked algorithm is fitted on the train split and scored on the test split (accuracy + confusion matrix). The model never leaves the node - train, evaluate and the optional query prediction all happen in one call, so caching stays correct. Connect query_features (e.g. an 'CV Coordinate Grid') to also classify arbitrary extra samples with the freshly trained model - reshape + colormap those predictions to render decision regions. Degenerate data (no samples, a single class, an algorithm that cannot fit - e.g. Boost with 3 classes) yields success=false with safe empty outputs and query predictions of the first class, never an error. model_yaml carries the serialized model for reuse outside ComfyUI (cv2.ml.*_load).
- features
- labels
- query_features
- predictions
- eval_labels
- accuracy
- train_accuracy
- confusion
- success
- query_predictions
- model_yaml
Inputs (15)
| Name | Type | Default | Description |
|---|---|---|---|
| features | NPARRAY | (N, D) training samples, one row per sample (any shape flattens to rows): stacked points, HOG rows, deep embeddings... | |
| labels | NPARRAY | (N,) integer class label per row - e.g. the labels of 'CV Stack Feature Classes'. | |
| algorithm | COMBO | SVM (support vector machine) | cv2.ml algorithm to train. SVM is the strong default; KNearest is instance-based (no real training); RTrees/DTrees/Boost are tree-based (Boost handles exactly 2 classes); NormalBayes fits gaussians; LogisticRegression is linear; ANN_MLP is a small fully-connected net trained by real backpropagation (from scratch - cv2 cannot finetune pretrained deep nets). |
| test_fraction | FLOAT | 0.300–0.9 | Held-out fraction per class for the accuracy / confusion outputs. 0 evaluates on the training set itself (optimistic - fine for decision-region demos). |
| query_featuresopt | NPARRAY | Optional extra samples to classify with the trained model (same feature length D), e.g. an 'OpenCV Coordinate Grid' to paint decision regions. | |
| svm_kernelopt | COMBO | RBF (gaussian) | SVM only: the kernel. RBF handles curved boundaries; linear is fastest and best when D is large (e.g. HOG/deep features). |
| svm_tuningopt | COMBO | manual (use C and gamma) | SVM only: manual uses the C / gamma below; trainAuto cross-validates a grid over them (slower, robust default for real features). |
| svm_copt | FLOAT | 1.000.000001–1000000 | SVM only (manual): soft-margin penalty C. Higher fits the training set tighter (risk of overfitting). |
| svm_gammaopt | FLOAT | 1.001e-9–1000000 | SVM only (manual, RBF/poly/sigmoid/chi2): kernel width. Higher = wigglier boundary. For pixel coordinates try 0.001-0.01; for normalized features 0.1-10. |
| knn_kopt | INT | 51–256 | KNearest only: how many neighbors vote. |
| tree_depthopt | INT | 81–64 | RTrees/DTrees/Boost only: maximum tree depth. |
| forest_sizeopt | INT | 641–2048 | RTrees: number of trees; Boost: number of weak learners. |
| mlp_hiddenopt | INT | 321–4096 | ANN_MLP only: neurons in the single hidden layer. |
| mlp_iterationsopt | INT | 3001–100000 | ANN_MLP: RPROP epochs; LogisticRegression: batch gradient iterations. |
| seedopt | INT | 00–2147483647 | Seed for the stratified shuffle split (and cv2's RNG) - same seed, same split, same model. |
Outputs (8)
| Name | Type | Description |
|---|---|---|
| predictions | NPARRAY | (M,) int32 predicted label of every EVAL sample (the held-out split, or the train set when test_fraction = 0). Aligned with eval_labels. |
| eval_labels | NPARRAY | (M,) int32 ground-truth label of every eval sample - compare against predictions. |
| accuracy | FLOAT | Fraction of eval samples classified correctly (chance = 1 / class count). |
| train_accuracy | FLOAT | Accuracy on the training split itself - much higher than 'accuracy' means overfitting. |
| confusion | NPARRAY | (K, K) int32 confusion matrix over the eval set: row = true class, column = predicted. Upscale with cv2.resize (INTER_NEAREST) + 'Preview CV Array' (heatmap) to see it. |
| success | BOOLEAN | False when training was impossible (no samples, one class, algorithm/data mismatch) - gate downstream consumers with if/else. |
| query_predictions | NPARRAY | (Q,) int32 predicted label per query_features row (empty when unconnected; all first-class when success=false, so reshapes stay valid). |
| model_yaml | STRING | The trained model serialized as OpenCV YAML (cv2.ml.SVM_load & co. read it back outside ComfyUI). Empty when unavailable. |