模型:

timm/lambda_resnet26t.c1_in1k

英文

lambda_resnet26t.c1_in1k 模型卡片

一个基于 ResNet 架构的 LambdaNet 图像分类模型。由 Ross Wightman 在 timm 中使用 ImageNet-1k 数据集进行训练。

注意:该模型没有遵循任何特定论文的配置,而是经过调整以获得合理的训练时间和减少自注意力块的频率。

使用的配方细节:

  • 基于 ResNet Strikes Back 个 C 特征
  • 使用带 Nesterov 的 SGD 优化器和 AGC(自适应梯度剪切)
  • 使用余弦学习率表和预热

该模型架构使用 timm 的灵活 BYOBNet (Bring-Your-Own-Blocks Network) 实现。

BYOB(具有 BYOANet 注意力特定块)允许配置:

  • 块/阶段布局
  • 块类型交错
  • 干线布局
  • 输出步幅(膨胀)
  • 激活和规范化层
  • 通道和空间/自注意力层

...同时还包括 timm 的许多其他架构常见特征,包括:

  • 随机深度
  • 梯度检查点
  • 逐层学习率衰减
  • 阶段特征提取

模型详细信息

  • 模型类型:图像分类 / 特征骨干
  • 模型统计:
    • 参数(百万):11.0
    • GMACs:3.0
    • 激活数(百万):11.9
    • 图像尺寸:256 x 256
  • 论文:
  • 数据集:ImageNet-1k

模型用途

图像分类

from urllib.request import urlopen
from PIL import Image
import timm

img = Image.open(urlopen(
    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))

model = timm.create_model('lambda_resnet26t.c1_in1k', pretrained=True)
model = model.eval()

# get model specific transforms (normalization, resize)
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)

output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1

top5_probabilities, top5_class_indices = torch.topk(output.softmax(dim=1) * 100, k=5)

特征图提取

from urllib.request import urlopen
from PIL import Image
import timm

img = Image.open(urlopen(
    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))

model = timm.create_model(
    'lambda_resnet26t.c1_in1k',
    pretrained=True,
    features_only=True,
)
model = model.eval()

# get model specific transforms (normalization, resize)
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)

output = model(transforms(img).unsqueeze(0))  # unsqueeze single image into batch of 1

for o in output:
    # print shape of each feature map in output
    # e.g.:
    #  torch.Size([1, 64, 128, 128])
    #  torch.Size([1, 256, 64, 64])
    #  torch.Size([1, 512, 32, 32])
    #  torch.Size([1, 1024, 16, 16])
    #  torch.Size([1, 2048, 8, 8])

    print(o.shape)

图像嵌入

from urllib.request import urlopen
from PIL import Image
import timm

img = Image.open(urlopen(
    'https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/beignets-task-guide.png'
))

model = timm.create_model(
    'lambda_resnet26t.c1_in1k',
    pretrained=True,
    num_classes=0,  # remove classifier nn.Linear
)
model = model.eval()

# get model specific transforms (normalization, resize)
data_config = timm.data.resolve_model_data_config(model)
transforms = timm.data.create_transform(**data_config, is_training=False)

output = model(transforms(img).unsqueeze(0))  # output is (batch_size, num_features) shaped tensor

# or equivalently (without needing to set num_classes=0)

output = model.forward_features(transforms(img).unsqueeze(0))
# output is unpooled, a (1, 2048, 8, 8) shaped tensor

output = model.forward_head(output, pre_logits=True)
# output is a (1, num_features) shaped tensor

模型比较

在 timm 的 model results 中探索该模型的数据集和运行时指标。

引用

@misc{rw2019timm,
  author = {Ross Wightman},
  title = {PyTorch Image Models},
  year = {2019},
  publisher = {GitHub},
  journal = {GitHub repository},
  doi = {10.5281/zenodo.4414861},
  howpublished = {\url{https://github.com/huggingface/pytorch-image-models}}
}
@article{Bello2021LambdaNetworksML,
  title={LambdaNetworks: Modeling Long-Range Interactions Without Attention},
  author={Irwan Bello},
  journal={ArXiv},
  year={2021},
  volume={abs/2102.08602}
}
@inproceedings{wightman2021resnet,
  title={ResNet strikes back: An improved training procedure in timm},
  author={Wightman, Ross and Touvron, Hugo and Jegou, Herve},
  booktitle={NeurIPS 2021 Workshop on ImageNet: Past, Present, and Future}
}