embedding_benchmark#
Attributes#
Classes#
The main class that runs the baseline methods. |
|
An abstract class that holds common code of our baseline methods. |
|
A lightly KNN Classifier modified to log mean average precision metric. |
|
A lightly Linear Classifier, modified to log the mean average precision |
|
A [Lightly] Callback that collects log metrics from the LightningModule and stores them after |
|
A lightly Linear Classifier, modified to log the mean average precision |
|
An abstract class that holds common code of our baseline methods. |
|
This module is used to extract features from the Vision Transformer Classifier in eval mode |
|
An abstract class that holds common code of our baseline methods. |
|
A fully supervised model that uses the Vision Transformer model from the torchvision library |
Functions#
|
[Modified version from lightly, which returns the scores instead of the predictions] |
|
Module Contents#
- class embedding_benchmark.Baseline[source]#
The main class that runs the baseline methods.
- calculate_mean_std(df: pandas.DataFrame, groupby: List[str])[source]#
Calculate the mean and standard deviation for all numerical columns grouped by two identifiers, and return a DataFrame with the results in the ± notation.
- Parameters:
df (pd.DataFrame) – Input DataFrame.
groupby (List[str]) – List of which columns to group by.
- Returns:
DataFrame with mean and standard deviation in ± notation.
- Return type:
pd.DataFrame
- run(args)[source]#
Run the class as specified in the config.
args: argparse.Namespace - The CLI arguments, used as kwargs to instantiate a Config object.
- methods: Dict[str, Type[BaselineMethod]][source]#
- class embedding_benchmark.BaselineMethod(cfg: Config)[source]#
An abstract class that holds common code of our baseline methods.
- The class runs:
embedding training
kNN evaluation
linear evaluation
- Reported metrics are:
Top-1 accuracy
Top-5 accuracy
Mean Average Precision (mAP)
The baseline method can be configured by inheriting from this class and overriding specific attributes or functions as well as passing a config object.
- get_embedding_model() torch.nn.Module[source]#
Must return a model that returns features on forward pass.
- knn_eval() None[source]#
Runs KNN evaluation on the given model.
Parameters follow InstDisc [0] settings.
- The most important settings are:
Num nearest neighbors: 200
Temperature: 0.1
References: - [0]: InstDict, 2018, https://arxiv.org/abs/1805.01978
- linear_eval() None[source]#
Runs a linear evaluation on the given model.
Parameters follow SimCLR [0] settings.
- The most important settings are:
Backbone: Frozen
Epochs: 90
Optimizer: SGD
Base Learning Rate: 0.1
Momentum: 0.9
Weight Decay: 0.0
LR Schedule: Cosine without warmup
References
[0]: SimCLR, 2020, https://arxiv.org/abs/2002.05709
- class embedding_benchmark.KNNClassifier(model: torch.nn.Module, num_classes: int, knn_k: int = 200, knn_t: float = 0.1, feature_dtype: torch.dtype = torch.float32, normalize: bool = True)[source]#
Bases:
MetricModuleA lightly KNN Classifier modified to log mean average precision metric. Also it now inherits from MetricModule and the logging logic has changed.
- class embedding_benchmark.LinearClassifier(model: torch.nn.Module, batch_size_per_device: int, feature_dim: int, num_classes: int, freeze_model: bool = False, enable_logging: bool = True)[source]#
Bases:
MetricModuleA lightly Linear Classifier, modified to log the mean average precision Also, the logging logic has changed + it now inherits from MetricModule Further, the LinearClassifier now also allows the instantiation of fully supervised models.
- class embedding_benchmark.MetricCallback[source]#
Bases:
pytorch_lightning.callbacks.CallbackA [Lightly] Callback that collects log metrics from the LightningModule and stores them after every epoch.
- on_train_end(trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) None[source]#
- class embedding_benchmark.MetricModule(num_classes: int)[source]#
Bases:
pytorch_lightning.LightningModule
- class embedding_benchmark.SwinL384(batch_size_per_device, feature_dim, num_classes)[source]#
Bases:
LinearClassifierA lightly Linear Classifier, modified to log the mean average precision Also, the logging logic has changed + it now inherits from MetricModule Further, the LinearClassifier now also allows the instantiation of fully supervised models.
- class embedding_benchmark.SwinL384Baseline(args)[source]#
Bases:
BaselineMethodAn abstract class that holds common code of our baseline methods.
- The class runs:
embedding training
kNN evaluation
linear evaluation
- Reported metrics are:
Top-1 accuracy
Top-5 accuracy
Mean Average Precision (mAP)
The baseline method can be configured by inheriting from this class and overriding specific attributes or functions as well as passing a config object.
- class embedding_benchmark.ViTEmbedding(model: ViT_B_16Classifier)[source]#
Bases:
pytorch_lightning.LightningModuleThis module is used to extract features from the Vision Transformer Classifier in eval mode
- class embedding_benchmark.ViT_B_16Baseline(cfg)[source]#
Bases:
BaselineMethodAn abstract class that holds common code of our baseline methods.
- The class runs:
embedding training
kNN evaluation
linear evaluation
- Reported metrics are:
Top-1 accuracy
Top-5 accuracy
Mean Average Precision (mAP)
The baseline method can be configured by inheriting from this class and overriding specific attributes or functions as well as passing a config object.
- class embedding_benchmark.ViT_B_16Classifier(batch_size_per_device, feature_dim, num_classes)[source]#
Bases:
LinearClassifierA fully supervised model that uses the Vision Transformer model from the torchvision library
The model uses the standard ViT_B_16 model and cross entropy (as in inherited from LinearClassifier) for training
- embedding_benchmark.knn_predict(feature: torch.Tensor, feature_bank: torch.Tensor, feature_labels: torch.Tensor, num_classes: int, knn_k: int = 200, knn_t: float = 0.1) torch.Tensor[source]#
[Modified version from lightly, which returns the scores instead of the predictions]
Run kNN predictions on features based on a feature bank
This method is commonly used to monitor performance of self-supervised learning methods.
The default parameters are the ones used in https://arxiv.org/pdf/1805.01978v1.pdf.
# code for kNN prediction from here: # https://colab.research.google.com/github/facebookresearch/moco/blob/colab-notebook/colab/moco_cifar10_demo.ipynb
- Parameters:
feature – Tensor with shape (B, D) for which you want predictions.
feature_bank – Tensor of shape (D, N) of a database of features used for kNN.
feature_labels – Labels with shape (N,) for the features in the feature_bank.
num_classes – Number of classes (e.g. 10 for CIFAR-10).
knn_k – Number of k neighbors used for kNN.
knn_t – Temperature parameter to reweights similarities for kNN.
- Returns:
A tensor containing the kNN scores
Examples
>>> images, targets, _ = batch >>> feature = backbone(images).squeeze() >>> # we recommend to normalize the features >>> feature = F.normalize(feature, dim=1) >>> pred_labels = knn_predict( >>> feature, >>> feature_bank, >>> targets_bank, >>> num_classes=10, >>> )