Skip to main content

Overview

The backbones module provides access to a wide variety of pre-trained neural network architectures for feature extraction in PatchCore. These backbones are loaded with ImageNet pre-trained weights.

Functions

load

def load(name: str) -> torch.nn.Module
Loads a pre-trained backbone network by name.
name
str
required
Name of the backbone architecture to load. See Available Backbones for the complete list.

Returns

backbone
torch.nn.Module
Pre-trained PyTorch model loaded with ImageNet weights.

Example

import patchcore.backbones

# Load WideResNet50 backbone
backbone = patchcore.backbones.load("wideresnet50")

# Load ResNet50 backbone
backbone = patchcore.backbones.load("resnet50")

# Load Vision Transformer
backbone = patchcore.backbones.load("vit_base")

Available Backbones

The following pre-trained backbones are available:

ResNet Family

resnet50
str
ResNet-50 architecture from torchvision
resnet101
str
ResNet-101 architecture from torchvision
resnet200
str
ResNet-200 architecture from timm
resnext101
str
ResNeXt-101 (32x8d) architecture from torchvision
resnest50
str
ResNeSt-50 (4s2x40d) architecture from timm

Wide ResNet Family

wideresnet50
str
Wide ResNet-50-2 architecture from torchvision. Recommended for PatchCore.
wideresnet101
str
Wide ResNet-101-2 architecture from torchvision

ResNetV2 (BiT) Family

resnetv2_50_bit
str
ResNetV2-50x3 trained with Big Transfer (BiT) on ImageNet-1k
resnetv2_50_21k
str
ResNetV2-50x3 trained with BiT on ImageNet-21k
resnetv2_101_bit
str
ResNetV2-101x3 trained with BiT on ImageNet-1k
resnetv2_101_21k
str
ResNetV2-101x3 trained with BiT on ImageNet-21k
resnetv2_101
str
ResNetV2-101 standard architecture
resnetv2_152_bit
str
ResNetV2-152x4 trained with BiT on ImageNet-1k
resnetv2_152_21k
str
ResNetV2-152x4 trained with BiT on ImageNet-21k
resnetv2_152_384
str
ResNetV2-152x2 teacher model with 384x384 input resolution

VGG Family

vgg11
str
VGG-11 architecture from torchvision
vgg19
str
VGG-19 architecture from torchvision
vgg19_bn
str
VGG-19 with batch normalization from torchvision

AlexNet

alexnet
str
AlexNet architecture from torchvision

DenseNet Family

densenet121
str
DenseNet-121 architecture from timm
densenet201
str
DenseNet-201 architecture from timm

EfficientNet Family

efficientnet_b1
str
EfficientNet-B1 (TensorFlow weights) from timm
efficientnet_b3
str
EfficientNet-B3 (TensorFlow weights) from timm
efficientnet_b3a
str
EfficientNet-B3a architecture from timm
efficientnet_b5
str
EfficientNet-B5 (TensorFlow weights) from timm
efficientnet_b7
str
EfficientNet-B7 (TensorFlow weights) from timm
efficientnetv2_m
str
EfficientNetV2-M (TensorFlow weights) from timm
efficientnetv2_l
str
EfficientNetV2-L (TensorFlow weights) from timm

MNASNet Family

mnasnet_100
str
MNASNet 1.0 architecture from timm
mnasnet_a1
str
MNASNet-A1 architecture from timm
mnasnet_b1
str
MNASNet-B1 architecture from timm

Vision Transformer (ViT) Family

vit_small
str
Vision Transformer Small with 16x16 patches (224x224 input)
vit_base
str
Vision Transformer Base with 16x16 patches (224x224 input)
vit_large
str
Vision Transformer Large with 16x16 patches (224x224 input)
vit_r50
str
Vision Transformer Large with ResNet-50 hybrid backbone

DeiT Family

vit_deit_base
str
Data-efficient Image Transformer (DeiT) Base with 16x16 patches
vit_deit_distilled
str
DeiT Base with distillation token (16x16 patches)

Swin Transformer Family

vit_swin_base
str
Swin Transformer Base with 4x4 patches and 7x7 window size
vit_swin_large
str
Swin Transformer Large with 4x4 patches and 7x7 window size

Inception

inception_v4
str
Inception-V4 architecture from timm

BN-Inception

bninception
str
Batch Normalized Inception trained on ImageNet (requires pretrainedmodels package)

Usage with PatchCore

import torch
import patchcore.backbones
from patchcore.patchcore import PatchCore

# Initialize device
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")

# Load backbone
backbone = patchcore.backbones.load("wideresnet50")

# Create PatchCore model
model = PatchCore(device)
model.load(
    backbone=backbone,
    layers_to_extract_from=["layer2", "layer3"],
    device=device,
    input_shape=(3, 224, 224),
    pretrain_embed_dimension=1024,
    target_embed_dimension=1024
)
Recommended Backbone: The original PatchCore paper uses wideresnet50 with features extracted from layer2 and layer3, which provides excellent performance for most industrial anomaly detection tasks.

Backbone Registry

The complete mapping of backbone names to their implementations is stored in the _BACKBONES dictionary:
_BACKBONES = {
    "alexnet": "models.alexnet(pretrained=True)",
    "resnet50": "models.resnet50(pretrained=True)",
    "wideresnet50": "models.wide_resnet50_2(pretrained=True)",
    "vit_base": 'timm.create_model("vit_base_patch16_224", pretrained=True)',
    # ... and many more
}

Dependencies

  • torchvision: For ResNet, VGG, AlexNet, and Wide ResNet models
  • timm: For Vision Transformers, EfficientNets, and many other architectures
  • pretrainedmodels (optional): For BN-Inception
All backbones are loaded with pre-trained weights. The pretrained=True parameter ensures that ImageNet weights are used, which is crucial for transfer learning in anomaly detection.

Build docs developers (and LLMs) love