# HieraDet (SAM 2)

**类别：** ViT · **代码：** [`timm/models/hieradet_sam2.py`](https://github.com/huggingface/pytorch-image-models/blob/main/timm/models/hieradet_sam2.py) · **架构：** 5 · **预训练权重：** 8（0 个具有 ImageNet-1k 结果）

HieraDet，Segment Anything 2 (SAM 2) 中基于 Hiera 的图像编码器，采用窗口注意力机制和窗口位置嵌入。

## 论文

* [SAM 2：在图像和视频中对任意内容进行分割](https://arxiv.org/abs/2408.00714)
* [Hiera：无需花哨组件的层级视觉 Transformer](https://arxiv.org/abs/2306.00989)

## 用法

请参阅[快速入门](../quickstart)了解如何加载预训练权重、进行图像预处理和微调，以及参阅[特征提取](../feature_extraction)了解如何将这些模型用作骨干网络。

```py
>>> import timm
>>> model = timm.create_model('sam2_hiera_base_plus.fb_r896', pretrained=True)
>>> data_config = timm.data.resolve_model_data_config(model)
```

## 模型 API 支持

| 功能 | `HieraDet` |
|---|---|
| [`features_only=True` 特征图](../model_api#intermediate-features) | ✓（步长 4、8、16、32） |
| [`forward_intermediates()`](../model_api#intermediate-features) | ✓ |
| [`prune_intermediate_layers()`](../model_api#intermediate-features) | ✓ |
| [任意输入尺寸，无需更改](../model_api#input-size) | ✓ |
| [`set_input_size()`](../model_api#input-size) | – |
| [`dynamic_img_size=True`](../model_api#input-size) | – |
| [`set_grad_checkpointing()`](../model_api#training-hooks) | ✓ |
| [逐层学习率衰减（`group_matcher()`）](../model_api#training-hooks) | ✓ |
| [`reset_classifier()`](../model_api#classifier) | ✓ |

✓ 支持，✓ (square) 仅支持方形尺寸，✗ 不支持，– 无需支持（模型本身已接受任意输入尺寸）。通过在每个功能上运行随机初始化的模型（`HieraDet` 使用 `sam2_hiera_tiny`，尺寸为 256）进行验证，预期同一类的其他架构表现相同。详情请参阅 [Model API](../model_api) 页面。

## 架构

| 架构 | 参数（M） | GMAC | 输入大小 | 预训练权重 |
|---|---:|---:|---:|---:|
| `hieradet_small` | 34.73 | 8.51 | 256 | 0 |
| `sam2_hiera_base_plus` | 68.68 | 227.48 | 896 | 2 |
| `sam2_hiera_large` | 212.15 | 907.48 | 1024 | 2 |
| `sam2_hiera_small` | 33.95 | 123.99 | 896 | 2 |
| `sam2_hiera_tiny` | 26.85 | 99.86 | 896 | 2 |

GMACs 和参数量对应于每种架构的默认输入大小和分类器，数据来自[基准测试结果](https://github.com/huggingface/pytorch-image-models/tree/main/results)。

## 预训练权重

Top-1 是在所列评估规模下，来自 [`results-imagenet.csv`](https://github.com/huggingface/pytorch-image-models/blob/main/results/results-imagenet.csv) 的 ImageNet-1k 验证准确率，- 表示该权重没有 ImageNet-1k 结果（例如预训练权重或图文权重）。Classes 是预训练分类器的大小，对于没有分类器的权重则为无。

| 权重 | 输入大小 | Top-1（%） | 评估大小 | 类 | 许可证 | Hub |
|---|---:|---:|---:|---:|---|---|
| `sam2_hiera_base_plus.fb_r896` | 896 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_base_plus.fb_r896) |
| `sam2_hiera_base_plus.fb_r896_2pt1` | 896 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_base_plus.fb_r896_2pt1) |
| `sam2_hiera_large.fb_r1024` | 1024 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_large.fb_r1024) |
| `sam2_hiera_large.fb_r1024_2pt1` | 1024 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_large.fb_r1024_2pt1) |
| `sam2_hiera_small.fb_r896` | 896 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_small.fb_r896) |
| `sam2_hiera_small.fb_r896_2pt1` | 896 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_small.fb_r896_2pt1) |
| `sam2_hiera_tiny.fb_r896` | 896 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_tiny.fb_r896) |
| `sam2_hiera_tiny.fb_r896_2pt1` | 896 | - | - | none | apache-2.0 | [timm](https://huggingface.co/timm/sam2_hiera_tiny.fb_r896_2pt1) |

