Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Add Multi-Framework Support to Kornia using Ivy (#3045)

Sam Armstrongf6f46a9776a9
diff --git a/README.md b/README.md
index 4234aa9e3..58d1d334a 100644
--- a/README.md
+++ b/README.md
@@ -172,6 +172,24 @@ onnx_seq.export("chained_model.onnx")
```
</details>
+## Multi-framework support
+
+You can now use Kornia with [TensorFlow](https://www.tensorflow.org/), [JAX](https://jax.readthedocs.io/en/latest/index.html), and [NumPy](https://numpy.org/). See [Multi-Framework Support](docs/source/get-started/multi-framework-support.rst) for more details.
+
+```python
+import kornia
+tf_kornia = kornia.to_tensorflow()
+```
+
+<p align="center">
+ Powered by
+ <a href="https://github.com/ivy-llc/ivy" target="_blank">
+ <div class="dark-light" style="display: block;" align="center">
+ <img class="dark-light" width="15%" src="https://raw.githubusercontent.com/ivy-llc/assets/refs/heads/main/assets/logos/ivy-long.svg"/>
+ </div>
+ </a>
+</p>
+
## Call For Contributors
Are you passionate about computer vision, AI, and open-source development? Join us in shaping the future of Kornia! We are actively seeking contributors to help expand and enhance our library, making it even more powerful, accessible, and versatile. Whether you're an experienced developer or just starting, there's a place for you in our community.
diff --git a/docs/source/get-started/multi-framework-support.rst b/docs/source/get-started/multi-framework-support.rst
new file mode 100644
index 000000000..b0877974a
--- /dev/null
+++ b/docs/source/get-started/multi-framework-support.rst
@@ -0,0 +1,119 @@
+.. raw:: html
+
+ <a href="https://github.com/ivy-llc/ivy" target="_blank">
+ <div style="display: block;" align="center">
+ <img class="dark-light" width="30%" src="https://raw.githubusercontent.com/ivy-llc/assets/refs/heads/main/assets/logos/ivy-long.svg"/>
+ </div>
+ </a>
+ <br>
+
+Multi-Framework Support
+=======================
+
+Kornia can now be used with `TensorFlow <https://www.tensorflow.org/>`_, `JAX <https://jax.readthedocs.io/en/latest/index.html>`_,
+and `Numpy <https://numpy.org/>`_ thanks to an integration with `Ivy <https://github.com/ivy-llc/ivy>`_.
+
+This can be accomplished using the following functions, which are now part of the Kornia api:
+
+* :code:`kornia.to_tensorflow()`
+
+* :code:`kornia.to_jax()`
+
+* :code:`kornia.to_numpy()`
+
+Here's an example of using kornia with TensorFlow:
+
+.. code:: python
+
+ import kornia
+ import tensorflow as tf
+
+ tf_kornia = kornia.to_tensorflow()
+
+ rgb_image = tf.random.normal((1, 3, 224, 224))
+ gray_image = tf_kornia.color.rgb_to_grayscale(rgb_image)
+
+So what's happening here? Let's break it down.
+
+#. Transpiling kornia to TensorFlow
+
+ This line lazily transpiles everything in the kornia api to TensorFlow, and creates a new module for this transpiled version of kornia.
+ Because the transpilation happens lazily, no function or class will be transpiled until it's actually called.
+
+ .. code-block:: python
+
+ tf_kornia = kornia.to_tensorflow()
+
+#. Calling a TF kornia function
+
+ We can now call any kornia function (or class) with TF arguments. However, this function will be very slow relative to
+ the original function - as the function is being transpiled during this step.
+
+ .. code-block:: python
+
+ rgb_image = tf.random.normal((1, 3, 224, 224))
+ gray_image = tf_kornia.color.rgb_to_grayscale(rgb_image) # slow
+
+#. Subsequent function calls
+
+ The good news is any calls of the function after the initial call will be much faster, as it has already been transpiled,
+ and should approximately match the speed of the original kornia function.
+
+ .. code-block:: python
+
+ gray_image = tf_kornia.color.rgb_to_grayscale(rgb_image) # fast
+
+#. Transpilations in different Python sessions
+
+ You may be wondering if you'll have to wait for these long initial transpilations to take place each time you start a
+ new Python session? The good news is that when a transpilation occurs, Ivy will save the generated source code in the
+ local directory, so if the same transpilation is ever attempted again from within the same directory, it will be
+ immediately retrieved and used.
+
+
+Kornia can be used with JAX and NumPy in the same way:
+
+.. code:: python
+
+ import kornia
+ import numpy as np
+
+ np_kornia = kornia.to_numpy()
+
+ rgb_image = np.random.normal(size=(1, 3, 224, 224))
+ gray_image = np_kornia.color.rgb_to_grayscale(rgb_image)
+
+
+.. code:: python
+
+ import kornia
+ import jax
+
+ jax_kornia = kornia.to_jax()
+
+ rgb_image = jax.random.normal(jax.random.key(42), shape=(1, 3, 224, 224))
+ gray_image = jax_kornia.color.rgb_to_grayscale(rgb_image)
+
+
+Limitations
+-----------
+
+* Converting Kornia to TensorFlow or JAX works for functions, classes and trainable modules; converting to NumPy supports functions and classes, but not trainable modules.
+
+* Transpilation does not currently work with custom kernels, such as flash attention.
+
+* Certain stateful classes cannot currently be transpiled, such as optimizers (torch.optim.Adam, etc.), trainers, and data loaders.
+
+* Compatibility with native compilers (*jax.jit* and *tf.function*) is somewhat limited with transpiled versions of Kornia,
+ particularly compared with *torch.compile* on standard Kornia. Improving compatibility with these is one of the key areas of
+ focus for the current development of Ivy.
+
+
+From the Ivy Team
+-----------------
+
+We hope you find using Kornia with TensorFlow, JAX and NumPy useful! Ivy is still very much under development,
+so if you find any issues/bugs, feel free to raise an issue on the `ivy <https://github.com/ivy-llc/ivy>`_ repository.
+We'd also really appreciate a star, if you'd like to show your support!
+
+To learn more about Ivy, we recommend taking a look through our `documentation <https://ivy.dev/docs/>`_.
diff --git a/docs/source/index.rst b/docs/source/index.rst
index a1e87c1b6..5fa8a3c49 100644
--- a/docs/source/index.rst
+++ b/docs/source/index.rst
@@ -55,6 +55,27 @@ Vision Transformer for image classification.
>>> logits = classifier(img) # BxN
>>> scores = logits.argmax(-1) # B
+Multi-framework support
+-----------------------
+
+You can now use Kornia with `NumPy <https://numpy.org/>`_, `TensorFlow <https://www.tensorflow.org/>`_, and `JAX <https://jax.readthedocs.io/en/latest/index.html>`_.
+
+.. code:: python
+
+ >>> import kornia
+ >>> tf_kornia = kornia.to_tensorflow()
+
+.. raw:: html
+
+ <p align="center">
+ Powered by
+ <a href="https://github.com/ivy-llc/ivy" target="_blank">
+ <div class="dark-light" style="display: block;" align="center">
+ <img class="dark-light" width="15%" src="https://raw.githubusercontent.com/ivy-llc/assets/refs/heads/main/assets/logos/ivy-long.svg"/>
+ </div>
+ </a>
+ </p>
+
Join the community
------------------
@@ -74,6 +95,7 @@ Join the community
get-started/installation
get-started/about
Tutorials <https://kornia.github.io/tutorials/>
+ get-started/multi-framework-support
get-started/training
OpenCV AI Kit <https://docs.luxonis.com/en/latest/pages/tutorials/creating-custom-nn-models/#kornia>
get-started/governance
diff --git a/kornia/__init__.py b/kornia/__init__.py
index b1b978274..15c6c8a78 100644
--- a/kornia/__init__.py
+++ b/kornia/__init__.py
@@ -24,6 +24,9 @@ from . import (
x,
)
+# Multi-framework support using ivy
+from .transpiler import to_jax, to_numpy, to_tensorflow
+
# NOTE: we are going to expose to top level very few things
from kornia.constants import pi
from kornia.utils import (
diff --git a/kornia/core/external.py b/kornia/core/external.py
index 8161d0546..47ac3fa92 100644
--- a/kornia/core/external.py
+++ b/kornia/core/external.py
@@ -138,3 +138,4 @@ boxmot = LazyLoader("boxmot")
segmentation_models_pytorch = LazyLoader("segmentation_models_pytorch")
basicsr = LazyLoader("basicsr")
requests = LazyLoader("requests")
+ivy = LazyLoader("ivy")
diff --git a/kornia/transpiler/__init__.py b/kornia/transpiler/__init__.py
new file mode 100644
index 000000000..06a836b08
--- /dev/null
+++ b/kornia/transpiler/__init__.py
@@ -0,0 +1,3 @@
+from .transpiler import *
+
+__all__ = ["to_jax", "to_numpy", "to_tensorflow"]
diff --git a/kornia/transpiler/transpiler.py b/kornia/transpiler/transpiler.py
new file mode 100644
index 000000000..596143c1d
--- /dev/null
+++ b/kornia/transpiler/transpiler.py
@@ -0,0 +1,80 @@
+import kornia
+from kornia.core.external import ivy
+
+
+def to_jax():
+ """Convert Kornia to JAX.
+
+ Transpiles the Kornia library to JAX using [ivy](https://github.com/ivy-llc/ivy). The transpilation process
+ occurs lazily, so the transpilation on a given kornia function/class will only occur when it's called or instantiated
+ for the first time. This will make any functions/classes slow when being used for the first time, but any
+ subsequent uses should be as fast as expected.
+
+ Return:
+ The Kornia library transpiled to JAX
+
+ Example:
+ >>> import kornia
+ >>> jax_kornia = kornia.to_jax()
+ >>> import jax
+ >>> input = jax.random.normal(jax.random.key(42), shape=(2, 3, 4, 5))
+ >>> gray = jax_kornia.color.gray.rgb_to_grayscale(input)
+ """
+ return ivy.transpile(
+ kornia,
+ source="torch",
+ target="jax",
+ )
+
+
+def to_numpy():
+ """Convert Kornia to NumPy.
+
+ Transpiles the Kornia library to NumPy using [ivy](https://github.com/ivy-llc/ivy). The transpilation process
+ occurs lazily, so the transpilation on a given kornia function/class will only occur when it's called or instantiated
+ for the first time. This will make any functions/classes slow when being used for the first time, but any
+ subsequent uses should be as fast as expected.
+
+ Return:
+ The Kornia library transpiled to NumPy
+
+ Example:
+ >>> import kornia
+ >>> np_kornia = kornia.to_numpy()
+ >>> import numpy as np
+ >>> input = np.random.normal(size=(2, 3, 4, 5))
+ >>> gray = np_kornia.color.gray.rgb_to_grayscale(input)
+
+ Note:
+ Ivy does not currently support transpiling trainable modules to NumPy.
+ """
+ return ivy.transpile(
+ kornia,
+ source="torch",
+ target="numpy",
+ )
+
+
+def to_tensorflow():
+ """Convert Kornia to TensorFlow.
+
+ Transpiles the Kornia library to TensorFlow using [ivy](https://github.com/ivy-llc/ivy). The transpilation process
+ occurs lazily, so the transpilation on a given kornia function/class will only occur when it's called or instantiated
+ for the first time. This will make any functions/classes slow when being used for the first time, but any
+ subsequent uses should be as fast as expected.
+
+ Return:
+ The Kornia library transpiled to TensorFlow
+
+ Example:
+ >>> import kornia
+ >>> tf_kornia = kornia.to_tensorflow()
+ >>> import tensorflow as tf
+ >>> input = tf.random.normal((2, 3, 4, 5))
+ >>> gray = tf_kornia.color.gray.rgb_to_grayscale(input)
+ """
+ return ivy.transpile(
+ kornia,
+ source="torch",
+ target="tensorflow",
+ )
diff --git a/requirements/requirements-dev.txt b/requirements/requirements-dev.txt
index ccc7a85c4..b162c48e2 100644
--- a/requirements/requirements-dev.txt
+++ b/requirements/requirements-dev.txt
@@ -1,6 +1,7 @@
accelerate
coverage
diffusers
+ivy>=1.0.0.0
mypy
numpy<2
onnx