Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Init Kornia.onnx module #89

Open
wants to merge 86 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
86 commits
Select commit Hold shift + click to select a range
7ff2a0a
init
shijianjian Sep 7, 2024
a162c61
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
772716e
update
shijianjian Sep 7, 2024
0fe5c04
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
50a999f
update
shijianjian Sep 7, 2024
fa864ca
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
68d5b77
update
shijianjian Sep 7, 2024
c41ef8d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
3765390
fix typing
shijianjian Sep 7, 2024
97d816e
update
shijianjian Sep 7, 2024
df2dfd2
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
d1411dd
update
shijianjian Sep 7, 2024
89a3e73
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
88e4625
update
shijianjian Sep 7, 2024
c059039
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
fa4c983
update
shijianjian Sep 7, 2024
95f065d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 7, 2024
3145282
update
shijianjian Sep 7, 2024
84ff540
update
shijianjian Sep 7, 2024
01a2613
update
shijianjian Sep 8, 2024
92ea7cf
update
shijianjian Sep 8, 2024
b71fa4d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
7ceaba6
update
shijianjian Sep 8, 2024
a552c1d
update
shijianjian Sep 9, 2024
a4c188c
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 8, 2024
62c4295
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 9, 2024
0a15677
update
shijianjian Sep 9, 2024
8897941
update
shijianjian Sep 10, 2024
1f0ae6e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 10, 2024
37032b6
update
shijianjian Sep 10, 2024
88983c7
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 11, 2024
b8dc783
update
shijianjian Sep 11, 2024
1625fd3
update
shijianjian Sep 11, 2024
6835aee
update
shijianjian Sep 11, 2024
4ebe1af
update
shijianjian Sep 12, 2024
9459fda
update
shijianjian Sep 13, 2024
6c172ee
update
shijianjian Sep 13, 2024
d883dd3
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
478e413
update
shijianjian Sep 13, 2024
ef413d9
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
68843c4
update
shijianjian Sep 13, 2024
fa39dd6
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
125b4eb
update
shijianjian Sep 13, 2024
beed3e8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
9a7be76
update
shijianjian Sep 13, 2024
e5cc24d
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
6a929e2
update
shijianjian Sep 13, 2024
1515047
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
b21d555
update
shijianjian Sep 13, 2024
56f8190
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 13, 2024
14c277c
update
shijianjian Sep 13, 2024
c110f3f
update
shijianjian Sep 14, 2024
93c4968
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 14, 2024
fc5ecb4
update
shijianjian Sep 14, 2024
898364c
update
shijianjian Sep 14, 2024
35af5f1
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 14, 2024
ba18fcd
update
shijianjian Sep 14, 2024
1c508f5
update
shijianjian Sep 14, 2024
e0cabde
update
shijianjian Sep 15, 2024
e486f29
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2024
aa707a5
update
shijianjian Sep 15, 2024
1dfaf1a
update
shijianjian Sep 15, 2024
d8a370e
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2024
01b53a9
update
shijianjian Sep 15, 2024
6c0ef12
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2024
8cb9bf3
update
shijianjian Sep 15, 2024
5b88c07
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2024
e629556
update
shijianjian Sep 15, 2024
d092f65
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 15, 2024
385e9cd
update
shijianjian Sep 16, 2024
abd2ffa
update
shijianjian Sep 16, 2024
de1e19e
update
shijianjian Sep 16, 2024
81cae97
update
shijianjian Sep 16, 2024
6824af8
update
shijianjian Sep 16, 2024
3daa151
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 16, 2024
30cffad
update
shijianjian Sep 17, 2024
842aeef
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 17, 2024
8373889
update
shijianjian Sep 17, 2024
e927844
bug fix
shijianjian Sep 17, 2024
2075367
tests fix
shijianjian Sep 19, 2024
44f54a8
updated
shijianjian Sep 19, 2024
0695a5f
Merge branch 'feat/onnx' of https://github.com/shijianjian/kornia int…
shijianjian Sep 19, 2024
82f4750
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2024
87a697f
update
shijianjian Sep 19, 2024
899f375
Merge branch 'feat/onnx' of https://github.com/shijianjian/kornia int…
shijianjian Sep 19, 2024
3aef7d8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Sep 19, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions docs/source/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ within the context of an Open Source community.

Ready to use with state-of-the art Deep Learning models:

