π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
:ski: add documentation for image classification and segmentation (#1332)
Edgar Riba6aaa2f6a2a6a
diff --git a/docs/source/_static/css/main.css b/docs/source/_static/css/main.cssindex 073e067c9..92dc2a0cb 100644--- a/docs/source/_static/css/main.css+++ b/docs/source/_static/css/main.css@@ -6,3 +6,8 @@body{font-family: 'Montserrat', sans-serif;}++/* The section headers in the toc tree */+.sidebar-scroll p.caption{+ background-color: #4d59ff;+}diff --git a/docs/source/applications/image_augmentations.rst b/docs/source/applications/image_augmentations.rstindex a3b307e33..ba5734f98 100644--- a/docs/source/applications/image_augmentations.rst+++ b/docs/source/applications/image_augmentations.rst@@ -43,7 +43,7 @@ Best Practices 1: Image AugmentationKornia augmentations provides simple on-device augmentation framework with the support of various syntax sugars(e.g. return transformation matrix, inverse geometric transform). Therefore, we provide advanced augmentation-container :py:class:`~kornia.augmentation.AugmentationSequential` to ease the pain of building augmenation pipelines. This API would also provide predefined routines+container :py:class:`~kornia.augmentation.container.AugmentationSequential` to ease the pain of building augmenation pipelines. This API would also provide predefined routinesfor automating the processing of masks, bounding boxes, and keypoints... code-block:: python@@ -73,8 +73,8 @@ Best Practices 2: Video AugmentationVideo data is a special case of 3D volumetric data that contains both spatial and temporal information, which can be referred as 2.5D than 3D.In most applications, augmenting video data requires a static temporal dimension to have the same augmentations are performed for each frame.-Thus, :py:class:`~kornia.augmentation.VideoSequential` can be used to do such trick as same as `nn.Sequential`.-Currently, :py:class:`~kornia.augmentation.VideoSequential` supports data format like :math:`(B, C, T, H, W)` and :math:`(B, T, C, H, W)`.+Thus, :py:class:`~kornia.augmentation.container.VideoSequential` can be used to do such trick as same as `nn.Sequential`.+Currently, :py:class:`~kornia.augmentation.container.VideoSequential` supports data format like :math:`(B, C, T, H, W)` and :math:`(B, T, C, H, W)`... code-block:: pythondiff --git a/docs/source/applications/image_classification.rst b/docs/source/applications/image_classification.rstnew file mode 100644index 000000000..621652a73--- /dev/null+++ b/docs/source/applications/image_classification.rst@@ -0,0 +1,67 @@+Image Classification+====================++.. image:: https://production-media.paperswithcode.com/thumbnails/task/task-0000000951-52325f45_O0tAMly.jpg+ :align: right+ :width: 20%++Image Classification is a fundamental task that attempts to comprehend an entire image as a whole.+The goal is to classify the image by assigning it to a specific label. Typically, Image Classification refers to images+in which only one object appears and is analyzed. In contrast, object detection involves both classification and+localization tasks, and is used to analyze more realistic cases in which multiple objects may exist in an image.++Learn more: `https://paperswithcode.com/task/image-classification <https://paperswithcode.com/task/image-classification>`_++Inference+---------++Kornia provides a couple of backbones based on `transformers <https://paperswithcode.com/methods/category/vision-transformer>`_+to perform image classification. Checkout the following apis :py:class:`~kornia.contrib.VisionTransformer`,+:py:class:`~kornia.contrib.ClassificationHead` and combine as follows to customize your own classifier:++.. code:: python++ import torch.nn as nn+ import kornia.contrib as K++ classifier = nn.Sequential(+ K.VisionTransformer(image_size=224, patch_size=16),+ K.ClassificationHead(num_classes=1000)+ )++ img = torch.rand(1, 3, 224, 224)+ out = classifier(img) # BxN+ scores = out.argmax(-1) # B++.. tip::+ Read more about our :ref:`kornia_vit`++Finetuning+----------++In order to customize your model with your own data you can use our :ref:`training_api` to perform the+`fine-tuning <https://paperswithcode.com/methods/category/fine-tuning>`_ of your model.++We provide :py:class:`~kornia.x.ImageClassifierTrainer` with a default training structure to train basic+image classification problems. However, one can leverage this is API using the models provided by Kornia or+use existing libraries from the PyTorch ecosystem such as `torchvision <https://pytorch.org/vision/stable/models.html>`_+or `timm <https://rwightman.github.io/pytorch-image-models/>`_.++.. literalinclude:: ../../../examples/train/image_classifier/main.py+ :language: python+ :lines: 20-46++Define your augmentations and callbacks:++.. literalinclude:: ../../../examples/train/image_classifier/main.py+ :language: python+ :lines: 49-66++Finally, instantiate the :py:class:`~kornia.x.ImageClassifierTrainer` and execute your training pipeline.++.. literalinclude:: ../../../examples/train/image_classifier/main.py+ :language: python+ :lines: 68-74++.. seealso::+ Play with the full example `here <https://github.com/kornia/kornia/tree/master/examples/train/image_classifier>`_diff --git a/docs/source/applications/semantic_segmentation.rst b/docs/source/applications/semantic_segmentation.rstnew file mode 100644index 000000000..eb4fcbc71--- /dev/null+++ b/docs/source/applications/semantic_segmentation.rst@@ -0,0 +1,50 @@+Semantic segmentation+=====================++.. image:: https://production-media.paperswithcode.com/thumbnails/task/task-0000000885-bec5f079_K84qLCL.jpg+ :align: right+ :width: 40%++Semantic segmentation, or image segmentation, is the task of clustering parts of an image together which belong to the+same object class. It is a form of pixel-level prediction because each pixel in an image is classified according to a+category. Some example benchmarks for this task are Cityscapes, PASCAL VOC and ADE20K. Models are usually evaluated with+the Mean Intersection-Over-Union (Mean IoU) and Pixel Accuracy metrics.++Learn more: `https://paperswithcode.com/task/semantic-segmentation <https://paperswithcode.com/task/semantic-segmentation>`_++Finetuning+----------++In order to customize your model with your own data you can use our :ref:`training_api` to perform the+`fine-tuning <https://paperswithcode.com/methods/category/fine-tuning>`_ of your model.++We provide :py:class:`~kornia.x.SemanticSegmentationTrainer` with a default training structure to train semantic+segmentation problems. However, one can leverage this is API using the models provided by Kornia or+use existing libraries from the PyTorch ecosystem such as `torchvision <https://pytorch.org/vision/stable/models.html>`_.++Create the dataloaders and transforms:++.. literalinclude:: ../../../examples/train/semantic_segmentation/main.py+ :language: python+ :lines: 21-47++Define your model, losses, optimizers and schedulers:++.. literalinclude:: ../../../examples/train/semantic_segmentation/main.py+ :language: python+ :lines: 49-61++Create your preprocessing and augmentations pipeline:++.. literalinclude:: ../../../examples/train/semantic_segmentation/main.py+ :language: python+ :lines: 63-82++Finally, instantiate the :py:class:`~kornia.x.SemanticSegmentationTrainer` and execute your training pipeline.++.. literalinclude:: ../../../examples/train/semantic_segmentation/main.py+ :language: python+ :lines: 101-111++.. seealso::+ Play with the full example `here <https://github.com/kornia/kornia/tree/master/examples/train/semantic_segmentation>`_diff --git a/docs/source/conf.py b/docs/source/conf.pyindex 0587a9cdf..170860459 100644--- a/docs/source/conf.py+++ b/docs/source/conf.py@@ -122,14 +122,15 @@ html_theme_options = {"color-sidebar-background": "#3980F5","color-sidebar-background-border": "#3980F5","color-sidebar-caption-text": "white",- "color-sidebar-link-text--top-level": "#122A4F",+ "color-sidebar-link-text--top-level": "white","sidebar-caption-font-size": "normal",+ "color-sidebar-item-background--hover": " #5dade2",},"dark_css_variables": {"color-sidebar-background": "#1a1c1e","color-sidebar-background-border": "#1a1c1e",- "color-sidebar-caption-text": "#81868d",- "color-sidebar-link-text--top-level": "#3980F5",+ "color-sidebar-caption-text": "white",+ "color-sidebar-link-text--top-level": "white",},# "announcement": """diff --git a/docs/source/get-started/training.rst b/docs/source/get-started/training.rstindex e869d27f3..63f756cc0 100644--- a/docs/source/get-started/training.rst+++ b/docs/source/get-started/training.rst@@ -1,3 +1,5 @@+.. _training_api:+Training API (experimental)===========================@@ -107,7 +109,7 @@ At this point you might think - *Is this API generic enough ?*Of course not ! What is next ? Let's have fun and **customize**.The :py:class:`~kornia.x.Trainer` internals are clearly defined such in a way so that e.g you can-subclass and just override the :py:meth:`~kornia.x.Trainer.evaluate` method and adjust+subclass and just override the :py:func:`~kornia.x.Trainer.evaluate` method and adjustaccording to your needs. We provide predefined classes for generic problems such as:py:class:`~kornia.x.ImageClassifierTrainer`, :py:class:`~kornia.x.SemanticSegmentationTrainer`.@@ -139,7 +141,7 @@ You can easily customize by creating your own class, or even through ``callbacks**Still not convinced ?**- You can even override the whole :py:class:`~kornia.x.ImageClassifierTrainer.fit()`+ You can even override the whole :py:func:`~kornia.x.ImageClassifierTrainer.fit()`method and implement your custom for loops and the trainer will setup for you using the Accelerator allthe data to the device and the rest of the story is just PyTorch :)@@ -164,7 +166,7 @@ You can easily customize by creating your own class, or even through ``callbacks.. note::The following hooks are available to override: ``preprocess``, ``augmentations``, ``evaluate``, ``fit``,- ``checkpoint``, ``on_epoch_end``, ``on_before_model``+ ``on_checkpoint``, ``on_epoch_end``, ``on_before_model``Preprocess and augmentations@@ -214,7 +216,7 @@ as follows passing as ``callbacks`` the classes :py:class:`~kornia.x.ModelCheckpearly_stop = EarlyStopping(monitor="top5")trainer = K.train.ImageClassifierTrainer(...,- callbacks={"checkpoint", model_checkpoint, "terminate": early_stop})+ callbacks={"on_checkpoint", model_checkpoint, "on_epoch_end": early_stop})Hyperparameter sweeps---------------------diff --git a/docs/source/index.rst b/docs/source/index.rstindex d45cab622..83b3e0e50 100644--- a/docs/source/index.rst+++ b/docs/source/index.rst@@ -53,9 +53,11 @@ Join the community:hidden:applications/intro- applications/image_registrationapplications/image_augmentations+ applications/image_classificationapplications/image_matching+ applications/image_registration+ applications/semantic_segmentationapplications/video_deblur.. toctree::diff --git a/docs/source/models/vit.rst b/docs/source/models/vit.rstindex 88d931de5..2cf01693c 100644--- a/docs/source/models/vit.rst+++ b/docs/source/models/vit.rst@@ -1,5 +1,7 @@+.. _kornia_vit:+Vision Transformer (ViT)-.........................+.......................... card:::link: https://paperswithcode.com/paper/an-image-is-worth-16x16-words-transformers-1@@ -35,6 +37,9 @@ One can use the *ViT* in Kornia as follows:vit = VisionTransformer(image_size=224, patch_size=16)out = vit(img)+Usage+~~~~~+``kornia-vit`` does not include any classification head.For this reason, we provide an :py:class:`~kornia.contrib.ClassificationHead` which can be easily combinedwith a `nn.Sequential` in order to easily build a custom image classification pipeline.diff --git a/kornia/x/trainer_classification.py b/kornia/x/trainer_classification.pyindex 0ad544c62..a50fa7cf9 100644--- a/kornia/x/trainer_classification.py+++ b/kornia/x/trainer_classification.py@@ -9,7 +9,8 @@ class ImageClassifierTrainer(Trainer):"""Module to be used for Image Classification purposes.The module subclasses :py:class:`~kornia.x.Trainer` and overrides the- :py:meth:`~kornia.x.Trainer.evaluate` function implementing a standard accuracy topk@[1, 5].+ :py:func:`~kornia.x.Trainer.evaluate` function implementing a standard+ :py:func:`~kornia.metrics.accuracy` topk@[1, 5]... seealso::Learn how to use this class in the following@@ -50,7 +51,7 @@ class SemanticSegmentationTrainer(Trainer):"""Module to be used for Semantic segmentation purposes.The module subclasses :py:class:`~kornia.x.Trainer` and overrides the- :py:meth:`~kornia.x.Trainer.evaluate` function implementing IoU :py:meth:`~kornia.utils.mean_iou`.+ :py:func:`~kornia.x.Trainer.evaluate` function implementing IoU :py:func:`~kornia.metrics.mean_iou`... seealso::Learn how to use this class in the following