peal.architectures.predictors.TorchvisionModel¶
- class peal.architectures.predictors.TorchvisionModel[source]¶
Bases:
ModulePretrained backbone plus a fresh linear head, behind one interface.
The
modelstring selects the backbone and decides where the classification head lives: for the torchvision ResNets and ViTs the head inside the backbone is replaced, for the feature-extractor backbones (DINOv2/DINOv3, OpenCLIP, UNI) a separateself.fcis added on top of the frozen-format embedding. All variants are reachable throughfeature_extractor(),get_last_layer()andforward(), which is what the PEAL adaptors rely on.Recognised
modelvalues¶resnet18,resnet50Pretrained torchvision ResNet with a bias-free
fc.resnet_loadedA pickled model given by
config.base_model, whose innerfcis replaced.dino_v2/dino_v2_small/dino_v2_baseHugging Face DINOv2 large/small/base plus its image processor.
dino_v3/dino_v3_small/dino_v3_base/dino_v3:<repo>DINOv3 from Hugging Face, falling back to the public timm weights when the repo is gated (
self.is_timmrecords which path was taken).open_clip:<model>:<pretrained>OpenCLIP image tower; the normalisation stats and input size are read back out of the generated preprocessing transform.
PLIP,UNIPathology foundation models.
vit_b_16torchvision ViT; the positional embedding is truncated or re-inited when
input_sizediffers from 224.
- param model:
Backbone selector, see above.
- type model:
- param num_classes:
Number of output logits of the classification head.
- type num_classes:
- param input_size:
Square input resolution; only used to adapt the ViT positional embedding.
- type input_size:
int, optional- param config:
Predictor config; only
config.base_modelis read, forresnet_loaded.- type config:
object, optional- raises ValueError:
If
modelis not one of the supported names.
Methods
Embed a batch of images with the backbone, without the head.
Classify a batch, optionally also returning the latent code.
Return the final linear layer, i.e. the classification head.
- __init__(model, num_classes, input_size=None, config=None)[source]¶
Build the selected backbone and attach the classification head.
- feature_extractor(x)[source]¶
Embed a batch of images with the backbone, without the head.
The path taken depends on
self.model_type:ResNets (and models predating the
model_typeattribute): every child module but the last is re-assembled into aSequential, so the result still carries the trailing spatial dimensions(B, C, 1, 1).DINOv2/DINOv3: the batch is resized to the processor’s crop size (224 for the timm fallback), normalised with the processor’s
image_mean/image_stdand reduced to the CLS token.OpenCLIP and PLIP: resized to
self.clip_size, normalised with the CLIP stats and passed through the image tower.UNI: the timm transform is applied and the model called directly.
otherwise (torchvision ViT): patch embedding, CLS token, encoder, CLS output.
- Parameters:
x (
torch.Tensor) – Images of shape(B, C, H, W)in[0, 1].- Returns:
Latent codes,
(B, D)for the transformer backbones and(B, D, 1, 1)for the ResNets.- Return type:
- get_last_layer()[source]¶
Return the final linear layer, i.e. the classification head.
This is
self.model.fcfor the ResNets and the separateself.fcfor the DINO, OpenCLIP and UNI backbones. Adaptors such as CLARC and the projection adaptor edit the weights of this layer.- Returns:
Nonefor backbones whose head is not exposed this way (for example the torchvision ViT).- Return type:
- forward(x, return_latents=False)[source]¶
Classify a batch, optionally also returning the latent code.
- Parameters:
x (
torch.Tensor) – Images of shape(B, C, H, W).return_latents (
bool, optional) – Return(latent_code, logits)instead of just the logits. For the ResNets the latent code is squeezed to(B, D); for the torchvision ViT branch the logits are returned twice.
- Returns:
Logits of shape
(B, num_classes), or the pair described above.- Return type:
torch.Tensorortupleoftorch.Tensor