Nodes/comfyui_cv/CV Train Classifier
ComfyUI Node

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).

By bmad4ever·Created 3 months ago·Updated 2 days ago· 0
CV Train Classifier
  • features
  • labels
  • query_features
  • predictions
  • eval_labels
  • accuracy
  • train_accuracy
  • confusion
  • success
  • query_predictions
  • model_yaml
algorithmSVM (support vector machine)
test_fraction0.30
svm_kernelRBF (gaussian)
svm_tuningmanual (use C and gamma)
svm_c1.00
svm_gamma1.00
knn_k5
tree_depth8
forest_size64
mlp_hidden32
mlp_iterations300
seed0
Categoryimage/CV/ml

Inputs (15)

NameTypeDefaultDescription
featuresNPARRAY(N, D) training samples, one row per sample (any shape flattens to rows): stacked points, HOG rows, deep embeddings...
labelsNPARRAY(N,) integer class label per row - e.g. the labels of 'CV Stack Feature Classes'.
algorithmCOMBOSVM (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_fractionFLOAT0.300–0.9Held-out fraction per class for the accuracy / confusion outputs. 0 evaluates on the training set itself (optimistic - fine for decision-region demos).
query_featuresoptNPARRAYOptional extra samples to classify with the trained model (same feature length D), e.g. an 'OpenCV Coordinate Grid' to paint decision regions.
svm_kerneloptCOMBORBF (gaussian)SVM only: the kernel. RBF handles curved boundaries; linear is fastest and best when D is large (e.g. HOG/deep features).
svm_tuningoptCOMBOmanual (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_coptFLOAT1.000.000001–1000000SVM only (manual): soft-margin penalty C. Higher fits the training set tighter (risk of overfitting).
svm_gammaoptFLOAT1.001e-9–1000000SVM 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_koptINT51–256KNearest only: how many neighbors vote.
tree_depthoptINT81–64RTrees/DTrees/Boost only: maximum tree depth.
forest_sizeoptINT641–2048RTrees: number of trees; Boost: number of weak learners.
mlp_hiddenoptINT321–4096ANN_MLP only: neurons in the single hidden layer.
mlp_iterationsoptINT3001–100000ANN_MLP: RPROP epochs; LogisticRegression: batch gradient iterations.
seedoptINT00–2147483647Seed for the stratified shuffle split (and cv2's RNG) - same seed, same split, same model.

Outputs (8)

NameTypeDescription
predictionsNPARRAY(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_labelsNPARRAY(M,) int32 ground-truth label of every eval sample - compare against predictions.
accuracyFLOATFraction of eval samples classified correctly (chance = 1 / class count).
train_accuracyFLOATAccuracy on the training split itself - much higher than 'accuracy' means overfitting.
confusionNPARRAY(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.
successBOOLEANFalse when training was impossible (no samples, one class, algorithm/data mismatch) - gate downstream consumers with if/else.
query_predictionsNPARRAY(Q,) int32 predicted label per query_features row (empty when unconnected; all first-class when success=false, so reshapes stay valid).
model_yamlSTRINGThe trained model serialized as OpenCV YAML (cv2.ml.SVM_load & co. read it back outside ComfyUI). Empty when unavailable.