Package: hiera-transformer
Hiera: A Hierarchical Vision Transformer without the Bells-and-WhistlesThis is the official implementation for our ICML 2023 Oral paper:Hiera: A Hierarchical Vision Transformer without the Bells-and-WhistlesChaitanya Ryali*,Yuan-Ting Hu*,Daniel Bolya*,Chen Wei,Haoqi Fan,Po-Yao Huang,Vaibhav Aggarwal,Arkabandhu Chowdhury,Omid Poursaeed,Judy Hoffman,Jitendra Malik,Yanghao Li*,Christoph Feichtenhofer*ICML '23 Oral|GitHub|arXiv|BibTeX*: Equal contribution.What is Hiera?Hierais ahierarchicalvision transformer that is fast, powerful, and, above all,simple. It outperforms the state-of-the-art across a wide array of image and video taskswhile being much faster.How does it work?Vision transformers likeViTuse 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 likeResNetaccounted for this by using fewer features at the start and less spatial resolution at the end.Several domain specific vision transformers have been introduced that employ this hierarchical design, such asSwinorMViT. 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 modelssloweroverall.We show that a lot of this bulk is actuallyunnecessary. Instead of manually adding spatial bases through architectural changes, we opt toteachthe model these biases instead. By training withMAE, we can simplify or removeallof these bulky modules in existing transformers andincrease accuracyin 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.News[2023.06.12]Added more in1k models and some video examples, see inference.ipynb (v0.1.1).[2023.06.01]Initial release.See thechangelogfor more details.InstallationHiera requires a reasonably recent version oftorch.
After that, you can install hiera throughpip:pipinstallhiera-transformerThis reposhouldsupport the latest timm version, but timm is a constantly updating package. Create an issue if you have problems with a newer version of timm.Installing from SourceIf usingtorch hub, you don't need to install thehierapackage. But, if you'd like to develop using hiera, it could be a good idea to install it from source:gitclonehttps://github.com/facebookresearch/hiera.gitcdhiera
pythonsetup.pybuilddevelopModel ZooHere we provide model checkpoints for Hiera. Each model listed is accessible ontorch hubeven without thehiera-transformerpackage installed, e.g. the following initializes a base model pretrained and finetuned on ImageNet-1k:model=torch.hub.load("facebookresearch/hiera",model="hiera_base_224",pretrained=True,checkpoint="mae_in1k_ft_in1k")If you want a model with MAE pretraining only, you can replace the checkpoint with"mae_in1k". Additionally, if you'd like to load the MAE decoder as well (e.g., to continue pretraining), addmae_the the start of the model name, e.g.:model=torch.hub.load("facebookresearch/hiera",model="mae_hiera_base_224",pretrained=True,checkpoint="mae_in1k")Note:Our MAE models were trained with anormalized pixel loss. That means that the patches were normalized before the network had to predict them. If you want to visualize the predictions, you'll have to unnormalize them using the visible patches (which might work but wouldn't be perfect) or unnormalize them using the ground truth. For model more names and corresponding checkpoint names see below.Note:the speeds listed here were benchmarkedwithoutPyTorch's optimizedscaled dot product attention. If using PyTorch 2.0 or above, your inference speed will probably be faster than what's listed here.Image ModelsModelModel NamePretrained Models(IN-1K MAE)Finetuned Models(IN-1K Supervised)IN-1KTop-1 (%)A100 fp16Speed (im/s)Hiera-Thiera_tiny_224mae_in1kmae_in1k_ft_in1k82.82758Hiera-Shiera_small_224mae_in1kmae_in1k_ft_in1k83.82211Hiera-Bhiera_base_224mae_in1kmae_in1k_ft_in1k84.51556Hiera-B+hiera_base_plus_224mae_in1kmae_in1k_ft_in1k85.21247Hiera-Lhiera_large_224mae_in1kmae_in1k_ft_in1k86.1531Hiera-Hhiera_huge_224mae_in1kmae_in1k_ft_in1k86.9274Each model inputs a 224x224 image.Video ModelsModelModel NamePretrained Models(K400 MAE)Finetuned Models(K400)K400 (3x5 views)Top-1 (%)A100 fp16Speed (clip/s)Hiera-Bhiera_base_16x224mae_k400mae_k400_ft_k40084.0133.6Hiera-B+hiera_base_plus_16x224mae_k400mae_k400_ft_k40085.084.1Hiera-Lhiera_large_16x224mae_k400mae_k400_ft_k40087.340.8Hiera-Hhiera_huge_16x224mae_k400mae_k400_ft_k40087.820.9Each model inputs 16 224x224 frames with a temporal stride of 4.UsageThis repo implements the code to run Hiera models for inference. This repository is still in progress. Here's what we currently have available and what we have planned:Image InferenceMAE implementationVideo InferenceMAE implementationFull Model ZooTraining scriptsSeeexamplesfor examples of how to use Hiera.InferenceSeeexamples/inferencefor an example of how to prepare the data for inference.Instantiate a model with eithertorch hubor byinstalling hieraand running:importhieramodel=hiera.hiera_base_224(pretrained=True,checkpoint="mae_in1k_ft_in1k")Then you can run inference like any other model:output=model(x)Video inference works the same way, just use a16x224model instead.Note: for efficiency, Hiera re-orders its tokens at the start of the network (see theRollandUnrollmodules inhiera_utils.py). Thus, tokensaren't in spatial orderby default. If you'd like to use intermediate feature maps for a downstream task, pass thereturn_intermediatesflag when running the model:output,intermediates=model(x,return_intermediates=True)MAE InferenceBy 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:importhieramodel=hiera.mae_hiera_base_224(pretrained=True,checkpoint="mae_in1k")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 thedeleted tokensonly. The returned mask will beTrueif the token is visible andFalseif it's deleted. You can change the masking ratio by passing it during inference:loss,preds,labels,mask=model(x,mask_ratio=0.6)The default mask ratio is0.6for images, but you should pass in0.9for video. See the paper for details.Note:We usenormalized pixel targetsfor 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. Seeget_pixel_label_2dinhiera_mae.pyfor details.BenchmarkingWe provide a script for easy benchmarking. Seeexamples/benchmarkto see how to use it.Scaled Dot Product AttentionPyTorch 2.0 introduced optimizedscaled dot product attention, which can speed up transformers quite a bit. We didn't use this in our original benchmarking, but since it's a free speed-up this repo will automatically use it if available. To get its benefits, make sure your torch version is 2.0 or above.TrainingComing soon.CitationIf you use Hiera or this code in your work, please cite:@article{ryali2023hiera,
  title={Hiera: A Hierarchical Vision Transformer without the Bells-and-Whistles},
  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},
  journal={ICML},
  year={2023}
}LicenseThis work is licensed under aCreative Commons Attribution-NonCommercial 4.0 International License.ContributingSeecontributingand thecode of conduct.