DexiNed edge detection model.

.. code-block:: python

image = kornia.utils.sample.get_sample_images()[0][None]
model = DexiNedBuilder.build()
model.save(image)

RTDETRDetector for object detection.

.. code-block:: python

image = kornia.utils.sample.get_sample_images()[0][None]
model = RTDETRDetectorBuilder.build()
model.save(image)

BoxMotTracker for object tracking.

.. code-block:: python

import kornia
image = kornia.utils.sample.get_sample_images()[0][None]
model = BoxMotTracker()
for i in range(4):
model.update(image)
model.save(image)

Vision Transformer for image classification.

.. code:: python

>>> import torch.nn as nn
Expand Down Expand Up @@ -66,9 +95,11 @@ Join the community
io
image
losses
models
metrics
morphology
nerf
onnx
tracking
testing
utils
Expand Down
135 changes: 135 additions & 0 deletions docs/source/models.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
Models Overview
===============

This section covers several of Kornia's built-in models for key computer vision tasks. Each model is documented with its respective API and example usage.

.. _RTDETRDetectorBuilder:

RTDETRDetectorBuilder
---------------------

The `RTDETRDetectorBuilder` class is a builder for constructing a detection model based on the RT-DETR architecture, which is designed for real-time object detection. It is capable of detecting multiple objects within an image and provides efficient inference suitable for real-world applications.

**Key Methods:**

- `build`: Constructs and returns an instance of the RTDETR detection model.
- `save`: Saves the processed image or results after applying the detection model.

.. autoclass:: kornia.models.detection.rtdetr.RTDETRDetectorBuilder
:members:
:undoc-members:
:show-inheritance:

.. rubric:: Example

The following code demonstrates how to use `RTDETRDetectorBuilder` to detect objects in an image:

.. code-block:: python

import kornia
image = kornia.utils.sample.get_sample_images()[0][None]
model = kornia.models.detection.rtdetr.RTDETRDetectorBuilder.build()
model.save(image)

.. _DexiNedBuilder:

DexiNedBuilder
--------------

The `DexiNedBuilder` class implements a state-of-the-art edge detection model based on DexiNed, which excels at detecting fine-grained edges in images. This model is well-suited for tasks like medical imaging, object contour detection, and more.

**Key Methods:**

- `build`: Builds and returns an instance of the DexiNed edge detection model.
- `save`: Saves the detected edges for further processing or visualization.

.. autoclass:: kornia.models.edge_detection.dexined.DexiNedBuilder
:members:
:undoc-members:
:show-inheritance:

.. rubric:: Example

The following code shows how to use the `DexiNedBuilder` to detect edges in an image:

.. code-block:: python

import kornia
image = kornia.utils.sample.get_sample_images()[0][None]
model = kornia.models.edge_detection.dexined.DexiNedBuilder.build()
model.save(image)

.. _SegmentationModels:

SegmentationModels
------------------

The `SegmentationModels` class offers a flexible API for implementing and running various segmentation models. It supports a variety of architectures such as UNet, FPN, and others, making it highly adaptable for tasks like semantic segmentation, instance segmentation, and more.

**Key Methods:**

- `__init__`: Initializes a segmentation model based on the chosen architecture (e.g., UNet, DeepLabV3, etc.).
- `forward`: Runs inference on an input tensor and returns segmented output.

**Parameters:**

- `model_name`: (str) Name of the segmentation architecture to use, e.g., `"Unet"`, `"DeepLabV3"`.
- `classes`: (int) The number of output classes for segmentation.

.. autoclass:: kornia.models.segmentation.segmentation_models.SegmentationModels
:members:
:undoc-members:
:show-inheritance:

.. rubric:: Example

Here's an example of how to use `SegmentationModels` for binary segmentation:

.. code-block:: python

import kornia
input_tensor = kornia.utils.sample.get_sample_images()[0][None]
model = kornia.models.segmentation.segmentation_models.SegmentationModels()
segmented_output = model(input_tensor)
print(segmented_output.shape)

.. _BoxMotTracker:

BoxMotTracker
-------------

The `BoxMotTracker` class is used for multi-object tracking in video streams. It is designed to track bounding boxes of objects across multiple frames, supporting various tracking algorithms for object detection and tracking continuity.

**Key Methods:**

