fovi.utils.knnprobe
- fovi.utils.knnprobe.knn_probe(trainer, k=20, temperature=0.07)[source]
Perform k-nearest neighbors classification using pretrained backbone features.
Extracts features from both training and validation sets using the trainer’s model, then performs weighted KNN classification to evaluate representation quality.
- Parameters:
trainer – Trainer object with model, val_loader, and cfg attributes.
k (int, optional) – Number of nearest neighbors to use for classification. Defaults to 20.
temperature (float, optional) – Temperature for softmax weighting of neighbor distances. Lower values make voting sharper. Defaults to 0.07.
- Returns:
- Dictionary containing:
knn_top1 (float): Top-1 accuracy percentage.
knn_top5 (float): Top-5 accuracy percentage.
train_features (torch.Tensor): Features from training set.
val_features (torch.Tensor): Features from validation set.
train_labels (torch.Tensor): Labels from training set.
val_labels (torch.Tensor): Labels from validation set.
- Return type:
- fovi.utils.knnprobe.knn_classifier(train_features, train_labels, test_features, test_labels, k, T, num_classes=1000)[source]
Perform weighted KNN classification and compute accuracy.
Uses cosine similarity (via dot product on L2-normalized features) to find k nearest neighbors, then performs temperature-scaled weighted voting.
- Parameters:
train_features (torch.Tensor) – Training set feature vectors of shape (N, D).
train_labels (torch.Tensor) – Training set labels of shape (N,).
test_features (torch.Tensor) – Test set feature vectors of shape (M, D).
test_labels (torch.Tensor) – Test set labels of shape (M,).
k (int) – Number of nearest neighbors to use.
T (float) – Temperature for softmax weighting. If None, uses unweighted voting.
num_classes (int, optional) – Number of classes. Defaults to 1000.
- Returns:
(top1, top5) accuracy percentages.
- Return type: