pip install timm
Or for an editable install,
git clone https://github.com/rwightman/pytorch-image-models
cd pytorch-image-models && pip install -e .
import timm
import torch
model = timm.create_model('resnet34')
x = torch.randn(1, 3, 224, 224)
model(x).shape
It is that simple to create a model using timm
. The create_model
function is a factory method that can be used to create over 300 models that are part of the timm
library.
To create a pretrained model, simply pass in pretrained=True
.
pretrained_resnet_34 = timm.create_model('resnet34', pretrained=True)
To create a model with a custom number of classes, simply pass in num_classes=<number_of_classes>
.
import timm
import torch
model = timm.create_model('resnet34', num_classes=10)
x = torch.randn(1, 3, 224, 224)
model(x).shape
timm.list_models()
returns a complete list of available models in timm
. To have a look at a complete list of pretrained models, pass in pretrained=True
in list_models
.
avail_pretrained_models = timm.list_models(pretrained=True)
len(avail_pretrained_models), avail_pretrained_models[:5]
There are a total of 271 models with pretrained weights currently available in timm
!
It is also possible to search for model architectures using Wildcard as below:
all_densenet_models = timm.list_models('*densenet*')
all_densenet_models
The fastai library has support for fine-tuning models from timm:
from fastai.vision.all import *
path = untar_data(URLs.PETS)/'images'
dls = ImageDataLoaders.from_name_func(
path, get_image_files(path), valid_pct=0.2,
label_func=lambda x: x[0].isupper(), item_tfms=Resize(224))
# if a string is passed into the model argument, it will now use timm (if it is installed)
learn = vision_learner(dls, 'vit_tiny_patch16_224', metrics=error_rate)
learn.fine_tune(1)