KotlinDL: High-level Deep Learning API in Kotlin
KotlinDL is a high-level Deep Learning API written in Kotlin and inspired by Keras. Under the hood, it uses TensorFlow Java API and ONNX Runtime API for Java. KotlinDL offers simple APIs for training deep learning models from scratch, importing existing Keras and ONNX models for inference, and leveraging transfer learning for tailoring existing pre-trained models to your tasks.
This project aims to make Deep Learning easier for JVM and Android developers and simplify deploying deep learning models in production environments.
Here's an example of what a classic convolutional neural network LeNet would look like in KotlinDL:
Table of Contents
- Library Structure
- How to configure KotlinDL in your project
- Working with KotlinDL in Android projects
- Working with KotlinDL in Jupyter Notebook
- KotlinDL, ONNX Runtime, Android, and JDK versions
- Documentation
- Examples and tutorials
- Running KotlinDL on GPU
- Logging
- Fat Jar issue
- Limitations
- Contributing
- Reporting issues/Support
- Code of Conduct
- License
Library Structure
KotlinDL consists of several modules:
* kotlin-deeplearning-api
api interfaces and classes
* kotlin-deeplearning-impl
implementation classes and utilities
* kotlin-deeplearning-onnx
inference with ONNX Runtime
* kotlin-deeplearning-tensorflow
learning and inference with TensorFlow
* kotlin-deeplearning-visualization
visualization utilities
* kotlin-deeplearning-dataset
dataset classes
Modules kotlin-deeplearning-tensorflow
and kotlin-deeplearning-dataset
are only available for desktop JVM, while other artifacts could also be used on Android.
How to configure KotlinDL in your project
To use KotlinDL in your project, ensure that mavenCentral
is added to the repositories list:
build.gradle
file.
To start with creating simple neural networks or downloading pre-trained models, just add the following dependency:
Use kotlin-deeplearning-onnx
module for inference with ONNX Runtime:
To use the full power of KotlinDL in your project for JVM, add the following dependencies to your build.gradle
file:
The latest stable KotlinDL version is 0.5.2
, latest unstable version is 0.6.0-alpha-1
.
For more details, as well as for pom.xml
and build.gradle.kts
examples, please refer to
the Quick Start Guide.
Working with KotlinDL in Jupyter Notebook
You can work with KotlinDL interactively in Jupyter Notebook with the Kotlin kernel. To do so, add the required dependencies in your notebook:
Working with KotlinDL in Android projects
KotlinDL supports an inference of ONNX models on the Android platform. To use KotlinDL in your Android project, add the following dependency to your build.gradle file:
KotlinDL, ONNX Runtime, Android, and JDK versions
This table shows the mapping between KotlinDL, TensorFlow, ONNX Runtime, Compile SDK for Android and minimum supported Java versions.
KotlinDL Version | Minimum Java Version | ONNX Runtime Version | TensorFlow Version | Android: Compile SDK Version |
---|---|---|---|---|
0.1.* | 8 | 1.15 | ||
0.2.0 | 8 | 1.15 | ||
0.3.0 | 8 | 1.8.1 | 1.15 | |
0.4.0 | 8 | 1.11.0 | 1.15 | |
0.5.0-0.5.1 | 11 | 1.12.1 | 1.15 | 31 |
0.5.2 | 11 | 1.14.0 | 1.15 | 31 |
0.6.* | 11 | 1.16.0 | 1.15 | 31 |
Documentation
- Presentations and videos:
- Deep Learning with KotlinDL (Zinoviev Alexey at Huawei Developer Group HDG UK 2021, slides)
- Introduction to Deep Learning with KotlinDL (Zinoviev Alexey at Kotlin Budapest User Group 2021, slides)
- Change log for KotlinDL
- Full KotlinDL API reference
Examples and tutorials
You do not need prior experience with Deep Learning to use KotlinDL.
We are working on including extensive documentation to help you get started. At this point, please feel free to check out the following tutorials we have prepared: - Quick Start Guide - Creating your first neural network - Importing a Keras model - Transfer learning - Transfer learning with Functional API - Running inference with ONNX models on JVM - Running inference with ONNX models on Android
For more inspiration, take a look at the code examples in this repository and Sample Android App.
Running KotlinDL on GPU
To enable the training and inference on a GPU, please read this TensorFlow GPU Support page and install the CUDA framework to allow calculations on a GPU device.
Note that only NVIDIA devices are supported.
You will also need to add the following dependencies in your project if you wish to leverage a GPU:
On Windows, the following distributions are required: - CUDA cuda_10.0.130_411.31_win10 - cudnn-7.6.3 - C++ redistributable parts
For inference of ONNX models on a CUDA device, you will also need to add the following dependencies to your project:
To find more info about ONNXRuntime and CUDA version compatibility, please refer to the ONNXRuntime CUDA Execution Provider page.
Logging
By default, the API module uses the kotlin-logging library to organize the logging process separately from the specific logger implementation.
You could use any widely known JVM logging library with a Simple Logging Facade for Java (SLF4J) implementation such as Logback or Log4j/Log4j2.
You will also need to add the following dependencies and configuration file log4j2.xml
to the src/resource
folder in your project if you wish to use log4j2:
If you wish to use Logback, include the following dependency and configuration file logback.xml
to src/resource
folder in your project
examples
module.
Fat Jar issue
There is a known Stack Overflow question and TensorFlow issue with Fat Jar creation and execution on Amazon EC2 instances.
Despite the fact that the bug describing this problem was closed in the release of TensorFlow 1.14, it was not fully fixed and required an additional line in the build script.
One simple solution is to add a TensorFlow version specification to the Jar's Manifest. Below is an example of a Gradle build task for Fat Jar creation.
Limitations
Currently, only a limited set of deep learning architectures are supported. Here's the list of available layers:
- Core layers:
Input
,Dense
,Flatten
,Reshape
,Dropout
,BatchNorm
.- Convolutional layers:
Conv1D
,Conv2D
,Conv3D
;Conv1DTranspose
,Conv2DTranspose
,Conv3DTranspose
;DepthwiseConv2D
;SeparableConv2D
.- Pooling layers:
MaxPool1D
,MaxPool2D
,MaxPooling3D
;AvgPool1D
,AvgPool2D
,AvgPool3D
;GlobalMaxPool1D
,GlobalMaxPool2D
,GlobalMaxPool3D
;GlobalAvgPool1D
,GlobalAvgPool2D
,GlobalAvgPool3D
.- Merge layers:
Add
,Subtract
,Multiply
;Average
,Maximum
,Minimum
;Dot
;Concatenate
.- Activation layers:
ELU
,LeakyReLU
,PReLU
,ReLU
,Softmax
,ThresholdedReLU
;ActivationLayer
.- Cropping layers:
Cropping1D
,Cropping2D
,Cropping3D
.- Upsampling layers:
UpSampling1D
,UpSampling2D
,UpSampling3D
.- Zero padding layers:
ZeroPadding1D
,ZeroPadding2D
,ZeroPadding3D
.- Other layers:
Permute
,RepeatVector
.
TensorFlow 1.15 Java API is currently used for layer implementation, but this project will be switching to TensorFlow 2.+ in the nearest future. This, however, does not affect the high-level API. Inference with TensorFlow models is currently supported only on desktops.
Contributing
Read the Contributing Guidelines.
Reporting issues/Support
Please use GitHub issues for filing feature requests and bug reports. You are also welcome to join the #kotlindl channel in Kotlin Slack.
Code of Conduct
This project and the corresponding community are governed by the JetBrains Open Source and Community Code of Conduct. Please make sure you read it.
License
KotlinDL is licensed under the Apache 2.0 License.