File size: 2,703 Bytes
36c95ba |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
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>`_
|