- `__init__`: Initializes the multi-object tracker.
- `update`: Updates the tracker with a new image frame.
- `save`: Saves the tracked object data or visualization for post-processing.

**Parameters:**

- `max_lost`: (int) The maximum number of frames where an object can be lost before it is removed from the tracker.

.. autoclass:: kornia.models.tracking.boxmot_tracker.BoxMotTracker
:members:
:undoc-members:
:show-inheritance:

.. rubric:: Example

The following example demonstrates how to track objects across multiple frames using `BoxMotTracker`:

.. code-block:: python

import kornia
image = kornia.utils.sample.get_sample_images()[0][None]
model = kornia.models.tracking.boxmot_tracker.BoxMotTracker()
for i in range(4):
model.update(image) # Update the tracker with new frames
model.save(image) # Save the tracking result

---

.. note::

This documentation provides detailed information about each model class, its methods, and usage examples. For further details on individual methods and arguments, refer to the respective code documentation.
137 changes: 137 additions & 0 deletions docs/source/onnx.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
ONNXSequential: Chain Multiple ONNX Models with Ease
====================================================

The `ONNXSequential` class is a powerful new feature that allows users to effortlessly combine and chain multiple ONNX models together. This is especially useful when you have several pre-trained models or custom ONNX operators that you want to execute sequentially as part of a larger pipeline.

Whether you're working with models for inference, experimentation, or optimization, `ONNXSequential` makes it easier to manage, combine, and run ONNX models in a streamlined manner. It also supports flexibility in execution environments with ONNXRuntime’s execution providers (CPU, CUDA, etc.).

Key Features
------------

- **Seamless Model Chaining**: Combine multiple ONNX models into a single computational graph.
- **Flexible Input/Output Mapping**: Control how the outputs of one model are passed as inputs to the next.
- **Optimized Execution**: Automatically create optimized `ONNXRuntime` sessions to speed up inference.
- **Export to ONNX**: Save the combined model into a single ONNX file for easy deployment and sharing.
- **Execution Providers Support**: Utilize ONNXRuntime's execution providers (e.g., `CUDAExecutionProvider`, `CPUExecutionProvider`) for accelerated inference on different hardware.
- **PyTorch-like Interface**: Use the `ONNXSequential` class like a PyTorch `nn.Sequential` model, including calling it directly for inference.

Quickstart Guide
----------------

Here's how you can quickly get started with `ONNXSequential`:

1. **Install ONNX and ONNXRuntime**

If you haven't already installed `onnx` and `onnxruntime`, you can install them using `pip`:

.. code-block:: bash

pip install onnx onnxruntime

2. **Combining ONNX Models**

You can initialize the `ONNXSequential` with a list of ONNX models or file paths. Models will be automatically chained together and optimized for inference.

.. code-block:: python

import numpy as np
from kornia.onnx import ONNXSequential

# Initialize ONNXSequential with two models, loading from our only repo
onnx_seq = ONNXSequential(
"hf://operators/kornia.color.gray.RgbToGrayscale",
"hf://operators/kornia.geometry.transform.affwarp.Resize_512x512"
)

# Prepare some input data
input_data = np.random.randn(1, 3, 256, 512).astype(np.float32)

# Perform inference
outputs = onnx_seq(input_data)

# Print the model outputs
print(outputs)

.. note::
By default, we assume each ONNX model contains only one input node named "input" and one output node named "output". For complex models, you may need to pass an `io_maps` argument.

3. **Input/Output Mapping Between Models**

When combining models, you can specify how the outputs of one model are mapped to the inputs of the next. This allows you to chain models in custom ways.

.. code-block:: python

io_map = [("model1_output_0", "model2_input_0"), ("model1_output_1", "model2_input_1")]
onnx_seq = ONNXSequential("model1.onnx", "model2.onnx", io_map=io_map)

4. **Exporting the Combined Model**

You can easily export the combined model to an ONNX file:

.. code-block:: python

# Export the combined model to a file
onnx_seq.export("combined_model.onnx")

5. **Optimizing with Execution Providers**

Leverage ONNXRuntime's execution providers for optimized inference. For example, to run the model on a GPU:

.. code-block:: python

# Initialize with CUDA execution provider
onnx_seq = ONNXSequential(
"hf://operators/kornia.geometry.transform.flips.Hflip",
# Or you may use a local model with either a filepath "YOUR_OWN_MODEL.onnx" or a loaded ONNX model.
"hf://models/kornia.models.detection.rtdetr_r18vd_640x640",
providers=['CUDAExecutionProvider']
)

