embedding_benchmark#

Attributes#

Classes#

Baseline

The main class that runs the baseline methods.

BaselineMethod

An abstract class that holds common code of our baseline methods.

Config

KNNClassifier

A lightly KNN Classifier modified to log mean average precision metric.

LinearClassifier

A lightly Linear Classifier, modified to log the mean average precision

MetricCallback

A [Lightly] Callback that collects log metrics from the LightningModule and stores them after

MetricModule

SwinL384

A lightly Linear Classifier, modified to log the mean average precision

SwinL384Baseline

An abstract class that holds common code of our baseline methods.

ViTEmbedding

This module is used to extract features from the Vision Transformer Classifier in eval mode

ViT_B_16Baseline

An abstract class that holds common code of our baseline methods.

ViT_B_16Classifier

A fully supervised model that uses the Vision Transformer model from the torchvision library

Functions#

clear_cache()

knn_predict(→ torch.Tensor)

[Modified version from lightly, which returns the scores instead of the predictions]

timing_decorator(func)

Module Contents#

class embedding_benchmark.Baseline[source]#

The main class that runs the baseline methods.

aggregate_metrics(cfg: Config)[source]#
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.

embedding_training()[source]#
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

run_baseline_method()[source]#
train(classifier, epochs, train_dataset, val_dataset, log_name)[source]#
cfg: Config[source]#
embedding_train_dataset: Iterable[Tuple[torch.Tensor, torch.Tensor]][source]#
embedding_train_transform[source]#
embedding_val_dataset: Iterable[Tuple[torch.Tensor, torch.Tensor]][source]#
eval_train_transform[source]#
feature_dim: int = 2048[source]#
knn_train_dataset: Iterable[Tuple[torch.Tensor, torch.Tensor]][source]#
knn_val_dataset: Iterable[Tuple[torch.Tensor, torch.Tensor]][source]#
linear_train_dataset: Iterable[Tuple[torch.Tensor, torch.Tensor]][source]#
linear_val_dataset: Iterable[Tuple[torch.Tensor, torch.Tensor]][source]#
method_dir[source]#
method_specific_augmentation[source]#
model: torch.nn.Module[source]#
property name: str[source]#
normalize_transform[source]#
resize_transform[source]#
skip_embedding_training: bool = False[source]#
val_transform[source]#
class embedding_benchmark.Config[source]#
accelerator: str = 'auto'[source]#
aggregate_metrics: bool = True[source]#
baseline_id: str | None = None[source]#
batch_size_per_device: int = 16[source]#
check_val_every_n_epoch: int = 5[source]#
checkpoint_path: pathlib.Path | None = None[source]#
devices: int = 1[source]#
epochs: int = 200[source]#
experiment_result_metrics: List[str] | None = [][source]#
log_dir: pathlib.Path[source]#
methods: List[str] | None = None[source]#
num_classes: int = 50[source]#
num_workers: int = 4[source]#
precision: str = '16-mixed'[source]#
profile = None[source]#
skip_embedding_training: bool = False[source]#
skip_knn_eval: bool = False[source]#
skip_linear_eval: bool = False[source]#
test_run: bool = True[source]#
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: MetricModule

A lightly KNN Classifier modified to log mean average precision metric. Also it now inherits from MetricModule and the logging logic has changed.

configure_optimizers() None[source]#
on_train_epoch_start() None[source]#
on_validation_end() None[source]#
on_validation_epoch_start() None[source]#
training_step(batch, batch_idx) None[source]#
validation_step(batch, batch_idx) None[source]#
feature_dtype[source]#
knn_k = 200[source]#
knn_t = 0.1[source]#
model[source]#
normalize = True[source]#
num_classes[source]#
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: MetricModule

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

build_classification_head(feature_dim: int, num_classes: int)[source]#
build_critierion()[source]#
configure_optimizers() Tuple[List[torch.optim.Optimizer], List[Dict[str, Any | str]]][source]#
forward(images: torch.Tensor) torch.Tensor[source]#
on_train_epoch_start() None[source]#
training_step(batch: Tuple[torch.Tensor, Ellipsis], batch_idx: int) torch.Tensor[source]#
validation_step(batch: Tuple[torch.Tensor, Ellipsis], batch_idx: int) torch.Tensor[source]#
batch_size_per_device[source]#
classification_head[source]#
criterion[source]#
enable_logging = True[source]#
feature_dim[source]#
freeze_model = False[source]#
model[source]#
num_classes[source]#
class embedding_benchmark.MetricCallback[source]#

Bases: pytorch_lightning.callbacks.Callback

A [Lightly] Callback that collects log metrics from the LightningModule and stores them after every epoch.

train_metrics[source]#

Dictionary that stores the last logged metrics after every train epoch.

val_metrics[source]#

Dictionary that stores the last logged metrics after every validation epoch.

on_train_end(trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) None[source]#
on_validation_end(trainer: pytorch_lightning.Trainer, pl_module: pytorch_lightning.LightningModule) None[source]#
train_metrics: Dict[str, List[float]][source]#
val_metrics: Dict[str, List[float]][source]#
class embedding_benchmark.MetricModule(num_classes: int)[source]#

Bases: pytorch_lightning.LightningModule

on_train_epoch_end()[source]#
on_validation_epoch_end()[source]#
update_train_metrics(pred_scores: torch.Tensor, targets: torch.Tensor)[source]#
update_val_metrics(pred_scores: torch.Tensor, targets: torch.Tensor)[source]#
enable_logging = True[source]#
num_classes[source]#
class embedding_benchmark.SwinL384(batch_size_per_device, feature_dim, num_classes)[source]#

Bases: LinearClassifier

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

build_critierion()[source]#
configure_optimizers()[source]#
forward(x: torch.Tensor) torch.Tensor[source]#
enable_logging: bool = False[source]#
class embedding_benchmark.SwinL384Baseline(args)[source]#

Bases: BaselineMethod

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.

feature_dim = 1536[source]#
method_specific_augmentation[source]#
model[source]#
skip_embedding_training = False[source]#
class embedding_benchmark.ViTEmbedding(model: ViT_B_16Classifier)[source]#

Bases: pytorch_lightning.LightningModule

This module is used to extract features from the Vision Transformer Classifier in eval mode

forward(x: torch.Tensor) torch.Tensor[source]#
model[source]#
class embedding_benchmark.ViT_B_16Baseline(cfg)[source]#

Bases: BaselineMethod

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()[source]#

Must return a model that returns features on forward pass.

feature_dim: int = 768[source]#
method_specific_augmentation[source]#
model[source]#
class embedding_benchmark.ViT_B_16Classifier(batch_size_per_device, feature_dim, num_classes)[source]#

Bases: LinearClassifier

A 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

configure_optimizers()[source]#

This optimizer is a inspired the optimizer used in the lightly benchmarks for their Vision Transformer backbones specifically the AIM Model.

model: torchvision.models.vision_transformer.VisionTransformer[source]#
embedding_benchmark.clear_cache()[source]#
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,
>>> )
embedding_benchmark.timing_decorator(func)[source]#
embedding_benchmark.args = None[source]#
embedding_benchmark.parser[source]#