""" Quick evaluation of OMNI-T on the THUNDER benchmark (https://github.com/MICS-Lab/thunder). Setup (once): pip install thunder-bench # see the THUNDER docs for the exact install export THUNDER_BASE_DATA_FOLDER=/path/to/thunder_data thunder download-datasets bach # or any other THUNDER dataset Usage: # Python API (default: knn + linear probing on BACH) python eval_thunder.py --datasets bach bracs --tasks knn linear_probing # or THUNDER's CLI, which picks up the class defined below thunder benchmark custom:eval_thunder.py bach linear_probing """ import argparse import torch from thunder import benchmark from thunder.models import PretrainedModel from transformers import AutoModel REPO_ID = "sofieneb/omni-tiny" class OmniThunder(PretrainedModel): """ THUNDER wrapper around an OMNI encoder. Linear probing / kNN use the average of the teacher tokens; segmentation uses the patch tokens. Args: repo_id (str): Hugging Face repo id or local folder of the model. """ def __init__(self, repo_id: str = REPO_ID): super().__init__() self.model = AutoModel.from_pretrained(repo_id, trust_remote_code=True).eval() self.name = repo_id.rstrip("/").split("/")[-1] # used by THUNDER to name output folders self.emb_dim = self.model.config.embed_dim self.vlm = False self._transform = self.model.get_transform() def forward(self, x: torch.Tensor) -> torch.Tensor: """ Args: x (torch.Tensor): Images of shape (B, 3, 224, 224). Returns: torch.Tensor: Tile embeddings of shape (B, D). """ return self.model(x) def get_transform(self): """Returns the image preprocessing used by OMNI.""" return self._transform def get_linear_probing_embeddings(self, x: torch.Tensor) -> torch.Tensor: """ Args: x (torch.Tensor): Images of shape (B, 3, 224, 224). Returns: torch.Tensor: Average of the teacher tokens, shape (B, D). """ return self.model(x) def get_segmentation_embeddings(self, x: torch.Tensor) -> torch.Tensor: """ Args: x (torch.Tensor): Images of shape (B, 3, 224, 224). Returns: torch.Tensor: Patch tokens of shape (B, 256, D). """ return self.model(x, return_patch_tokens=True)["patch_tokens"] def main(): parser = argparse.ArgumentParser(description="Evaluate OMNI-T on THUNDER.") parser.add_argument("--repo-id", default=REPO_ID, help="Hugging Face repo id or local folder.") parser.add_argument("--datasets", nargs="+", default=["bach"], help="THUNDER dataset names.") parser.add_argument( "--tasks", nargs="+", default=["knn", "linear_probing"], help="THUNDER tasks (knn, linear_probing, simple_shot, ...)." ) args = parser.parse_args() device = "cuda" if torch.cuda.is_available() else "cpu" model = OmniThunder(args.repo_id).to(device).eval() for dataset in args.datasets: # Embeddings are computed once, then re-used by every task. benchmark(model, dataset=dataset, task="pre_computing_embeddings", recomp_embs=True) for task in args.tasks: # Linear probing is trained directly on the pre-computed embeddings. extra = {"loading_mode": "embedding_pre_loading"} if task == "linear_probing" else {} benchmark(model, dataset=dataset, task=task, retrain_model=True, **extra) if __name__ == "__main__": main()