# Run inference
outputs = onnx_seq(input_data)


Frequently Asked Questions (FAQ)
--------------------------------

**1. Can I chain models from different sources?**

Yes! You can chain models from different ONNX files or directly from `onnx.ModelProto` objects. `ONNXSequential` handles the integration and merging of their graphs.

**2. What happens if the input/output sizes of models don't match?**

You can use the `io_map` parameter to control how outputs of one model are mapped to the inputs of the next. This allows for greater flexibility when chaining models with different architectures.

**3. Can I use custom ONNXRuntime session options?**

Absolutely! You can pass your own session options to the `create_session` method to fine-tune performance, memory usage, or logging.

Why Choose ONNXSequential?
--------------------------

With the increasing adoption of ONNX for model interoperability and deployment, `ONNXSequential` provides a simple yet powerful interface for combining models and operators. By leveraging ONNXRuntime’s optimization and execution provider capabilities, it gives you the flexibility to:
- Deploy on different hardware (CPU, GPU).
- Run complex pipelines in production environments.
- Combine and experiment with models effortlessly.

Whether you're building an advanced deep learning pipeline or simply trying to chain pre-trained models, `ONNXSequential` makes it easy to manage, optimize, and execute ONNX models at scale.

Get started today and streamline your ONNX workflows!


API Documentation
-----------------
.. autoclass:: kornia.onnx.sequential.ONNXSequential
:members:

.. autoclass:: kornia.onnx.utils.ONNXLoader

.. code-block:: python

onnx_loader = ONNXLoader()
# Load a HuggingFace operator
onnx_loader.load_model("hf://operators/kornia.color.gray.GrayscaleToRgb") # doctest: +SKIP
# Load a local converted/downloaded operator
onnx_loader.load_model("operators/kornia.color.gray.GrayscaleToRgb") # doctest: +SKIP

:members:
3 changes: 3 additions & 0 deletions kornia/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,15 @@
color,
contrib,
core,
config,
enhance,
feature,
io,
losses,
metrics,
models,
morphology,
onnx,
tracking,
utils,
x,
Expand Down
4 changes: 4 additions & 0 deletions kornia/augmentation/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ class _BasicAugmentationBase(Module):
the batch form ``False``.
"""

# TODO: Hard to support. Many codes are not ONNX-friendly that contains lots of if-else blocks, etc.
# Please contribute if anyone interested.
ONNX_EXPORTABLE = False

def __init__(
self,
p: float = 0.5,
Expand Down
11 changes: 10 additions & 1 deletion kornia/color/gray.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from __future__ import annotations

from typing import Optional
from typing import ClassVar, Optional

import torch

Expand Down Expand Up @@ -125,6 +125,9 @@ class GrayscaleToRgb(Module):
>>> output = rgb(input) # 2x3x4x5
"""

ONNX_DEFAULT_INPUTSHAPE: ClassVar[list[int]] = [-1, 1, -1, -1]
ONNX_DEFAULT_OUTPUTSHAPE: ClassVar[list[int]] = [-1, 3, -1, -1]

def forward(self, image: Tensor) -> Tensor:
return grayscale_to_rgb(image)

Expand All @@ -147,6 +150,9 @@ class RgbToGrayscale(Module):
>>> output = gray(input) # 2x1x4x5
"""

ONNX_DEFAULT_INPUTSHAPE: ClassVar[list[int]] = [-1, 3, -1, -1]
ONNX_DEFAULT_OUTPUTSHAPE: ClassVar[list[int]] = [-1, 1, -1, -1]

def __init__(self, rgb_weights: Optional[Tensor] = None) -> None:
super().__init__()
if rgb_weights is None:
Expand Down Expand Up @@ -175,5 +181,8 @@ class BgrToGrayscale(Module):
>>> output = gray(input) # 2x1x4x5
"""

ONNX_DEFAULT_INPUTSHAPE: ClassVar[list[int]] = [-1, 3, -1, -1]
ONNX_DEFAULT_OUTPUTSHAPE: ClassVar[list[int]] = [-1, 1, -1, -1]

def forward(self, image: Tensor) -> Tensor:
return bgr_to_grayscale(image)
Loading
Loading