When you test image classification models, you spend time before comparison begins if each architecture uses a different implementation and weight-loading method. timm is a library that lets you create and use hundreds of image classification models through a shared API. I installed it and tested whether a pretrained model could run inference with no network connection.
timm lets you work with many image classification architectures, including ResNet, MobileNet, and Vision Transformer, through the shared create_model interface. The project publishes its source under the Apache 2.0 License (*1).
1. Install timm and fetch the model
Fetch the pretrained weights for the lightweight mobilenetv3_small_100 model during the build, with network access enabled.
FROM docker.io/library/python:3.12.13-slim
RUN pip install --no-cache-dir --quiet 'torch==2.13.0' 'torchvision==0.28.0' --index-url https://download.pytorch.org/whl/cpu
RUN pip install --no-cache-dir --quiet 'timm==1.0.28'
COPY warmup_model.py /tmp/warmup_model.py
RUN python /tmp/warmup_model.py
WORKDIR /work
podman build -t timm-sandbox .
2. Run inference on a synthetic image
I used a simple synthetic image for the test. I generated it in the same run and passed it to the model with no network connection.
import torch
from PIL import Image, ImageDraw
import timm
model = timm.create_model("mobilenetv3_small_100", pretrained=True)
model.eval()
config = timm.data.resolve_data_config({}, model=model)
transform = timm.data.create_transform(**config)
img = Image.new("RGB", (224, 224), color=(90, 160, 230))
draw = ImageDraw.Draw(img)
draw.rectangle((0, 160, 224, 224), fill=(60, 140, 60))
draw.ellipse((150, 20, 200, 70), fill=(250, 210, 60))
input_tensor = transform(img).unsqueeze(0)
with torch.no_grad():
output = model(input_tensor)
probs = torch.nn.functional.softmax(output[0], dim=0)
top5_prob, top5_idx = torch.topk(probs, 5)
for prob, idx in zip(top5_prob.tolist(), top5_idx.tolist()):
print(f"class {idx:4d} {prob*100:.2f}%")
podman run --rm --network none --memory 2g --cpus 2 --pids-limit 512 \
-v "$(pwd)":/work -w /work timm-sandbox \
python classify_demo.py
The -v option lets the container read from and write to the specified directory. Run this command in an empty directory reserved for the experiment.
The run produced the following output.
parameters: 2,542,856
top-5 ImageNet class indices and probabilities:
class 722 33.15%
class 852 9.72%
class 417 2.36%
class 714 1.64%
class 107 1.49%
The lightweight model has about 2.54 million parameters. A single-line call to the shared API's create_model invoked it, and the model completed inference without network access. The output contains index numbers for ImageNet's 1,000 classes. You can use a published mapping table to match them with class names. This experiment passed simple synthetic shapes to the model, so it did not evaluate the semantic plausibility of the top-ranked classes.
Use one calling convention across models
When each architecture needs its own loading method and preprocessing, model comparison takes more work. A shared interface such as timm lets you handle many models through the same API. You can separate model selection from execution.
Sources
*1 timm (pytorch-image-models, GitHub), https://github.com/huggingface/pytorch-image-models, Apache 2.0 License