merve HF staff commited on
Commit
4be2bdd
1 Parent(s): 5fb5e3c

Create README.md

Browse files
Files changed (1) hide show
  1. README.md +91 -0
README.md ADDED
@@ -0,0 +1,91 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ---
2
+ license: apache-2.0
3
+ pipeline_tag: image-classification
4
+ ---
5
+ # Hiera Base MAE Trained on IN1K fine-tuned on IN1K
6
+
7
+
8
+ **Hiera** is a _hierarchical_ vision transformer that is fast, powerful, and, above all, _simple_. It outperforms the state-of-the-art across a wide array of image and video tasks _while being much faster_.
9
+
10
+ <p align="center">
11
+ <img src="https://github.com/facebookresearch/hiera/raw/main/examples/img/inference_speed.png" width="75%">
12
+ </p>
13
+
14
+ ## How does it work?
15
+ ![A diagram of Hiera's architecture.](https://github.com/facebookresearch/hiera/raw/main/examples/img/hiera_arch.png)
16
+
17
+ Vision transformers like [ViT](https://arxiv.org/abs/2010.11929) use the same spatial resolution and number of features throughout the whole network. But this is inefficient: the early layers don't need that many features, and the later layers don't need that much spatial resolution. Prior hierarchical models like [ResNet](https://arxiv.org/abs/1512.03385) accounted for this by using fewer features at the start and less spatial resolution at the end.
18
+
19
+ Several domain specific vision transformers have been introduced that employ this hierarchical design, such as [Swin](https://arxiv.org/abs/2103.14030) or [MViT](https://arxiv.org/abs/2104.11227). But in the pursuit of state-of-the-art results using fully supervised training on ImageNet-1K, these models have become more and more complicated as they add specialized modules to make up for spatial biases that ViTs lack. While these changes produce effective models with attractive FLOP counts, under the hood the added complexity makes these models _slower_ overall.
20
+
21
+ We show that a lot of this bulk is actually _unnecessary_. Instead of manually adding spatial bases through architectural changes, we opt to _teach_ the model these biases instead. By training with [MAE](https://arxiv.org/abs/2111.06377), we can simplify or remove _all_ of these bulky modules in existing transformers and _increase accuracy_ in the process. The result is Hiera, an extremely efficient and simple architecture that outperforms the state-of-the-art in several image and video recognition tasks.
22
+
23
+ ## Installation
24
+
25
+ Hiera requires a reasonably recent version of [torch](https://pytorch.org/get-started/locally/).
26
+ After that, you can install hiera through [pip](https://pypi.org/project/hiera-transformer/):
27
+ ```bash
28
+ pip install hiera-transformer
29
+ ```
30
+ This repo _should_ support the latest timm version, but timm is a constantly updating package. Create an issue if you have problems with a newer version of timm.
31
+ With the `hiera-transformer` and `huggingface-hub` packages installed, you can simply run, e.g.,
32
+ ```py
33
+ from hiera import Hiera
34
+ model = Hiera.from_pretrained("facebook/hiera_base_224.mae_in1k_ft_in1k") # mae pt then in1k ft'd model
35
+ model = Hiera.from_pretrained("facebook/hiera_base_224.mae_in1k") # just mae pt, no ft
36
+ ```
37
+ to load a model. Use `<model_name>.<checkpoint_name>` from model zoo below.
38
+
39
+ If you want to save a model, use `model.config` as the config, e.g.,
40
+ ```py
41
+ model.save_pretrained("hiera-base-224", config=model.config)
42
+ ```
43
+
44
+
45
+
46
+ ### Inference
47
+
48
+ ```py
49
+ import hiera
50
+ model = hiera.hiera_base_224(pretrained=True, checkpoint="mae_in1k_ft_in1k")
51
+ ```
52
+ Then you can run inference like any other model:
53
+ ```py
54
+ output = model(x)
55
+ ```
56
+ Video inference works the same way, just use a `16x224` model instead.
57
+
58
+ **Note**: for efficiency, Hiera re-orders its tokens at the start of the network (see the `Roll` and `Unroll` modules in `hiera_utils.py`). Thus, tokens _aren't in spatial order_ by default. If you'd like to use intermediate feature maps for a downstream task, pass the `return_intermediates` flag when running the model:
59
+ ```py
60
+ output, intermediates = model(x, return_intermediates=True)
61
+ ```
62
+
63
+ #### MAE Inference
64
+ By default, the models do not include the MAE decoder. If you would like to use the decoder or compute MAE loss, you can instantiate an mae version by running:
65
+ ```py
66
+ import hiera
67
+ model = hiera.mae_hiera_base_224(pretrained=True, checkpoint="mae_in1k")
68
+ ```
69
+ Then when you run inference on the model, it will return a 4-tuple of `(loss, predictions, labels, mask)` where predictions and labels are for the _deleted tokens_ only. The returned mask will be `True` if the token is visible and `False` if it's deleted. You can change the masking ratio by passing it during inference:
70
+ ```py
71
+ loss, preds, labels, mask = model(x, mask_ratio=0.6)
72
+ ```
73
+ The default mask ratio is `0.6` for images, but you should pass in `0.9` for video. See the paper for details.
74
+
75
+ **Note:** We use _normalized pixel targets_ for MAE pretraining, meaning the patches are each individually normalized before the model model has to predict them. Thus, you have to unnormalize them using the ground truth before visualizing them. See `get_pixel_label_2d` in `hiera_mae.py` for details.
76
+
77
+
78
+
79
+ ## Citation
80
+ If you use Hiera or this code in your work, please cite:
81
+ ```
82
+ @article{ryali2023hiera,
83
+ title={Hiera: A Hierarchical Vision Transformer without the Bells-and-Whistles},
84
+ author={Ryali, Chaitanya and Hu, Yuan-Ting and Bolya, Daniel and Wei, Chen and Fan, Haoqi and Huang, Po-Yao and Aggarwal, Vaibhav and Chowdhury, Arkabandhu and Poursaeed, Omid and Hoffman, Judy and Malik, Jitendra and Li, Yanghao and Feichtenhofer, Christoph},
85
+ journal={ICML},
86
+ year={2023}
87
+ }
88
+ ```
89
+
90
+ [arxiv-link]: https://arxiv.org/abs/2306.00989/
91
+ [icml-link]: https://icml.cc/Conferences/2023