Documentation Index
Fetch the complete documentation index at: https://mintlify.com/mwalmsley/zoobot/llms.txt
Use this file to discover all available pages before exploring further.
define_model.py contains the fundamental building blocks used by all Zoobot models: a timm-based encoder factory, a Dirichlet head factory, the ZoobotTree LightningModule, and the GenericLightningModule base class. Most users interact with the higher-level FinetuneableZoobot classes rather than these functions directly, but the factories are useful when integrating Zoobot encoders into custom pipelines.
Import
get_pytorch_encoder
timm.create_model that sets num_classes=0 to return a global-pooled feature vector rather than class logits.
Parameters
Name of the timm architecture to instantiate. Must be present in
timm.list_models(). Common choices used in Zoobot include 'convnext_nano', 'efficientnet_b0', and 'resnet50'. Passing the legacy alias 'efficientnet' is accepted but deprecated — use 'efficientnet_b0' explicitly.Number of input channels. Use
3 for RGB images and 1 for greyscale. Passed to timm as in_chans.Additional keyword arguments forwarded directly to
timm.create_model. Examples include drop_path_rate=0.2 (stochastic depth for EfficientNet), pretrained=True (ImageNet weights), or any other timm model-level argument.Returns
torch.nn.Module — a timm model configured as a feature extractor. The forward pass returns a (batch, encoder_dim) feature tensor.
Example
get_pytorch_dirichlet_head
Sequential module contains:
- A
Dropoutlayer (orPermaDropoutwhentest_time_dropout=True) with probabilitydropout_rate. - A custom linear layer that maps the encoder output to
output_dimDirichlet concentrations (custom_top_dirichlet).
FinetuneableZoobotTree.
Parameters
Dimensionality of the encoder output — the input size expected by this head. Obtain via
get_encoder_dim(encoder).Number of output dimensions. Must equal the total number of answers in the decision tree (e.g.
34 for GZ DECaLS, or len(schema.label_cols) for a custom schema).If
True, uses PermaDropout so that dropout remains active at inference time, enabling Monte Carlo dropout uncertainty estimates. Set to False for deterministic predictions.Probability of an element being zeroed during dropout. See
torch.nn.Dropout docs.Returns
torch.nn.Sequential — a PyTorch module expecting a (batch, encoder_dim) tensor and returning (batch, output_dim) Dirichlet concentrations.
get_encoder_dim
channels=1.
Parameters
The encoder whose output dimension you want to discover.
Number of input channels to use in the test forward pass. Falls back to
1 automatically if a channel-mismatch RuntimeError is raised.Returns
int — the last dimension of the encoder’s output tensor (i.e. the feature vector length).
load_pretrained_zoobot
ZoobotTree or FinetuneableZoobotTree Lightning checkpoint. The rest of the module (head, loss, optimizer state) is discarded.
Internally, the function first tries to load the checkpoint as a ZoobotTree; if that raises a TypeError it falls back to FinetuneableZoobotTree. On CPU-only machines the function automatically sets map_location so GPU-trained checkpoints load correctly.
Most users do not need to call this directly.
FinetuneableZoobotAbstract.__init__ calls it automatically when you pass zoobot_checkpoint_loc=....Parameters
Path to the Lightning
.ckpt file. The checkpoint must have been saved from a module that exposes an .encoder attribute (i.e. ZoobotTree, FinetuneableZoobotClassifier, or FinetuneableZoobotTree).Returns
torch.nn.Module — the pretrained timm encoder extracted from the checkpoint.
Example
GenericLightningModule
GenericLightningModule is the abstract base class shared by all Zoobot LightningModules. It defines the common training loop structure (training_step, validation_step, test_step, predict_step) and metric-logging utilities. You should not instantiate it directly — use ZoobotTree or one of the FinetuneableZoobot subclasses.
Most users interact with
FinetuneableZoobotClassifier, FinetuneableZoobotRegressor, or FinetuneableZoobotTree rather than the factory functions on this page. The factories described here are most relevant when building custom architectures or integrating a Zoobot encoder into an external pipeline.