🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add docs for MobileViT (#1430)
Justin Wu55aef61dbbaa
diff --git a/README.md b/README.mdindex e2e5bab81..f672c8012 100644--- a/README.md+++ b/README.md@@ -116,7 +116,7 @@ Run our Jupyter notebooks [tutorials](https://kornia-tutorials.readthedocs.io/enIf you are using kornia in your research-related documents, it is recommended that you cite the paper. See more in [CITATION](https://github.com/kornia/kornia/blob/master/CITATION.md).- ```bash+ ```bibtex@inproceedings{eriba2019kornia,author = {E. Riba, D. Mishkin, D. Ponsa, E. Rublee and G. Bradski},title = {Kornia: an Open Source Differentiable Computer Vision Library for PyTorch},diff --git a/README_zh-CN.md b/README_zh-CN.mdindex 563c0f08f..e680efb3e 100644--- a/README_zh-CN.md+++ b/README_zh-CN.md@@ -112,7 +112,7 @@如果您在与研究相关的文档中使用 Kornia,您可以引用我们的论文。更多信息可以在 [CITATION](https://github.com/kornia/kornia/blob/master/CITATION.md) 看到。- ```bash+ ```bibtex@inproceedings{eriba2019kornia,author = {E. Riba, D. Mishkin, D. Ponsa, E. Rublee and G. Bradski},title = {Kornia: an Open Source Differentiable Computer Vision Library for PyTorch},diff --git a/docs/source/index.rst b/docs/source/index.rstindex 3c2ac1431..999e5a82d 100644--- a/docs/source/index.rst+++ b/docs/source/index.rst@@ -67,6 +67,7 @@ Join the community:hidden:models/vit+ models/vit_mobilemodels/loftrmodels/defmomodels/hardnetdiff --git a/docs/source/models/vit_mobile.rst b/docs/source/models/vit_mobile.rstnew file mode 100644index 000000000..4bab978a2--- /dev/null+++ b/docs/source/models/vit_mobile.rst@@ -0,0 +1,55 @@+.. _kornia_vit_mobile:++MobileViT+.........++.. card::+ :link: https://arxiv.org/abs/2110.02178++ **MobileViT: Light-weight, General-purpose, and Mobile-friendly Vision Transformer**+ ^^^+ **Abstract:** Light-weight convolutional neural networks (CNNs) are the de-facto for mobile vision tasks. Their spatial inductive biases allow them to learn representations with fewer parameters across different vision tasks. However, these networks are spatially local. To learn global representations, self-attention-based vision trans-formers (ViTs) have been adopted. Unlike CNNs, ViTs are heavy-weight. In this paper, we ask the following question: is it possible to combine the strengths of CNNs and ViTs to build a light-weight and low latency network for mobile vision tasks? Towards this end, we introduce MobileViT, a light-weight and general-purpose vision transformer for mobile devices. MobileViT presents a different perspective for the global processing of information with transformers, i.e., transformers as convolutions. Our results show that MobileViT significantly outperforms CNN- and ViT-based networks across different tasks and datasets. On the ImageNet-1k dataset, MobileViT achieves top-1 accuracy of 78.4% with about 6 million parameters, which is 3.2% and 6.2% more accurate than MobileNetv3 (CNN-based) and DeIT (ViT-based) for a similar number of parameters. On the MS-COCO object detection task, MobileViT is 5.7% more accurate than Mo-bileNetv3 for a similar number of parameters.++ **Tasks:** Image Classification, Object Detection, Semantic Segmentation++ **Datasets:** ImageNet, MS-COCO, PASCAL VOC++ ++++ **Authors:** Sachin Mehta, Mohammad Rastegari++.. image:: https://user-images.githubusercontent.com/67839539/136470152-2573529e-1a24-4494-821d-70eb4647a51d.png+ :align: center+++Kornia-MobileViT+----------------++We provide :py:class:`~kornia.contrib.MobileViT` which can be used for many downstream tasks, e.g., classification, object detection and semantic segmentation.+One can use the *MobileViT* in Kornia as follows:++.. code:: python++ img = torch.rand(1, 3, 256, 256)+ mvit = MobileViT(mode='xxs')+ out = mvit(img)+++Usage+~~~~~++Similar to ``Kornia-ViT``, ``Kornia-MobileViT`` does not include any classification head. But you can add it simply by doing:++.. code:: python++ import torch.nn as nn+ import kornia.contrib as K++ classifier = nn.Sequential(+ K.MobileViT(mode='xxs'),+ nn.AvgPool2d(256 // 32, 1),+ nn.Flatten(),+ nn.Linear(320, 1000)+ )++ img = torch.rand(1, 3, 256, 256)+ out = classifier(img) # 1x1000diff --git a/kornia/contrib/vit_mobile.py b/kornia/contrib/vit_mobile.pyindex b6dc80b79..7092ed444 100644--- a/kornia/contrib/vit_mobile.py+++ b/kornia/contrib/vit_mobile.py@@ -11,7 +11,7 @@ class SiLU(nn.Module):Paper: https://arxiv.org/abs/1702.03118"""- def forward(self, x):+ def forward(self, x: torch.Tensor) -> torch.Tensor:return x * torch.sigmoid(x)@@ -37,7 +37,7 @@ class PreNorm(nn.Module):self.norm = nn.LayerNorm(dim)self.fn = fn- def forward(self, x: torch.Tensor, **kwargs) -> nn.Module:+ def forward(self, x: torch.Tensor, **kwargs) -> torch.Tensor:return self.fn(self.norm(x), **kwargs)