timm
PyTorch Image Models — collection of SOTA vision models, pretrained weights, layers, optimizers, and training utilities by Ross Wightman. Current version is 1.0.15 (Mar 2026). Primary weight source is now Hugging Face Hub. Import path for layers changed: timm.models.layers → timm.layers.
Warnings
- breaking timm.models.layers module moved to timm.layers in 0.9.x. Direct module imports (import timm.models.layers.module) fail. Only top-level from timm.models.layers import name still works via deprecation shim — which will be removed.
- breaking Model naming changed to architecture.pretrained_tag format in 0.9+. Old names like resnet50_21k still work via deprecation remapping but new weight variants are only accessible via the new format (e.g. resnet50.a1_in1k).
- breaking Pretrained weights now loaded from Hugging Face Hub (https://huggingface.co/timm) not GitHub releases. Old GitHub release URLs hardcoded in custom code will 404.
- gotcha timm.create_model with num_classes=0 removes the classifier entirely and returns features. num_classes=None is NOT the same — it keeps the default head. Setting wrong num_classes silently produces wrong output shapes.
- gotcha Each model has its own expected preprocessing (mean, std, input size). Using generic ImageNet normalization values directly instead of model-specific config produces degraded accuracy.
- gotcha Not all model variants have pretrained weights — timm lists models without weights too. timm.create_model('some_model', pretrained=True) raises RuntimeError if no weights exist for that variant.
Install
-
pip install timm
Imports
- timm.layers
from timm.layers import PatchEmbed, Mlp, DropPath # or import timm.layers
- create_model
import timm # Load with pretrained weights model = timm.create_model('resnet50', pretrained=True) # Load specific weight variant using architecture.tag format model = timm.create_model('resnet50.a1_in1k', pretrained=True) # Custom num_classes for fine-tuning model = timm.create_model('efficientnet_b0', pretrained=True, num_classes=10) # Feature extraction (removes classifier) model = timm.create_model('resnet50', features_only=True, pretrained=True)
Quickstart
import timm
import torch
from PIL import Image
from timm.data import resolve_data_config, create_transform
# List available models
print(timm.list_models('resnet*')[:5])
# Load pretrained model
model = timm.create_model('efficientnet_b0.ra_in1k', pretrained=True)
model.eval()
# Get model-specific preprocessing
config = resolve_data_config({}, model=model)
transform = create_transform(**config)
# Inference
img = Image.open('image.jpg').convert('RGB')
tensor = transform(img).unsqueeze(0)
with torch.no_grad():
output = model(tensor) # [1, 1000] logits
probs = torch.softmax(output, dim=1)
top5 = torch.topk(probs, 5)
# Fine-tune with custom head
model = timm.create_model('resnet50', pretrained=True, num_classes=10)